|
| 1 | +package org.example.memosm.ui.component.item.media |
| 2 | + |
| 3 | +import androidx.compose.animation.core.Animatable |
| 4 | +import androidx.compose.foundation.background |
| 5 | +import androidx.compose.foundation.gestures.detectVerticalDragGestures |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.foundation.layout.statusBarsPadding |
| 10 | +import androidx.compose.foundation.pager.HorizontalPager |
| 11 | +import androidx.compose.foundation.pager.rememberPagerState |
| 12 | +import androidx.compose.foundation.shape.CircleShape |
| 13 | +import androidx.compose.material.icons.Icons |
| 14 | +import androidx.compose.material.icons.outlined.Close |
| 15 | +import androidx.compose.material3.Icon |
| 16 | +import androidx.compose.material3.IconButton |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.runtime.LaunchedEffect |
| 19 | +import androidx.compose.runtime.SideEffect |
| 20 | +import androidx.compose.runtime.remember |
| 21 | +import androidx.compose.runtime.rememberCoroutineScope |
| 22 | +import androidx.compose.ui.Alignment |
| 23 | +import androidx.compose.ui.Modifier |
| 24 | +import androidx.compose.ui.graphics.Color |
| 25 | +import androidx.compose.ui.graphics.graphicsLayer |
| 26 | +import androidx.compose.ui.input.pointer.pointerInput |
| 27 | +import androidx.compose.ui.platform.LocalView |
| 28 | +import androidx.compose.ui.res.stringResource |
| 29 | +import androidx.activity.compose.PredictiveBackHandler |
| 30 | +import kotlinx.coroutines.flow.catch |
| 31 | +import androidx.compose.ui.unit.dp |
| 32 | +import androidx.compose.ui.window.Dialog |
| 33 | +import androidx.compose.ui.window.DialogProperties |
| 34 | +import androidx.compose.ui.window.DialogWindowProvider |
| 35 | +import androidx.core.view.WindowCompat |
| 36 | +import androidx.core.view.WindowInsetsCompat |
| 37 | +import androidx.core.view.WindowInsetsControllerCompat |
| 38 | +import kotlinx.coroutines.launch |
| 39 | +import org.example.memosm.R |
| 40 | +import org.example.memosm.model.Attachment |
| 41 | +import org.example.memosm.ui.component.item.AttachmentCard |
| 42 | +import org.example.memosm.ui.component.item.AttachmentCompactMode |
| 43 | +import kotlin.math.abs |
| 44 | + |
| 45 | +@Composable |
| 46 | +fun FullScreenAttachmentViewer( |
| 47 | + attachments: List<Attachment>, |
| 48 | + initialIndex: Int, |
| 49 | + token: String?, |
| 50 | + hostUrl: String, |
| 51 | + onDismiss: () -> Unit, |
| 52 | + onPageChanged: ((Int) -> Unit)? = null |
| 53 | +) { |
| 54 | + if (attachments.isEmpty() || initialIndex < 0 || initialIndex >= attachments.size) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + Dialog( |
| 59 | + onDismissRequest = onDismiss, properties = DialogProperties( |
| 60 | + usePlatformDefaultWidth = false, decorFitsSystemWindows = false |
| 61 | + ) |
| 62 | + ) { |
| 63 | + val window = (LocalView.current.parent as? DialogWindowProvider)?.window |
| 64 | + if (window != null) { |
| 65 | + SideEffect { |
| 66 | + val controller = WindowCompat.getInsetsController(window, window.decorView) |
| 67 | + controller.hide(WindowInsetsCompat.Type.systemBars()) |
| 68 | + controller.systemBarsBehavior = |
| 69 | + WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + val pagerState = rememberPagerState(initialPage = initialIndex, pageCount = { attachments.size }) |
| 74 | + val coroutineScope = rememberCoroutineScope() |
| 75 | + |
| 76 | + // Vertical drag to dismiss |
| 77 | + val dismissOffset = remember { Animatable(0f) } |
| 78 | + |
| 79 | + // Predictive back gesture state |
| 80 | + val backProgress = remember { Animatable(0f) } |
| 81 | + val backEdgeProgress = remember { Animatable(0f) } |
| 82 | + |
| 83 | + val dismissDragProgress = (abs(dismissOffset.value) / 300f).coerceIn(0f, 1f) |
| 84 | + |
| 85 | + // Combine background alpha and scale from both gestures |
| 86 | + val combinedAlpha = ((1f - dismissDragProgress) * (1f - backProgress.value * 0.5f)).coerceIn(0f, 1f) |
| 87 | + val combinedScale = ((1f - (abs(dismissOffset.value) / 1000f)) * (1f - backProgress.value * 0.1f)).coerceIn(0.6f, 1f) |
| 88 | + |
| 89 | + LaunchedEffect(pagerState.currentPage) { |
| 90 | + onPageChanged?.invoke(pagerState.currentPage) |
| 91 | + } |
| 92 | + |
| 93 | + PredictiveBackHandler { progress -> |
| 94 | + try { |
| 95 | + progress.collect { backEvent -> |
| 96 | + backProgress.snapTo(backEvent.progress) |
| 97 | + // Edge 0 is left, 1 is right |
| 98 | + backEdgeProgress.snapTo(if (backEvent.swipeEdge == 0) 1f else -1f) |
| 99 | + } |
| 100 | + onDismiss() |
| 101 | + } catch (e: Exception) { |
| 102 | + // Cancelled |
| 103 | + backProgress.animateTo(0f) |
| 104 | + backEdgeProgress.animateTo(0f) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Box( |
| 109 | + modifier = Modifier |
| 110 | + .fillMaxSize() |
| 111 | + .background(Color.Black.copy(alpha = combinedAlpha)) |
| 112 | + ) { |
| 113 | + Box( |
| 114 | + modifier = Modifier |
| 115 | + .fillMaxSize() |
| 116 | + .pointerInput(Unit) { |
| 117 | + detectVerticalDragGestures( |
| 118 | + onDragEnd = { |
| 119 | + coroutineScope.launch { |
| 120 | + if (abs(dismissOffset.value) > 200f) { |
| 121 | + onDismiss() |
| 122 | + } else { |
| 123 | + dismissOffset.animateTo(0f) |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + onVerticalDrag = { change, dragAmount -> |
| 128 | + change.consume() |
| 129 | + coroutineScope.launch { |
| 130 | + dismissOffset.snapTo(dismissOffset.value + dragAmount) |
| 131 | + } |
| 132 | + } |
| 133 | + ) |
| 134 | + } |
| 135 | + .graphicsLayer { |
| 136 | + scaleX = combinedScale |
| 137 | + scaleY = combinedScale |
| 138 | + translationY = dismissOffset.value |
| 139 | + // Shift horizontally slightly based on predictive back edge |
| 140 | + translationX = backProgress.value * 100f * backEdgeProgress.value |
| 141 | + } |
| 142 | + ) { |
| 143 | + HorizontalPager( |
| 144 | + state = pagerState, |
| 145 | + modifier = Modifier.fillMaxSize() |
| 146 | + ) { page -> |
| 147 | + val attachment = attachments[page] |
| 148 | + |
| 149 | + // The AttachmentCard should handle its own zooming internally if isFullScreen=true. |
| 150 | + // The swipe up/down gesture is captured by the parent box above, |
| 151 | + // unless the child consumes it (which it shouldn't if we just want swipe to dismiss). |
| 152 | + AttachmentCard( |
| 153 | + attachment = attachment, |
| 154 | + token = token, |
| 155 | + hostUrl = hostUrl, |
| 156 | + modifier = Modifier.fillMaxSize(), |
| 157 | + showInfo = false, |
| 158 | + showActions = false, |
| 159 | + showSize = false, |
| 160 | + showFilename = false, |
| 161 | + compactMode = AttachmentCompactMode.Never, |
| 162 | + isFullScreen = true, |
| 163 | + onDismiss = onDismiss |
| 164 | + ) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Close Button |
| 169 | + Box( |
| 170 | + modifier = Modifier |
| 171 | + .align(Alignment.TopEnd) |
| 172 | + .padding(16.dp) |
| 173 | + .statusBarsPadding() |
| 174 | + .graphicsLayer { alpha = combinedAlpha }) { |
| 175 | + IconButton( |
| 176 | + onClick = onDismiss, |
| 177 | + modifier = Modifier.background(Color.Black.copy(alpha = 0.4f), CircleShape) |
| 178 | + ) { |
| 179 | + Icon( |
| 180 | + imageVector = Icons.Outlined.Close, |
| 181 | + contentDescription = stringResource(R.string.common_close), |
| 182 | + tint = Color.White |
| 183 | + ) |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments