Skip to content

Commit 51fa045

Browse files
change ZoomData to extension param of ZoomState
ZoomData is removed from ZoomState and added as extension and callbacks are now nullable to create a new ZoomData on each call even if it's not needed
1 parent f5e57a1 commit 51fa045

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ open class BaseEnhancedZoomState constructor(
109109
limitPan: Boolean = false
110110
) : ZoomState(
111111
initialZoom = initialZoom,
112-
initialRotation = 1f,
112+
initialRotation = 0f,
113113
minZoom = minZoom,
114114
maxZoom = maxZoom,
115115
zoomEnabled = zoomEnabled,

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ fun Modifier.zoom(
3535
consume: Boolean = true,
3636
clip: Boolean = true,
3737
zoomState: ZoomState,
38-
onGestureStart: (ZoomData) -> Unit = {},
39-
onGesture: (ZoomData) -> Unit = {},
40-
onGestureEnd: (ZoomData) -> Unit = {},
38+
onGestureStart: ((ZoomData) -> Unit)? = null,
39+
onGesture: ((ZoomData) -> Unit)? = null,
40+
onGestureEnd: ((ZoomData) -> Unit)? = null
4141
) = composed(
4242
factory = {
4343
val coroutineScope = rememberCoroutineScope()
@@ -51,10 +51,10 @@ fun Modifier.zoom(
5151
detectTransformGestures(
5252
consume = consume,
5353
onGestureStart = {
54-
onGestureStart(zoomState.zoomData)
54+
onGestureStart?.invoke(zoomState.zoomData)
5555
},
5656
onGestureEnd = {
57-
onGestureEnd(zoomState.zoomData)
57+
onGestureEnd?.invoke(zoomState.zoomData)
5858
},
5959
onGesture = { centroid, pan, zoom, rotation, _, _ ->
6060

@@ -68,7 +68,7 @@ fun Modifier.zoom(
6868
)
6969
}
7070

71-
onGesture(zoomState.zoomData)
71+
onGesture?.invoke(zoomState.zoomData)
7272
}
7373
)
7474
}
@@ -144,9 +144,9 @@ fun Modifier.zoom(
144144
consume: Boolean = true,
145145
clip: Boolean = true,
146146
zoomState: ZoomState,
147-
onGestureStart: (ZoomData) -> Unit = {},
148-
onGesture: (ZoomData) -> Unit = {},
149-
onGestureEnd: (ZoomData) -> Unit = {},
147+
onGestureStart: ((ZoomData) -> Unit)? = null,
148+
onGesture: ((ZoomData) -> Unit)? = null,
149+
onGestureEnd: ((ZoomData) -> Unit)? = null
150150
) = composed(
151151
factory = {
152152
val coroutineScope = rememberCoroutineScope()
@@ -160,10 +160,10 @@ fun Modifier.zoom(
160160
detectTransformGestures(
161161
consume = consume,
162162
onGestureStart = {
163-
onGestureStart(zoomState.zoomData)
163+
onGestureStart?.invoke(zoomState.zoomData)
164164
},
165165
onGestureEnd = {
166-
onGestureEnd(zoomState.zoomData)
166+
onGestureEnd?.invoke(zoomState.zoomData)
167167
},
168168
onGesture = { centroid, pan, zoom, rotation, _, _ ->
169169

@@ -177,7 +177,7 @@ fun Modifier.zoom(
177177
)
178178
}
179179

180-
onGesture(zoomState.zoomData)
180+
onGesture?.invoke(zoomState.zoomData)
181181
}
182182
)
183183
}
@@ -253,9 +253,9 @@ fun Modifier.zoom(
253253
consume: Boolean = true,
254254
clip: Boolean = true,
255255
zoomState: ZoomState,
256-
onGestureStart: (ZoomData) -> Unit = {},
257-
onGesture: (ZoomData) -> Unit = {},
258-
onGestureEnd: (ZoomData) -> Unit = {},
256+
onGestureStart: ((ZoomData) -> Unit)? = null,
257+
onGesture: ((ZoomData) -> Unit)? = null,
258+
onGestureEnd: ((ZoomData) -> Unit)? = null
259259
) = composed(
260260
factory = {
261261
val coroutineScope = rememberCoroutineScope()
@@ -269,10 +269,10 @@ fun Modifier.zoom(
269269
detectTransformGestures(
270270
consume = consume,
271271
onGestureStart = {
272-
onGestureStart(zoomState.zoomData)
272+
onGestureStart?.invoke(zoomState.zoomData)
273273
},
274274
onGestureEnd = {
275-
onGestureEnd(zoomState.zoomData)
275+
onGestureEnd?.invoke(zoomState.zoomData)
276276
},
277277
onGesture = { centroid, pan, zoom, rotation, _, _ ->
278278

@@ -286,7 +286,7 @@ fun Modifier.zoom(
286286
)
287287
}
288288

289-
onGesture(zoomState.zoomData)
289+
onGesture?.invoke(zoomState.zoomData)
290290
}
291291
)
292292
}
@@ -357,9 +357,9 @@ fun Modifier.zoom(
357357
clip = clip,
358358
consume = true,
359359
zoomState = zoomState,
360-
onGestureStart = {},
361-
onGestureEnd = {},
362-
onGesture = {}
360+
onGestureStart = null,
361+
onGestureEnd = null,
362+
onGesture = null
363363
)
364364

365365
/**
@@ -383,9 +383,9 @@ fun Modifier.zoom(
383383
clip = clip,
384384
consume = true,
385385
zoomState = zoomState,
386-
onGestureStart = {},
387-
onGestureEnd = {},
388-
onGesture = {}
386+
onGestureStart = null,
387+
onGestureEnd = null,
388+
onGesture = null
389389
)
390390

391391
/**
@@ -407,7 +407,14 @@ fun Modifier.zoom(
407407
clip = clip,
408408
consume = true,
409409
zoomState = zoomState,
410-
onGestureStart = {},
411-
onGestureEnd = {},
412-
onGesture = {}
410+
onGestureStart = null,
411+
onGestureEnd = null,
412+
onGesture = null
413413
)
414+
415+
internal val ZoomState.zoomData: ZoomData
416+
get() = ZoomData(
417+
zoom = zoom,
418+
pan = pan,
419+
rotation = rotation
420+
)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,14 @@ open class ZoomState(
6767
val isAnimationRunning: Boolean
6868
get() = isZooming || isPanning || isRotating
6969

70-
val zoomData: ZoomData
71-
get() = ZoomData(
72-
zoom = zoom,
73-
pan = pan,
74-
rotation = rotation
75-
)
76-
7770
internal open fun updateBounds(lowerBound: Offset?, upperBound: Offset?) {
7871
animatablePan.updateBounds(lowerBound, upperBound)
7972
}
8073

74+
/**
75+
* Get bounds of Composables that can be panned based on zoom level
76+
* @param size is size of Composable that this modifier is applied to.
77+
*/
8178
internal open fun getBounds(size: IntSize): Offset {
8279
val maxX = (size.width * (zoom - 1) / 2f).coerceAtLeast(0f)
8380
val maxY = (size.height * (zoom - 1) / 2f).coerceAtLeast(0f)
@@ -113,6 +110,9 @@ open class ZoomState(
113110
}
114111
}
115112

113+
/**
114+
* Reset [pan], [zoom] and [rotation] with animation.
115+
*/
116116
internal open suspend fun resetWithAnimation(
117117
pan: Offset = Offset.Zero,
118118
zoom: Float = 1f,

0 commit comments

Comments
 (0)