Skip to content

Commit

Permalink
Pdf Plain Reader
Browse files Browse the repository at this point in the history
Upping versionCode to 106
  • Loading branch information
Dima-Android committed Oct 11, 2024
1 parent 1cec279 commit bb8bce8
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import org.zotero.android.pdf.annotation.data.PdfAnnotationArgs
import org.zotero.android.pdf.annotationmore.data.PdfAnnotationMoreArgs
import org.zotero.android.pdf.annotationmore.editpage.data.PdfAnnotationEditPageArgs
import org.zotero.android.pdf.colorpicker.data.PdfReaderColorPickerArgs
import org.zotero.android.pdf.pdffilter.data.PdfFilterArgs
import org.zotero.android.pdf.reader.pdfsearch.data.PdfReaderSearchArgs
import org.zotero.android.pdf.reader.plainreader.data.PdfPlainReaderArgs
import org.zotero.android.pdf.settings.data.PdfSettingsArgs
import org.zotero.android.pdf.pdffilter.data.PdfFilterArgs
import org.zotero.android.screens.addnote.data.AddOrEditNoteArgs
import org.zotero.android.screens.allitems.data.AllItemsArgs
import org.zotero.android.screens.collectionedit.data.CollectionEditArgs
Expand Down Expand Up @@ -44,4 +45,5 @@ object ScreenArguments {
lateinit var pdfReaderColorPickerArgs: PdfReaderColorPickerArgs
lateinit var shareCollectionPickerArgs: ShareCollectionPickerArgs
lateinit var pdfReaderSearchArgs: PdfReaderSearchArgs
lateinit var pdfPlainReaderArgs: PdfPlainReaderArgs
}
22 changes: 22 additions & 0 deletions app/src/main/java/org/zotero/android/pdf/PdfReaderNavigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.zotero.android.pdf.pdffilter.PdfFilterNavigation
import org.zotero.android.pdf.pdffilter.pdfFilterNavScreens
import org.zotero.android.pdf.pdffilter.toPdfFilterScreen
import org.zotero.android.pdf.reader.PdfReaderScreen
import org.zotero.android.pdf.reader.plainreader.PdfPlanReaderScreen
import org.zotero.android.pdf.settings.PdfSettingsScreen

internal const val ARG_PDF_SCREEN = "pdfScreenArgs"
Expand All @@ -36,6 +37,7 @@ internal fun NavGraphBuilder.pdfReaderScreenAndNavigationForTablet(
onBack = navigation::onBack,
navigateToPdfFilter = navigation::toPdfFilterNavigation,
navigateToPdfSettings = navigation::toPdfSettings,
navigateToPdfPlainReader = navigation::toPdfPlainReader,
navigateToPdfColorPicker = navigation::toPdfColorPicker,
navigateToPdfAnnotation = navigation::toPdfAnnotationNavigation,
navigateToPdfAnnotationMore = navigation::toPdfAnnotationMoreNavigation,
Expand Down Expand Up @@ -81,6 +83,8 @@ internal fun NavGraphBuilder.pdfReaderScreenAndNavigationForTablet(
) {
PdfReaderColorPickerScreen(onBack = navigation::onBack)
}
pdfPlainReader(navigation)

}

internal fun NavGraphBuilder.pdfReaderNavScreensForPhone(
Expand All @@ -91,13 +95,15 @@ internal fun NavGraphBuilder.pdfReaderNavScreensForPhone(
onBack = navigation::onBack,
navigateToPdfFilter = navigation::toPdfFilterScreen,
navigateToPdfSettings = navigation::toPdfSettings,
navigateToPdfPlainReader = navigation::toPdfPlainReader,
navigateToPdfAnnotation = navigation::toPdfAnnotationScreen,
navigateToPdfAnnotationMore = navigation::toPdfAnnotationMoreScreen,
navigateToPdfColorPicker = navigation::toPdfColorPicker,
navigateToTagPicker = navigateToTagPicker,
)
pdfFilterNavScreens(navigation)
pdfSettings(navigation)
pdfPlainReader(navigation)
pdfColorPicker(navigation)
pdfAnnotationMoreNavScreens(navigation)
pdfAnnotationNavScreens(navigation)
Expand All @@ -112,6 +118,15 @@ private fun NavGraphBuilder.pdfSettings(navigation: ZoteroNavigation) {
}
}

private fun NavGraphBuilder.pdfPlainReader(navigation: ZoteroNavigation) {
composable(
route = PdfReaderDestinations.PDF_PLAIN_READER,
arguments = listOf(),
) {
PdfPlanReaderScreen(onBack = navigation::onBack)
}
}

