Skip to content

Instantly share code, notes, and snippets.

View darrarski's full-sized avatar
:octocat:
🍏🦕

Dariusz Rybicki darrarski

:octocat:
🍏🦕
View GitHub Profile
@darrarski
darrarski / SwiftUI_LazyVGrid_ContextMenu_Delete.swift
Created February 11, 2022 23:41
SwiftUI workaround for broken animation when deleting collection item with context menu.
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 January 16, 2022 23:28
SwiftUI extension that adds bottom bar (like toolbar tor tabbar) to the provided view.
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 January 16, 2022 23:27
SwiftUI extension that attaches geometry reader to a view and notifies whenever provided geometry changes.
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 February 14, 2024 17:08
Swift UI vie modifier for retrieving the view size
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 August 23, 2021 17:28
SwiftUI CoreMotion driven DeviceOrientationObserver
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 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 {