Skip to content

Instantly share code, notes, and snippets.

View BrunoCerberus's full-sized avatar

Bruno Lopes BrunoCerberus

View GitHub Profile
/*
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 / 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 {
@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 / 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 / ProductsViewModel.swift
Last active August 2, 2023 04:46
Unidirectional Data Flow Architecture
import Combine
import Foundation
/*
+----------------+ +------------------------+ +-------------------+
| View | ----> | ViewModel | ----> | Interactor |
| | | | | |
| ViewEvents | <---- | ViewState DomainMap | <---- | DomainState |
+----------------+ +------------------------+ +-------------------+
^ |