This file contains hidden or 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 extension View { | |
/// Perform action when interface orientation of the view changes | |
/// - Parameter perform: Action to perform | |
/// - Returns: Modified view | |
func onInterfaceOrientationChange(perform: @escaping (UIInterfaceOrientation) -> Void) -> some View { | |
modifier(OnInterfaceOrientationChangeViewModifier(onChange: perform)) | |
} | |
} |
This file contains hidden or 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 final class DeviceOrientationObserver: ObservableObject { | |
public init(notificationCenter: NotificationCenter = .default) { | |
observation = notificationCenter.addObserver( | |
forName: UIDevice.orientationDidChangeNotification, | |
object: nil, | |
queue: .main, | |
using: { [weak self] notification in | |
self?.value = (notification.object as? UIDevice)?.orientation ?? .unknown |
This file contains hidden or 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
protocol CaseSwitchable: CaseIterable, Equatable {} | |
extension CaseSwitchable { | |
mutating func `switch`() { | |
self = next() | |
} | |
func next() -> Self { | |
self.next() ?? Self.allCases.first! | |
} |
This file contains hidden or 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
protocol CaseTogglable {} | |
extension CaseTogglable where Self: CaseIterable, Self: Equatable { | |
mutating func toggle() { | |
assert(Self.allCases.isEmpty == false, "CaseTogglable cannot be applied to an enum with no cases!") | |
let index = Self.allCases.firstIndex(of: self)! | |
let nextIndex = Self.allCases.index(after: index) | |
if nextIndex < Self.allCases.endIndex { | |
self = Self.allCases[nextIndex] | |
} else { |
This file contains hidden or 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
// Based on "Lazy navigation in SwiftUI" blogpost by Majid Jabrayilov | |
// Blogpost url: https://swiftwithmajid.com/2021/01/27/lazy-navigation-in-swiftui/ | |
// This gist shows an issue with using conditional NavigationLinks. | |
// When using StackNavigationViewStyle push animations are not present. | |
import SwiftUI | |
@main | |
struct LazyNavApp: App { | |
var body: some Scene { |
This file contains hidden or 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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: cyan; icon-glyph: clock; | |
// list of time zones: https://gist.github.com/rxaviers/8481876 | |
let configs = [ | |
{name: "Warsaw", timzeZone: "Europe/Berlin", bgColor: new Color("BF0D3E",1)}, | |
{name: "Recife", timeZone: "America/Recife", bgColor: new Color("009639",1)}, | |
{name: "Los Angeles", timeZone: "America/Los_Angeles", bgColor: new Color("3878d1",1)} | |
] |
This file contains hidden or 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 { | |
@ViewBuilder func modify<Modified: View>( | |
if condition: @autoclosure () -> Bool, | |
then transform: (Self) -> Modified | |
) -> some View { | |
if condition() { | |
transform(self) | |
} else { | |
self | |
} |
This file contains hidden or 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 | |
public protocol JSONEncodable: Encodable { | |
static var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy { get } | |
static var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get } | |
static var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get } | |
static var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy { get } | |
func toJSON() throws -> Data | |
} |
This file contains hidden or 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 Combine | |
public final class CustomPublisher<Output, Failure>: Publisher where Failure: Error { | |
public init(subscribe subscribeClosure: @escaping (AnySubscriber<Output, Failure>) -> Subscription) { | |
self.subscribeClosure = subscribeClosure | |
} | |
public func receive<S>(subscriber: S) where S: Combine.Subscriber, S.Input == Output, S.Failure == Failure { | |
let subscription = subscribeClosure(AnySubscriber(subscriber)) |
This file contains hidden or 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 Combine | |
public final class SimplePublisher<Output, Failure>: Publisher where Failure: Error { | |
public init(_ closure: @escaping (Receiver<Output, Failure>) -> Disposable) { | |
self.closure = closure | |
} | |
public func receive<S>(subscriber: S) where S: Subscriber, S.Input == Output, S.Failure == Failure { | |
subscriber.receive(subscription: Subscription(subscriber: subscriber, closure: closure)) | |
} |