better read

hauntED
yova 4 months ago
parent 2b816a923d
commit 96c2fefd0d

@ -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
@ -175,11 +178,38 @@ class MainActivity : AppCompatActivity() {
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()
}
}) })
} }
} }
@ -235,9 +265,11 @@ class MainActivity : AppCompatActivity() {
if (contentResolver.getType(parse(intent.dataString))!!.split("/")[0] == "text") { if (contentResolver.getType(parse(intent.dataString))!!.split("/")[0] == "text") {
readFile(intent.data!!) readFile(intent.data!!)
intent.data = null intent.data = null
} Log.d(javaClass.simpleName,"Loading on resume from intent data: ${intent.data}")
} else {}
} else { // No start with intent data. Load previously used file } else { // No start with intent data. Load previously used file
if (thisFileUri !== null) readFile(thisFileUri!!) if (thisFileUri !== null) readFile(thisFileUri!!)
Log.d(javaClass.simpleName,"Loading on resume from thisFileUri: ${thisFileUri}")
} }
} }
webView.evaluateJavascript("onRead();") {} webView.evaluateJavascript("onRead();") {}
@ -281,7 +313,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") {
@ -831,8 +863,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,9 +1022,9 @@ class MainActivity : AppCompatActivity() {
).show() ).show()
}) })
} }
try { CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.Main).launch { withContext(Dispatchers.IO) {
withContext(Dispatchers.IO) { try {
contentResolver.openInputStream(uri)?.use { inputStream -> contentResolver.openInputStream(uri)?.use { inputStream ->
BufferedReader(InputStreamReader(inputStream)).use { reader -> BufferedReader(InputStreamReader(inputStream)).use { reader ->
var i = "" var i = ""
@ -1003,39 +1035,42 @@ class MainActivity : AppCompatActivity() {
i += line + '\n' i += line + '\n'
line = reader.readLine() 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()
})
}
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()
})
}
this@MainActivity.runOnUiThread({
val script = "easyMDE.codemirror.doc.setValue(`${mdeValue}`);"
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
} }

@ -68,7 +68,14 @@ 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()
} }
@ -76,7 +83,7 @@ function toggleBar() {
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",

Loading…
Cancel
Save