private fun NavGraphBuilder.pdfColorPicker(navigation: ZoteroNavigation) {
composable(
route = PdfReaderDestinations.PDF_COLOR_PICKER,
Expand All @@ -127,6 +142,7 @@ private fun NavGraphBuilder.pdfScreen(
onBack: () -> Unit,
navigateToPdfFilter: () -> Unit,
navigateToPdfSettings: () -> Unit,
navigateToPdfPlainReader: () -> Unit,
navigateToPdfColorPicker: () -> Unit,
navigateToPdfAnnotation: () -> Unit,
navigateToPdfAnnotationMore: () -> Unit,
Expand All @@ -142,6 +158,7 @@ private fun NavGraphBuilder.pdfScreen(
onBack = onBack,
navigateToPdfFilter = navigateToPdfFilter,
navigateToPdfSettings = navigateToPdfSettings,
navigateToPdfPlainReader = navigateToPdfPlainReader,
navigateToPdfAnnotation = navigateToPdfAnnotation,
navigateToPdfAnnotationMore = navigateToPdfAnnotationMore,
navigateToPdfColorPicker = navigateToPdfColorPicker,
Expand All @@ -154,6 +171,7 @@ private object PdfReaderDestinations {
const val PDF_FILTER_NAVIGATION = "pdfFilterNavigation"
const val PDF_SCREEN = "pdfScreen"
const val PDF_SETTINGS = "pdfSettings"
const val PDF_PLAIN_READER = "pdfPlainReader"
const val PDF_COLOR_PICKER = "pdfColorPicker"
const val PDF_ANNOTATION_NAVIGATION = "pdfAnnotationNavigation"
const val PDF_ANNOTATION_MORE_NAVIGATION = "pdfAnnotationMoreNavigation"
Expand All @@ -179,6 +197,10 @@ private fun ZoteroNavigation.toPdfSettings() {
navController.navigate(PdfReaderDestinations.PDF_SETTINGS)
}

private fun ZoteroNavigation.toPdfPlainReader() {
navController.navigate(PdfReaderDestinations.PDF_PLAIN_READER)
}

private fun ZoteroNavigation.toPdfAnnotationNavigation() {
navController.navigate(PdfReaderDestinations.PDF_ANNOTATION_NAVIGATION)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal fun PdfReaderScreen(
onBack: () -> Unit,
navigateToPdfFilter: () -> Unit,
navigateToPdfSettings: () -> Unit,
navigateToPdfPlainReader: () -> Unit,
navigateToPdfColorPicker: () -> Unit,
navigateToPdfAnnotation: () -> Unit,
navigateToPdfAnnotationMore: () -> Unit,
Expand Down Expand Up @@ -101,6 +102,11 @@ internal fun PdfReaderScreen(
}
navigateToPdfSettings()
}

is PdfReaderViewEffect.ShowPdfPlainReader -> {
viewModel.removeFragment()
navigateToPdfPlainReader()
}
is PdfReaderViewEffect.ShowPdfColorPicker -> {
if (!layoutType.isTablet()) {
viewModel.removeFragment()
Expand All @@ -126,6 +132,7 @@ internal fun PdfReaderScreen(
onBack = onBack,
onShowHideSideBar = viewModel::toggleSideBar,
toPdfSettings = viewModel::navigateToPdfSettings,
toPdfPlainReader = viewModel::navigateToPlainReader,
showPdfSearch = viewState.showPdfSearch,
toggleToolbarButton = viewModel::toggleToolbarButton,
isToolbarButtonSelected = viewState.showCreationToolbar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ internal fun PdfReaderTopBar(
onBack: () -> Unit,
onShowHideSideBar: () -> Unit,
toPdfSettings: () -> Unit,
toPdfPlainReader: () -> Unit,
onShowHidePdfSearch: () -> Unit,
toggleToolbarButton:() -> Unit,
toggleToolbarButton: () -> Unit,
isToolbarButtonSelected: Boolean,
showSideBar: Boolean,
showPdfSearch: Boolean,
Expand All @@ -45,7 +46,10 @@ internal fun PdfReaderTopBar(
isSelected = showSideBar

)
}
},
{
IconWithPadding(drawableRes = Drawables.pdf_raw_reader, onClick = toPdfPlainReader)
},
), rightContainerContent = listOf(
{
ToggleIconWithPadding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import org.zotero.android.pdf.pdffilter.data.PdfFilterResult
import org.zotero.android.pdf.reader.AnnotationKey.Kind
import org.zotero.android.pdf.reader.pdfsearch.data.PdfReaderSearchArgs
import org.zotero.android.pdf.reader.pdfsearch.data.PdfReaderSearchResultSelected
import org.zotero.android.pdf.reader.plainreader.data.PdfPlainReaderArgs
import org.zotero.android.pdf.reader.sidebar.data.Outline
import org.zotero.android.pdf.reader.sidebar.data.PdfReaderOutlineOptionsWithChildren
import org.zotero.android.pdf.reader.sidebar.data.PdfReaderSliderOptions
Expand Down Expand Up @@ -2159,6 +2160,11 @@ class PdfReaderViewModel @Inject constructor(
triggerEffect(PdfReaderViewEffect.ShowPdfSettings)
}

fun navigateToPlainReader() {
ScreenArguments.pdfPlainReaderArgs = PdfPlainReaderArgs(this.document)
triggerEffect(PdfReaderViewEffect.ShowPdfPlainReader)
}

override fun showToolOptions() {
val tool = this.activeAnnotationTool ?: return

Expand Down Expand Up @@ -3262,6 +3268,7 @@ sealed class PdfReaderViewEffect : ViewEffect {
object NavigateBack : PdfReaderViewEffect()
object ShowPdfFilters : PdfReaderViewEffect()
object ShowPdfSettings : PdfReaderViewEffect()
object ShowPdfPlainReader: PdfReaderViewEffect()
object ShowPdfAnnotationMore: PdfReaderViewEffect()
object ShowPdfColorPicker: PdfReaderViewEffect()
data class ShowPdfAnnotationAndUpdateAnnotationsList(val scrollToIndex: Int, val showAnnotationPopup: Boolean): PdfReaderViewEffect()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.zotero.android.pdf.reader.plainreader

import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.pspdfkit.ui.PdfReaderView

@Composable
fun PdfPlainReaderPspdfKitView(
viewModel: PdfPlainReaderViewModel,
) {
val activity = LocalContext.current as? AppCompatActivity ?: return
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
val frameLayout = FrameLayout(context)
frameLayout.layoutParams = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
val pdfReaderView = PdfReaderView(activity)
pdfReaderView.layoutParams = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
frameLayout.addView(pdfReaderView)
viewModel.init(
pdfReaderView = pdfReaderView
)
frameLayout
},
update = { _ ->
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.zotero.android.pdf.reader.plainreader

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import org.zotero.android.uicomponents.Strings
import org.zotero.android.uicomponents.theme.CustomTheme
import org.zotero.android.uicomponents.topbar.NewCustomTopBar
import org.zotero.android.uicomponents.topbar.NewHeadingTextButton

@Composable
internal fun PdfPlainReaderTopBar(
onBack: () -> Unit,
title: String,
) {
NewCustomTopBar(
title = title,
backgroundColor = CustomTheme.colors.surface,
leftGuidelineStartPercentage = 0.2f,
rightGuidelineStartPercentage = 0.05f,
leftContainerContent = listOf(
{
NewHeadingTextButton(
onClick = onBack,
text = stringResource(Strings.back_button),
)
},
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.zotero.android.pdf.reader.plainreader

import androidx.lifecycle.viewModelScope
import com.pspdfkit.configuration.PdfConfiguration
import com.pspdfkit.configuration.theming.ThemeMode
import com.pspdfkit.ui.PdfReaderView
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.zotero.android.architecture.BaseViewModel2
import org.zotero.android.architecture.Defaults
import org.zotero.android.architecture.ScreenArguments
import org.zotero.android.architecture.ViewEffect
import org.zotero.android.architecture.ViewState
import org.zotero.android.pdf.data.PdfReaderCurrentThemeEventStream
import org.zotero.android.pdf.data.PdfReaderThemeDecider
import org.zotero.android.pdf.reader.plainreader.data.PdfPlainReaderArgs
import javax.inject.Inject

@HiltViewModel
class PdfPlainReaderViewModel @Inject constructor(
private val pdfReaderCurrentThemeEventStream: PdfReaderCurrentThemeEventStream,
private val pdfReaderThemeDecider: PdfReaderThemeDecider,
private val defaults: Defaults,
) : BaseViewModel2<PdfPlainReaderViewState, PdfPlainReaderViewEffect>(PdfPlainReaderViewState()) {

private val screenArgs: PdfPlainReaderArgs by lazy {
ScreenArguments.pdfPlainReaderArgs
}

fun init(
pdfReaderView: PdfReaderView
) {
startObservingTheme()

val pdfSettings = defaults.getPDFSettings()
pdfReaderThemeDecider.setPdfPageAppearanceMode(pdfSettings.appearanceMode)

val isCalculatedThemeDark = pdfReaderCurrentThemeEventStream.currentValue()!!.isDark
val themeMode = when (isCalculatedThemeDark) {
true -> ThemeMode.NIGHT
false -> ThemeMode.DEFAULT
}

val configuration = PdfConfiguration.Builder()
.invertColors(isCalculatedThemeDark)
.themeMode(themeMode)
.build()
pdfReaderView.setDocument(screenArgs.pdfDocument, configuration)
pdfReaderView.show()

updateState {
copy(title = screenArgs.pdfDocument.title ?: "")
}
}

private var pdfReaderThemeCancellable: Job? = null

private fun startObservingTheme() {
this.pdfReaderThemeCancellable = pdfReaderCurrentThemeEventStream.flow()
.onEach { data ->
val isDark = data!!.isDark
updateState {
copy(isDark = isDark)
}
triggerEffect(PdfPlainReaderViewEffect.ScreenRefresh)
}
.launchIn(viewModelScope)
}

fun setOsTheme(isDark: Boolean) {
pdfReaderThemeDecider.setCurrentOsTheme(isOsThemeDark = isDark)
}

}

data class PdfPlainReaderViewState(
val isDark: Boolean = false,
val title: String = "",
) : ViewState

sealed class PdfPlainReaderViewEffect : ViewEffect {
object NavigateBack : PdfPlainReaderViewEffect()
object ScreenRefresh : PdfPlainReaderViewEffect()
}
Loading

0 comments on commit bb8bce8

Please sign in to comment.