Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
dfd771adfe |
8 changed files with 80 additions and 26 deletions
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -12,12 +12,12 @@ import java.io.Reader
|
|||
|
||||
class MarkdownViewModel : ViewModel() {
|
||||
val fileName = MutableLiveData<String>("Untitled.md")
|
||||
val markdownUpdates = MutableLiveData<String>()
|
||||
val originalMarkdown = MutableLiveData<String>()
|
||||
val editorCommands = MutableLiveData<EditorCommand>()
|
||||
val markdown = MutableLiveData<String>()
|
||||
val uri = MutableLiveData<Uri>()
|
||||
|
||||
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()
|
||||
}
|
||||
|
|
10
app/src/main/res/drawable/ic_redo.xml
Normal file
10
app/src/main/res/drawable/ic_redo.xml
Normal file
|
@ -0,0 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M18.4,10.6C16.55,8.99 14.15,8 11.5,8c-4.65,0 -8.58,3.03 -9.96,7.22L3.9,16c1.05,-3.19 4.05,-5.5 7.6,-5.5 1.95,0 3.73,0.72 5.12,1.88L13,16h9V7l-3.6,3.6z"/>
|
||||
</vector>
|
10
app/src/main/res/drawable/ic_undo.xml
Normal file
10
app/src/main/res/drawable/ic_undo.xml
Normal file
|
@ -0,0 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M12.5,8c-2.65,0 -5.05,0.99 -6.9,2.6L2,7v9h9l-3.62,-3.62c1.39,-1.16 3.16,-1.88 5.12,-1.88 3.54,0 6.55,2.31 7.6,5.5l2.37,-0.78C21.08,11.03 17.15,8 12.5,8z"/>
|
||||
</vector>
|
|
@ -7,19 +7,31 @@
|
|||
android:title="@string/action_share"
|
||||
app:showAsAction="ifRoom" />
|
||||
<item
|
||||
android:id="@+id/action_new"
|
||||
android:title="@string/action_new"
|
||||
app:showAsAction="never" />
|
||||
android:id="@+id/action_undo"
|
||||
android:icon="@drawable/ic_undo"
|
||||
android:title="@string/action_undo"
|
||||
app:showAsAction="ifRoom" />
|
||||
<item
|
||||
android:id="@+id/action_load"
|
||||
android:title="@string/action_open"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_save"
|
||||
android:title="@string/action_save" />
|
||||
<item
|
||||
android:id="@+id/action_save_as"
|
||||
android:title="@string/action_save_as" />
|
||||
android:id="@+id/action_redo"
|
||||
android:icon="@drawable/ic_redo"
|
||||
android:title="@string/action_redo"
|
||||
app:showAsAction="ifRoom" />
|
||||
<group android:id="@+id/editGroup">
|
||||
<item
|
||||
android:id="@+id/action_new"
|
||||
android:title="@string/action_new"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_load"
|
||||
android:title="@string/action_open"
|
||||
app:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_save"
|
||||
android:title="@string/action_save" />
|
||||
<item
|
||||
android:id="@+id/action_save_as"
|
||||
android:title="@string/action_save_as" />
|
||||
</group>
|
||||
<item
|
||||
android:id="@+id/action_lock_swipe"
|
||||
android:checkable="true"
|
||||
|
|
|
@ -77,6 +77,8 @@
|
|||
<string name="action_rate">Rate SimpleMarkdown</string>
|
||||
<string name="support_thank_you">Thank you so much for your support!</string>
|
||||
<string name="description_heart">Heart</string>
|
||||
<string name="action_undo">Undo</string>
|
||||
<string name="action_redo">Redo</string>
|
||||
<string-array name="pref_entries_dark_mode">
|
||||
<item>@string/pref_value_light</item>
|
||||
<item>@string/pref_value_dark</item>
|
||||
|
|
Loading…
Reference in a new issue