fix #210, keep note text on device rotation with autosave disabled

This commit is contained in:
tibbi 2018-09-11 20:55:53 +02:00
parent 6ef08ae411
commit 96b9f2e206
3 changed files with 23 additions and 9 deletions

View File

@ -134,7 +134,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (config.autosaveNotes) { if (config.autosaveNotes) {
saveCurrentNote() saveCurrentNote(false)
} }
when (item.itemId) { 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 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() { private fun displayDeleteNotePrompt() {
DeleteNoteDialog(this, mCurrentNote) { DeleteNoteDialog(this, mCurrentNote) {
@ -532,7 +532,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
} }
private fun saveNote() { private fun saveNote() {
saveCurrentNote() saveCurrentNote(true)
showSaveButton = false showSaveButton = false
invalidateOptionsMenu() invalidateOptionsMenu()
} }

View File

@ -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 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() fun focusEditText(position: Int) = fragments[position]?.focusEditText()

View File

@ -34,8 +34,9 @@ import java.io.File
// text history handling taken from https://gist.github.com/zeleven/0cfa738c1e8b65b23ff7df1fc30c9f7e // text history handling taken from https://gist.github.com/zeleven/0cfa738c1e8b65b23ff7df1fc30c9f7e
class NoteFragment : Fragment() { class NoteFragment : Fragment() {
private var textHistory = TextHistory() private val TEXT = "text"
private var textHistory = TextHistory()
private var isUndoOrRedo = false private var isUndoOrRedo = false
private var noteId = 0 private var noteId = 0
lateinit var note: Note lateinit var note: Note
@ -102,7 +103,7 @@ class NoteFragment : Fragment() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
if (context!!.config.autosaveNotes) { if (context!!.config.autosaveNotes) {
saveText() saveText(false)
} }
view.notes_view.removeTextChangedListener(textWatcher) view.notes_view.removeTextChangedListener(textWatcher)
} }
@ -110,7 +111,7 @@ class NoteFragment : Fragment() {
override fun setMenuVisibility(menuVisible: Boolean) { override fun setMenuVisibility(menuVisible: Boolean) {
super.setMenuVisibility(menuVisible) super.setMenuVisibility(menuVisible)
if (!menuVisible && noteId != 0 && context?.config?.autosaveNotes == true) { if (!menuVisible && noteId != 0 && context?.config?.autosaveNotes == true) {
saveText() saveText(false)
} }
if (menuVisible && noteId != 0) { 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 getNotesView() = view.notes_view
fun saveText() { fun saveText(force: Boolean) {
if (note.path.isNotEmpty() && !File(note.path).exists()) { if (note.path.isNotEmpty() && !File(note.path).exists()) {
return return
} }
@ -134,7 +148,7 @@ class NoteFragment : Fragment() {
val newText = getCurrentNoteViewText() val newText = getCurrentNoteViewText()
val oldText = note.getNoteStoredValue() val oldText = note.getNoteStoredValue()
if (newText != null && newText != oldText) { if (newText != null && (newText != oldText || force)) {
note.value = newText note.value = newText
saveNoteValue(note) saveNoteValue(note)
context!!.updateWidget() context!!.updateWidget()