Skip to content

Instantly share code, notes, and snippets.

View pteasima's full-sized avatar

Petr Sima pteasima

  • simapps s.r.o.
  • Prague, Czech Republic
  • X @pteasima
View GitHub Profile
import SwiftUI
import Dependencies
struct Foo: DependencyKey {
static var liveValue: Self {
.init {
"foo"
}
}
@pteasima
pteasima / LifetimeTask.swift
Created June 22, 2022 20:04
LifetimeTask and ThrowingLifetimeTask modifiers
import SwiftUI
private struct LifetimeTask: ViewModifier {
@State @Reference private var hasAppeared = false
@State @Reference private var task: Task<Void, Never>?
let perform: () async -> Void
func body(content: Content) -> some View {
content
.onFirstAppear {
@pteasima
pteasima / OnDestroy.swift
Created June 22, 2022 04:37
SwiftUI OnDestroy modifier
import SwiftUI
private struct OnDestroy: ViewModifier {
let onDestroy: () -> Void
final class Lifetime {
var onDestroy: () -> Void = { }
deinit { onDestroy() }
}
@State var lifetime: Lifetime = .init()
func body(content: Content) -> some View {
@pteasima
pteasima / InsetPageTabView.swift
Created February 9, 2022 16:27
InsetPageTabView
import SwiftUI
import Introspect
//TODO: what if we use more custom tags in nested hierarchy. We need to somehow confine them to a single level.
enum CustomTag: PreferenceKey {
static var defaultValue: [AnyHashable] = []
static func reduce(value: inout [AnyHashable], nextValue: () -> [AnyHashable]) {
value.append(contentsOf: nextValue())
}
}
import SwiftUI
#if os(macOS)
import Cocoa
protocol ViewRepresentable: NSViewRepresentable {
func makeView(context: Context) -> NSViewType
func updateView(_ view: NSViewType, context: Context)
}
extension ViewRepresentable {
func makeNSView(context: Context) -> NSViewType {
@pteasima
pteasima / MissionControl.swift
Created May 30, 2020 20:08
MissionControl - Debug tools for ComposableArchitecture
struct MissionControlState<State> {
var currentState: State
}
enum MissionControlAction<Action> {
case app(Action)
case lldb
}
struct MissionControl<State, Action, Environment, Content: View>: View {
@pteasima
pteasima / Auth+Combine.swift
Last active February 6, 2021 13:52
Firebase + Combine extensions
import FirebaseAuth
import Combine
extension PublishersNamespace where Base: FirebaseAuth.Auth {
var currentUser: AnyPublisher<User?, Never> {
let userSubject = PassthroughSubject<User?, Never>()
let handle = base.addStateDidChangeListener { auth, user in
userSubject.send(user)
}
contents
@pteasima
pteasima / ForEach.swift
Last active July 8, 2019 16:58
Useless Static ForEach (use Group instead)
import SwiftUI
extension ForEach where Data == Range<Int>{
init(_ v1: @autoclosure @escaping () -> Content) {
self.init(0..<1) { _ in
v1()
}
}
init<V1, V2>(
_ v1: @autoclosure @escaping () -> V1,
// I wrote this and then decided its an overengineered piece of bullshit code. Just deleting it makes me sad so let it forever take up space on Github
// a property that flips back to its default value when it is read
// TODO: does this have proper value semantics with the class inside? And do we even care?
@propertyWrapper struct ReadOnce<Value> {
init(initialValue: Value) {
defaultValue = initialValue
box = ValueBox(value: initialValue)
}
private let defaultValue: Value