mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-03-12 14:40:07 +01:00
fix #210, keep note text on device rotation with autosave disabled
This commit is contained in:
parent
6ef08ae411
commit
96b9f2e206
@ -134,7 +134,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
if (config.autosaveNotes) {
|
||||
saveCurrentNote()
|
||||
saveCurrentNote(false)
|
||||
}
|
||||
|
||||
when (item.itemId) {
|
||||
@ -481,7 +481,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
|
||||
private fun addTextToCurrentNote(text: String) = (view_pager.adapter as NotesPagerAdapter).appendText(view_pager.currentItem, text)
|
||||
|
||||
private fun saveCurrentNote() = (view_pager.adapter as NotesPagerAdapter).saveCurrentNote(view_pager.currentItem)
|
||||
private fun saveCurrentNote(force: Boolean) = (view_pager.adapter as NotesPagerAdapter).saveCurrentNote(view_pager.currentItem, force)
|
||||
|
||||
private fun displayDeleteNotePrompt() {
|
||||
DeleteNoteDialog(this, mCurrentNote) {
|
||||
@ -532,7 +532,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
}
|
||||
|
||||
private fun saveNote() {
|
||||
saveCurrentNote()
|
||||
saveCurrentNote(true)
|
||||
showSaveButton = false
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, val activity
|
||||
|
||||
fun appendText(position: Int, text: String) = fragments[position]?.getNotesView()?.append(text)
|
||||
|
||||
fun saveCurrentNote(position: Int) = fragments[position]?.saveText()
|
||||
fun saveCurrentNote(position: Int, force: Boolean) = fragments[position]?.saveText(force)
|
||||
|
||||
fun focusEditText(position: Int) = fragments[position]?.focusEditText()
|
||||
|
||||
|
@ -34,8 +34,9 @@ import java.io.File
|
||||
|
||||
// text history handling taken from https://gist.github.com/zeleven/0cfa738c1e8b65b23ff7df1fc30c9f7e
|
||||
class NoteFragment : Fragment() {
|
||||
private var textHistory = TextHistory()
|
||||
private val TEXT = "text"
|
||||
|
||||
private var textHistory = TextHistory()
|
||||
private var isUndoOrRedo = false
|
||||
private var noteId = 0
|
||||
lateinit var note: Note
|
||||
@ -102,7 +103,7 @@ class NoteFragment : Fragment() {
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
if (context!!.config.autosaveNotes) {
|
||||
saveText()
|
||||
saveText(false)
|
||||
}
|
||||
view.notes_view.removeTextChangedListener(textWatcher)
|
||||
}
|
||||
@ -110,7 +111,7 @@ class NoteFragment : Fragment() {
|
||||
override fun setMenuVisibility(menuVisible: Boolean) {
|
||||
super.setMenuVisibility(menuVisible)
|
||||
if (!menuVisible && noteId != 0 && context?.config?.autosaveNotes == true) {
|
||||
saveText()
|
||||
saveText(false)
|
||||
}
|
||||
|
||||
if (menuVisible && noteId != 0) {
|
||||
@ -121,9 +122,22 @@ class NoteFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
outState.putString(TEXT, getCurrentNoteViewText())
|
||||
}
|
||||
|
||||
override fun onViewStateRestored(savedInstanceState: Bundle?) {
|
||||
super.onViewStateRestored(savedInstanceState)
|
||||
if (savedInstanceState != null) {
|
||||
note.value = savedInstanceState.getString(TEXT) ?: ""
|
||||
view.notes_view.setText(note.value)
|
||||
}
|
||||
}
|
||||
|
||||
fun getNotesView() = view.notes_view
|
||||
|
||||
fun saveText() {
|
||||
fun saveText(force: Boolean) {
|
||||
if (note.path.isNotEmpty() && !File(note.path).exists()) {
|
||||
return
|
||||
}
|
||||
@ -134,7 +148,7 @@ class NoteFragment : Fragment() {
|
||||
|
||||
val newText = getCurrentNoteViewText()
|
||||
val oldText = note.getNoteStoredValue()
|
||||
if (newText != null && newText != oldText) {
|
||||
if (newText != null && (newText != oldText || force)) {
|
||||
note.value = newText
|
||||
saveNoteValue(note)
|
||||
context!!.updateWidget()
|
||||
|
Loading…
x
Reference in New Issue
Block a user