Skip to content

Commit

Permalink
Remove Deep Links and use alternate approach to allow going back to a…
Browse files Browse the repository at this point in the history
…pp that opened the activity when pressing back
  • Loading branch information
soupslurpr committed Dec 14, 2023
1 parent beb3a22 commit da00c1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<activity
android:name="dev.soupslurpr.beautyxt.MainActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:theme="@style/Theme.beautyxt">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
28 changes: 11 additions & 17 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navDeepLink
import dev.soupslurpr.beautyxt.constants.mimeTypeDocx
import dev.soupslurpr.beautyxt.constants.mimeTypeHtml
import dev.soupslurpr.beautyxt.constants.mimeTypeMarkdown
Expand Down Expand Up @@ -488,9 +487,10 @@ fun FileTypeSelectionDialogItem(

@Composable
fun BeauTyXTApp(
modifier: Modifier,
fileViewModel: FileViewModel,
preferencesViewModel: PreferencesViewModel,
modifier: Modifier,
isActionViewOrEdit: Boolean,
) {
val navController = rememberNavController()

Expand Down Expand Up @@ -1278,7 +1278,15 @@ ${
) { innerPadding ->
NavHost(
navController = navController,
startDestination = BeauTyXTScreens.Start.name,
// Use this instead of deeplinks so that when back is pressed
// 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) {
BeauTyXTScreens.FileEdit.name
} else {
BeauTyXTScreens.Start.name
},
modifier = modifier.padding(innerPadding),
) {
composable(route = BeauTyXTScreens.Start.name) {
Expand Down Expand Up @@ -1342,20 +1350,6 @@ ${
}
composable(
route = BeauTyXTScreens.FileEdit.name,
deepLinks = listOf(
navDeepLink {
mimeType = mimeTypePlainText
},
navDeepLink {
mimeType = "application/json"
},
navDeepLink {
mimeType = "application/xml"
},
navDeepLink {
mimeType = mimeTypeMarkdown
}
),
) {
FileEditScreen(
name = fileUiState.name.value,
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ class MainActivity : ComponentActivity() {
)
val fileViewModel: FileViewModel = viewModel()

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

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

fileViewModel.setReadOnly(readOnly)

intent.data?.let { fileViewModel.setUri(it, LocalContext.current) }
}

val preferencesUiState by preferencesViewModel.uiState.collectAsState()

BeauTyXTTheme(
Expand All @@ -48,9 +51,10 @@ class MainActivity : ComponentActivity() {
ReviewPrivacyPolicyAndLicense(preferencesViewModel = preferencesViewModel)
} else if (preferencesUiState.acceptedPrivacyPolicyAndLicense.second.value) {
BeauTyXTApp(
modifier = Modifier,
fileViewModel = fileViewModel,
preferencesViewModel = preferencesViewModel,
modifier = Modifier
isActionViewOrEdit = isActionViewOrEdit,
)
}
}
Expand Down

0 comments on commit da00c1c

Please sign in to comment.