From 79d7855973f8bb8b79095ebe332633fc68e5296b Mon Sep 17 00:00:00 2001 From: yova Date: Thu, 25 Jan 2024 14:02:45 +0100 Subject: [PATCH] wait till editor's loaded before start --- .idea/deploymentTargetDropDown.xml | 17 --- .../java/org/wntr/mdeditor/MainActivity.kt | 119 +++++++++++++++--- app/src/main/java/org/wntr/mdeditor/mdMeta.kt | 3 +- app/src/main/res/raw/controller.js | 58 --------- 4 files changed, 106 insertions(+), 91 deletions(-) delete mode 100644 .idea/deploymentTargetDropDown.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index 4264c2f..0000000 --- a/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/java/org/wntr/mdeditor/MainActivity.kt b/app/src/main/java/org/wntr/mdeditor/MainActivity.kt index 919f4b7..f871959 100644 --- a/app/src/main/java/org/wntr/mdeditor/MainActivity.kt +++ b/app/src/main/java/org/wntr/mdeditor/MainActivity.kt @@ -56,6 +56,66 @@ import kotlin.concurrent.fixedRateTimer class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding + private val easyMDEscript = """ + const easyMDE = new EasyMDE({ + spellChecker: false, + nativeSpellcheck: false, + maxHeight: String(windowHeight-120)+"px", + inputStyle: "textarea", + autoDownloadFontAwesome: false, + theme: "solarized", + status: [ + { + className: "editor-statusbar-left", + onUpdate: (el) => { + el.innerHTML = "" + } + }, + { + className: "displayName", + defaultValue: "None", + onUpdate: (el) => { + el.innerHTML = displayName() + }, + }, "lines", "words", "cursor", + { + className: "editor-statusbar-right", + onUpdate: (el) => { + el.innerHTML = "" + } + } + ], + toolbar: [ + { + name: "toggleTheme", + action: toggleTheme, + className: "fa fa-moon", + title: "Toggle Theme" + }, + { + name: "share", + action: shareText, + className: "fa fa-share-nodes", + title: "Share" + },"strikethrough", "horizontal-rule","undo", + { + name: "preview", + action: myPreview, + className: "fa fa-eye", + title: "Preview", + noDisable: true + },"redo", + "bold", "italic","link","code", + { + name: "toggle", + action: toggleBar, + className: "fa fa-expand", + title: "Toggle Bar", + } + ] + }); +""" + companion object { const val CREATE_FILE = 1 @@ -76,7 +136,8 @@ class MainActivity : AppCompatActivity() { var ghostConnection = false lateinit var credManager: CredentialManager var intentScheme = "none" - var lastSaved = Instant.now().toEpochMilli() + var easyMDELoaded = false + } override fun onCreate(savedInstanceState: Bundle?) { @@ -101,6 +162,16 @@ class MainActivity : AppCompatActivity() { Log.d("WebView", consoleMessage.message()) return true } + override fun onProgressChanged(view: WebView?, newProgress: Int) { + super.onProgressChanged(view, newProgress) + Log.d(javaClass.simpleName, "new progress: ${newProgress}") + if (newProgress == 100) { + webView.evaluateJavascript(easyMDEscript, { + easyMDELoaded = true + Log.d(javaClass.simpleName, "easyMDE loaded") + }) + } + } } webView.loadUrl("file:///android_res/raw/index.html") @@ -159,7 +230,7 @@ class MainActivity : AppCompatActivity() { } @JavascriptInterface - fun triggerGhost(sharedText: String, ) { + fun triggerGhost(sharedText: String) { shareGhost(sharedText, ::sendPost) } @@ -239,9 +310,11 @@ class MainActivity : AppCompatActivity() { ghostMetaData = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { saveFile() } + fixedRateTimer("timer",true,0,5000){ this@MainActivity.runOnUiThread { - webView.evaluateJavascript("easyMDE.codemirror.doc.isClean();", { + val script = "easyMDE.codemirror.doc.isClean();" + webView.evaluateJavascript(script , { if (it == "false" && thisFileUri != null) { saveFile() this@MainActivity.runOnUiThread { @@ -257,7 +330,11 @@ class MainActivity : AppCompatActivity() { super.onResume() loadMetaFromSharedPrefs() - if (thisFileUri !== null) { + if (intent.data !== null) { + readFile(intent.data!!) + Log.d(javaClass.simpleName,"Loading on resume from intent: ${thisFileUri}") + intent.data = null + } else if (thisFileUri !== null ) { readFile(thisFileUri!!) Log.d(javaClass.simpleName,"Loading on resume from thisFileUri: ${thisFileUri}") } @@ -322,7 +399,9 @@ class MainActivity : AppCompatActivity() { pushImage(uri) intent.putExtra(Intent.EXTRA_STREAM, null as Uri?) } else if (mimeType == "text" && intent.data != null ) { - readFile(intent.data!!) + thisFileUri = uri + saveMetaToSharedPrefs() + Log.d(javaClass.simpleName,"wanna start app with new txt") } } } @@ -977,7 +1056,7 @@ class MainActivity : AppCompatActivity() { } @Throws(IOException::class) - private fun readFile(uri: Uri): Boolean { + private fun readFile(uri: Uri) { //TODO: Which permissions are really needed? try { contentResolver.takePersistableUriPermission( @@ -1044,22 +1123,32 @@ class MainActivity : AppCompatActivity() { }) return@withContext } + while (!easyMDELoaded) { + sleep(100) + Log.d(javaClass.simpleName,"waiting for easyMDE") + } this@MainActivity.runOnUiThread({ - val script = "easyMDE.codemirror.doc.setValue(`${mdeValue}`);" + - "easyMDE.codemirror.doc.setCursor(JSON.parse(`${JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line"))}`));" + - "pasteText();" + + val script = "if (typeof easyMDE !== 'undefined') {" + + "easyMDE.codemirror.doc.setValue(`${mdeValue}`);" + + "easyMDE.codemirror.doc.markClean();" + + "easyMDE.codemirror.focus();" + + "easyMDE.codemirror.doc.setCursor(JSON.parse(`${metaData.cursor}`));" + + "pasteText();}" + Log.d(javaClass.simpleName, "executing in webview:\n${script}") - webView.evaluateJavascript(script, {}) + webView.evaluateJavascript(script, { + thisFileUri = uri + Log.d(javaClass.simpleName,"File read: ${thisFileUri}") + }) }) - thisFileUri = uri - saveMetaToSharedPrefs() - Log.d(javaClass.simpleName,"File read: ${thisFileUri}") + } } - return true } private fun saveMetaToSharedPrefs() { + Log.d(javaClass.simpleName, "saving to shared prefs cursor: ${metaData.cursor} in file: ${thisFileUri}") getSharedPreferences("prefs", Context.MODE_PRIVATE) .edit().apply { putString("lastFile", thisFileUri.toString()) @@ -1076,7 +1165,7 @@ class MainActivity : AppCompatActivity() { selectFileForSaveAs() } else { thisFileUri = parse(uriString) - metaData.cursor = prefs.getString("cursor", "nocursor") + metaData.cursor = prefs.getString("cursor", "nocursor") ?: "{ line: 0, ch: 0, sticky: null }" Log.i(javaClass.simpleName,"Loaded cursor: ${metaData.cursor}") } } diff --git a/app/src/main/java/org/wntr/mdeditor/mdMeta.kt b/app/src/main/java/org/wntr/mdeditor/mdMeta.kt index dd27573..2b3fdd2 100644 --- a/app/src/main/java/org/wntr/mdeditor/mdMeta.kt +++ b/app/src/main/java/org/wntr/mdeditor/mdMeta.kt @@ -11,7 +11,8 @@ class mdMeta { var metaData = mutableMapOf() var ID: String? = null var updatedAt: String? = null - var cursor: String? = null + var cursor: String = "{ line: 0, ch: 0, sticky: null }" + fun put(key: String, value: String) { metaData.put(key, value) diff --git a/app/src/main/res/raw/controller.js b/app/src/main/res/raw/controller.js index f16a140..42b6fbf 100644 --- a/app/src/main/res/raw/controller.js +++ b/app/src/main/res/raw/controller.js @@ -86,61 +86,3 @@ function toggleTheme() { if (i>themes.length-1) i=0 } -const easyMDE = new EasyMDE({ - spellChecker: false, - nativeSpellcheck: false, - maxHeight: String(windowHeight-120)+"px", - inputStyle: "textarea", - autoDownloadFontAwesome: false, - theme: "solarized", - status: [ - { - className: "editor-statusbar-left", - onUpdate: (el) => { - el.innerHTML = "" - } - }, - { - className: "displayName", - defaultValue: "None", - onUpdate: (el) => { - el.innerHTML = `${displayName()}` - }, - }, "lines", "words", "cursor", - { - className: "editor-statusbar-right", - onUpdate: (el) => { - el.innerHTML = "" - } - } - ], - toolbar: [ - { - name: "toggleTheme", - action: toggleTheme, - className: "fa fa-moon", - title: "Toggle Theme" - }, - { - name: "share", - action: shareText, - className: "fa fa-share-nodes", - title: "Share" - },"strikethrough", "horizontal-rule","undo", - { - name: "preview", - action: myPreview, - className: "fa fa-eye", - title: "Preview", - noDisable: true - },"redo", - "bold", "italic","link","code", - { - name: "toggle", - action: toggleBar, - className: "fa fa-expand", - title: "Toggle Bar", - } - ] -}); -onRead(); \ No newline at end of file