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 { | |
@ViewBuilder func modify<Modified: View>( | |
if condition: @autoclosure () -> Bool, | |
then transform: (Self) -> Modified | |
) -> some View { | |
if condition() { | |
transform(self) | |
} else { | |
self | |
} |
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 | |
func IfLet<T, ThenOut: View>( | |
_ value: T?, | |
then: (T) -> ThenOut | |
) -> some View { | |
ViewBuilder.buildIf(value.map { then($0) }) | |
} | |
func IfLet<T, ThenOut: View, ElseOut: View>( |
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
// | |
// Created by Dariusz Rybicki on 17/04/16. | |
// Copyright © 2016 Darrarski. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
/** | |
Returns number of seconds since system became idle | |
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
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 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 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 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 CoreMotion | |
import SwiftUI | |
public final class DeviceOrientationObserver: ObservableObject { | |
public init() { | |
manager.startAccelerometerUpdates(to: OperationQueue.main) { [weak self] data, error in | |
guard error == nil, let acceleration = data?.acceleration else { | |
self?.value = .unknown | |
return | |
} |
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 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)) | |
} |
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 Quick | |
import Nimble | |
class CodableDecimalSpec: QuickSpec { | |
override func spec() { | |
context("encode Model to JSON") { | |
var model: Model! | |
var json: [String: String]? | |
beforeEach { |
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 { | |
func geometryReader<Geometry: Codable>( | |
geometry: @escaping (GeometryProxy) -> Geometry, | |
onChange: @escaping (Geometry) -> Void | |
) -> some View { | |
modifier(GeometryReaderViewModifier( | |
geometry: geometry, | |
onChange: onChange |