Skip to content

Instantly share code, notes, and snippets.

import SwiftUI
/// `PartialForEach` is used to show a partial number of items, and a view builder to toggle the expansion.
public struct PartialForEach<Data: RandomAccessCollection, ID: Hashable, ItemView: View, ExpandView: View> {
public enum ExpandPlacement {
case first, last
}
/// This type defines a lazy map from `Data.indices` to `(index, (value, id))` tuple.
/// The `index` is required for bindable `ForEach` initializer.
public struct TTDrawerModifier<DrawerContent: View, OverlayContent: View, Detent: TTDrawerDetent> {
struct AnimationValue: Equatable {
let isDragging: Bool
let drawerState: TTDrawerState<Detent>
init(view: TTDrawerModifier<DrawerContent, OverlayContent, Detent>) {
self.isDragging = view.dragHeight != nil
self.drawerState = view.state
}
}
@rl-pavel
rl-pavel / 0 - Example.swift
Last active August 17, 2024 10:39
Resizable SwiftUI helper protocol that fixes the iOS 15 UIHostingController sizing bug.
import SwiftUI
struct SomeView: View, Resizable {
var updateSize: () -> Void?
var body: some View {
// Option 1: call the closure in the body.
let _ = updateSize?()
Text("Hello World")
// Option 2: call the closure using a SizeReader helper below.
@rl-pavel
rl-pavel / Example.swift
Last active March 5, 2024 07:21
SwiftUI Particle Emitter
var body: some View {
ParticlesEmitter {
Particle(.circle(4))
.color(.red)
.velocity(25)
.scale(0.10)
.alphaSpeed(-0.3)
.spinRange(1)
.lifetime(5)
@rl-pavel
rl-pavel / Example.swift
Last active October 15, 2020 00:32
Optional `get(orSet:)` helper extension.
var foo: String?
var bar: String? = "bar"
print(foo) // nil
print(foo.get(orSet: "foo")) // foo
print(foo) // Optional("foo")
print(bar) // Optional("bar")
print(bar.get(orSet: "test")) // bar
print(bar) // Optional("bar")
@rl-pavel
rl-pavel / 1-Example.swift
Last active October 20, 2020 03:50
Optional Comparable and Equatable helpers.
let value1: Int? = 1
let value2: Int? = 2
let nilValue: Int? = nil
value1 <? value2 // true
value1 <? nilValue // false
value2 != nilValue // true
value2 !=? nilValue // false
@rl-pavel
rl-pavel / 1-Example.swift
Last active February 1, 2023 03:17
Localized Date Formatting
// US locale:
// Oct 14, 2020, 7:50 PM
Date().format(with: [.monthShort, .dayOfMonth, .yearFull, .hour, .minute], locale: Locale(identifier: "en_US"))!
// French locale:
// 14 Oct 2020 à 19:50
Date().format(with: [.monthShort, .dayOfMonth, .yearFull, .hour, .minute], locale: Locale(identifier: "fr"))!