generated from JetBrains/compose-multiplatform-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PhotoGalleryScreen.kt
306 lines (268 loc) · 9.76 KB
/
PhotoGalleryScreen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package ui.screens
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.animateOffsetAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathOperation
import androidx.compose.ui.graphics.drawscope.clipPath
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import data.UnsplashPhotoData
import data.UnsplashPhotoSize
import models.Wonder
import ui.composables.NetworkImage
import ui.composables.SimpleGrid
import ui.theme.black
import ui.utils.eightWaySwipeDetector
import ui.utils.roundToIntOffset
import ui.utils.simpleTransformable
// TODO: move into separate file
val BackgroundColor = Color.Black
@Composable
fun PhotoGallery(
wonder: Wonder,
) = BoxWithConstraints(Modifier.background(BackgroundColor)) {
val gridSize = 5
val imgCount = gridSize * gridSize
var currentIndex by remember { mutableStateOf((gridSize * gridSize / 2)) }
val density = LocalDensity.current
val collectionId = wonder.unsplashCollectionId
val photoIds = remember(collectionId) {
var ids = UnsplashPhotoData.getPhotosByCollectionId(collectionId)
if (ids.isEmpty()) return@remember emptyList() // avoid infinite loop
// Ensure we have enough images to fill the grid, repeat if necessary
while (ids.size < imgCount) ids += ids
ids.subList(0, imgCount)
}
val itemSize =
when (orientation) {
Orientation.Vertical -> DpSize(maxWidth * .66f, maxHeight * .5f) // Portrait
Orientation.Horizontal -> DpSize(maxWidth * .5f, maxHeight * .66f) // Landscape
}
DpSize(maxWidth * 0.7f, maxHeight * 0.5f)
val itemSizePx = with(density) { itemSize.toSize() }
val padding = 10.dp
val originOffset = Offset(itemSizePx.width, itemSizePx.height) * 2f
val col = currentIndex % gridSize
val row = currentIndex / gridSize
val indexedOffset by animateOffsetAsState(
Offset(-itemSizePx.width * col, -itemSizePx.height * row)
)
val gridOffset = originOffset + indexedOffset
val urls = remember(photoIds) {
photoIds.map {
UnsplashPhotoData.getSelfHostedUrl(it, UnsplashPhotoSize.LARGE)
}
}
var showFullScreenImage by remember { mutableStateOf(false) }
val hapticFeedback = LocalHapticFeedback.current
LaunchedEffect(currentIndex) {
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
}
fun onTap(i: Int) {
if (i == currentIndex) {
showFullScreenImage = true
} else {
currentIndex = i
}
}
fun onSwipe(direction: Offset) {
val (x, y) = direction
// Calculate next index using magic
val newIndex = currentIndex +
(if (y == 0f) 0 else if (y > 0) -5 else 5) +
(if (x == 0f) 0 else if (x > 0) -1 else 1)
// Check if the index is valid; This discards trying to cross top/bottom boundaries
if (newIndex in 0..<imgCount) {
// for valid a index, first check if we are crossing left/right boundaries
val swipedLeftWhenAtLeftBoundary = x < 0 && newIndex % gridSize == 0
val swipedRightWhenAtRightBoundary = x > 0 && newIndex % gridSize == gridSize - 1
if (swipedLeftWhenAtLeftBoundary || swipedRightWhenAtRightBoundary) {
return
}
currentIndex = newIndex
}
}
SimpleGrid(
modifier = Modifier
.requiredSize(itemSize * gridSize)
.eightWaySwipeDetector(onSwipe = ::onSwipe)
.offset { gridOffset.roundToIntOffset() },
columnCount = gridSize,
) {
urls.forEachIndexed { i, photoId ->
UnsplashImage(
modifier = Modifier.size(itemSize).padding(padding),
onTap = { onTap(i) },
isSelected = i == currentIndex,
photoUrl = photoId,
)
}
}
OverlayWithAnimatedCutOut(
key = currentIndex,
modifier = Modifier.fillMaxSize(),
cutoutSize = itemSize - DpSize(padding, padding),
)
AnimatedVisibility(
showFullScreenImage, enter = fadeIn(), exit = fadeOut()
) {
FullscreenUrlImgViewer(
url = urls[currentIndex],
onDismiss = { showFullScreenImage = false },
)
}
}
/**
* An overlay gradient with a rounded rectangular cutout at the center.
* The cutout's height gets animated when the [key] changes.
*
* @param key changing this key will animate the cutout
* @param modifier for the overlay
* @param cutoutSize size of cutout
* @param cutoutCornerRadius corner radius of cutout. The corners are circular.
*/
@Composable
private fun OverlayWithAnimatedCutOut(
key: Any,
modifier: Modifier = Modifier,
cutoutSize: DpSize,
cutoutCornerRadius: Dp = 12.dp,
) {
val animatable = remember(key) { Animatable(0.8f) }
LaunchedEffect(key) {
animatable.animateTo(1f, animationSpec = tween(durationMillis = 500))
}
val cutOutHeight = cutoutSize.height * animatable.value
Box(
modifier
.roundedRectangularCutout(cutoutSize.copy(height = cutOutHeight), cutoutCornerRadius)
.background(
Brush.radialGradient(
0f to BackgroundColor.copy(alpha = 0.4f),
1f to BackgroundColor.copy(alpha = 0.8f),
)
)
)
}
@Composable
private fun UnsplashImage(
modifier: Modifier = Modifier,
onTap: () -> Unit,
isSelected: Boolean,
photoUrl: String,
) {
val animSpec = tween<Float>(durationMillis = 800)
val imageScale by animateFloatAsState(if (isSelected) 1.1f else 1f, animSpec)
Box(
modifier = modifier
.clip(RoundedCornerShape(12.dp))
.scale(imageScale)
.clickable(onClick = onTap),
contentAlignment = Alignment.Center,
) {
NetworkImage(
photoUrl,
contentDescription = null,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
onLoading = {
// CircularProgressIndicator()
},
onError = {
// Image(
// painterResource(ImagePaths.noImagePlaceholder),
// contentDescription = null,
// contentScale = ContentScale.Crop,
// )
}
)
}
}
@Composable
fun FullscreenUrlImgViewer(url: String, onDismiss: () -> Unit) {
Box(
Modifier.fillMaxSize().background(black.copy(alpha = .6f)).clickable { onDismiss() },
contentAlignment = Alignment.Center,
) {
NetworkImage(
url,
contentDescription = null,
modifier = Modifier.simpleTransformable().fillMaxSize().padding(24.dp),
onLoading = {
// CircularProgressIndicator()
}
)
}
}
/**
* Creates a rounded rectangular cutout at the center
*/
private fun Modifier.roundedRectangularCutout(
cutoutSize: DpSize,
cornerRadius: Dp,
) = drawWithContent {
val padX = (size.width - cutoutSize.width.toPx()) / 2
val padY = (size.height - cutoutSize.height.toPx()) / 2
clipPath(
Path.combine(
PathOperation.Difference,
Path().apply {
addRect(Rect(0f, 0f, size.width, size.height))
},
Path().apply {
addRoundRect(
RoundRect(
padX,
padY,
size.width - padX,
size.height - padY,
CornerRadius(cornerRadius.toPx())
)
)
}
)
) { [email protected]() }
}
/**
* Not actual orientation; but perceived orientation based on aspect ration
*/
val BoxWithConstraintsScope.orientation get() = if (maxWidth > maxHeight) Orientation.Horizontal else Orientation.Vertical