Skip to content

Instantly share code, notes, and snippets.

View AhmedMourad0's full-sized avatar
🐧
Farming Penguins

Ahmed Mourad AhmedMourad0

🐧
Farming Penguins
View GitHub Profile
@AhmedMourad0
AhmedMourad0 / sticker-immutable.kt
Last active March 1, 2024 16:13
Code snippet for the `Leveraging the Power of Snapshots in Jetpack Compose` Medium article.
@Immutable
class Sticker(
val type: StickerType,
val width: Dp,
val height: Dp,
val offset: Offset,
val zIndex: Int,
val rotation: Float,
val scaleX: Float,
val scaleY: Float,
@AhmedMourad0
AhmedMourad0 / sticker-stable.kt
Last active March 1, 2024 16:14
Code snippet for the `Leveraging the Power of Snapshots in Jetpack Compose` Medium article.
@Stable
class Sticker(
type: StickerType,
width: Dp,
height: Dp,
offset: Offset,
zIndex: Int,
rotation: Float,
scaleX: Float,
scaleY: Float
@AhmedMourad0
AhmedMourad0 / sticker-board-initial.kt
Last active March 2, 2024 16:49
Code snippet for the `Leveraging the Power of Snapshots in Jetpack Compose` Medium article.
@Composable
fun StickerBoard(
stickers: List<Sticker>,
modifier: Modifier = Modifier
) {
Box(modifier.fillMaxWidth().background(Color.White).clipToBounds()) {
stickers.forEach { sticker ->
key(sticker.id) {
Sticker(sticker)
}
@AhmedMourad0
AhmedMourad0 / stable-sticker-with-type.kt
Last active March 1, 2024 16:11
Code snippet for the `Leveraging the Power of Snapshots in Jetpack Compose` Medium article.
@Stable
sealed interface StickerType {
@Stable
class Shape(
shape: androidx.compose.ui.graphics.Shape,
color: Color,
borderWidth: Dp,
borderColor: Color
) : StickerType {
var shape by mutableStateOf(shape)
@AhmedMourad0
AhmedMourad0 / board-action.kt
Created March 1, 2024 17:14
Code snippet for the `Leveraging the Power of Snapshots in Jetpack Compose` Medium article.
@Immutable
sealed interface BoardAction {
@Immutable
data class Add(val sticker: Sticker) : BoardAction
@Immutable
data class Remove(val id: String) : BoardAction
@Immutable
@AhmedMourad0
AhmedMourad0 / undo-manager-incomplete.kt
Last active March 1, 2024 18:05
Code snippet for the `Leveraging the Power of Snapshots in Jetpack Compose` Medium article.
@Stable
class UndoManager(private val stickers: () -> List<Sticker>) {
private val undoHistory = mutableStateListOf<BoardAction>()
private val redoHistory = mutableStateListOf<BoardAction>()
fun register(action: BoardAction) {
val invertedAction = action.invert(oldSticker = ???)
undoHistory.add(invertedAction)
redoHistory.clear()