Skip to content

Instantly share code, notes, and snippets.

View mdb1's full-sized avatar
🎯
Focusing

Manu mdb1

🎯
Focusing
View GitHub Profile
@mdb1
mdb1 / NavigationRouterTests.swift
Last active April 24, 2025 13:41
NavigationRouter - Unit Tests
import XCTest
final class NavigationRouterTests: XCTestCase {
private enum TestRoute: Hashable {
case home, detail, settings, profile
}
private var router: NavigationRouter<TestRoute>!
override func setUp() {
@mdb1
mdb1 / NavigationRouter.swift
Created April 24, 2025 13:07
Simple Navigation Router - SwiftUI
import Observation
import SwiftUI
/// A generic navigation router that manages a stack-based navigation.
///
/// `NavigationRouter` is an observable object that tracks a navigation stack using an array of routes.
/// It provides methods to push, pop, and reset navigation state, allowing for simple navigation flows.
///
/// - Note: The `Route` type must conform to `Hashable`.
/// - Note: You can see a visual representation of the stack using the `pathDebugDescription` property.
@mdb1
mdb1 / ImageSelectorOptionsModifier.swift
Created January 30, 2024 22:54
A modifier that adds image selection capabilities to a view
import Foundation
import SwiftUI
struct ImageSelectorOptionsModifier: ViewModifier {
var title: String
var titleVisibility: Visibility
var galleryOptionTitle: String
var takePictureOptionTitle: String
@Binding var isPresentingSourceSelector: Bool
@Binding var selectedImage: UIImage?
@mdb1
mdb1 / ImagePicker.swift
Created January 30, 2024 22:53
SwiftUI Wrapper for UIImagePickerController.
import SwiftUI
/// SwiftUI Wrapper for UIImagePickerController.
struct ImagePicker: UIViewControllerRepresentable {
var sourceType: UIImagePickerController.SourceType = .photoLibrary
@Binding var selectedImage: UIImage?
@Environment(\.dismiss) private var dismiss
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
@mdb1
mdb1 / LongPressShrinkModifier.swift
Created October 22, 2023 15:04
LongPressShrinkModifier
import SwiftUI
extension View {
/// Adds the `LongPressShrinkModifier` modifier.
/// A modifier that adds a LongPressGesture action, that shrinks the view while it's being tapped.
/// - Parameters:
/// - animationDuration: The shrink animation duration. `Default = 0.3`.
/// - shrinkScale: The scale value for when the view is pressed. `Default = 0.85`.
/// - defaultScale: The scale value for when the view is not pressed: `Default = 1.0`.
/// - minimumLongPressDuration: The minimum long press duration. `Default = 0.5`.
@mdb1
mdb1 / TapShrinkModifier.swift
Created October 22, 2023 15:02
TapShrinkModifier
import SwiftUI
extension View {
/// Adds the `TapShrinkModifier` modifier.
/// A modifier that adds a shrink animation on tap.
/// - Parameters:
/// - animationDuration: The shrink animation duration. `Default = 0.3`.
/// - shrinkScale: The scale value for when the view is pressed. `Default = 0.85`.
/// - defaultScale: The scale value for when the view is not pressed: `Default = 1.0`.
/// - action: The action to execute.
@mdb1
mdb1 / AdaptableStack.swift
Last active October 9, 2023 20:16
An Adaptable component that switches between HStack and VStack based on the properties and the dynamic type size environment value.
import SwiftUI
/// An adaptable stack view that switches between `HStack` and `VStack` based on the dynamic text size.
struct AdaptableStack<Content>: View where Content: View {
@Environment(\.dynamicTypeSize) private var size: DynamicTypeSize
/// The primary axis along which the stack arranges its children.
private var axis: StackAxis
/// The dynamic type size threshold above which the stack axis will switch.
private var sizeThreshold: DynamicTypeSize
/// Alignment of children along the X-axis for vertical stack.
@mdb1
mdb1 / NotificationCenterMock.swift
Created August 12, 2023 23:20
Notification Center Mock
/// Mock instance of NotificationCenter, to be used in tests' targets
public final class NotificationCenterMock: NotificationCenter {
public var postCalls = 0
public var postedNotifications: [NSNotification.Name] = []
public var postReceivedObject: Any?
public var postReceivedUserInfos: [[AnyHashable: Any]] = []
public var addObserverCalls = 0
public var addObserverReceivedSelector: Selector?
public var addObserverReceivedName: NSNotification.Name?
@mdb1
mdb1 / NotificationPublisherProtocol.swift
Created August 12, 2023 23:18
Notification Center Publisher protocol
/// Reflects notification publisher functionality of `NotificationCenter`.
public protocol NotificationPublisherProtocol: AnyObject {
/// Returns a publisher that emits events when broadcasting notifications.
/// - Parameters:
/// - name: The name of the notification to publish.
/// - object: The object posting the named notification. If `nil`, the publisher emits elements for any object
/// producing a notification with the given name.
/// - Returns: A publisher that emits events when broadcasting notifications.
func publisher(for name: Notification.Name, object: AnyObject?) -> NotificationCenter.Publisher
}
@mdb1
mdb1 / NotificationObserverProtocol.swift
Created August 12, 2023 23:17
Notification Center Observer protocol
/// Reflects notification observer functionality of `NotificationCenter`.
public protocol NotificationObserverProtocol: AnyObject {
/// Adds an entry to the notification center to call the provided selector with the notification.
/// - Parameters:
/// - observer: An object to register as an observer.
/// - aSelector: A selector that specifies the message the receiver sends observer to alert it to the notification
/// posting. The method that aSelector specifies must have one and only one argument
/// (an instance of NSNotification).
/// - aName: The name of the notification to register for delivery to the observer. Specify a notification name to
/// deliver only entries with this notification name.