Skip to content

Instantly share code, notes, and snippets.

View laevandus's full-sized avatar

Toomas Vahter laevandus

View GitHub Profile
// This API is available in iOS 16, macOS 13.0, tvOS 16.0, watchOS 9.0.
// Using backDeployed we can make it available in old OSes, like iOS 15 etc
extension Font {
@backDeployed(before: iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0)
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public static func system(size: CGFloat, weight: Font.Weight? = nil, design: Font.Design? = nil) -> Font {
fatalError("Implement")
}
}
struct Circle {
let centre: CGPoint
let radius: Double
}
extension Circle {
init(diameter: Double) {
centre = .zero
radius = diameter / 2
}
}
extension RangeReplaceableCollection where Element: Equatable {
@discardableResult mutating func remove(_ element: Element) -> Bool {
guard let index = self.firstIndex(of: element) else { return false }
remove(at: index)
// Remove other duplicate elements by recursivly calling it again
remove(element)
return true
}
}
struct SubtitledButton: View {
let title: LocalizedStringKey
let subtitle: LocalizedStringKey
let action: () -> Void
var body: some View {
Button(action: action, label: {
VStack(spacing: 4) {
Text(title)
Text(subtitle)
.font(.footnote)
let first = ["a": 1, "b": 2]
let second = ["a": 9, "c": 3]
// value in `first` wins if the same key in both
let merged1 = first.merging(second, uniquingKeysWith: { current, _ in current })
// value in `second` wins if the same key in both
let merged2 = first.merging(second, uniquingKeysWith: { _, new in new })
extension Dictionary {
func mergingUniqueKeys(from other: [Key: Value]) -> [Key: Value] {
import UIKit
final class FittingHeightCollectionViewController: UICollectionViewController {
private let items: [[String]]
init(items: [[String]]) {
self.items = items
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
layout.itemSize = UICollectionViewFlowLayout.automaticSize
extension ContentView {
final class ViewModel: ObservableObject {
@Published var selectedImage: UIImage?
@Published var isPresentingImagePicker = false
private(set) var sourceType: ImagePicker.SourceType = .camera
func choosePhoto() {
sourceType = .photoLibrary
isPresentingImagePicker = true
}
struct ImagePicker: UIViewControllerRepresentable {
typealias UIViewControllerType = UIImagePickerController
typealias SourceType = UIImagePickerController.SourceType
let sourceType: SourceType
let completionHandler: (UIImage?) -> Void
func makeUIViewController(context: Context) -> UIImagePickerController {
let viewController = UIImagePickerController()
viewController.delegate = context.coordinator
extension Publisher where Self.Failure == Never {
public func notifyObjectWillChange(_ objectWillChange: ObservableObjectPublisher) -> AnyCancellable {
return self.sink { _ in
objectWillChange.send()
}
}
}
extension ContentView {
final class ViewModel: ObservableObject {
private let package: Package
private var cancellables = [AnyCancellable]()
init(package: Package) {
self.package = package
// Model -> View Model
package.recipient.publisher(for: \.firstName)