Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Visser Thomvis

View GitHub Profile
View gist:14ab58224bc92c530243f416dc722043
https://open.spotify.com/playlist/5xdvmNizfHeCz8fVvaXL4F?si=1f_Q5o4KRh-63p-mwckUlg&dl_branch=1
@Thomvis
Thomvis / Migrated.swift
Last active Aug 31, 2021
An approach towards migrating properties in a Codable struct without having to write a custom Codable implementation
View Migrated.swift
import Cocoa
// Say we have a Person model
enum V1 {
struct Person: Codable {
let name: String
var age: Int
}
}
View timelane.swift
let timer = Timer.publish(every: 1.0, on: RunLoop.main, in: .common)
.autoconnect()
.prepend(Date())
.map { $0.timeIntervalSince1970 }
.lane("shared timer")
.share()
.lane("timer")
timer
.sink { print($0) }.store(in: &cancellable)
View challenge4solution.swift
struct ContentView: View {
var body: some View {
VStack(alignment: .customAlignment) {
HStack {
Text("100 m").font(Font.body.bold())
Text("Usain Bolt").setCustomAlignmentToLeading()
}
HStack {
Text("5 km").font(Font.body.bold())
Text("Joshua Cheptegei").setCustomAlignmentToLeading()
@Thomvis
Thomvis / NestedClosureWeakSelfRetainCycle.swift
Last active Apr 13, 2021
Capturing a weak reference to self in a nested closure could cause a retain cycle. `[weak self]` weakly captures self from the outer closure. Since the outer closure did not declare a weak reference to self, it will keep a strong one.
View NestedClosureWeakSelfRetainCycle.swift
class ViewModel {
let intGetterGetter: () -> () -> Int?
var currentInt = 2
init() {
self.intGetterGetter = {
return { [weak self] in
return self?.currentInt
}
@Thomvis
Thomvis / onPreferenceChangeWorkAround.swift
Created Feb 6, 2020
It seems that `onPreferenceChange` is not always called. I have an override using `overlayPreferenceValue` and `View.id`. I suspect the fact this view is inside a `ScrollView` matters.
View onPreferenceChangeWorkAround.swift
content
.onPreferenceChange(SizeKey.self, perform: { sizes in
// this is not always called
})
.overlayPreferenceValue(SizeKey.self) { sizes in
EmptyView().onAppear {
// this is called more reliably
// the view's id controls for which changes this is called
}.id(sizes.map { $0.width*1000+$0.height })
}
View StateDrivenNavigationView.swift
//
// ContentView.swift
// NavigationViewTest
//
// Created by Thomas Visser on 04/11/2019.
// Copyright © 2019 Thomas Visser. All rights reserved.
//
import SwiftUI
View NavigationViewActiveIssue.swift
struct ContentView: View {
@State var tab = 0
@State var selections: [Int:Int] = [:]
var body: some View {
ZStack {
TabView(selection: $tab) {
NavigationView {
PageView(n: 0, selections: $selections)
}.navigationViewStyle(StackNavigationViewStyle()).tabItem { Text("Pages") }.tag(0)
View CV+ScrollView.swift
//
// ContentView.swift
// FlowLayoutST
//
// Created by Chris Eidhof on 22.08.19.
// Copyright © 2019 Chris Eidhof. All rights reserved.
//
import SwiftUI
struct FlowLayout {
@Thomvis
Thomvis / UIControlEventPublisher.swift
Last active Jul 4, 2019
UIControlEventPublisher.swift
View UIControlEventPublisher.swift
class ActionTarget {
let handler: () -> Void
init(handler: @escaping () -> Void) {
self.handler = handler
}
@IBAction func handle() {
handler()
}