Compare commits
1 commit
main
...
multiple-w
Author | SHA1 | Date | |
---|---|---|---|
c9c9d82a55 |
2 changed files with 4 additions and 40 deletions
|
@ -12,7 +12,6 @@ import android.os.Bundle
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import android.webkit.MimeTypeMap
|
import android.webkit.MimeTypeMap
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AlertDialog
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
@ -27,6 +26,7 @@ import androidx.preference.PreferenceManager
|
||||||
import com.wbrawner.simplemarkdown.R
|
import com.wbrawner.simplemarkdown.R
|
||||||
import com.wbrawner.simplemarkdown.utility.ErrorHandler
|
import com.wbrawner.simplemarkdown.utility.ErrorHandler
|
||||||
import com.wbrawner.simplemarkdown.utility.errorHandlerImpl
|
import com.wbrawner.simplemarkdown.utility.errorHandlerImpl
|
||||||
|
import com.wbrawner.simplemarkdown.view.activity.MainActivity
|
||||||
import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter
|
import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter
|
||||||
import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel
|
import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel
|
||||||
import kotlinx.android.synthetic.main.fragment_main.*
|
import kotlinx.android.synthetic.main.fragment_main.*
|
||||||
|
@ -129,7 +129,9 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.action_new -> {
|
R.id.action_new -> {
|
||||||
promptSaveOrDiscardChanges()
|
startActivity(Intent(requireContext(), MainActivity::class.java).apply {
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
|
||||||
|
})
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.action_lock_swipe -> {
|
R.id.action_lock_swipe -> {
|
||||||
|
@ -251,31 +253,6 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun promptSaveOrDiscardChanges() {
|
|
||||||
if (viewModel.shouldPromptSave()) {
|
|
||||||
viewModel.reset("Untitled.md")
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
|
|
||||||
remove(getString(R.string.pref_key_autosave_uri))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val context = context ?: return
|
|
||||||
AlertDialog.Builder(context)
|
|
||||||
.setTitle(R.string.save_changes)
|
|
||||||
.setMessage(R.string.prompt_save_changes)
|
|
||||||
.setNegativeButton(R.string.action_discard) { _, _ ->
|
|
||||||
viewModel.reset("Untitled.md")
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
|
|
||||||
remove(getString(R.string.pref_key_autosave_uri))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.setPositiveButton(R.string.action_save) { _, _ ->
|
|
||||||
requestFileOp(REQUEST_SAVE_FILE)
|
|
||||||
}
|
|
||||||
.create()
|
|
||||||
.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun requestFileOp(requestType: Int) {
|
private fun requestFileOp(requestType: Int) {
|
||||||
val context = context ?: return
|
val context = context ?: return
|
||||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
|
|
@ -16,11 +16,9 @@ class MarkdownViewModel : ViewModel() {
|
||||||
val markdownUpdates = MutableLiveData<String>()
|
val markdownUpdates = MutableLiveData<String>()
|
||||||
val editorActions = MutableLiveData<EditorAction>()
|
val editorActions = MutableLiveData<EditorAction>()
|
||||||
val uri = MutableLiveData<Uri?>()
|
val uri = MutableLiveData<Uri?>()
|
||||||
private val isDirty = AtomicBoolean(false)
|
|
||||||
|
|
||||||
fun updateMarkdown(markdown: String?) {
|
fun updateMarkdown(markdown: String?) {
|
||||||
this.markdownUpdates.postValue(markdown ?: "")
|
this.markdownUpdates.postValue(markdown ?: "")
|
||||||
isDirty.set(true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun load(context: Context, uri: Uri?): Boolean {
|
suspend fun load(context: Context, uri: Uri?): Boolean {
|
||||||
|
@ -35,7 +33,6 @@ class MarkdownViewModel : ViewModel() {
|
||||||
// If we don't get anything back, then we can assume that reading the file failed
|
// If we don't get anything back, then we can assume that reading the file failed
|
||||||
return@withContext false
|
return@withContext false
|
||||||
}
|
}
|
||||||
isDirty.set(false)
|
|
||||||
editorActions.postValue(EditorAction.Load(content))
|
editorActions.postValue(EditorAction.Load(content))
|
||||||
markdownUpdates.postValue(content)
|
markdownUpdates.postValue(content)
|
||||||
this@MarkdownViewModel.fileName.postValue(fileName)
|
this@MarkdownViewModel.fileName.postValue(fileName)
|
||||||
|
@ -68,16 +65,6 @@ class MarkdownViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reset(untitledFileName: String) {
|
|
||||||
fileName.postValue(untitledFileName)
|
|
||||||
uri.postValue(null)
|
|
||||||
markdownUpdates.postValue("")
|
|
||||||
editorActions.postValue(EditorAction.Load(""))
|
|
||||||
isDirty.set(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun shouldPromptSave() = isDirty.get()
|
|
||||||
|
|
||||||
sealed class EditorAction {
|
sealed class EditorAction {
|
||||||
val consumed = AtomicBoolean(false)
|
val consumed = AtomicBoolean(false)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue