wait till editor's loaded before start

hauntED
yova 4 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() { class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding 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 { companion object {
const val CREATE_FILE = 1 const val CREATE_FILE = 1
@ -76,7 +136,8 @@ class MainActivity : AppCompatActivity() {
var ghostConnection = false var ghostConnection = false
lateinit var credManager: CredentialManager lateinit var credManager: CredentialManager
var intentScheme = "none" var intentScheme = "none"
var lastSaved = Instant.now().toEpochMilli() var easyMDELoaded = false
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -101,6 +162,16 @@ class MainActivity : AppCompatActivity() {
Log.d("WebView", consoleMessage.message()) Log.d("WebView", consoleMessage.message())
return true 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") webView.loadUrl("file:///android_res/raw/index.html")
@ -159,7 +230,7 @@ class MainActivity : AppCompatActivity() {
} }
@JavascriptInterface @JavascriptInterface
fun triggerGhost(sharedText: String, ) { fun triggerGhost(sharedText: String) {
shareGhost(sharedText, ::sendPost) shareGhost(sharedText, ::sendPost)
} }
@ -239,9 +310,11 @@ class MainActivity : AppCompatActivity() {
ghostMetaData = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { ghostMetaData = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
saveFile() saveFile()
} }
fixedRateTimer("timer",true,0,5000){ fixedRateTimer("timer",true,0,5000){
this@MainActivity.runOnUiThread { this@MainActivity.runOnUiThread {
webView.evaluateJavascript("easyMDE.codemirror.doc.isClean();", { val script = "easyMDE.codemirror.doc.isClean();"
webView.evaluateJavascript(script , {
if (it == "false" && thisFileUri != null) { if (it == "false" && thisFileUri != null) {
saveFile() saveFile()
this@MainActivity.runOnUiThread { this@MainActivity.runOnUiThread {
@ -257,7 +330,11 @@ class MainActivity : AppCompatActivity() {
super.onResume() super.onResume()
loadMetaFromSharedPrefs() 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!!) readFile(thisFileUri!!)
Log.d(javaClass.simpleName,"Loading on resume from thisFileUri: ${thisFileUri}") Log.d(javaClass.simpleName,"Loading on resume from thisFileUri: ${thisFileUri}")
} }
@ -322,7 +399,9 @@ class MainActivity : AppCompatActivity() {
pushImage(uri) pushImage(uri)
intent.putExtra(Intent.EXTRA_STREAM, null as Uri?) intent.putExtra(Intent.EXTRA_STREAM, null as Uri?)
} else if (mimeType == "text" && intent.data != null ) { } 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) @Throws(IOException::class)
private fun readFile(uri: Uri): Boolean { private fun readFile(uri: Uri) {
//TODO: Which permissions are really needed? //TODO: Which permissions are really needed?
try { try {
contentResolver.takePersistableUriPermission( contentResolver.takePersistableUriPermission(
@ -1044,22 +1123,32 @@ class MainActivity : AppCompatActivity() {
}) })
return@withContext return@withContext
} }
while (!easyMDELoaded) {
sleep(100)
Log.d(javaClass.simpleName,"waiting for easyMDE")
}
this@MainActivity.runOnUiThread({ this@MainActivity.runOnUiThread({
val script = "easyMDE.codemirror.doc.setValue(`${mdeValue}`);" +
"easyMDE.codemirror.doc.setCursor(JSON.parse(`${JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line"))}`));" + val script = "if (typeof easyMDE !== 'undefined') {" +
"pasteText();" "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}") 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() { private fun saveMetaToSharedPrefs() {
Log.d(javaClass.simpleName, "saving to shared prefs cursor: ${metaData.cursor} in file: ${thisFileUri}")
getSharedPreferences("prefs", Context.MODE_PRIVATE) getSharedPreferences("prefs", Context.MODE_PRIVATE)
.edit().apply { .edit().apply {
putString("lastFile", thisFileUri.toString()) putString("lastFile", thisFileUri.toString())
@ -1076,7 +1165,7 @@ class MainActivity : AppCompatActivity() {
selectFileForSaveAs() selectFileForSaveAs()
} else { } else {
thisFileUri = parse(uriString) 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}") Log.i(javaClass.simpleName,"Loaded cursor: ${metaData.cursor}")
} }
} }

@ -11,7 +11,8 @@ class mdMeta {
var metaData = mutableMapOf<String,String>() var metaData = mutableMapOf<String,String>()
var ID: String? = null var ID: String? = null
var updatedAt: 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) { fun put(key: String, value: String) {
metaData.put(key, value) metaData.put(key, value)

@ -86,61 +86,3 @@ function toggleTheme() {
if (i>themes.length-1) i=0 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