From 41816099d3be8731887f6acae85b73408c0c9446 Mon Sep 17 00:00:00 2001 From: soupslurpr <92235850+soupslurpr@users.noreply.github.com> Date: Thu, 25 Jan 2024 23:53:04 -0800 Subject: [PATCH] Don't compile Markdown on first load when render markdown is off --- .../dev/soupslurpr/beautyxt/BeauTyXT.kt | 22 ++++++++++++++----- .../dev/soupslurpr/beautyxt/MainActivity.kt | 6 ++++- .../soupslurpr/beautyxt/ui/FileViewModel.kt | 13 +++++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt b/app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt index 7ed621b..cc2741d 100644 --- a/app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt +++ b/app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt @@ -517,9 +517,15 @@ fun BeauTyXTApp( val preferencesUiState by preferencesViewModel.uiState.collectAsState() + var previewMarkdownRenderedToFullscreen by rememberSaveable { mutableStateOf(false) } + val openFileLauncher = rememberLauncherForActivityResult(contract = OpenDocument()) { if (it != null) { - fileViewModel.bindIsolatedService(it) + fileViewModel.bindIsolatedService( + it, + preferencesUiState.renderMarkdown.second.value, + previewMarkdownRenderedToFullscreen + ) fileViewModel.setReadOnly(false) fileViewModel.setUri(it, context) navController.navigate(BeauTyXTScreens.FileEdit.name) @@ -528,7 +534,11 @@ fun BeauTyXTApp( val createTxtFileLauncher = rememberLauncherForActivityResult(contract = CreateDocument(mimeTypePlainText)) { if (it != null) { - fileViewModel.bindIsolatedService(it) + fileViewModel.bindIsolatedService( + it, + preferencesUiState.renderMarkdown.second.value, + previewMarkdownRenderedToFullscreen + ) fileViewModel.setReadOnly(false) fileViewModel.setUri(it, context) navController.navigate(BeauTyXTScreens.FileEdit.name) @@ -537,7 +547,11 @@ fun BeauTyXTApp( val createMdFileLauncher = rememberLauncherForActivityResult(contract = CreateDocument(mimeTypeMarkdown)) { if (it != null) { - fileViewModel.bindIsolatedService(it) + fileViewModel.bindIsolatedService( + it, + preferencesUiState.renderMarkdown.second.value, + previewMarkdownRenderedToFullscreen + ) fileViewModel.setReadOnly(false) fileViewModel.setUri(it, context) navController.navigate(BeauTyXTScreens.FileEdit.name) @@ -629,8 +643,6 @@ fun BeauTyXTApp( } } - var previewMarkdownRenderedToFullscreen by rememberSaveable { mutableStateOf(false) } - var previewTypstProjectRenderedToFullscreen by rememberSaveable { mutableStateOf(false) } val randomValue = Random.nextInt(0, 10) diff --git a/app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt b/app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt index ab41dc3..a5184f3 100644 --- a/app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt +++ b/app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt @@ -44,7 +44,11 @@ class MainActivity : ComponentActivity() { fileViewModel.setReadOnly(readOnly) intent.data?.let { - fileViewModel.bindIsolatedService(it) + fileViewModel.bindIsolatedService( + it, + preferencesViewModel.uiState.value.renderMarkdown.second.value, + false, + ) fileViewModel.setUri(it, LocalContext.current) } } diff --git a/app/src/main/kotlin/dev/soupslurpr/beautyxt/ui/FileViewModel.kt b/app/src/main/kotlin/dev/soupslurpr/beautyxt/ui/FileViewModel.kt index ca331b0..b3a1cfa 100644 --- a/app/src/main/kotlin/dev/soupslurpr/beautyxt/ui/FileViewModel.kt +++ b/app/src/main/kotlin/dev/soupslurpr/beautyxt/ui/FileViewModel.kt @@ -29,6 +29,7 @@ import kotlinx.coroutines.launch import java.io.BufferedReader import java.io.FileOutputStream import java.io.InputStreamReader +import kotlin.properties.Delegates private const val TAG = "FileViewModel" @@ -42,6 +43,9 @@ class FileViewModel(application: Application) : AndroidViewModel(application) { var rustService: IFileViewModelRustLibraryAidlInterface? = null + private var renderMarkdown by Delegates.notNull() + private var previewMarkdownRenderedToHtmlFullscreen by Delegates.notNull() + private val serviceConnection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName?, service: IBinder?) { val rustService = IFileViewModelRustLibraryAidlInterface.Stub.asInterface(service) @@ -50,7 +54,9 @@ class FileViewModel(application: Application) : AndroidViewModel(application) { this@FileViewModel.rustService = rustService - if (uiState.value.mimeType.value == mimeTypeMarkdown) { + if ((renderMarkdown or previewMarkdownRenderedToHtmlFullscreen) + && uiState.value.mimeType.value == mimeTypeMarkdown + ) { _uiState.value.contentConvertedToHtml.value = rustService.markdownToHtml(uiState.value.content.value) } } @@ -62,7 +68,10 @@ class FileViewModel(application: Application) : AndroidViewModel(application) { private val intentService = Intent(getApplication(), FileViewModelRustLibraryIsolatedService::class.java) - fun bindIsolatedService(uri: Uri) { + fun bindIsolatedService(uri: Uri, renderMarkdown: Boolean, previewMarkdownRenderedToHtmlFullscreen: Boolean) { + this.renderMarkdown = renderMarkdown + this.previewMarkdownRenderedToHtmlFullscreen = previewMarkdownRenderedToHtmlFullscreen + getApplication().bindIsolatedService( intentService, Context.BIND_AUTO_CREATE,