Skip to content

Instantly share code, notes, and snippets.

View chrsp's full-sized avatar

Charles Prado chrsp

  • Post Finance
  • Portugal
View GitHub Profile
@chrsp
chrsp / ValidateNIF.swift
Last active April 7, 2019 23:25
Implementação em Swift da validação de NIF português. https://pt.wikipedia.org/wiki/N%C3%BAmero_de_identifica%C3%A7%C3%A3o_fiscal
func isNifValid(nif: String) -> Bool {
guard nif.count == 9, let nifPrefix = Int(nif.prefix(1)) else { return false }
if [1, 2, 5, 6, 9].contains(nifPrefix) {
var checkDigit = nifPrefix * 9
for index in 2...nif.count - 1 {
let indexBound = nif.index(nif.startIndex, offsetBy: index - 1)
checkDigit += (Int(String(nif[indexBound])) ?? 0) * (10 - index)
@chrsp
chrsp / DynamicCGColor.swift
Last active September 17, 2019 10:43
Snippets of code to prepare your app for dark mode (https://developer.apple.com/videos/play/wwdc2019/214)
// Only contents of UIKit can use DynamicColors. So if you need the dynamic color behavior
// on CALayer, CGColor etc you need to use one of these 3 options:
// Option 1
let resolvedColor = UIColor.label.resolvedColor(with: traitCollection)
layer.borderColor = resolvedColor.cgColor
// Option 2
traitCollection.perfomAsCurrent {
layer.borderColor = UIColor.label.cgColor
extension Reactive where Base == UIScreen {
@available(iOS 13.0, *)
func userInterfaceStyle() -> Observable<UIUserInterfaceStyle> {
let currentUserInterfaceStyle = UITraitCollection.current.userInterfaceStyle
let initial = Observable.just(currentUserInterfaceStyle)
let selector = #selector(UIScreen.traitCollectionDidChange(_:))
let following = self.base
.rx
.methodInvoked(selector)
.flatMap { (args) -> Observable<UIUserInterfaceStyle> in
[
{
"themeName": "base",
"colors": [
{
"name": "colorPrimary",
"hex": "fafafa"
},
{
"name": "colorOnPrimary",
// Module: Domain
struct KitchenUseCase {
// Obtains stuff already stored in some place inside the restaurant
// For example: fetching it from CoreData, KeychainManager, UserDefaults and so on.
private let localDataSource: KitchenLocalRepository
// Obtains stuff needed from outside of our restaurant.
// For example: doing API calls to a NetworkManager.
// Module: Domain
protocol Cookable {
func cook()
}
protocol Recipe: Cookable {
// Recipe definition
}
public protocol KitchenLocalRepository {
public func getIngredients(_ recipe: Recipe) -> [Ingredient]?
}
public protocol KitchenRemoteRepository {
public func getIngredients(_ recipe: Recipe, completion: ([Ingredient]?) -> Void))
}
public protocol CookerProtocol {
public func cook(_ recipe: Recipe)
// Module: Data
import Domain
public struct: ItalianCooker: CookerProtocol {
func cook(_ recipe: Recipe) {
switch recipe {
case recipe is SpaghettiCarbonara:
cookSpaghettiCarbonara(recipe)
// Module: Data
import Domain
public struct KitchenLocalDataSource: KitchenLocalRepository {
public func getIngredients(_ recipe: Recipe) -> [Ingredient]? {
// do something to fetch the ingredients needed from some local store: UserDefaults, CoreData, Memory Cache, KeychainManager etc
}
}
// Module: CleanArchitectureRestaurant
import Domain
import Data
import Presentation
final class DinningHallDIContainer {
func makeKitchenUseCase() -> KitchenUseCase {
let cooker = ItalianCooker()