@ -3,7 +3,6 @@ package org.wntr.mdeditor
import android.app.Activity
import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.net.Uri.parse
@ -37,7 +36,6 @@ import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import org.json.JSONObject
import org.wntr.mdeditor.databinding.ActivityMainBinding
import retrofit2.Response
import java.io.BufferedReader
import java.io.File
@ -52,7 +50,6 @@ import kotlin.concurrent.fixedRateTimer
class MainActivity : AppCompatActivity ( ) {
private lateinit var binding : ActivityMainBinding
private val easyMDEscript = """
const easyMDE = new EasyMDE ( {
spellChecker : false ,
@ -62,69 +59,64 @@ class MainActivity : AppCompatActivity() {
autoDownloadFontAwesome : false ,
theme : " solarized " ,
status : [
{
className : " editor-statusbar-left " ,
onUpdate : ( el ) = > {
el . innerHTML = saveStatus ( )
}
} ,
{
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> "
}
}
{
className : " editor-statusbar-left " ,
onUpdate : ( el ) = > {
el . innerHTML = saveStatus ( )
}
} ,
{
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 " ,
}
{
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
const val OPEN _FILE = 2
const val DELETE _GHOST = 4
var deleteVisible = false
var mdeValue : String = " "
var metaData = mdMeta ( )
var mdToAppend : String = " "
var thisFileUri : Uri ? = null
var truncate = false
lateinit var tempFile : File
lateinit var pickMultipleVisualMedia : ActivityResultLauncher < PickVisualMediaRequest >
lateinit var ghostSettings : ActivityResultLauncher < Intent >
lateinit var ghostMetaData : ActivityResultLauncher < Intent >
@ -139,7 +131,6 @@ class MainActivity : AppCompatActivity() {
}
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
setContentView ( R . layout . activity _main )
@ -173,19 +164,9 @@ class MainActivity : AppCompatActivity() {
webView . loadUrl ( " file:///android_res/raw/index.html " )
val jsi = object {
@JavascriptInterface
fun getValue ( ) : String {
return mdeValue
}
@JavascriptInterface
fun reportChange ( value : String ) {
Log . i ( javaClass . simpleName , value )
}
@JavascriptInterface
fun triggerSaveFile ( value : String ) {
mdeValue = value
fun triggerSaveFile ( ) {
saveFile ( )
}
@ -198,11 +179,6 @@ class MainActivity : AppCompatActivity() {
return height
} * /
@JavascriptInterface
fun refresh ( ) {
readFile ( thisFileUri !! )
}
@JavascriptInterface
fun triggerDisplayName ( ) : String {
return getDisplayName ( applicationContext , thisFileUri )
@ -210,12 +186,17 @@ class MainActivity : AppCompatActivity() {
@JavascriptInterface
fun triggerShare ( sharedText : String ) {
shareHtml ( sharedText )
}
@JavascriptInterface
fun triggerGhost ( sharedText : String ) {
shareGhost ( sharedText , :: sendPost )
val htmlFile = createHtmlFile ( applicationContext , sharedText )
if ( htmlFile == null ) return
val htmlUri = FileProvider . getUriForFile ( applicationContext , " org.wntr.mdeditor.fileprovider " , htmlFile )
with ( Intent ( Intent . ACTION _SEND ) ) {
htmlFile ?. also {
type = " text/plain "
putExtra ( Intent . EXTRA _STREAM , htmlUri )
startActivity ( Intent . createChooser ( this , " Share html " ) )
}
}
}
@JavascriptInterface
@ -225,13 +206,6 @@ class MainActivity : AppCompatActivity() {
return md
}
@JavascriptInterface
fun getCursor ( ) : String {
val cursor = JSONObject ( JSONObject ( metaData . cursor ) , arrayOf ( " ch " , " line " ) )
Log . i ( javaClass . simpleName , " delivering cursor: $cursor " )
return cursor . toString ( )
}
@JavascriptInterface
fun isFullscreen ( ) : Boolean {
return supportActionBar !! . isShowing
@ -315,7 +289,19 @@ class MainActivity : AppCompatActivity() {
Log . d ( javaClass . simpleName , " AutosaveTimer started. " )
return
}
loadMetaFromSharedPrefs ( )
if ( ! loadMetaFromSharedPrefs ( applicationContext ) ) {
with ( AlertDialog . Builder ( this ) ) {
setPositiveButton ( " Open " , { dialog , id ->
openFile ( )
} )
setNeutralButton ( " New " , { dialog , id ->
selectFileForSaveAs ( )
} )
show ( )
}
}
if ( intent . data !== null ) {
readFile ( intent . data !! )
Log . d ( javaClass . simpleName , " Loading on resume from intent: ${thisFileUri} " )
@ -377,7 +363,7 @@ class MainActivity : AppCompatActivity() {
intent . putExtra ( Intent . EXTRA _STREAM , null as Uri ? )
} else if ( mimeType == " text " && intent . data != null ) {
thisFileUri = uri
saveMetaToSharedPrefs ( )
saveMetaToSharedPrefs ( applicationContext )
Log . d ( javaClass . simpleName , " wanna start app with new txt " )
}
}
@ -705,62 +691,7 @@ class MainActivity : AppCompatActivity() {
return true
}
fun shareHtml ( htmlString : String ) : Boolean {
try {
createHTMLFile ( )
} catch ( e : Exception ) {
Log . d ( javaClass . simpleName , " Problem with accessing file \n ${e.stackTraceToString()} " )
Toast . makeText (
this ,
" Problem with accessing file \n $e " ,
Toast . LENGTH _LONG
) . show ( )
return false
}
try {
FileOutputStream ( tempFile ) . use {
it . write ( htmlString . toByteArray ( ) )
}
} catch ( e : Exception ) {
Toast . makeText (
this ,
" Error during writing. \n $e " ,
Toast . LENGTH _LONG
) . show ( )
return false
}
Log . d ( javaClass . simpleName , " File saved: ${tempFile.toURI()} " )
Toast . makeText (
this ,
" HTML output produced. " ,
Toast . LENGTH _LONG
) . show ( )
val htmlUri = FileProvider . getUriForFile ( this , " org.wntr.mdeditor.fileprovider " , tempFile )
with ( Intent ( Intent . ACTION _SEND ) ) {
tempFile ?. also {
type = " text/plain "
putExtra ( Intent . EXTRA _STREAM , htmlUri )
startActivity ( Intent . createChooser ( this , " Share html " ) )
}
}
return true
}
@Throws ( IOException :: class )
private fun createHTMLFile ( ) {
// Create an image file name
val storageDir = File ( cacheDir , " html " )
storageDir . mkdir ( )
tempFile = File ( storageDir . path + " / ${getDisplayName(applicationContext, thisFileUri).split(".")[0]} .html " )
if ( tempFile . exists ( ) ) tempFile . delete ( )
tempFile . createNewFile ( )
}
fun openFile ( ) {
fun openFile ( ) {
val intent = Intent ( Intent . ACTION _OPEN _DOCUMENT ) . apply {
addCategory ( Intent . CATEGORY _OPENABLE )
putExtra ( DocumentsContract . EXTRA _INITIAL _URI , thisFileUri )
@ -774,8 +705,6 @@ class MainActivity : AppCompatActivity() {
startActivityForResult ( intent , OPEN _FILE )
}
fun selectFileForSaveAs ( ) {
val intent = Intent ( Intent . ACTION _CREATE _DOCUMENT ) . apply {
addCategory ( Intent . CATEGORY _OPENABLE )
@ -837,7 +766,7 @@ class MainActivity : AppCompatActivity() {
Intent . FLAG _GRANT _READ _URI _PERMISSION and Intent . FLAG _GRANT _WRITE _URI _PERMISSION
)
thisFileUri = uri
saveMetaToSharedPrefs ( )
saveMetaToSharedPrefs ( applicationContext )
if ( metaData . metaData . get ( " url " ) == null ) {
deleteVisible = false
invalidateOptionsMenu ( )
@ -975,7 +904,7 @@ class MainActivity : AppCompatActivity() {
Toast . LENGTH _LONG
) . show ( )
textFile . close ( )
saveMetaToSharedPrefs ( )
saveMetaToSharedPrefs ( applicationContext )
Log . i ( javaClass . simpleName , " file newly written " )
} catch ( e : Exception ) {
Toast . makeText (
@ -1136,38 +1065,6 @@ class MainActivity : AppCompatActivity() {
}
}
private fun saveMetaToSharedPrefs ( ) {
if ( thisFileUri == null ) return
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 ( ) )
putString ( " cursor " , metaData . cursor )
apply ( )
}
}
private fun loadMetaFromSharedPrefs ( ) {
val prefs = getSharedPreferences ( " prefs " , Context . MODE _PRIVATE )
val uriString = prefs . getString ( " lastFile " , " noLastFile " )
if ( uriString == " noLastFile " ) {
with ( AlertDialog . Builder ( this ) ) {
setPositiveButton ( " Open " , { dialog , id ->
openFile ( )
} )
setNeutralButton ( " New " , { dialog , id ->
selectFileForSaveAs ( )
} )
show ( )
}
} else {
thisFileUri = parse ( uriString )
metaData . cursor = prefs . getString ( " cursor " , " nocursor " ) ?: " { line: 0, ch: 0, sticky: null } "
Log . i ( javaClass . simpleName , " Loaded cursor: ${metaData.cursor} " )
}
}
override fun onPause ( ) {
super . onPause ( )
try {
@ -1179,7 +1076,7 @@ class MainActivity : AppCompatActivity() {
webView . evaluateJavascript ( " easyMDE.codemirror.doc.getCursor(); " ) {
metaData . cursor = it
Log . i ( javaClass . simpleName , " Cursor: $it " )
saveMetaToSharedPrefs ( )
saveMetaToSharedPrefs ( applicationContext )
}
Log . i ( javaClass . simpleName , " \" onPause \" durchlaufen " )
}
@ -1189,5 +1086,3 @@ class MainActivity : AppCompatActivity() {
Log . i ( javaClass . simpleName , " \" onRestoreInstanceState \" durchlaufen " )
}
}