Skip to content

Instantly share code, notes, and snippets.

@PhilipDukhov
PhilipDukhov / CALayer+animate.swift
Last active September 21, 2020 05:38
safe way to animate CALayer
import QuartzCore
extension CALayer {
struct Animation<Value> {
let keyPath: ReferenceWritableKeyPath<CALayer, Value>
let value: Value
let duration: TimeInterval
let timingFunction: CAMediaTimingFunction? = nil
let beginFromCurrentState = false
}
import Foundation
extension NSError {
convenience init(description: String, code: Int = 0) {
self.init(
domain: "plist script",
code: code,
userInfo: [NSLocalizedDescriptionKey: description]
)
}
struct GradientTestView: View {
@State private var startPoint = UnitPoint(x: 0.762648, y: -0.376472566)
@State private var endPoint = UnitPoint(x: 0.812653333, y: 0.803190265)
let scale: CGFloat = 1.5
var body: some View {
ZStack {
GeometryReader { proxy in
let size = CGSize(
width: proxy.size.width / scale,
@PhilipDukhov
PhilipDukhov / ForEachIndexed.swift
Last active October 29, 2021 07:58
ForEachIndexed: ForEach enumerated to use index inside
struct ForEachIndexed<Data: RandomAccessCollection, Content: View, ID: Hashable>: View, DynamicViewContent where Data.Index: Hashable {
var data: [(Data.Index, Data.Element)] {
forEach.data
}
let forEach: ForEach<[(Data.Index, Data.Element)], ID, Content>
init(
_ data: Data,
id: KeyPath<Data.Element, ID>,
@PhilipDukhov
PhilipDukhov / MiddleEllipsisText.kt
Created September 7, 2021 10:17
Middle ellipsis in Text for Jetpack Compose
@Composable
fun MiddleEllipsisText(
text: String,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
@Composable
fun <I, O> rememberLambda(lambda: (I) -> O) = remember { lambda }
@Composable
fun <I, O> rememberLambda(
key1: Any?,
lambda: (I) -> O
) = remember(key1) { lambda }
@Composable
@PhilipDukhov
PhilipDukhov / EmptyNavigationLink.swift
Created September 10, 2021 12:39
NavigationLink with empty content and optional selection Binding
fun Modifier.swipeableLeftRight(onLeft: () -> Unit, onRight: () -> Unit): Modifier = composed {
var width by rememberSaveable { mutableStateOf(0f) }
val swipeableState = rememberSwipeableState(
SwipeDirection.Initial,
animationSpec = snap()
)
val anchorWidth = remember(width) {
if (width == 0f) {
1f
} else {
fun Modifier.swipeableTopBottom(onTop: () -> Unit, onBottom: () -> Unit): Modifier = composed {
var width by rememberSaveable { mutableStateOf(0f) }
val swipeableState = rememberSwipeableState(
SwipeDirection.Initial,
animationSpec = snap()
)
val anchorWidth = remember(width) {
if (width == 0f) {
1f
} else {
@Composable
inline fun LtrRow(
modifier: Modifier = Modifier,
horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
verticalAlignment: Alignment.Vertical = Alignment.Top,
crossinline content: @Composable RowScope.() -> Unit
) {
val direction = LocalLayoutDirection.current
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
Row(