Skip to content

Instantly share code, notes, and snippets.

View darrarski's full-sized avatar
🍏

Dariusz Rybicki darrarski

🍏
View GitHub Profile
@darrarski
darrarski / SwiftUI_onInterfaceOrientationChange.swift
Created August 23, 2021 10:50
SwiftUI view modifier that performs action when UIInterfaceOrientation changes
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))
}
}
@darrarski
darrarski / SwiftUI_DeviceOrientationObserver.swift
Last active July 20, 2021 10:11
SwiftUI DeviceOrientationObserver
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
@darrarski
darrarski / CaseSwitchable.swift
Created July 13, 2021 16:32
Swift `switch()` and `next()` extension for enums
protocol CaseSwitchable: CaseIterable, Equatable {}
extension CaseSwitchable {
mutating func `switch`() {
self = next()
}
func next() -> Self {
self.next() ?? Self.allCases.first!
}
@darrarski
darrarski / CaseTogglable.swift
Created July 13, 2021 12:50
Swift `toggle()` extension for enums
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 {
@darrarski
darrarski / App.swift
Created April 7, 2021 15:56
Lazy navigation in SwiftUI - animations issue
// 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 {
@darrarski
darrarski / TimeZones.js
Last active April 16, 2022 12:54 — forked from fcrespo82/TimeZones.js
Scriptable time zones widget
// 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)}
]
extension View {
@ViewBuilder func modify<Modified: View>(
if condition: @autoclosure () -> Bool,
then transform: (Self) -> Modified
) -> some View {
if condition() {
transform(self)
} else {
self
}
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
}
@darrarski
darrarski / CustomPublisher.swift
Last active February 15, 2020 08:08
CustomPublisher - customizable publisher for Combine framework
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))
@darrarski
darrarski / SimplePublisher.swift
Last active November 22, 2021 23:34
SimplePublisher - custom publisher for Combine framework
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))
}