Skip to content

Instantly share code, notes, and snippets.

Avatar

Jaden Geller JadenGeller

View GitHub Profile
View Cadence.swift
struct CadenceViewModifier: ViewModifier {
let timeInternal: Double
let action: () -> Void
@State var lastFire: Date?
func body(content: Content) -> some View {
content
.onAppear() // https://stackoverflow.com/questions/61190398/swiftui-viewmodifier-doesnt-listen-to-onreceive-events
.onReceive(Timer.publish(every: timeInternal + (lastFire?.timeIntervalSinceNow ?? 0), on: .main, in: .default).autoconnect()) { _ in
self.lastFire = Date()
View SelectableTextField.swift
struct SelectableTextField: UIViewRepresentable {
let titleKey: String
@RangeReplaceableBinding var text: String
@Binding var selectedRange: Range<String.Index>?
let onCommit: () -> Void
init(_ titleKey: String, text: Binding<String>, selectedRange: Binding<Range<String.Index>?>, onCommit: @escaping () -> Void = {}) {
self.init(titleKey, text: RangeReplaceableBinding(text), selectedRange: selectedRange, onCommit: onCommit)
}
@JadenGeller
JadenGeller / TemporaryState.swift
Last active Jul 4, 2020
TemporaryStateBinding
View TemporaryState.swift
@propertyWrapper
struct TemporaryState<Value>: DynamicProperty {
@State var modifiedValue: Value? = nil
let currentValue: Value
init(_ value: Value) {
self.currentValue = value
}
var wrappedValue: Value {
modifiedValue ?? currentValue
View DebouncedBinding.swift
@propertyWrapper
struct DebouncedBinding<Value>: DynamicProperty {
@Binding var remote: Value
@State var local: Value? = nil
init(_ remote: Binding<Value>) {
self._remote = remote
}
var wrappedValue: Value {
View KeyboardPaddingModifier.swift
import SwiftUI
import Combine
struct KeyboardPaddingModifier: ViewModifier {
@State private var keyboardMinY: CGFloat? = nil
private var keyboardNotificationPublisher: AnyPublisher<Notification, Never> {
Publishers.Merge(
NotificationCenter.default
.publisher(for: UIResponder.keyboardWillShowNotification),
@JadenGeller
JadenGeller / Editing.swift
Last active Jun 15, 2020
SwiftUI editing Binding for UIControl firstResponder
View Editing.swift
import SwiftUI
import Dispatch
extension View {
// Warning: This will affect the layout of the view that's wrapped! :(
public func editing(_ isEditing: Binding<Bool>) -> some View {
EditingProxy(rootView: self, isEditing: isEditing)
}
}
@JadenGeller
JadenGeller / Grid4x4.swift
Created Apr 9, 2020
Grid4x4 SwiftUI View
View Grid4x4.swift
struct Grid4x4<TopLeft: View, TopRight: View, BottomLeft: View, BottomRight: View>: View {
let topLeft: TopLeft
let topRight: TopRight
let bottomLeft: BottomLeft
let bottomRight: BottomRight
init(@ViewBuilder content: () -> TupleView<(TopLeft, TopRight, BottomLeft, BottomRight)>) {
(topLeft, topRight, bottomLeft, bottomRight) = content().value
}
@JadenGeller
JadenGeller / Bag.swift
Created Mar 11, 2020
Add numbers in a bag that only exposes flatMap and count
View Bag.swift
public struct Bag<Member> {
private var members: [Member] = []
public var count: Int {
members.count
}
public func flatMap<NewMember>(_ transform: (Member) throws -> Bag<NewMember>) rethrows -> Bag<NewMember> {
Bag<NewMember>(members: try members.flatMap({ member in try transform(member).members }))
@JadenGeller
JadenGeller / Combine-Denormalization-Example.swift
Created Mar 7, 2020
Denormalizing automatically using Combine
View Combine-Denormalization-Example.swift
import Combine
extension Publishers {
typealias Pair<Upstream> = Publishers.Map<Publishers.Scan<Upstream, (Upstream.Output?, Upstream.Output?)>, (Upstream.Output?, Upstream.Output)> where Upstream: Publisher
}
extension Publisher {
func pair() -> Publishers.Pair<Self> {
scan((nil as Output?, nil as Output?), { previousPair, value in
(previousPair.1, value)
}).map({ pair in
@JadenGeller
JadenGeller / Combine-Pair-Sketchy.swift
Last active Mar 7, 2020
Pair Publisher in Combine
View Combine-Pair-Sketchy.swift
import Combine
extension Publisher {
func pair() -> Publishers.Pair<Self> {
Publishers.Pair(upstream: self)
}
}
extension Publishers {
class Pair<Upstream: Publisher>: Publisher {
You can’t perform that action at this time.