Skip to content

Instantly share code, notes, and snippets.

View bocato's full-sized avatar

Eduardo Bocato bocato

View GitHub Profile
@bocato
bocato / DictionaryRepresentable.swift
Created March 15, 2024 15:37
DictionaryRepresentable.swift
import Foundation
#if DEBUG
/// Describes a objects as a dictionary.
/// - Used for debugging or testing purpuses.
public protocol DictionaryRepresentable {}
public extension DictionaryRepresentable {
func asDictionary() -> [String: Any] {
var dictionary: [String: Any] = .init()
# General Options
--swiftversion 5.7
# File Options
--exclude **.generated.swift, **.testing.swift ## use this to exclude whatever you need, in my case I have all generated files and testing utils with the extensions presented here.
# Format Options
--allman false
--binarygrouping none
--closingparen balanced
@bocato
bocato / RefreshControlPOC.swift
Last active December 15, 2022 12:56
Refresh Control for iOS 14 or less
import SwiftUI
import Introspect
import UIKit
struct ContentView: View {
@ObservedObject private var viewModel = ViewModel()
var body: some View {
NavigationView {
Group {
@bocato
bocato / Snippets.swift
Last active September 26, 2022 08:07
Features / Interface Modules SwiftUI
// 1. Feature definition (Module entry point / Root)
public struct ActivitiesFeatureRoutesHandler: RoutesHandler {
public var routes: [SceneRoute.Type] {
[
WODListRoute.self,
WODRouletteRoute.self,
WODDetailsRoute.self
]
}
@bocato
bocato / ErasedValue.swift
Last active September 12, 2022 08:01
Unwrapping erased values
import Foundation
protocol ErasedType {
var erasedValue: Any { get }
}
extension ErasedType {
func recursivelyUnwrap<T>(as type: T.Type) -> T? {
return recrusivelyUnwrapErasedType(
value: erasedValue,
originalType: T.self,
import Combine
public struct SomeRepository {
// Well defined errors, when possible.
public enum Error: Swift.Error {
case invalidData
}
// Public API as closures.
public let getList: () -> AnyPublisher<[String], Error>
/// Defines the `SomeRepository` contract.
protocol SomeRepositoryProtocol {
/// Gets the list from service or cache.
/// - Parameter completion: returns the list
func getList(then completion: @escaping ([String]?) -> Void)
}
struct SomeRepository: SomeRepositoryProtocol {
// MARK: - Dependencies
/// Defines the `SomeRepository` contract.
protocol SomeRepositoryProtocol {
/// Returns the cached data.
var cachedList: [String]? { get }
/// Gets the list from service or cache.
/// - Parameter completion: returns the list
func getList(then completion: @escaping ([String]?) -> Void)
}
@bocato
bocato / UIKitEnvironment.swift
Last active February 15, 2022 13:02
Implements @Environment, but for UIKit stuff.
// Implementation
public protocol UIKitEnvironmentKey {
associatedtype Value
static var defaultValue: Value { get }
}
public struct UIKitEnvironmentValues: CustomStringConvertible {
private class Wrapper {
init() {}
var value: Any?
struct ToSwiftUIView<UIKitView: UIView>: UIViewRepresentable {
typealias UIViewType = UIKitView
let uikitView: () -> UIKitView
func makeUIView(context: Context) -> UIKitView {
uikitView()
}
func updateUIView(_ uiView: UIKitView, context: Context) {}
}