This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ProcessInfo { | |
var isSwiftUIPreview: Bool { | |
environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" | |
} | |
// or: | |
static func isOnPreview() -> Bool { | |
return processInfo.processName == "XCPreviewAgent" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit: https://www.apeth.com/UnderstandingCombine/subscribers/subscribersoneshot.html | |
var cancellable: AnyCancellable? | |
cancellable = pub.sink(receiveCompletion: {_ in | |
cancellable?.cancel() | |
}) { image in | |
self.imageView.image = image | |
} | |
// Or just subscribe to the Subscribers.Sink: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
public struct ScrollToModifier<T: Hashable>: ViewModifier { | |
@Binding var id: T? | |
public func body(content: Content) -> some View { | |
ScrollViewReader { proxy in | |
content | |
.onChange(of: id) { newValue in | |
if let id { | |
scrollTo(id: id, using: proxy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code from: https://alexdremov.me/swift-actors-common-problems-and-tips/ | |
import Foundation | |
actor ActivitiesStorage { | |
var cache = [UUID: Task<Data?, Never>]() | |
func retrieveHeavyData(for id: UUID) async -> Data? { | |
if let task = cache[id] { | |
return await task.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension View { | |
func onTapGesture(perform action: () async -> Void) -> some View { | |
self.onTapGesture { | |
Task { await action() } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum State { | |
case idle | |
case running(speed: Double) | |
case paused(duration: TimeInterval) | |
case stopped(reason: String) | |
} | |
let states: [State] = [.idle, .paused(duration: 2), .running(speed: 10), .running(speed: 2)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func fileComponents(_ fileName: String) -> (name: String, extension: String) { | |
let fileExtension = URL(fileURLWithPath: fileName).pathExtension | |
let fileNameOnly = fileExtension.isEmpty ? fileName : String(fileName.dropLast(fileExtension.count + 1)) | |
return (fileNameOnly, fileExtension) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
/// Synchronizes the bindings so changes in any of them are propagated to the other. | |
/// | |
/// The following example shows how to synchronize a Published property in a view model with a Binding used for presenting a sheet: | |
/// ``` | |
/// class ViewModel: ObservableObject { | |
/// @Published var isPresented = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source: https://developer.apple.com/forums/thread/682448 | |
struct Parent: View { | |
@FocusState var focusedField: UUID? | |
var body: some View { | |
VStack { | |
Child(focusedField: _focusedField) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit: https://steipete.com/posts/forbidden-controls-in-catalyst-mac-idiom/ | |
extension UIDevice { | |
/// Checks if we run in Mac Catalyst Optimized For Mac Idiom | |
var isCatalystMacIdiom: Bool { | |
if #available(iOS 14, *) { | |
return UIDevice.current.userInterfaceIdiom == .mac | |
} else { | |
return false | |
} |
NewerOlder