Skip to content

Instantly share code, notes, and snippets.

View mdb1's full-sized avatar
🎯
Focusing

Manu mdb1

🎯
Focusing
View GitHub Profile
@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
@mdb1
mdb1 / SwiftlintBuildPhase.sh
Created July 23, 2023 15:12
An Xcode BuildPhase to enforce a minimum SwiftLint version
if [[ "$(uname -m)" == arm64 ]]; then
export PATH="/opt/homebrew/bin:$PATH"
fi
SWIFTLINT_VERSION=$(swiftlint --version | awk '{print $NF}')
MIN_VERSION="0.52.4"
# Only run if `installed version` >= `minimum version`.
if [[ "$(printf '%s\n' "$MIN_VERSION" "$SWIFTLINT_VERSION" | sort -V | head -n1)" == "$MIN_VERSION" ]]; then
echo "SwiftLint version: $SWIFTLINT_VERSION"
@mdb1
mdb1 / LoggerVerbosityPicker.swift
Created May 6, 2023 13:50
A SwiftUI view to pick the verbosity of the Logger
import SwiftUI
struct LoggerVerbosityPicker: View {
/// User defaults value.
@AppStorage(Logger.defaultsKey) private var storedSelection: String?
/// Current selection.
@State var selection: Logger.Verbosity = Logger.Verbosity.verbose
var body: some View {
Picker("Verbosity", selection: $selection) {
@mdb1
mdb1 / Logger.swift
Created May 6, 2023 13:48
A simple console Logger for Swift
import Foundation
/// The Console Logger to use across the app.
/// You can change the verbosity level like this: `Logger.verbosity = $newValue`.
public enum Logger {
/// The key for user defaults.
public static let defaultsKey = "logger.verbosity"
/// The verbosity level of the logger.
@mdb1
mdb1 / UserPreferencesTests.swift
Created April 18, 2023 19:13
UserPreferencesTests
final class UserPreferencesTests: XCTestCase {
private var userPreferences: UserPreferences!
override func setUp() {
super.setUp()
let userDefaults = UserDefaults(suiteName: #file)
userDefaults?.removePersistentDomain(forName: #file)
userPreferences = .init(userDefaults: userDefaults!)
}
@mdb1
mdb1 / UserPreferences.swift
Created April 18, 2023 19:12
UserPreferences: A wrapper for UserDefaults
/// A class that provides a centralized way to access and modify stored preferences.
final class UserPreferences {
static let shared = UserPreferences()
private let userDefaults: UserDefaults
/// Initializes the UserPreferences object with a UserDefaults instance.
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
@mdb1
mdb1 / EmbedInStack.swift
Created March 24, 2023 15:48
Dynamic stack container
extension View {
func embedInStack() -> some View {
modifier(EmbedInStack())
}
}
struct EmbedInStack: ViewModifier {
@Environment(\.sizeCategory) var sizeCategory
func body(content: Content) -> some View {