Skip to content

Commit

Permalink
Fix for "Navigate away from the app while in the midst of reading a p…
Browse files Browse the repository at this point in the history
…df. Reopen the app many hours later It crashes."

Upping versionName to 48
  • Loading branch information
Dima-Android committed Feb 6, 2024
1 parent 16b1859 commit e3c8e51
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
30 changes: 29 additions & 1 deletion app/src/main/java/org/zotero/android/ZoteroApplication.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.zotero.android

import android.app.Application
import android.content.Context
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import com.google.gson.Gson
import com.pspdfkit.PSPDFKit
import dagger.hilt.android.HiltAndroidApp
import org.zotero.android.BuildConfig.EVENT_AND_CRASH_LOGGING_ENABLED
import org.zotero.android.androidx.content.longToast
import org.zotero.android.api.ForGsonWithRoundedDecimals
import org.zotero.android.architecture.Defaults
import org.zotero.android.architecture.coroutines.ApplicationScope
import org.zotero.android.architecture.crashreporting.FirebaseCrashReportingTree
import org.zotero.android.architecture.logging.crash.CrashFileWriter
Expand Down Expand Up @@ -38,6 +42,9 @@ open class ZoteroApplication: Application(), DefaultLifecycleObserver {
@Inject
lateinit var crashFileWriter: CrashFileWriter

@Inject
lateinit var defaults: Defaults

@Inject
@ForGsonWithRoundedDecimals
lateinit var gsonWithRoundedDecimals: Gson
Expand All @@ -48,13 +55,34 @@ open class ZoteroApplication: Application(), DefaultLifecycleObserver {

override fun onCreate() {
super<Application>.onCreate()

instance = this
setUpLogging()

controllers.init()

ProcessLifecycleOwner.get().lifecycle.addObserver(this)
initializePspdfKit()
}

private fun initializePspdfKit() {
val initializationResult = attemptToInitializePspdfKit(this)
defaults.setPspdfkitInitialized(initializationResult)

}

private fun attemptToInitializePspdfKit(context: Context): Boolean {
try {
if (BuildConfig.PSPDFKIT_KEY.isNotBlank()) {
PSPDFKit.initialize(context, BuildConfig.PSPDFKIT_KEY)
} else {
PSPDFKit.initialize(context, null)
}
} catch (e: Exception) {
Timber.e(e, "Unable to initialize PSPDFKIT")
context.longToast("Unable to initialize PSPDFKIT")
return false
}
return true
}

override fun onStart(owner: LifecycleOwner) {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/org/zotero/android/architecture/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ open class Defaults @Inject constructor(
private val tagPickerShowAutomaticTags = "tagPickerShowAutomaticTags"
private val tagPickerDisplayAllTags = "tagPickerDisplayAllTags"
private val isDebugLogEnabled = "isDebugLogEnabled"
private val wasPspdfkitInitialized = "wasPspdfkitInitialized"
private val pdfSettings = "pdfSettings"
private val highlightColorHex = "highlightColorHex"
private val noteColorHex = "noteColorHex"
Expand Down Expand Up @@ -233,6 +234,14 @@ open class Defaults @Inject constructor(
return sharedPreferences.getBoolean(isDebugLogEnabled, false)
}

fun setPspdfkitInitialized(newValue: Boolean) {
sharedPreferences.edit { putBoolean(wasPspdfkitInitialized, newValue) }
}

fun wasPspdfkitInitialized(): Boolean {
return sharedPreferences.getBoolean(wasPspdfkitInitialized, false)
}

fun reset() {
setUsername("")
setDisplayName("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ internal fun DashboardRootPhoneNavigation(
onOpenFile: (file: File, mimeType: String) -> Unit,
onOpenWebpage: (uri: Uri) -> Unit,
viewModel: DashboardViewModel,
wasPspdfkitInitialized: Boolean,
) {
val viewState by viewModel.viewStates.observeAsState(DashboardViewState())
LaunchedEffect(key1 = viewModel) {
Expand Down Expand Up @@ -117,7 +118,10 @@ internal fun DashboardRootPhoneNavigation(
navigateToZoterWebViewScreen = navigation::toZoteroWebViewScreen,
navigateToTagFilter = navigation::toTagFilter,
navigateToCollectionPicker = navigation::toCollectionPicker,
onShowPdf = { navigation.toPdfScreen(context) },
onShowPdf = { navigation.toPdfScreen(
context = context,
wasPspdfkitInitialized = wasPspdfkitInitialized
) },
)
itemDetailsScreen(
navigateToCreatorEdit = navigation::toCreatorEdit,
Expand All @@ -131,7 +135,7 @@ internal fun DashboardRootPhoneNavigation(
onOpenFile = onOpenFile,
onOpenWebpage = onOpenWebpage,
onPickFile = { onPickFile(EventBusConstants.FileWasSelected.CallPoint.ItemDetails) },
onShowPdf = { navigation.toPdfScreen(context) },
onShowPdf = { navigation.toPdfScreen(context, wasPspdfkitInitialized) },
)

composable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal fun DashboardRootTopLevelTabletNavigation(
onOpenFile: (file: File, mimeType: String) -> Unit,
onOpenWebpage: (uri: Uri) -> Unit,
viewModel: DashboardViewModel,
wasPspdfkitInitialized: Boolean,
) {
val context = LocalContext.current
val navController = rememberAnimatedNavController()
Expand All @@ -49,7 +50,7 @@ internal fun DashboardRootTopLevelTabletNavigation(
onOpenFile = onOpenFile,
onOpenWebpage = onOpenWebpage,
viewModel = viewModel,
onShowPdf = { navigation.toPdfScreen(context) },
onShowPdf = { navigation.toPdfScreen(context, wasPspdfkitInitialized) },
toAddOrEditNote = navigation::toAddOrEditNote,
toZoteroWebViewScreen = navigation::toZoteroWebViewScreen,
)
Expand Down
24 changes: 4 additions & 20 deletions app/src/main/java/org/zotero/android/pdf/PdfReaderNavigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavGraphBuilder
import com.google.accompanist.navigation.animation.composable
import com.pspdfkit.PSPDFKit
import org.zotero.android.BuildConfig
import org.zotero.android.androidx.content.longToast
import org.zotero.android.architecture.navigation.ZoteroNavigation
import org.zotero.android.architecture.navigation.dialogFixedDimens
Expand All @@ -25,7 +23,6 @@ import org.zotero.android.pdf.settings.PdfSettingsScreen
import org.zotero.android.pdffilter.PdfFilterNavigation
import org.zotero.android.pdffilter.pdfFilterNavScreens
import org.zotero.android.pdffilter.toPdfFilterScreen
import timber.log.Timber

internal fun NavGraphBuilder.pdfReaderScreenAndNavigationForTablet(
navigation: ZoteroNavigation,
Expand Down Expand Up @@ -155,27 +152,14 @@ private object PdfReaderDestinations {
const val PDF_ANNOTATION_MORE_NAVIGATION = "pdfAnnotationMoreNavigation"
}

fun ZoteroNavigation.toPdfScreen(context: Context) {
if (initializePspdfKit(context)) {
fun ZoteroNavigation.toPdfScreen(context: Context, wasPspdfkitInitialized: Boolean) {
if (wasPspdfkitInitialized) {
navController.navigate(PdfReaderDestinations.PDF_SCREEN)
} else {
context.longToast("Unable to open a PDF. PSPDFKIT was not initialized")
}
}

private fun initializePspdfKit(context: Context): Boolean {
try {
if (BuildConfig.PSPDFKIT_KEY.isNotBlank()) {
PSPDFKit.initialize(context, BuildConfig.PSPDFKIT_KEY)
} else {
PSPDFKit.initialize(context, null)
}
} catch (e: Exception) {
Timber.e(e, "Unable to initialize PSPDFKIT")
context.longToast("Unable to initialize PSPDFKIT")
return false
}
return true
}

private fun ZoteroNavigation.toPdfFilterNavigation() {
navController.navigate(PdfReaderDestinations.PDF_FILTER_NAVIGATION)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEventList
import org.greenrobot.eventbus.EventBus
import org.zotero.android.BuildConfig
import org.zotero.android.architecture.BaseActivity
import org.zotero.android.architecture.Defaults
import org.zotero.android.architecture.EventBusConstants
import org.zotero.android.architecture.EventBusConstants.FileWasSelected.CallPoint
import org.zotero.android.architecture.EventBusConstants.FileWasSelected.CallPoint.AllItems
Expand All @@ -27,6 +28,7 @@ import org.zotero.android.architecture.navigation.tablet.DashboardRootTopLevelTa
import org.zotero.android.architecture.ui.CustomLayoutSize
import org.zotero.android.uicomponents.theme.CustomTheme
import java.io.File
import javax.inject.Inject

@AndroidEntryPoint
internal class DashboardActivity : BaseActivity() {
Expand All @@ -37,6 +39,9 @@ internal class DashboardActivity : BaseActivity() {

private var pickFileCallPoint: CallPoint = AllItems

@Inject
lateinit var defaults: Defaults

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -78,6 +83,7 @@ internal class DashboardActivity : BaseActivity() {
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
}
val wasPspdfkitInitialized = defaults.wasPspdfkitInitialized()

setContent {
CustomTheme {
Expand All @@ -88,13 +94,15 @@ internal class DashboardActivity : BaseActivity() {
viewModel = viewModel,
onOpenFile = onOpenFile,
onOpenWebpage = onOpenWebpage,
wasPspdfkitInitialized = wasPspdfkitInitialized,
)
} else {
DashboardRootPhoneNavigation(
onPickFile = onPickFile,
viewModel = viewModel,
onOpenFile = onOpenFile,
onOpenWebpage = onOpenWebpage,
wasPspdfkitInitialized = wasPspdfkitInitialized,
)
}

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 = 47 // Must be updated on every build
val versionCode = 48 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit e3c8e51

Please sign in to comment.