Skip to content

Commit

Permalink
support for files from ACTION_SEND
Browse files Browse the repository at this point in the history
  • Loading branch information
soupslurpr committed Mar 2, 2024
1 parent bb99156 commit efff88c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.MoreVert
Expand Down Expand Up @@ -181,7 +180,7 @@ fun BeauTyXTAppBar(
if (canNavigateBack) {
IconButton(onClick = navigateUp) {
Icon(
imageVector = Icons.Filled.ArrowBack,
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = stringResource(R.string.back_button)
)
}
Expand Down Expand Up @@ -498,6 +497,7 @@ fun BeauTyXTApp(
typstProjectViewModel: TypstProjectViewModel,
preferencesViewModel: PreferencesViewModel,
isActionViewOrEdit: Boolean,
isActionSend: Boolean,
) {
val navController = rememberNavController()

Expand Down Expand Up @@ -1302,7 +1302,7 @@ ${
// it goes to the app this app was opened by. Deeplinks not doing
// that is intentional, see figure 4 at
// https://developer.android.com/guide/navigation/principles#deep-link
startDestination = if (isActionViewOrEdit) {
startDestination = if (isActionViewOrEdit || isActionSend) {
BeauTyXTScreens.FileEdit.name
} else {
BeauTyXTScreens.Start.name
Expand Down
41 changes: 34 additions & 7 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.soupslurpr.beautyxt

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -36,25 +37,50 @@ class MainActivity : ComponentActivity() {
factory = PreferencesViewModel.PreferencesViewModelFactory(dataStore)
)

val isActionViewOrEdit = (intent.action == Intent.ACTION_VIEW) or (intent.action == Intent.ACTION_EDIT)
val preferencesUiState by preferencesViewModel.uiState.collectAsState()

val isActionViewOrEdit = (intent.action == Intent.ACTION_VIEW) or
(intent.action == Intent.ACTION_EDIT)

val isActionSend = intent.action == Intent.ACTION_SEND

if (isActionViewOrEdit) {
val readOnly = intent.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION == 0

fileViewModel.setReadOnly(readOnly)

intent.data?.let {
val uri: Uri = intent.data ?: intent.getParcelableExtra(Intent.EXTRA_STREAM) ?: throw RuntimeException(
"intent" +
".data was" +
" unexpectedly null!"
)

fileViewModel.bindIsolatedService(
uri,
preferencesUiState.renderMarkdown.second.value,
false,
)
fileViewModel.setUri(uri, LocalContext.current)
} else if (isActionSend) {
val extraText = intent.getStringExtra(Intent.EXTRA_TEXT)
val extraStream: Uri? = intent.getParcelableExtra(Intent.EXTRA_STREAM)

if (extraStream != null) {
val readOnly = intent.flags and Intent.FLAG_GRANT_WRITE_URI_PERMISSION == 0

fileViewModel.setReadOnly(readOnly)

fileViewModel.bindIsolatedService(
it,
preferencesViewModel.uiState.value.renderMarkdown.second.value,
extraStream,
preferencesUiState.renderMarkdown.second.value,
false,
)
fileViewModel.setUri(it, LocalContext.current)
fileViewModel.setUri(extraStream, LocalContext.current)
} else if (extraText != null) {
throw RuntimeException("Sharing text is not supported yet!") // TODO: Handle this
}
}

val preferencesUiState by preferencesViewModel.uiState.collectAsState()

BeauTyXTTheme(
preferencesViewModel = preferencesViewModel
) {
Expand All @@ -67,6 +93,7 @@ class MainActivity : ComponentActivity() {
typstProjectViewModel = typstProjectViewModel,
preferencesViewModel = preferencesViewModel,
isActionViewOrEdit = isActionViewOrEdit,
isActionSend = isActionSend,
)
}
}
Expand Down

0 comments on commit efff88c

Please sign in to comment.