Skip to content

Instantly share code, notes, and snippets.

View agiokas's full-sized avatar

Apostolos Giokas agiokas

View GitHub Profile
@agiokas
agiokas / Datasource_v1.swift
Last active January 2, 2022 20:59
ViewModel test example
import Foundation
final class Datasource {
func pets() -> [Pet] {
[
Pet(id: "1", name: "Max", ownerId: "1"),
Pet(id: "2", name: "Azor", ownerId: "1"),
Pet(id: "3", name: "Monty", ownerId: "2"),
Pet(id: "4", name: "Zizou", ownerId: "3"),
]
@agiokas
agiokas / Functions.swift
Created January 1, 2022 20:51
Unit Testing 101 - Functions
public func addInt(_ a: Int, _ b: Int) throws -> Int {
let result = a.addingReportingOverflow(b)
if result.overflow {
throw AddError.overflow
}
return result.partialValue
}
enum AddError: Error {
case overflow
@agiokas
agiokas / ContentView.swift
Created October 16, 2021 14:41
UIKit - SwiftUI Comparison
import SwiftUI
struct ContentView: View {
@ObservedObject var viewModel: ContentVM
var body: some View {
NavigationView {
List(viewModel.values, id: \.self) { value in
Text(value)
}.navigationBarTitle("Our Cells")
//
// Created by Apostolos Giokas on 23.03.21.
//
final class MyViewControllerFactory: ViewCοntrollerFactory {
override init() {
super.init()
register(vm: Data1.self) { UIViewController1(data: $0) }
register(vm: Data2.self) { UIViewController2(data: $0) }
}
//
// Created by Apostolos Giokas on 23.03.21.
//
import UIKit
class ViewController: UIViewController, IViewControllerNavigator {
let button1 = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 45))
let button2 = UIButton(frame: CGRect(x: 50, y: 200, width: 200, height: 45))
protocol IViewControllerNavigator: UIViewController {
func navigate(data: Any, factory: ViewCοntrollerFactory)
}
extension IViewControllerNavigator {
func navigate(data: Any, factory: ViewCοntrollerFactory = MyViewControllerFactory()) {
guard let vmToPush = factory.create(for: data) else {
return
}
present(vmToPush, animated: true, completion: nil)
//
// Created by Apostolos Giokas on 23.03.21.
//
import UIKit
open class ViewCοntrollerFactory: Factory<UIViewController> {
public func register<T, OutP>(vm: T.Type, factory: @escaping (T) -> OutP) where OutP: UIViewController {
register(for: vm, factory: { factory($0) })
}
//
// Created by Apostolos Giokas on 10.03.21.
//
import Foundation
private protocol ICreator {
var identifier: ObjectIdentifier { get }
func create(for: Any) -> Any?
}
import Combine
import CoreData
import Foundation
class CDPublisher<Entity>: NSObject, NSFetchedResultsControllerDelegate, Publisher where Entity: NSManagedObject {
typealias Output = [Entity]
typealias Failure = Error
private let request: NSFetchRequest<Entity>
private let context: NSManagedObjectContext
import CoreData
import Foundation
import os.log
import RxCocoa
import RxSwift
class CDObservable<T>: NSObject, ObservableType, NSFetchedResultsControllerDelegate where T: NSManagedObject {
private var fetchedResultsController: NSFetchedResultsController<NSManagedObject>?
private let context: NSManagedObjectContext
private let fetchRequest: NSFetchRequest<T>