Compare commits

...

3 Commits

Author SHA1 Message Date
yova 482c3e9756 toggle themes
4 months ago
yova 96c2fefd0d better read
4 months ago
yova 2b816a923d run openInputstream in IO context
4 months ago

@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.net.Uri.parse import android.net.Uri.parse
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.ParcelFileDescriptor import android.os.ParcelFileDescriptor
import android.provider.DocumentsContract import android.provider.DocumentsContract
@ -16,6 +17,8 @@ import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.Menu.NONE import android.view.Menu.NONE
import android.view.MenuItem import android.view.MenuItem
import android.view.WindowInsets
import android.view.WindowManager
import android.webkit.ConsoleMessage import android.webkit.ConsoleMessage
import android.webkit.JavascriptInterface import android.webkit.JavascriptInterface
import android.webkit.ValueCallback import android.webkit.ValueCallback
@ -169,17 +172,43 @@ class MainActivity : AppCompatActivity() {
@JavascriptInterface @JavascriptInterface
fun getCursor(): String { fun getCursor(): String {
Log.i(javaClass.simpleName,"delivering cursor: ${metaData.cursor}")
val cursor = JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line")) val cursor = JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line"))
Log.i(javaClass.simpleName,"delivering cursor: $cursor") Log.i(javaClass.simpleName,"delivering cursor: $cursor")
return cursor.toString() return cursor.toString()
} }
@JavascriptInterface
fun isFullscreen() : Boolean{
return supportActionBar!!.isShowing
}
@JavascriptInterface @JavascriptInterface
fun toggleBar() { fun toggleBar() {
this@MainActivity.runOnUiThread({ this@MainActivity.runOnUiThread({
if (supportActionBar!!.isShowing) supportActionBar!!.hide() if (supportActionBar!!.isShowing) {
else supportActionBar!!.show() @Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
0
)
}
supportActionBar!!.hide()
}
else {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.insetsController?.show(WindowInsets.Type.statusBars())
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
supportActionBar!!.show()
}
}) })
} }
} }
@ -228,28 +257,18 @@ class MainActivity : AppCompatActivity() {
super.onResume() super.onResume()
loadMetaFromSharedPrefs() loadMetaFromSharedPrefs()
CoroutineScope(Dispatchers.Main).launch { if (thisFileUri !== null) {
withContext(Dispatchers.IO) { readFile(thisFileUri!!)
//if (contentResolver.getType(parse(intent.dataString))!!.split("/")[0] == "text" && intentScheme == "content") { Log.d(javaClass.simpleName,"Loading on resume from thisFileUri: ${thisFileUri}")
if (intent.data != null) { }
if (contentResolver.getType(parse(intent.dataString))!!.split("/")[0] == "text") {
readFile(intent.data!!)
intent.data = null
}
} else { // No start with intent data. Load previously used file
if (thisFileUri !== null) readFile(thisFileUri!!)
}
}
webView.evaluateJavascript("onRead();") {}
if (metaData.metaData.get("url") !== null) { if (metaData.metaData.get("url") !== null) {
val url = parse(metaData.metaData.get("url")) val url = parse(metaData.metaData.get("url"))
deleteVisible = true deleteVisible = true
invalidateOptionsMenu() invalidateOptionsMenu()
val apiHost = url.scheme + "://" + url.host val apiHost = url.scheme + "://" + url.host
Log.i(javaClass.simpleName, "Starting api controller for: $apiHost") Log.i(javaClass.simpleName, "Starting api controller for: $apiHost")
api = ghostAPI(applicationContext, apiHost) api = ghostAPI(applicationContext, apiHost)
}
} }
} }
override fun onNewIntent(intent:Intent) { override fun onNewIntent(intent:Intent) {
@ -281,7 +300,7 @@ class MainActivity : AppCompatActivity() {
} }
} }
} }
Log.i(javaClass.simpleName,"Uri: $uri") Log.i(javaClass.simpleName,"Got a new intent with URI: $uri")
if (uri != null && uri.toString()!="null") { if (uri != null && uri.toString()!="null") {
intentScheme = uri.scheme!! intentScheme = uri.scheme!!
if (intentScheme == "content") { if (intentScheme == "content") {
@ -296,7 +315,6 @@ class MainActivity : AppCompatActivity() {
if (intentScheme == "link" && uri.toString() !="null") { if (intentScheme == "link" && uri.toString() !="null") {
mdToAppend += "[](${uri})\n" mdToAppend += "[](${uri})\n"
webView.evaluateJavascript("onRead();"){}
intent.putExtra(Intent.EXTRA_TEXT, null as CharSequence?) intent.putExtra(Intent.EXTRA_TEXT, null as CharSequence?)
} }
if (intentScheme == "content") { if (intentScheme == "content") {
@ -304,12 +322,7 @@ 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 ) {
CoroutineScope(Dispatchers.Main).launch { readFile(intent.data!!)
withContext(Dispatchers.IO) {
readFile(intent.data!!)
}
webView.evaluateJavascript("onRead();") {}
}
} }
} }
} }
@ -831,8 +844,8 @@ class MainActivity : AppCompatActivity() {
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION and Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION and Intent.FLAG_GRANT_WRITE_URI_PERMISSION
) )
readFile(uri) thisFileUri = uri
saveMetaToSharedPrefs()
if (metaData.metaData.get("url") == null) { if (metaData.metaData.get("url") == null) {
deleteVisible = false deleteVisible = false
invalidateOptionsMenu() invalidateOptionsMenu()
@ -990,48 +1003,59 @@ class MainActivity : AppCompatActivity() {
).show() ).show()
}) })
} }
try { CoroutineScope(Dispatchers.Main).launch {
contentResolver.openInputStream(uri)?.use { inputStream -> withContext(Dispatchers.IO) {
BufferedReader(InputStreamReader(inputStream)).use { reader -> try {
var i = "" contentResolver.openInputStream(uri)?.use { inputStream ->
if (reader.ready()) { BufferedReader(InputStreamReader(inputStream)).use { reader ->
i = reader.readLine() var i = ""
var line: String? = "" if (reader.ready()) {
while (line != null) { i = reader.readLine()
i += line + '\n' var line: String? = ""
line = reader.readLine() while (line != null) {
i += line + '\n'
line = reader.readLine()
}
}
mdeValue = metaData.extractMetadataFromMarkdown(i)
} }
mdeValue = metaData.extractMetadataFromMarkdown(i)
} }
} }
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",
Toast.LENGTH_LONG
).show()
})
return@withContext
}
catch (e: Exception) {
Log.d(javaClass.simpleName, e.stackTraceToString())
this@MainActivity.runOnUiThread({
Toast.makeText(
this@MainActivity,
"Error during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
return@withContext
}
this@MainActivity.runOnUiThread({
val script = "easyMDE.codemirror.doc.setValue(`${mdeValue}`);" +
"easyMDE.codemirror.doc.setCursor(JSON.parse(`${JSONObject(JSONObject(metaData.cursor), arrayOf("ch", "line"))}`));" +
"pasteText();"
Log.d(javaClass.simpleName, "executing in webview:\n${script}")
webView.evaluateJavascript(script, {})
})
thisFileUri = uri
saveMetaToSharedPrefs()
Log.d(javaClass.simpleName,"File read: ${thisFileUri}")
} }
} catch (e: java.io.FileNotFoundException) {
Log.d(javaClass.simpleName, "File not found during reading:\n${e.stackTraceToString()}")
this.runOnUiThread({
selectFileForSaveAs()
Toast.makeText(
this,
"File not found during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
} }
catch (e: Exception) {
Log.d(javaClass.simpleName, e.stackTraceToString())
this.runOnUiThread({
Toast.makeText(
this,
"Error during reading.\n$e",
Toast.LENGTH_LONG
).show()
})
return false
}
thisFileUri = uri
saveMetaToSharedPrefs()
this.runOnUiThread({
webView.evaluateJavascript("onRead();", {})
})
return true return true
} }

