Skip to content

Instantly share code, notes, and snippets.

Avatar

Peter Steinberger steipete

View GitHub Profile
@steipete
steipete / Sparkle.swift
Last active May 5, 2021
Sparkle + SwiftUI
View Sparkle.swift
lass AppUpdateHandler: ObservableObject {
#if SPARKLE
private let delegateHandler = SparkleDelegateHandler()
let sparkle: SPUStandardUpdaterController
init() {
// Setup sparkle updater
// https://docs.microsoft.com/en-us/appcenter/distribution/sparkleupdates
// https://rambo.codes/posts/2021-01-08-distributing-mac-apps-outside-the-app-store
sparkle = SPUStandardUpdaterController(updaterDelegate: delegateHandler, userDriverDelegate: delegateHandler)
@steipete
steipete / TouchBarSwiftUIHack.swift
Created Apr 19, 2021
Your SwiftUI app crashes in *** Assertion failure in -[NSTouchBarLayout setLeadingWidgetWidth:], NSTouchBarLayout.m:78 ? Use this hack to work around the problem!
View TouchBarSwiftUIHack.swift
import Foundation
import InterposeKit
import OSLog
/// Hack tow work around Assertion failure in -[NSTouchBarLayout setLeadingWidgetWidth:], NSTouchBarLayout.m:78
/// This sometimes happens when macOS restores a window.
/// This even runs if there is no OS-level touch bar.
class MacOSWorkarounds {
static let logger = Logger(category: "MacOSWorkarounds")
View SwiftUIThingsToKnow.swift
- In almost all cases where you type `@ObservedObject`, you really want `@StateObject`.
If you need to support iOS 13, use `@State` on parent and pass value into child with `@ObservedObject` to simulate `@StateObject`.
Pass arguments to that model in `onAppear`. Example: https://github.com/ra1028/SwiftUI-Hooks/blob/main/Sources/Hooks/HookScope.swift#L39-L41
```
.onAppear {
model.onAppear(userTag: userTag)
}
.onDisappear {
model.onDisappear()
@steipete
steipete / SwiftUIPerformance.swift
Created Apr 16, 2021
SwiftUI Performance Notes (Adding as I learn)
View SwiftUIPerformance.swift
- Optimize Hashable and Equatable. e.g. if you have an id, just use that - instead of having the system use reflection and diff all properties:
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public static func == (lhs: UserBox, rhs: UserBox) -> Bool {
return lhs.id == rhs.id
}
@steipete
steipete / RandomColor.swift
Created Apr 6, 2021
Random Color for SwiftUI
View RandomColor.swift
extension Color {
/// Return a random color
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
@steipete
steipete / View.swift
Created Apr 4, 2021
Accept dropping a file onto SwiftUI (both iOS and macOS)
View View.swift
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
if let loadableProvider = providers.first(where: { $0.canLoadObject(ofClass: URL.self) }) {
_ = loadableProvider.loadObject(ofClass: URL.self) { fileURL, _ in
if let fileURL = fileURL, fileURL.pathExtension.lowercased() == "zip" {
self.logger.info("Dropped \(fileURL.path)")
DispatchQueue.main.async {
importer.open(zipArchiveURL: fileURL)
}
}
}
@steipete
steipete / CombineHelper.swift
Created Mar 30, 2021
Combine helper that runs a Future after a delay on a background queue
View CombineHelper.swift
import Foundation
import Combine
/// Run a block in the background with a delay and make it cancellable.
/// - Parameters:
/// - delay: Delay in milliseconds before the background work starts
/// - queue: Background queue to use
/// - worker: Worker block to execute on queue
/// - completion: Completion handler executed on main thread.
/// - Returns: AnyCancellable token
@steipete
steipete / ViewExtensions.swift
Created Feb 24, 2021
If you use it please credit PSPDFKit GmbH. MIT License
View ViewExtensions.swift
import Foundation
import SwiftUI
@available(iOS 13.0, *)
extension View {
/// Wraps view into an AnyView
func eraseToAnyView() -> AnyView {
AnyView(self)
}
@steipete
steipete / ContentView.swift
Last active Feb 20, 2021
FB9013209: SwiftUI: Preview and actual layout differ in this example. (SwiftUI Bug)
View ContentView.swift
//
// ContentView.swift
// Shared
//
// Created by Peter Steinberger on 20.02.21.
//
import SwiftUI
struct SettingsGroup<Content: View>: View {
View LayoutPlayground.swift
//: A Cocoa based Playground to present user interface
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
HStack {
VStack {
HStack {