Skip to content

Instantly share code, notes, and snippets.

View kaszabimre's full-sized avatar
🎯
Focusing

Imre Kaszab kaszabimre

🎯
Focusing
View GitHub Profile
@kaszabimre
kaszabimre / ParallaxCarouselItem.kt
Created September 27, 2023 08:21
ParallaxCarouselItem where the magic happens
@Composable
fun ParallaxCarouselItem(
imageResId: Int,
parallaxOffset: Float,
pagerHeight: Dp,
screenWidth: Dp,
density: Float,
) {
// Load the image bitmap
val imageBitmap = ImageBitmap.imageResource(id = imageResId)
@kaszabimre
kaszabimre / ParallaxCarouselExtensions.kt
Created September 25, 2023 12:03
calculateDrawSize() and toIntPx() helper functions
// Function to calculate draw size for the image
private fun ImageBitmap.calculateDrawSize(density: Float, screenWidth: Dp, pagerHeight: Dp): IntSize {
val originalImageWidth = width / density
val originalImageHeight = height / density
val frameAspectRatio = screenWidth / pagerHeight
val imageAspectRatio = originalImageWidth / originalImageHeight
val drawWidth = xOffset + if (frameAspectRatio > imageAspectRatio) {
@kaszabimre
kaszabimre / ParallaxCarousel.kt
Created September 25, 2023 11:47
ParrallaxCarousel composable with padding, shadow and shape values
// Padding values
private val cardPadding = 25.dp
private val imagePadding = 10.dp
// Shadow and shape values for the card
private val shadowElevation = 15.dp
private val borderRadius = 15.dp
private val shape = RoundedCornerShape(borderRadius)
@kaszabimre
kaszabimre / ReducerViewModel.swift
Created July 7, 2023 14:15
The ReducerViewModel class wraps the state of KMM ViewModels into Combine's Published properties, enabling reactive programming and data binding on the iOS platform. V2
import shared
import Combine
@MainActor
final class ReducerViewModel<S, V>: ObservableObject where S: UiState, V: ViewModel, V: StateFlowAdaptable {
@LazyKoin var viewModel: V
@Published public var state: S?
@Published public var error: String?
private var cancellables = Set<AnyCancellable>()
@kaszabimre
kaszabimre / Reducer.kt
Last active July 7, 2023 14:11
Reducer.kt with StateFlowAdaptable
interface StateFlowAdaptable {
val stateFlowAdapter: FlowAdapter<*>
val errorFlowAdapter: FlowAdapter<*>
}
interface UiState
interface UiEvent
abstract class Reducer<S : UiState, E : UiEvent>(initialVal: S) : ViewModel(), StateFlowAdaptable {
guard let reducer = (viewModel as? Reducer<S, shared.UiEvent>) else {
return
}
@kaszabimre
kaszabimre / ReducerViewModel.swift
Last active July 7, 2023 14:14
The ReducerViewModel class wraps the state of KMM ViewModels into Combine's Published properties, enabling reactive programming and data binding on the iOS platform. V1
import shared
import Combine
@MainActor
final class ReducerViewModel<S: UiState, V: ViewModel>: ObservableObject {
@LazyKoin var viewModel: V
@Published public var state: S?
@Published public var error: String?
init() {
@kaszabimre
kaszabimre / Reducer.kt
Last active July 7, 2023 14:10
Reducer implementation v1
interface UiState
interface UiEvent
abstract class Reducer<S : UiState, E : UiEvent>(initialVal: S) : ViewModel() {
//...
private val _state: MutableStateFlow<S> = MutableStateFlow(initialVal)
val state: StateFlow<S>
get() = _state
@kaszabimre
kaszabimre / BlurMaskFilter.kt
Created June 26, 2023 13:45
The setMaskFilter implementation - Android
import android.graphics.BlurMaskFilter
import androidx.compose.ui.graphics.NativePaint
internal actual fun NativePaint.setMaskFilter(blurRadius: Float) {
this.maskFilter = BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.NORMAL)
}
@kaszabimre
kaszabimre / BlurMaskFilter.kt
Created June 26, 2023 13:43
The setMaskFilter implementation - iOS
import androidx.compose.ui.graphics.NativePaint
import org.jetbrains.skia.FilterBlurMode
import org.jetbrains.skia.MaskFilter
internal actual fun NativePaint.setMaskFilter(blurRadius: Float) {
this.maskFilter = MaskFilter.makeBlur(FilterBlurMode.NORMAL, blurRadius / 2, true)
}