Compare commits

...

2 Commits

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

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

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

Loading…
Cancel
Save