wait till editor's loaded before start

hauntED
yova 3 months ago
parent 482c3e9756
commit 79d7855973

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="10.0.0.10:5555" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-01-23T18:08:32.458084060Z" />
</component>
</project>

@ -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 = "<i class=\"fa fa-circle\"></i>"
}
},
{
className: "displayName",
defaultValue: "None",
onUpdate: (el) => {
el.innerHTML = displayName()
},
}, "lines", "words", "cursor",
{
className: "editor-statusbar-right",
onUpdate: (el) => {
el.innerHTML = "<i class=\"fa fa-square\"></i>"
}
}
],
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}")
}
}

@ -11,7 +11,8 @@ class mdMeta {
var metaData = mutableMapOf<String,String>()
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)

@ -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 = "<i class=\"fa fa-circle\"></i>"
}
},
{
className: "displayName",
defaultValue: "None",
onUpdate: (el) => {
el.innerHTML = `${displayName()}`
},
}, "lines", "words", "cursor",
{
className: "editor-statusbar-right",
onUpdate: (el) => {
el.innerHTML = "<i class=\"fa fa-square\"></i>"
}
}
],
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();
Loading…
Cancel
Save