Skip to content

Instantly share code, notes, and snippets.

View BrunoCerberus's full-sized avatar

Bruno Lopes BrunoCerberus

View GitHub Profile
@BrunoCerberus
BrunoCerberus / ProductsViewModel.swift
Last active August 2, 2023 04:46
Unidirectional Data Flow Architecture
import Combine
import Foundation
/*
+----------------+ +------------------------+ +-------------------+
| View | ----> | ViewModel | ----> | Interactor |
| | | | | |
| ViewEvents | <---- | ViewState DomainMap | <---- | DomainState |
+----------------+ +------------------------+ +-------------------+
^ |
@BrunoCerberus
BrunoCerberus / Network.swift
Last active July 30, 2023 15:41
Network-Combine
import Combine
import Foundation
// As any Publisher
func fetchMovies() -> AnyPublisher<MovieResponse, Error> {
let url = URL(string: "https://api.themoviedb.org/3/movie/upcoming?api_key=123")!
return URLSession
.shared
.dataTaskPublisher(for: url)
@BrunoCerberus
BrunoCerberus / PropertyWrapper.swift
Last active July 31, 2023 03:58
Property Wrapper
import Foundation
/// A property wrapper that provides type-safe access to user defaults.
@propertyWrapper
struct UserDefault<Value> {
// MARK: Properties
/// The wrapped value representing the user default.
var wrappedValue: Value {
get {
@BrunoCerberus
BrunoCerberus / CustomEnvironmentValues.swift
Last active July 31, 2023 03:55
SwiftUI Custom Environment Values
import SwiftUI
/// 1. Create the environment key to hold the custom caption background color.
private struct CaptionColorKey: EnvironmentKey {
static let defaultValue = Color(.secondarySystemBackground)
}
/// 2. Extend the environment to provide access to the custom caption background color.
extension EnvironmentValues {
var captionBackgroundColor: Color {
/*
MARK: GENERICS
enables you to write flexible, reusable functions and types that can work with any type
*/
// MARK: Generic Functions
// MARK: T is a Type Parameter (T, U, V)
// MARK: solution without Generics
// with inout you can change a parameter value, becase by default they are constants
@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.
}
@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.
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 / 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]
}
class Person {
let name: String
var pet: Pet?
init(name: String) {
self.name = name
print("\(name) is being initialized.")
}
deinit {