Skip to content

Commit b423228

Browse files
move logic from EnhancedZoomModifier to EnhancedZoomState
1 parent 9eda6bb commit b423228

File tree

2 files changed

+74
-55
lines changed

2 files changed

+74
-55
lines changed

image/src/main/java/com/smarttoolfactory/image/zoom/EnhancedZoomModifier.kt

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,58 +42,25 @@ fun Modifier.enhancedZoom(
4242
onDown(enhancedZoomState.enhancedZoomData)
4343
},
4444
onGestureEnd = {
45-
46-
// Reset to bounds gesture
47-
val zoom = enhancedZoomState.zoom.coerceAtLeast(1f)
48-
val pan = enhancedZoomState.pan
49-
50-
val bounds = enhancedZoomState.getBounds(size)
51-
52-
// val newOffset = Offset(
53-
// pan.x.coerceIn(-bounds.x, bounds.x),
54-
// pan.y.coerceIn(-bounds.y, bounds.y)
55-
// )
56-
//
57-
// coroutineScope.run {
58-
// enhancedZoomState.resetTracking()
59-
// launch { enhancedZoomState.animateZoomTo(zoom) }
60-
// launch { enhancedZoomState.animatePanTo(newOffset) }
61-
// }
62-
63-
// Fling Gesture
6445
coroutineScope.launch {
6546
enhancedZoomState.onGestureEnd()
6647
}
6748

6849
onUp(enhancedZoomState.enhancedZoomData)
6950
},
70-
onGesture = { _, gesturePan, gestureZoom, gestureRotate, mainPointer, pointerList ->
51+
onGesture = { centroid, pan, zoom, rotate, mainPointer, pointerList ->
7152

7253
coroutineScope.launch {
7354

74-
enhancedZoomState.updateZoomState(
75-
size = size,
76-
gestureZoom = gestureZoom,
77-
gesturePan = gesturePan,
78-
gestureRotate = gestureRotate
55+
enhancedZoomState.onGesture(
56+
centroid,
57+
pan,
58+
zoom,
59+
rotate,
60+
mainPointer,
61+
pointerList
7962
)
8063

81-
// Fling Gesture
82-
if (pointerList.size == 1) {
83-
84-
// val bounds = enhancedZoomState.getBounds(size)
85-
//
86-
// enhancedZoomState.boundPan(
87-
// lowerBound = Offset(-bounds.x, -bounds.y),
88-
// upperBound = Offset(bounds.x, bounds.y)
89-
// )
90-
91-
enhancedZoomState.addPosition(
92-
mainPointer.uptimeMillis,
93-
mainPointer.position
94-
)
95-
}
96-
9764
onMove(enhancedZoomState.enhancedZoomData)
9865
}
9966
}
@@ -104,7 +71,7 @@ fun Modifier.enhancedZoom(
10471
detectTapGestures(
10572
onDoubleTap = {
10673
coroutineScope.launch {
107-
enhancedZoomState.onDoubleTap()
74+
enhancedZoomState.resetWithAnimation()
10875
}
10976
}
11077
)

image/src/main/java/com/smarttoolfactory/image/zoom/EnhancedZoomState.kt

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import kotlinx.coroutines.launch
3434
*/
3535
@Composable
3636
fun rememberEnhancedZoomState(
37-
size: Size,
37+
size: IntSize,
3838
initialZoom: Float = 1f,
3939
initialRotation: Float = 0f,
4040
minZoom: Float = 1f,
@@ -80,7 +80,7 @@ fun rememberEnhancedZoomState(
8080
*/
8181
@Composable
8282
fun rememberEnhancedZoomState(
83-
size: Size,
83+
size: IntSize,
8484
initialZoom: Float = 1f,
8585
initialRotation: Float = 0f,
8686
minZoom: Float = 1f,
@@ -126,7 +126,7 @@ fun rememberEnhancedZoomState(
126126
*/
127127
@Composable
128128
fun rememberEnhancedZoomState(
129-
size: Size,
129+
size: IntSize,
130130
initialZoom: Float = 1f,
131131
initialRotation: Float = 0f,
132132
minZoom: Float = 1f,
@@ -164,7 +164,7 @@ fun rememberEnhancedZoomState(
164164
* @param rotationEnabled when set to true rotation is enabled
165165
*/
166166
open class EnhancedZoomState constructor(
167-
var size: Size,
167+
var size: IntSize,
168168
initialZoom: Float = 1f,
169169
initialRotation: Float = 0f,
170170
minZoom: Float = .5f,
@@ -181,7 +181,8 @@ open class EnhancedZoomState constructor(
181181
val isPanning = animatablePan.isRunning
182182
val isRotating = animatableRotation.isRunning
183183

184-
var rectDraw = Rect(offset = Offset.Zero, size = size)
184+
var rectDraw =
185+
Rect(offset = Offset.Zero, size = Size(size.width.toFloat(), size.height.toFloat()))
185186
var rectImage = rectDraw.copy()
186187
var rectCrop = rectDraw
187188

@@ -196,6 +197,9 @@ open class EnhancedZoomState constructor(
196197
cropRect = rectCrop
197198
)
198199

200+
private fun getBounds(): Offset {
201+
return getBounds(size)
202+
}
199203

200204
// Touch gestures
201205
internal open suspend fun onDown(change: PointerInputChange) = coroutineScope {
@@ -211,42 +215,90 @@ open class EnhancedZoomState constructor(
211215
}
212216

213217
// Transform Gestures
214-
internal open suspend fun onGestureStart() = coroutineScope {
218+
internal open suspend fun onGestureStart(change: PointerInputChange) = coroutineScope {
215219

216220
}
217221

218-
internal open suspend fun onGesture() = coroutineScope {
222+
internal open suspend fun onGesture(
223+
centroid: Offset,
224+
pan: Offset,
225+
zoom: Float,
226+
rotation: Float,
227+
mainPointer: PointerInputChange,
228+
changes: List<PointerInputChange>
229+
) = coroutineScope {
230+
231+
updateZoomState(
232+
size = size,
233+
gestureZoom = zoom,
234+
gesturePan = pan,
235+
gestureRotate = rotation
236+
)
219237

238+
// Fling Gesture
239+
if (changes.size == 1) {
240+
addPosition(
241+
mainPointer.uptimeMillis,
242+
mainPointer.position
243+
)
244+
}
220245
}
221246

222247
internal suspend fun onGestureEnd() {
223-
val velocity = velocityTracker.calculateVelocity()
224-
fling(Offset(velocity.x, velocity.y))
248+
fling()
249+
resetToValidBounds()
225250
}
226251

227-
internal fun addPosition(timeMillis: Long, position: Offset) {
252+
private suspend fun resetToValidBounds() = coroutineScope {
253+
254+
val zoom = zoom.coerceAtLeast(1f)
255+
val bounds = getBounds()
256+
257+
val pan = Offset(
258+
pan.x.coerceIn(-bounds.x, bounds.x),
259+
pan.y.coerceIn(-bounds.y, bounds.y)
260+
)
261+
262+
resetWithAnimation(pan = pan,zoom = zoom)
263+
}
264+
265+
266+
// Fling gesture
267+
private fun addPosition(timeMillis: Long, position: Offset) {
228268
velocityTracker.addPosition(
229269
timeMillis = timeMillis,
230270
position = position
231271
)
232272
}
233273

234274

235-
private suspend fun fling(velocity: Offset) = coroutineScope {
275+
private suspend fun fling() = coroutineScope {
276+
val velocityTracker = velocityTracker.calculateVelocity()
277+
val velocity = Offset(velocityTracker.x, velocityTracker.y)
278+
236279
launch {
237280
val animationResult = animatablePan.animateDecay(
238281
velocity,
239-
exponentialDecay()
282+
exponentialDecay(),
283+
block = {
284+
// val pan = this.value
285+
// val bounds = getBounds()
286+
// if(pan.x < bounds.x || pan.y < bounds.y){
287+
// throw CancellationException()
288+
// }
289+
290+
}
240291
)
241292

293+
242294
// if (!animationResult.endState.isRunning) {
243295
// resetTracking()
244296
// }
245297
}
246298
}
247299

248300

249-
internal fun resetTracking() {
301+
private fun resetTracking() {
250302
velocityTracker.resetTracking()
251303
}
252304

0 commit comments

Comments
 (0)