Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
🍏🦕

Dariusz Rybicki darrarski

:octocat:
🍏🦕
View GitHub Profile
@darrarski
darrarski / SwiftUI_InterfaceOrientationObservingViewModifier.swift
Created Feb 18, 2022
SwiftUI view modifier that performs action whenever UIInterfaceOrientation reported by UIViewController changes
View SwiftUI_InterfaceOrientationObservingViewModifier.swift
import SwiftUI
struct InterfaceOrientationObservingViewModifier: ViewModifier {
let onChange: (UIInterfaceOrientation) -> Void
func body(content: Content) -> some View {
content.background(InterfaceOrientationObservingView(onChange: onChange))
}
}
@darrarski
darrarski / SwiftUI_DeviceOrientationObservingViewModifier.swift
Created Feb 18, 2022
SwiftUI view modifier that performs action whenever accelerometer-based UIDeviceOrientation changes
View SwiftUI_DeviceOrientationObservingViewModifier.swift
import CoreMotion
import SwiftUI
struct DeviceOrientationObservingViewModifier: ViewModifier {
let onChange: (UIDeviceOrientation) -> Void
func body(content: Content) -> some View {
content.background(DeviceOrientationObservingView(onChange: onChange))
}
}
@darrarski
darrarski / SwiftUI_LazyVGrid_ContextMenu_Delete.swift
Created Feb 11, 2022
SwiftUI workaround for broken animation when deleting collection item with context menu.
View SwiftUI_LazyVGrid_ContextMenu_Delete.swift
import SwiftUI
struct Item: Equatable, Identifiable {
var id = UUID()
var color: Color
}
struct ContentView: View {
@State var items = Array(repeating: [Color.red, .green, .blue], count: 10)
.flatMap { $0 }
@darrarski
darrarski / SwiftUI_BottomBarViewModifier.swift
Created Jan 16, 2022
SwiftUI extension that adds bottom bar (like toolbar tor tabbar) to the provided view.
View SwiftUI_BottomBarViewModifier.swift
import SwiftUI
extension View {
func bottomBar<Bar: View>(
ignoresKeyboard: Bool = true,
frameChangeAnimation: Animation? = .default,
@ViewBuilder bar: @escaping () -> Bar
) -> some View {
modifier(BottomBarViewModifier(
ignoresKeyboard: ignoresKeyboard,
@darrarski
darrarski / SwiftUI_GeometryReaderViewModifier.swift
Last active Jan 16, 2022
SwiftUI extension that attaches geometry reader to a view and notifies whenever provided geometry changes.
View SwiftUI_GeometryReaderViewModifier.swift
import SwiftUI
extension View {
func geometryReader<Geometry: Codable>(
geometry: @escaping (GeometryProxy) -> Geometry,
onChange: @escaping (Geometry) -> Void
) -> some View {
modifier(GeometryReaderViewModifier(
geometry: geometry,
onChange: onChange
@darrarski
darrarski / SwiftUI_SizeChangeModifier.swift
Last active Aug 25, 2021
Swift UI vie modifier for retrieving the view size
View SwiftUI_SizeChangeModifier.swift
import SwiftUI
public extension View {
func onSizeChange(
ignoreSafeArea: Bool = false,
round: Bool = false,
perform action: @escaping (CGSize) -> Void
) -> some View {
modifier(SizeChangeModifier(
ignoreSafeArea: ignoreSafeArea,
@darrarski
darrarski / SwiftUI_DeviceOrientationObserver.swift
Created Aug 23, 2021
SwiftUI CoreMotion driven DeviceOrientationObserver
View SwiftUI_DeviceOrientationObserver.swift
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
}
@darrarski
darrarski / SwiftUI_onInterfaceOrientationChange.swift
Created Aug 23, 2021
SwiftUI view modifier that performs action when UIInterfaceOrientation changes
View SwiftUI_onInterfaceOrientationChange.swift
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 Jul 20, 2021
SwiftUI DeviceOrientationObserver
View SwiftUI_DeviceOrientationObserver.swift
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 Jul 13, 2021
Swift `switch()` and `next()` extension for enums
View CaseSwitchable.swift
protocol CaseSwitchable: CaseIterable, Equatable {}
extension CaseSwitchable {
mutating func `switch`() {
self = next()
}
func next() -> Self {
self.next() ?? Self.allCases.first!
}