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 / 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()
@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 / 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 / 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 / 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-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,
import com.dequeue.app.common.log
import com.dequeue.app.common.logging.D
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.plugins.resources.*
import io.ktor.serialization.kotlinx.json.*
fun ktorHttpClient(baseUrl: String): HttpClient = ktorClient {
@AhmedMourad0
AhmedMourad0 / build.gradle
Created May 11, 2021 21:38
Idea plugin build.gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id "org.jetbrains.intellij" version "0.7.3"
id "org.jetbrains.kotlin.jvm"
}
repositories {
mavenCentral()
maven {
@AhmedMourad0
AhmedMourad0 / value-based-copy-unresolved-example.kt
Created September 5, 2020 01:11
Code snippets for the `value-based classes and error-handling` Medium article.
val password = Password.of("Some34Really434Hard21Password!").orThrow() //Throw on violation just for demonstration
val haha = password.copy(value = "gotcha") //Unresolved reference: copy
@AhmedMourad0
AhmedMourad0 / value-based-adt-list-with-nocopy-example.kt
Created September 5, 2020 01:05
Code snippets for the `value-based classes and error-handling` Medium article.
@NoCopy
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Either<List<Violation>, Password> {
val violations = buildList {
if (value.isTooShort()) { add(Violation.PasswordTooShort(MIN_LENGTH)) }
if (value.containsNoNumbers()) { add(Violation.PasswordContainsNoNumbers) }
}
return if (violations.isEmpty()) Password(value).right() else violations.left()
}