diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt index 46958fa..caece4a 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/EditFragment.kt @@ -24,6 +24,7 @@ import com.wbrawner.simplemarkdown.model.Readability import com.wbrawner.simplemarkdown.utility.hideKeyboard import com.wbrawner.simplemarkdown.utility.showKeyboard import com.wbrawner.simplemarkdown.view.ViewPagerPage +import com.wbrawner.simplemarkdown.viewmodel.EditorCommand import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel import kotlinx.coroutines.* import kotlin.coroutines.CoroutineContext @@ -94,8 +95,11 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { } false } - markdownEditor?.setText(viewModel.markdownUpdates.value) - viewModel.originalMarkdown.observe(viewLifecycleOwner, Observer { + markdownEditor?.setText(viewModel.markdown.value) + viewModel.editorCommands.observe(viewLifecycleOwner, Observer { + when (it) { + is EditorCommand.Undo -> markdownEditor?.text?. + } markdownEditor?.setText(it) }) launch { diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt index d06ac70..eb8103a 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt @@ -75,7 +75,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba } R.id.action_share -> { val shareIntent = Intent(Intent.ACTION_SEND) - shareIntent.putExtra(Intent.EXTRA_TEXT, viewModel.markdownUpdates.value) + shareIntent.putExtra(Intent.EXTRA_TEXT, viewModel.markdown.value) shareIntent.type = "text/plain" startActivity(Intent.createChooser( shareIntent, @@ -223,7 +223,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba } private fun promptSaveOrDiscardChanges() { - if (viewModel.originalMarkdown.value == viewModel.markdownUpdates.value) { + if (viewModel.originalMarkdown.value == viewModel.markdown.value) { viewModel.reset("Untitled.md") return } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt index 8931524..be09c02 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/PreviewFragment.kt @@ -63,8 +63,8 @@ class PreviewFragment : Fragment(), CoroutineScope { override fun onAttach(context: Context) { super.onAttach(context) - updateWebContent(viewModel.markdownUpdates.value ?: "") - viewModel.markdownUpdates.observe(this, Observer { + updateWebContent(viewModel.markdown.value ?: "") + viewModel.markdown.observe(this, Observer { updateWebContent(it) }) } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt index 36fbbb6..f28f586 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt @@ -12,12 +12,12 @@ import java.io.Reader class MarkdownViewModel : ViewModel() { val fileName = MutableLiveData("Untitled.md") - val markdownUpdates = MutableLiveData() - val originalMarkdown = MutableLiveData() + val editorCommands = MutableLiveData() + val markdown = MutableLiveData() val uri = MutableLiveData() fun updateMarkdown(markdown: String?) { - this.markdownUpdates.postValue(markdown ?: "") + this.markdown.postValue(markdown ?: "") } suspend fun load(context: Context, uri: Uri?): Boolean { @@ -28,8 +28,8 @@ class MarkdownViewModel : ViewModel() { val fileInput = FileInputStream(it.fileDescriptor) val fileName = uri.getName(context) val content = fileInput.reader().use(Reader::readText) - originalMarkdown.postValue(content) - markdownUpdates.postValue(content) + markdown.postValue(content) + editorCommands.postValue(EditorCommand.Load(content)) this@MarkdownViewModel.fileName.postValue(fileName) this@MarkdownViewModel.uri.postValue(uri) true @@ -48,7 +48,7 @@ class MarkdownViewModel : ViewModel() { context.contentResolver.openOutputStream(uri, "rwt") ?.writer() ?.use { - it.write(markdownUpdates.value ?: "") + it.write(markdown.value ?: "") } ?: return@withContext false this@MarkdownViewModel.fileName.postValue(fileName) @@ -62,7 +62,23 @@ class MarkdownViewModel : ViewModel() { fun reset(untitledFileName: String) { fileName.postValue(untitledFileName) - originalMarkdown.postValue("") - markdownUpdates.postValue("") + editorCommands.postValue(EditorCommand.Load("")) + markdown.postValue("") + } + + fun undo() { + editorCommands.postValue(EditorCommand.Undo()) + } + + fun redo() { + editorCommands.postValue(EditorCommand.Redo()) } } + +sealed class EditorCommand { + val consumed = false + + class Undo: EditorCommand() + class Redo: EditorCommand() + class Load(val text: String): EditorCommand() +} diff --git a/app/src/main/res/drawable/ic_redo.xml b/app/src/main/res/drawable/ic_redo.xml new file mode 100644 index 0000000..37e9b2a --- /dev/null +++ b/app/src/main/res/drawable/ic_redo.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_undo.xml b/app/src/main/res/drawable/ic_undo.xml new file mode 100644 index 0000000..bb1b629 --- /dev/null +++ b/app/src/main/res/drawable/ic_undo.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/menu/menu_edit.xml b/app/src/main/res/menu/menu_edit.xml index f293b24..4b7f0ca 100644 --- a/app/src/main/res/menu/menu_edit.xml +++ b/app/src/main/res/menu/menu_edit.xml @@ -7,19 +7,31 @@ android:title="@string/action_share" app:showAsAction="ifRoom" /> + android:id="@+id/action_undo" + android:icon="@drawable/ic_undo" + android:title="@string/action_undo" + app:showAsAction="ifRoom" /> - - + android:id="@+id/action_redo" + android:icon="@drawable/ic_redo" + android:title="@string/action_redo" + app:showAsAction="ifRoom" /> + + + + + + Rate SimpleMarkdown Thank you so much for your support! Heart + Undo + Redo @string/pref_value_light @string/pref_value_dark