From c42223f79638d00f771ca2e9a86f0f9238400e7c Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Sun, 10 Nov 2019 10:18:51 -0600 Subject: [PATCH] Simplify loading/saving files to reduce bugs --- .../viewmodel/MarkdownViewModel.kt | 61 ++++++------------- 1 file changed, 19 insertions(+), 42 deletions(-) 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 ceb4ccc..f74f6f8 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/viewmodel/MarkdownViewModel.kt @@ -8,8 +8,6 @@ import com.wbrawner.simplemarkdown.utility.getName import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.FileInputStream -import java.io.InputStream -import java.io.OutputStream import java.io.Reader import kotlin.coroutines.CoroutineContext @@ -29,60 +27,39 @@ class MarkdownViewModel : ViewModel() { suspend fun load(context: Context, uri: Uri?): Boolean { if (uri == null) return false return withContext(Dispatchers.IO) { - context.contentResolver.openFileDescriptor(uri, "r")?.use { - val fileInput = FileInputStream(it.fileDescriptor) - val fileName = uri.getName(context) - return@withContext if (load(fileInput)) { + try { + context.contentResolver.openFileDescriptor(uri, "r")?.use { + val fileInput = FileInputStream(it.fileDescriptor) + val fileName = uri.getName(context) + val content = fileInput.reader().use(Reader::readText) + originalMarkdown.postValue(content) + markdownUpdates.postValue(content) this@MarkdownViewModel.fileName.postValue(fileName) this@MarkdownViewModel.uri.postValue(uri) true - } else { - false - } - } ?: false - } - } - - suspend fun load(inputStream: InputStream): Boolean { - return try { - withContext(coroutineContext) { - val content = inputStream.reader().use(Reader::readText) - originalMarkdown.postValue(content) - markdownUpdates.postValue(content) + } ?: false + } catch (ignored: Exception) { + false } - true - } catch (e: Throwable) { - e.printStackTrace() - false } } suspend fun save(context: Context, givenUri: Uri? = this.uri.value): Boolean { val uri = givenUri ?: this.uri.value ?: return false return withContext(Dispatchers.IO) { - val fileName = uri.getName(context) - val outputStream = context.contentResolver.openOutputStream(uri) - ?: return@withContext false - if (save(outputStream)) { - this@MarkdownViewModel.fileName.postValue(fileName) - this@MarkdownViewModel.uri.postValue(uri) - true - } else { - false - } - } - } - - suspend fun save(outputStream: OutputStream): Boolean { - return try { - withContext(coroutineContext) { + try { + val fileName = uri.getName(context) + val outputStream = context.contentResolver.openOutputStream(uri) + ?: return@withContext false outputStream.writer().use { it.write(markdownUpdates.value ?: "") } + this@MarkdownViewModel.fileName.postValue(fileName) + this@MarkdownViewModel.uri.postValue(uri) + true + } catch (ignored: Exception) { + false } - true - } catch (e: Throwable) { - false } }