Compare commits

...

2 Commits

Author SHA1 Message Date
yova 2f777055ea some error handling
3 months ago
yova 67050857e3 open/new alert dialog unCreate
3 months ago

@ -2,6 +2,7 @@ package org.wntr.mdeditor
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
@ -186,33 +187,20 @@ class MainActivity : AppCompatActivity() {
Log.i(javaClass.simpleName, value)
}
@JavascriptInterface
fun triggerNewBuffer(value: String) {
saveFile()
mdeValue = value
metaData = mdMeta()
selectFileForSaveAs()
}
@JavascriptInterface
fun triggerOpenFile() {
openFile()
}
@JavascriptInterface
fun triggerSaveFile(value: String) {
mdeValue = value
saveFile()
}
@JavascriptInterface
/*@JavascriptInterface
fun getHeight(): Int {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
Log.i(javaClass.simpleName, "display is $height pixels high.")
return height
}
}*/
@JavascriptInterface
fun refresh() {
@ -317,9 +305,6 @@ class MainActivity : AppCompatActivity() {
webView.evaluateJavascript(script , {
if (it == "false" && thisFileUri != null) {
saveFile()
this@MainActivity.runOnUiThread {
webView.evaluateJavascript("easyMDE.codemirror.doc.markClean();", {})
}
}
})
}
@ -462,7 +447,6 @@ class MainActivity : AppCompatActivity() {
} else
shareGhost(msg, ::sendPost)
})
}
R.id.settings -> {
ghostSettings.launch(Intent(this, LoginActivity::class.java))
@ -829,8 +813,7 @@ class MainActivity : AppCompatActivity() {
putExtra(DocumentsContract.EXTRA_INITIAL_URI, thisFileUri)
type = "text/*"
getDisplayName(thisFileUri).apply { putExtra(Intent.EXTRA_TITLE, getDisplayName(
thisFileUri)) }
addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
@ -1003,6 +986,9 @@ class MainActivity : AppCompatActivity() {
}
Log.d(javaClass.simpleName, "File saved: ${thisFileUri}")
textFile.close()
this@MainActivity.runOnUiThread {
webView.evaluateJavascript("easyMDE.codemirror.doc.markClean();", {})
}
}
})
} catch (e: Exception) {
@ -1080,6 +1066,7 @@ class MainActivity : AppCompatActivity() {
).show()
})
}
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
try {
@ -1101,7 +1088,6 @@ class MainActivity : AppCompatActivity() {
catch (e: java.io.FileNotFoundException) {
Log.d(javaClass.simpleName, "File not found during reading:\n${e.stackTraceToString()}")
this@MainActivity.runOnUiThread({
selectFileForSaveAs()
Toast.makeText(
this@MainActivity,
"File not found during reading.\n$e",
@ -1110,6 +1096,17 @@ class MainActivity : AppCompatActivity() {
})
return@withContext
}
catch (e: java.lang.NullPointerException) {
Log.d(javaClass.simpleName, "Nullpointerexception. Maybe file deleted in the meantime?.\n$e")
this@MainActivity.runOnUiThread({
Toast.makeText(
this@MainActivity,
"Nullpointerexception. Maybe file deleted in the meantime?.\n$e",
Toast.LENGTH_LONG
).show()
})
return@withContext
}
catch (e: Exception) {
Log.d(javaClass.simpleName, e.stackTraceToString())
this@MainActivity.runOnUiThread({
@ -1132,15 +1129,16 @@ class MainActivity : AppCompatActivity() {
"easyMDE.codemirror.doc.markClean();" +
"easyMDE.codemirror.focus();" +
"easyMDE.codemirror.doc.setCursor(JSON.parse(`${metaData.cursor}`));" +
"pasteText();}"
"pasteText();" +
"easyMDE.updateStatusBar(\"displayName\",\"${getDisplayName(uri)}\");}"
Log.d(javaClass.simpleName, "executing in webview:\n${script}")
webView.evaluateJavascript(script, {
thisFileUri = uri
Log.d(javaClass.simpleName,"File read: ${thisFileUri}")
})
webView.requestFocus()
})
}
}
}
@ -1160,7 +1158,15 @@ class MainActivity : AppCompatActivity() {
val uriString = prefs.getString("lastFile", "noLastFile")
if (uriString == "noLastFile") {
selectFileForSaveAs()
with(AlertDialog.Builder(this)){
setPositiveButton("Open", { dialog, id ->
openFile()
})
setNeutralButton("New", { dialog, id ->
selectFileForSaveAs()
})
show()
}
} else {
thisFileUri = parse(uriString)
metaData.cursor = prefs.getString("cursor", "nocursor") ?: "{ line: 0, ch: 0, sticky: null }"

@ -6,22 +6,11 @@ function onRead() {
pasteText()
}
function saveAs() {
Android.triggerNewBuffer(easyMDE.value())
}
function saveFile() {
if (!easyMDE.codemirror.doc.isClean()) Android.triggerSaveFile(easyMDE.value())
easyMDE.codemirror.doc.markClean()
}
function blankBuffer() {
Android.triggerNewBuffer("")
}
function openFile() {
saveFile()
Android.triggerOpenFile()
onRead()
}
function dispatchCut() {
console.log("dispatch cut")
easyMDE.codemirror.getTextArea().dispatchEvent(new Event("cut"))

Loading…
Cancel
Save