diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt index 196ce48..8e54dee 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt @@ -21,6 +21,7 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes intent?.data?.let { launch { viewModel.load(this@MainActivity, it) + intent?.data = null } } } 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 f353afa..46958fa 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 @@ -94,6 +94,7 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { } false } + markdownEditor?.setText(viewModel.markdownUpdates.value) viewModel.originalMarkdown.observe(viewLifecycleOwner, Observer { markdownEditor?.setText(it) }) @@ -115,7 +116,6 @@ class EditFragment : Fragment(), ViewPagerPage, CoroutineScope { } readabilityWatcher = null } - } } 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 39d9887..36fbbb6 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt @@ -9,13 +9,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.FileInputStream import java.io.Reader -import kotlin.coroutines.CoroutineContext class MarkdownViewModel : ViewModel() { - private val coroutineContext: CoroutineContext = Dispatchers.IO - val fileName = MutableLiveData().apply { - postValue("Untitled.md") - } + val fileName = MutableLiveData("Untitled.md") val markdownUpdates = MutableLiveData() val originalMarkdown = MutableLiveData() val uri = MutableLiveData() @@ -49,11 +45,12 @@ class MarkdownViewModel : ViewModel() { return withContext(Dispatchers.IO) { try { val fileName = uri.getName(context) - val outputStream = context.contentResolver.openOutputStream(uri, "rwt") + context.contentResolver.openOutputStream(uri, "rwt") + ?.writer() + ?.use { + it.write(markdownUpdates.value ?: "") + } ?: return@withContext false - outputStream.writer().use { - it.write(markdownUpdates.value ?: "") - } this@MarkdownViewModel.fileName.postValue(fileName) this@MarkdownViewModel.uri.postValue(uri) true