From c9c9d82a5560ab0a0c9bfbd6194fefe628b237d3 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Fri, 9 Oct 2020 12:21:17 -0700 Subject: [PATCH] Open new files in new windows --- .../view/fragment/MainFragment.kt | 31 +++---------------- .../viewmodel/MarkdownViewModel.kt | 13 -------- 2 files changed, 4 insertions(+), 40 deletions(-) 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 ed21617..70c2999 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 @@ -12,7 +12,6 @@ import android.os.Bundle import android.view.* import android.webkit.MimeTypeMap import android.widget.Toast -import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat @@ -27,6 +26,7 @@ import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.utility.ErrorHandler import com.wbrawner.simplemarkdown.utility.errorHandlerImpl +import com.wbrawner.simplemarkdown.view.activity.MainActivity import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel import kotlinx.android.synthetic.main.fragment_main.* @@ -129,7 +129,9 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba true } 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 } R.id.action_lock_swipe -> { @@ -251,31 +253,6 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba 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) { val context = context ?: return if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) 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 d7dd37c..071f7ff 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt @@ -16,11 +16,9 @@ class MarkdownViewModel : ViewModel() { val markdownUpdates = MutableLiveData() val editorActions = MutableLiveData() val uri = MutableLiveData() - private val isDirty = AtomicBoolean(false) fun updateMarkdown(markdown: String?) { this.markdownUpdates.postValue(markdown ?: "") - isDirty.set(true) } 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 return@withContext false } - isDirty.set(false) editorActions.postValue(EditorAction.Load(content)) markdownUpdates.postValue(content) 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 { val consumed = AtomicBoolean(false)