properly handle password protection of menu items

This commit is contained in:
tibbi 2021-05-20 23:12:19 +02:00
parent 81c6da22d4
commit 0f5456086e
5 changed files with 32 additions and 17 deletions

View File

@ -169,26 +169,27 @@ class MainActivity : SimpleActivity() {
saveCurrentNote(false) saveCurrentNote(false)
} }
val fragment = getCurrentFragment()
when (item.itemId) { when (item.itemId) {
R.id.open_search -> openSearch() R.id.open_search -> fragment?.handleUnlocking { openSearch() }
R.id.open_note -> displayOpenNoteDialog() R.id.open_note -> displayOpenNoteDialog()
R.id.save_note -> saveNote() R.id.save_note -> fragment?.handleUnlocking { saveNote() }
R.id.undo -> undo() R.id.undo -> undo()
R.id.redo -> redo() R.id.redo -> redo()
R.id.new_note -> displayNewNoteDialog() R.id.new_note -> displayNewNoteDialog()
R.id.rename_note -> displayRenameDialog() R.id.rename_note -> fragment?.handleUnlocking { displayRenameDialog() }
R.id.share -> shareText() R.id.share -> fragment?.handleUnlocking { shareText() }
R.id.lock_note -> lockNote() R.id.lock_note -> lockNote()
R.id.unlock_note -> unlockNote() R.id.unlock_note -> unlockNote()
R.id.open_file -> tryOpenFile() R.id.open_file -> tryOpenFile()
R.id.import_folder -> openFolder() R.id.import_folder -> openFolder()
R.id.export_as_file -> tryExportAsFile() R.id.export_as_file -> fragment?.handleUnlocking { tryExportAsFile() }
R.id.export_all_notes -> tryExportAllNotes() R.id.export_all_notes -> tryExportAllNotes()
R.id.print -> printText() R.id.print -> fragment?.handleUnlocking { printText() }
R.id.delete_note -> displayDeleteNotePrompt() R.id.delete_note -> fragment?.handleUnlocking { displayDeleteNotePrompt() }
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java)) R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
R.id.about -> launchAbout() R.id.about -> launchAbout()
R.id.remove_done_items -> removeDoneItems() R.id.remove_done_items -> fragment?.handleUnlocking { removeDoneItems() }
else -> return super.onOptionsItemSelected(item) else -> return super.onOptionsItemSelected(item)
} }
return true return true
@ -421,6 +422,8 @@ class MainActivity : SimpleActivity() {
} }
} }
private fun getCurrentFragment() = mAdapter?.getFragment(view_pager.currentItem)
private val currentTextFragment: TextFragment? get() = mAdapter?.textFragment(view_pager.currentItem) private val currentTextFragment: TextFragment? get() = mAdapter?.textFragment(view_pager.currentItem)
private fun selectSearchMatch(editText: MyEditText) { private fun selectSearchMatch(editText: MyEditText) {
@ -775,7 +778,7 @@ class MainActivity : SimpleActivity() {
var failCount = 0 var failCount = 0
NotesHelper(this).getNotes { NotesHelper(this).getNotes {
mNotes = it mNotes = it
mNotes.forEachIndexed { index, note -> mNotes.filter { !it.isLocked() }.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension" val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename) val file = File(parent, filename)
if (!filename.isAValidFilename()) { if (!filename.isAValidFilename()) {

View File

@ -45,6 +45,8 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
} }
} }
fun getFragment(position: Int) = fragments[position]
fun isChecklistFragment(position: Int): Boolean = (fragments[position] is ChecklistFragment) fun isChecklistFragment(position: Int): Boolean = (fragments[position] is ChecklistFragment)
fun textFragment(position: Int): TextFragment? = (fragments[position] as? TextFragment) fun textFragment(position: Int): TextFragment? = (fragments[position] as? TextFragment)

View File

@ -25,7 +25,6 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.*
class ChecklistFragment : NoteFragment(), ChecklistItemsListener { class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
private var noteId = 0L private var noteId = 0L
private var note: Note? = null
lateinit var view: ViewGroup lateinit var view: ViewGroup

View File

@ -7,6 +7,7 @@ import com.simplemobiletools.commons.dialogs.SecurityDialog
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.getPercentageFontSize import com.simplemobiletools.notes.pro.extensions.getPercentageFontSize
import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Note
@ -14,6 +15,7 @@ import kotlinx.android.synthetic.main.fragment_checklist.view.*
abstract class NoteFragment : Fragment() { abstract class NoteFragment : Fragment() {
protected var shouldShowLockedContent = false protected var shouldShowLockedContent = false
protected var note: Note? = null
protected fun setupLockedViews(view: ViewGroup, note: Note) { protected fun setupLockedViews(view: ViewGroup, note: Note) {
view.apply { view.apply {
@ -26,12 +28,22 @@ abstract class NoteFragment : Fragment() {
note_locked_show.setTextColor(context!!.getAdjustedPrimaryColor()) note_locked_show.setTextColor(context!!.getAdjustedPrimaryColor())
note_locked_show.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getPercentageFontSize()) note_locked_show.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getPercentageFontSize())
note_locked_show.setOnClickListener { note_locked_show.setOnClickListener {
SecurityDialog(activity!!, note.protectionHash, note.protectionType) { hash, type, success -> handleUnlocking()
if (success) { }
shouldShowLockedContent = true }
checkLockState() }
}
} fun handleUnlocking(callback: (() -> Unit)? = null) {
if (callback != null && (note!!.protectionType == PROTECTION_NONE || shouldShowLockedContent)) {
callback()
return
}
SecurityDialog(activity!!, note!!.protectionHash, note!!.protectionType) { hash, type, success ->
if (success) {
shouldShowLockedContent = true
checkLockState()
callback?.invoke()
} }
} }
} }

View File

@ -38,7 +38,6 @@ class TextFragment : NoteFragment() {
private var isUndoOrRedo = false private var isUndoOrRedo = false
private var skipTextUpdating = false private var skipTextUpdating = false
private var noteId = 0L private var noteId = 0L
private var note: Note? = null
lateinit var view: ViewGroup lateinit var view: ViewGroup