Skip to content

Commit

Permalink
Don't compile Markdown on first load when render markdown is off
Browse files Browse the repository at this point in the history
  • Loading branch information
soupslurpr committed Jan 26, 2024
1 parent 1127775 commit 4181609
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
22 changes: 17 additions & 5 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -629,8 +643,6 @@ fun BeauTyXTApp(
}
}

var previewMarkdownRenderedToFullscreen by rememberSaveable { mutableStateOf(false) }

var previewTypstProjectRenderedToFullscreen by rememberSaveable { mutableStateOf(false) }

val randomValue = Random.nextInt(0, 10)
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/ui/FileViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -42,6 +43,9 @@ class FileViewModel(application: Application) : AndroidViewModel(application) {

var rustService: IFileViewModelRustLibraryAidlInterface? = null

private var renderMarkdown by Delegates.notNull<Boolean>()
private var previewMarkdownRenderedToHtmlFullscreen by Delegates.notNull<Boolean>()

private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val rustService = IFileViewModelRustLibraryAidlInterface.Stub.asInterface(service)
Expand All @@ -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)
}
}
Expand All @@ -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<Application>().bindIsolatedService(
intentService,
Context.BIND_AUTO_CREATE,
Expand Down

0 comments on commit 4181609

Please sign in to comment.