@ -2,7 +2,6 @@ function onRead() {
easyMDE.codemirror.doc.setValue(Android.getValue()) easyMDE.codemirror.doc.setValue(Android.getValue())
easyMDE.codemirror.doc.markClean() easyMDE.codemirror.doc.markClean()
easyMDE.codemirror.focus() easyMDE.codemirror.focus()
console.log(Android.getCursor())
easyMDE.codemirror.doc.setCursor(JSON.parse(Android.getCursor())) easyMDE.codemirror.doc.setCursor(JSON.parse(Android.getCursor()))
pasteText() pasteText()
} }
@ -68,15 +67,29 @@ function pasteText() {
document.getElementsByClassName("CodeMirror-scroll")[0].dispatchEvent(event); document.getElementsByClassName("CodeMirror-scroll")[0].dispatchEvent(event);
saveFile() saveFile()
} }
windowHeight = window.innerHeight
function toggleBar() { function toggleBar() {
if (Android.isFullscreen()) {
easyMDE.codemirror.getScrollerElement().style.height=String(windowHeight-40)+"px"
} else {
easyMDE.codemirror.getScrollerElement().style.height=String(windowHeight-120)+"px"
}
Android.toggleBar() Android.toggleBar()
easyMDE.codemirror.focus() easyMDE.codemirror.focus()
} }
themes = [ "solarized", "3024-night"]
i=1
function toggleTheme() {
easyMDE.codemirror.setOption("theme",themes[i])
i++
if (i>themes.length-1) i=0
}
const easyMDE = new EasyMDE({ const easyMDE = new EasyMDE({
spellChecker: false, spellChecker: false,
nativeSpellcheck: false, nativeSpellcheck: false,
maxHeight: String(window.innerHeight-120)+"px", maxHeight: String(windowHeight-120)+"px",
inputStyle: "textarea", inputStyle: "textarea",
autoDownloadFontAwesome: false, autoDownloadFontAwesome: false,
theme: "solarized", theme: "solarized",
@ -103,24 +116,17 @@ const easyMDE = new EasyMDE({
], ],
toolbar: [ toolbar: [
{ {
name: "day", name: "toggleTheme",
action: () => easyMDE.codemirror.setOption("theme","solarized"), action: toggleTheme,
className: "fa fa-sun",
title: "Day Theme"
},
{
name: "night",
action: () => easyMDE.codemirror.setOption("theme","3024-night"),
className: "fa fa-moon", className: "fa fa-moon",
title: "Night Theme" title: "Toggle Theme"
}, },
"guide",
{ {
name: "share", name: "share",
action: shareText, action: shareText,
className: "fa fa-share-nodes", className: "fa fa-share-nodes",
title: "Share" title: "Share"
}, "undo", },"strikethrough", "horizontal-rule","undo",
{ {
name: "preview", name: "preview",
action: myPreview, action: myPreview,

Loading…
Cancel
Save