Skip to content

Instantly share code, notes, and snippets.

View BrunoCerberus's full-sized avatar

Bruno Lopes BrunoCerberus

View GitHub Profile
@BrunoCerberus
BrunoCerberus / Coordinator.md
Last active August 16, 2023 07:43
Coordinator with NavigationPath

SwiftUI NavigationPath Coordinator Pattern Documentation

This documentation provides an overview of a SwiftUI implementation that employs the Coordinator pattern. The Coordinator pattern separates navigation logic from view logic, providing a centralized place to handle navigation.

Page

An enumeration representing the different pages or views available in the application.

  • home: Represents the home page of the application.
  • detail(Movie): Represents the detailed view for a particular movie.
@BrunoCerberus
BrunoCerberus / Merge.swift
Created August 13, 2023 20:14
Merge Publisher
import Combine
// A set to store any active Combine subscriptions (cancellables).
// This is essential for ensuring that the subscriptions are kept alive and not deallocated prematurely.
var cancellables = Set<AnyCancellable>()
// Create a publisher from an array of even integers.
let evenIntegersPublisher = [2, 4, 6, 8, 10].publisher
// Create a publisher from an array of odd integers.
@BrunoCerberus
BrunoCerberus / Zip.swift
Created August 13, 2023 19:58
Zip Publishers
import Combine
// A set to store any active Combine subscriptions (cancellables).
// This is essential for ensuring that the subscriptions are kept alive and not deallocated prematurely.
var cancellables = Set<AnyCancellable>()
// Create a publisher from an array of even integers.
let evenIntegersPublisher = [2, 4, 6, 8].publisher
// Create a publisher from an array of odd integers.
@BrunoCerberus
BrunoCerberus / CombineLatest.swift
Created August 13, 2023 19:55
CombineLatest of Publishers
import Combine
// A set to store any active Combine subscriptions (cancellables).
// This ensures that subscriptions are kept alive and not deallocated prematurely.
var cancellables = Set<AnyCancellable>()
// Create a publisher from an array of even integers.
let evenIntegersPublisher = [2, 4, 6, 8].publisher
// Create a publisher from an array of odd integers.
@BrunoCerberus
BrunoCerberus / APIRequest.swift
Created August 6, 2023 14:56
APIRequest + APIFetcher
import Foundation
import Combine
/// HTTP Methods
public enum HTTPMethod: String {
case GET
case POST
case DELETE
case PUT
}
class Person {
let name: String
var pet: Pet?
init(name: String) {
self.name = name
print("\(name) is being initialized.")
}
deinit {
@BrunoCerberus
BrunoCerberus / OpaqueReturnTypes.swift
Created August 2, 2023 04:46
Opaque Return Types (some and any)
import Foundation
// Basically opaque return types you delegate the
// inferring type to the compilator instead of defining
// explicitly its type.
// with some
func getData() -> some Collection {
return [1, 2, 3, 4, 5]
}
import Foundation
// Define the distance in kilometers
let distanceInKilometers = 1.2
// Print the distance in kilometers
print("Distance is", distanceInKilometers, "km")
// Create a Measurement instance with the distance value and unit in kilometers
let measurement = Measurement(
@BrunoCerberus
BrunoCerberus / Transform+Operators.swift
Last active August 2, 2023 04:46
Transform Operators
import Foundation
import Combine
// Helper function to print the description of the example before running it.
public func example(of description: String, action: () -> Void) {
print("\n——— Example of:", description, "———")
action()
}
// Simple struct to represent a coordinate with x and y values.
@BrunoCerberus
BrunoCerberus / Publisher+Subscribers.swift
Created July 31, 2023 03:31
Publisher and Subscribers
import Combine
import SwiftUI
class CustomSubscriber: Subscriber {
typealias Input = Int
typealias Failure = Never
func receive(subscription: Subscription) {
subscription.request(.unlimited) // Request unlimited values.
}