Simplify loading/saving files to reduce bugs
This commit is contained in:
parent
21cd8d1324
commit
c42223f796
1 changed files with 19 additions and 42 deletions
|
@ -8,8 +8,6 @@ import com.wbrawner.simplemarkdown.utility.getName
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.InputStream
|
|
||||||
import java.io.OutputStream
|
|
||||||
import java.io.Reader
|
import java.io.Reader
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
|
@ -29,60 +27,39 @@ class MarkdownViewModel : ViewModel() {
|
||||||
suspend fun load(context: Context, uri: Uri?): Boolean {
|
suspend fun load(context: Context, uri: Uri?): Boolean {
|
||||||
if (uri == null) return false
|
if (uri == null) return false
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
context.contentResolver.openFileDescriptor(uri, "r")?.use {
|
try {
|
||||||
val fileInput = FileInputStream(it.fileDescriptor)
|
context.contentResolver.openFileDescriptor(uri, "r")?.use {
|
||||||
val fileName = uri.getName(context)
|
val fileInput = FileInputStream(it.fileDescriptor)
|
||||||
return@withContext if (load(fileInput)) {
|
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.fileName.postValue(fileName)
|
||||||
this@MarkdownViewModel.uri.postValue(uri)
|
this@MarkdownViewModel.uri.postValue(uri)
|
||||||
true
|
true
|
||||||
} else {
|
} ?: false
|
||||||
false
|
} catch (ignored: Exception) {
|
||||||
}
|
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)
|
|
||||||
}
|
}
|
||||||
true
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
e.printStackTrace()
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun save(context: Context, givenUri: Uri? = this.uri.value): Boolean {
|
suspend fun save(context: Context, givenUri: Uri? = this.uri.value): Boolean {
|
||||||
val uri = givenUri ?: this.uri.value ?: return false
|
val uri = givenUri ?: this.uri.value ?: return false
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
val fileName = uri.getName(context)
|
try {
|
||||||
val outputStream = context.contentResolver.openOutputStream(uri)
|
val fileName = uri.getName(context)
|
||||||
?: return@withContext false
|
val outputStream = context.contentResolver.openOutputStream(uri)
|
||||||
if (save(outputStream)) {
|
?: return@withContext false
|
||||||
this@MarkdownViewModel.fileName.postValue(fileName)
|
|
||||||
this@MarkdownViewModel.uri.postValue(uri)
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun save(outputStream: OutputStream): Boolean {
|
|
||||||
return try {
|
|
||||||
withContext(coroutineContext) {
|
|
||||||
outputStream.writer().use {
|
outputStream.writer().use {
|
||||||
it.write(markdownUpdates.value ?: "")
|
it.write(markdownUpdates.value ?: "")
|
||||||
}
|
}
|
||||||
|
this@MarkdownViewModel.fileName.postValue(fileName)
|
||||||
|
this@MarkdownViewModel.uri.postValue(uri)
|
||||||
|
true
|
||||||
|
} catch (ignored: Exception) {
|
||||||
|
false
|
||||||
}
|
}
|
||||||
true
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue