Skip to content

Commit

Permalink
Fixing AllByIdentifierScreen crashing after app going into background…
Browse files Browse the repository at this point in the history
… for too long.

Upping versionCode to 62
  • Loading branch information
Dima-Android committed May 16, 2024
1 parent d18b18b commit 087be82
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun NavGraphBuilder.allItemsScreen(
navigateToAddOrEditNote: () -> Unit,
navigateToSinglePicker: () -> Unit,
navigateToAllItemsSort: () -> Unit,
navigateToAddByIdentifier: () -> Unit,
navigateToAddByIdentifier: (addByIdentifierParams: String) -> Unit,
navigateToVideoPlayerScreen: () -> Unit,
navigateToImageViewerScreen: () -> Unit,
navigateToZoterWebViewScreen: (String) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.dialog
import org.zotero.android.uicomponents.theme.CustomTheme
Expand All @@ -28,10 +29,12 @@ fun NavGraphBuilder.dialogFixedDimens(

fun NavGraphBuilder.dialogFixedMaxHeight(
route: String,
arguments: List<NamedNavArgument> = emptyList(),
content: @Composable () -> Unit,
) {
customDialog(
route = route,
arguments = arguments,
dialogModifier = Modifier.requiredHeightIn(max = 400.dp),
content = content
)
Expand All @@ -51,10 +54,12 @@ fun NavGraphBuilder.dialogDynamicHeight(
private fun NavGraphBuilder.customDialog(
route: String,
dialogModifier: Modifier,
arguments: List<NamedNavArgument> = emptyList(),
content: @Composable () -> Unit,
) {
dialog(
route = route,
arguments = arguments,
dialogProperties = DialogProperties(
dismissOnBackPress = true,
dismissOnClickOutside = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
Expand Down Expand Up @@ -56,6 +58,8 @@ import org.zotero.android.uicomponents.singlepicker.SinglePickerScreen
import org.zotero.android.uicomponents.theme.CustomTheme
import java.io.File

internal const val ARG_ADD_BY_IDENTIFIER = "addByIdentifierArg"

@Composable
internal fun DashboardRootPhoneNavigation(
onPickFile: (callPoint: EventBusConstants.FileWasSelected.CallPoint) -> Unit,
Expand Down Expand Up @@ -172,8 +176,10 @@ internal fun DashboardRootPhoneNavigation(
}

composable(
route = DashboardRootPhoneDestinations.ADD_BY_IDENTIFIER,
arguments = listOf(),
route = "${DashboardRootPhoneDestinations.ADD_BY_IDENTIFIER}/{$ARG_ADD_BY_IDENTIFIER}",
arguments = listOf(
navArgument(ARG_ADD_BY_IDENTIFIER) { type = NavType.StringType },
),
) {
AddByIdentifierScreen(
onClose = navigation::onBack,
Expand Down Expand Up @@ -235,8 +241,8 @@ private fun ZoteroNavigation.toSinglePicker() {
navController.navigate(DashboardRootPhoneDestinations.SINGLE_PICKER)
}

private fun ZoteroNavigation.toAddByIdentifier() {
navController.navigate(DashboardRootPhoneDestinations.ADD_BY_IDENTIFIER)
private fun ZoteroNavigation.toAddByIdentifier(params: String) {
navController.navigate("${DashboardRootPhoneDestinations.ADD_BY_IDENTIFIER}/$params")
}

private fun ZoteroNavigation.toCollectionPicker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.google.accompanist.insets.navigationBarsPadding
import org.zotero.android.architecture.EventBusConstants.FileWasSelected.CallPoint
import org.zotero.android.architecture.navigation.CommonScreenDestinations
Expand All @@ -15,6 +17,7 @@ import org.zotero.android.architecture.navigation.dialogFixedMaxHeight
import org.zotero.android.architecture.navigation.imageViewerScreen
import org.zotero.android.architecture.navigation.itemDetailsScreen
import org.zotero.android.architecture.navigation.loadingScreen
import org.zotero.android.architecture.navigation.phone.ARG_ADD_BY_IDENTIFIER
import org.zotero.android.architecture.navigation.toImageViewerScreen
import org.zotero.android.architecture.navigation.toItemDetails
import org.zotero.android.architecture.navigation.toVideoPlayerScreen
Expand Down Expand Up @@ -110,7 +113,10 @@ internal fun TabletRightPaneNavigation(
}

dialogFixedMaxHeight(
route = TabletRightPaneDestinations.ADD_BY_IDENTIFIER_DIALOG,
route = "${TabletRightPaneDestinations.ADD_BY_IDENTIFIER_DIALOG}/{$ARG_ADD_BY_IDENTIFIER}",
arguments = listOf(
navArgument(ARG_ADD_BY_IDENTIFIER) { type = NavType.StringType },
),
) {
AddByIdentifierScreen(
onClose = {
Expand Down Expand Up @@ -147,8 +153,8 @@ private fun ZoteroNavigation.toSinglePickerDialog() {
navController.navigate(TabletRightPaneDestinations.SINGLE_PICKER_DIALOG)
}

private fun ZoteroNavigation.toAddByIdentifierDialog() {
navController.navigate(TabletRightPaneDestinations.ADD_BY_IDENTIFIER_DIALOG)
private fun ZoteroNavigation.toAddByIdentifierDialog(params: String) {
navController.navigate("${TabletRightPaneDestinations.ADD_BY_IDENTIFIER_DIALOG}/$params")
}

private fun ZoteroNavigation.toCollectionPickerDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal fun AllItemsScreen(
onOpenWebpage: (uri: Uri) -> Unit,
navigateToCollectionsScreen: () -> Unit,
navigateToSinglePicker: () -> Unit,
navigateToAddByIdentifier: () -> Unit,
navigateToAddByIdentifier: (addByIdentifierParams: String) -> Unit,
navigateToAllItemsSort: () -> Unit,
navigateToCollectionPicker: () -> Unit,
navigateToItemDetails: () -> Unit,
Expand Down Expand Up @@ -72,8 +72,8 @@ internal fun AllItemsScreen(
navigateToSinglePicker()
}

AllItemsViewEffect.ShowAddByIdentifierEffect -> {
navigateToAddByIdentifier()
is AllItemsViewEffect.ShowAddByIdentifierEffect -> {
navigateToAddByIdentifier(consumedEffect.params)
}

AllItemsViewEffect.ShowSortPickerEffect -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,9 +1043,10 @@ internal class AllItemsViewModel @Inject constructor(
}

fun onAddByIdentifier() {
ScreenArguments.addByIdentifierPickerArgs =
val addByIdentifierPickerArgs =
AddByIdentifierPickerArgs(restoreLookupState = false)
triggerEffect(ShowAddByIdentifierEffect)
val params = navigationParamsMarshaller.encodeObjectToBase64(addByIdentifierPickerArgs)
triggerEffect(ShowAddByIdentifierEffect(params))
}

fun dismissDownloadedFilesPopup() {
Expand Down Expand Up @@ -1135,7 +1136,7 @@ internal sealed class AllItemsViewEffect : ViewEffect {
object ShowItemDetailEffect: AllItemsViewEffect()
object ShowAddOrEditNoteEffect: AllItemsViewEffect()
object ShowItemTypePickerEffect : AllItemsViewEffect()
object ShowAddByIdentifierEffect : AllItemsViewEffect()
data class ShowAddByIdentifierEffect(val params: String) : AllItemsViewEffect()
object ShowSortPickerEffect : AllItemsViewEffect()
object ShowCollectionPickerEffect: AllItemsViewEffect()
object ShowFilterEffect : AllItemsViewEffect()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zotero.android.uicomponents.addbyidentifier

import android.content.Context
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -9,15 +10,18 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.zotero.android.api.pojo.sync.KeyBaseKeyPair
import org.zotero.android.architecture.BaseViewModel2
import org.zotero.android.architecture.ScreenArguments
import org.zotero.android.architecture.ViewEffect
import org.zotero.android.architecture.ViewState
import org.zotero.android.architecture.navigation.NavigationParamsMarshaller
import org.zotero.android.architecture.navigation.phone.ARG_ADD_BY_IDENTIFIER
import org.zotero.android.architecture.require
import org.zotero.android.attachmentdownloader.RemoteAttachmentDownloader
import org.zotero.android.attachmentdownloader.RemoteAttachmentDownloaderEventStream
import org.zotero.android.database.objects.FieldKeys
import org.zotero.android.files.FileStore
import org.zotero.android.sync.LibraryIdentifier
import org.zotero.android.sync.SchemaController
import org.zotero.android.uicomponents.addbyidentifier.data.AddByIdentifierPickerArgs
import org.zotero.android.uicomponents.addbyidentifier.data.ISBNParser
import org.zotero.android.uicomponents.addbyidentifier.data.LookupRow
import org.zotero.android.uicomponents.addbyidentifier.data.LookupRowItem
Expand All @@ -32,8 +36,15 @@ internal class AddByIdentifierViewModel @Inject constructor(
private val schemaController: SchemaController,
private val remoteFileDownloader: RemoteAttachmentDownloader,
private val context: Context,
stateHandle: SavedStateHandle,
private val navigationParamsMarshaller: NavigationParamsMarshaller,
) : BaseViewModel2<AddByIdentifierViewState, AddByIdentifierViewEffect>(AddByIdentifierViewState()) {

private val screenArgs: AddByIdentifierPickerArgs by lazy {
val argsEncoded = stateHandle.get<String>(ARG_ADD_BY_IDENTIFIER).require()
navigationParamsMarshaller.decodeObjectFromBase64(argsEncoded)
}

private val scannerPatternRegex =
"10.\\d{4,9}\\/[-._;()\\/:a-zA-Z0-9]+"

Expand All @@ -42,7 +53,7 @@ internal class AddByIdentifierViewModel @Inject constructor(
val collectionKeys =
fileStore.getSelectedCollectionId().keyGet?.let { setOf(it) } ?: emptySet()
val libraryId = fileStore.getSelectedLibrary()
val restoreLookupState = ScreenArguments.addByIdentifierPickerArgs.restoreLookupState
val restoreLookupState = screenArgs.restoreLookupState
initState(
restoreLookupState = restoreLookupState,
hasDarkBackground = false,
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 33

val versionCode = 61 // Must be updated on every build
val versionCode = 62 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 087be82

Please sign in to comment.