Skip to content

Instantly share code, notes, and snippets.

View mdb1's full-sized avatar
🎯
Focusing

Manu Herrera mdb1

🎯
Focusing
View GitHub Profile
@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.
@mdb1
mdb1 / NotificationPosterProtocol.swift
Created August 12, 2023 23:17
Notification Center Poster protocol
/// Reflects notification posting functionality of `NotificationCenter`.
public protocol NotificationPosterProtocol: AnyObject {
/// Creates a notification with a given name, sender and user info and posts it to the notification center.
/// - Parameters:
/// - aName: The name of the notification.
/// - anObject: The object posting the notification.
/// - userInfo: A user info dictionary with optional information about the notification.
func post(name aName: NSNotification.Name, object anObject: Any?, userInfo: [AnyHashable: Any]?)
/// Creates a notification with a given name and sender and posts it to the notification center.
/// - Parameters:
@mdb1
mdb1 / fastlane_ pr_lane_example.rb
Last active August 15, 2023 17:44
Fastlane PR Lane example
default_platform(:ios)
GITHUB_REPO_NAME="your-repo-name" # Example: mdb1/fastlane-example
GIT_DEFAULT_BRANCH="main"
platform :ios do
desc "Description of what the lane does"
lane :custom_lane do
# add actions here: https://docs.fastlane.tools/actions
end