Skip to content

Commit

Permalink
Refactoring of the UI and VM callbacks on PdfReaderScreen.
Browse files Browse the repository at this point in the history
Upping versionCode to 87.
  • Loading branch information
Dima Petrov committed Aug 6, 2024
1 parent 1de9c65 commit 1f0202b
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.zotero.android.uicomponents.theme.CustomTheme
@Composable
internal fun BoxScope.PdfReaderBottomPanel(
layoutType: CustomLayoutSize.LayoutType,
viewModel: PdfReaderViewModel,
vMInterface: PdfReaderVMInterface,
viewState: PdfReaderViewState
) {
Box(
Expand All @@ -41,7 +41,7 @@ internal fun BoxScope.PdfReaderBottomPanel(
.padding(start = 24.dp)
.align(Alignment.CenterStart),
drawableRes = filterDrawable,
onClick = viewModel::showFilterPopup
onClick = vMInterface::showFilterPopup
)
}
}
129 changes: 129 additions & 0 deletions app/src/main/java/org/zotero/android/pdf/reader/PdfReaderModes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.zotero.android.pdf.reader

import android.net.Uri
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.ContentTransform
import androidx.compose.animation.SizeTransform
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.with
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import org.zotero.android.architecture.ui.CustomLayoutSize
import org.zotero.android.pdf.reader.sidebar.PdfReaderSidebar
import org.zotero.android.pdf.reader.sidebar.SidebarDivider
import org.zotero.android.uicomponents.theme.CustomTheme

@Composable
internal fun PdfReaderTabletMode(
vMInterface: PdfReaderVMInterface,
viewState: PdfReaderViewState,
lazyListState: LazyListState,
layoutType: CustomLayoutSize.LayoutType,
uri: Uri,
focusRequester: FocusRequester,
) {
Row(modifier = Modifier.fillMaxSize()) {
AnimatedContent(targetState = viewState.showSideBar, transitionSpec = {
createSidebarTransitionSpec()
}, label = "") { showSideBar ->
if (showSideBar) {
Column(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth(0.3f)
) {
PdfReaderSidebar(
vMInterface = vMInterface,
viewState = viewState,
lazyListState = lazyListState,
layoutType = layoutType,
focusRequester = focusRequester,
)
}
}
}
if (viewState.showSideBar) {
SidebarDivider(
modifier = Modifier
.width(2.dp)
.fillMaxHeight()
)
}

PdfReaderPspdfKitBox(
uri = uri,
viewState = viewState,
vMInterface = vMInterface,
)
}
}

@Composable
internal fun PdfReaderPhoneMode(
vMInterface: PdfReaderVMInterface,
viewState: PdfReaderViewState,
lazyListState: LazyListState,
layoutType: CustomLayoutSize.LayoutType,
uri: Uri,
focusRequester: FocusRequester,
) {
Box(
modifier = Modifier
.fillMaxSize()
) {
PdfReaderPspdfKitBox(
uri = uri,
viewState = viewState,
vMInterface = vMInterface
)
AnimatedContent(
targetState = viewState.showSideBar,
transitionSpec = {
createSidebarTransitionSpec()
}, label = ""
) { showSideBar ->
if (showSideBar) {
Column(
modifier = Modifier
.fillMaxSize()
.background(CustomTheme.colors.pdfAnnotationsFormBackground)
) {
PdfReaderSidebar(
viewState = viewState,
vMInterface = vMInterface,
lazyListState = lazyListState,
layoutType = layoutType,
focusRequester = focusRequester
)
}
}
}
}
}

