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
// Taken from Apple's App Dev Training: https://developer.apple.com/tutorials/app-dev-training/ | |
/// This color is either black or white, whichever is more accessible when viewed against the scrum color. | |
var accessibleFontColor: Color { | |
var red: CGFloat = 0 | |
var green: CGFloat = 0 | |
var blue: CGFloat = 0 | |
UIColor(self).getRed(&red, green: &green, blue: &blue, alpha: nil) | |
return isLightColor(red: red, green: green, blue: blue) ? .black : .white | |
} |
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
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
// code from the book “Thinking in SwiftUI” by Chris Eidhof | |
extension View { | |
func badge(count: Int) -> some View { | |
overlay( | |
ZStack { | |
if count != 0 { | |
Circle() | |
.fill(Color.red) | |
Text("\(count)") |
NewerOlder