private fun AnimatedContentTransitionScope<Boolean>.createSidebarTransitionSpec(): ContentTransform {
val intOffsetSpec = tween<IntOffset>()
return (slideInHorizontally(intOffsetSpec) { -it } with
slideOutHorizontally(intOffsetSpec) { -it }).using(
// Disable clipping since the faded slide-in/out should
// be displayed out of bounds.
SizeTransform(
clip = false,
sizeAnimationSpec = { _, _ -> tween() }
))
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private val pdfReaderToolsList = listOf(
@Composable
internal fun PdfReaderPspdfKitBox(
uri: Uri,
viewModel: PdfReaderViewModel,
vMInterface: PdfReaderVMInterface,
viewState: PdfReaderViewState
) {
val density = LocalDensity.current
Expand Down Expand Up @@ -133,11 +133,11 @@ internal fun PdfReaderPspdfKitBox(
)
}
) {
PdfReaderPspdfKitView(uri = uri, viewModel = viewModel)
PdfReaderPspdfKitView(uri = uri, vMInterface = vMInterface)
if (viewState.showCreationToolbar) {
PdfReaderAnnotationCreationToolbar(
viewModel = viewModel,
viewState = viewState,
vMInterface = vMInterface,
state = anchoredDraggableState,
onShowSnapTargetAreas = { shouldShowSnapTargetAreas = true },
shouldShowSnapTargetAreas = shouldShowSnapTargetAreas
Expand All @@ -153,16 +153,16 @@ enum class DragAnchors(val fraction: Float) {

@Composable
fun BoxScope.PdfReaderAnnotationCreationToolbar(
viewModel: PdfReaderViewModel,
viewState: PdfReaderViewState,
vMInterface: PdfReaderVMInterface,
state: AnchoredDraggableState<DragAnchors>,
onShowSnapTargetAreas: () -> Unit,
shouldShowSnapTargetAreas: Boolean,
) {
val roundCornerShape = RoundedCornerShape(size = 4.dp)
val draggableInteractionSource = remember { MutableInteractionSource() }
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(key1 = viewModel) {
LaunchedEffect(key1 = vMInterface) {
draggableInteractionSource.interactions.onEach {
onShowSnapTargetAreas()
}.launchIn(coroutineScope)
Expand Down Expand Up @@ -233,21 +233,21 @@ fun BoxScope.PdfReaderAnnotationCreationToolbar(
pdfReaderToolsList.forEach { tool ->
if (!tool.isHidden) {
AnnotationCreationToggleButton(
viewModel = viewModel,
activeAnnotationTool = vMInterface.activeAnnotationTool,
pdfReaderTool = tool,
toggleButton = viewModel::toggle
toggleButton = vMInterface::toggle
)
Spacer(modifier = Modifier.height(20.dp))
}
}
val activeAnnotationTool = viewModel.activeAnnotationTool
val activeAnnotationTool = vMInterface.activeAnnotationTool
if (viewState.isColorPickerButtonVisible && activeAnnotationTool != null) {
if (activeAnnotationTool == AnnotationTool.ERASER) {
EmptyFilterCircle(onClick = { viewModel.showToolOptions() })
EmptyFilterCircle(onClick = vMInterface::showToolOptions)
} else {
val color = viewModel.toolColors[activeAnnotationTool]
val color = vMInterface.toolColors[activeAnnotationTool]
if (color != null) {
FilledFilterCircle(hex = color, onClick = { viewModel.showToolOptions() })
FilledFilterCircle(hex = color, onClick = vMInterface::showToolOptions)
}
}
}
Expand All @@ -257,21 +257,21 @@ fun BoxScope.PdfReaderAnnotationCreationToolbar(
modifier = Modifier.align(Alignment.BottomCenter)
) {
AnnotationCreationButton(
isEnabled = viewModel.canUndo(),
isEnabled = vMInterface.canUndo(),
iconInt = Drawables.undo_24px,
onButtonClick = viewModel::onUndoClick
onButtonClick = vMInterface::onUndoClick
)
Spacer(modifier = Modifier.height(20.dp))
AnnotationCreationButton(
isEnabled = viewModel.canRedo(),
isEnabled = vMInterface.canRedo(),
iconInt = Drawables.redo_24px,
onButtonClick = viewModel::onRedoClick
onButtonClick = vMInterface::onRedoClick
)
Spacer(modifier = Modifier.height(20.dp))
AnnotationCreationButton(
isEnabled = true,
iconInt = Drawables.cancel_24px,
onButtonClick = viewModel::onCloseClick
onButtonClick = vMInterface::onCloseClick
)
Spacer(modifier = Modifier.height(20.dp))
}
Expand All @@ -280,11 +280,11 @@ fun BoxScope.PdfReaderAnnotationCreationToolbar(

@Composable
private fun AnnotationCreationToggleButton(
viewModel: PdfReaderViewModel,
activeAnnotationTool: AnnotationTool?,
pdfReaderTool: PdfReaderTool,
toggleButton: (AnnotationTool) -> Unit
) {
val isSelected = viewModel.activeAnnotationTool == pdfReaderTool.type
val isSelected = activeAnnotationTool == pdfReaderTool.type
val tintColor = if (isSelected) {
Color.White
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import org.zotero.android.architecture.ui.CustomLayoutSize
import timber.log.Timber

@Composable
fun PdfReaderPspdfKitView(uri: Uri, viewModel: PdfReaderViewModel) {
fun PdfReaderPspdfKitView(
uri: Uri,
vMInterface: PdfReaderVMInterface
) {
val activity = LocalContext.current as? AppCompatActivity ?: return
val annotationMaxSideSize = annotationMaxSideSize()
val fragmentManager = activity.supportFragmentManager
val layoutType = CustomLayoutSize.calculateLayoutType()
viewModel.annotationMaxSideSize = annotationMaxSideSize
vMInterface.annotationMaxSideSize = annotationMaxSideSize
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
Expand All @@ -45,7 +48,7 @@ fun PdfReaderPspdfKitView(uri: Uri, viewModel: PdfReaderViewModel) {
pdfThumbnailBar.layoutParams = thumbnailBarLayoutParams
frameLayout.addView(pdfThumbnailBar)

viewModel.init(
vMInterface.init(
isTablet = layoutType.isTablet(),
uri = uri,
containerId = fragmentContainerView.id,
Expand Down
Loading

0 comments on commit 1f0202b

Please sign in to comment.