Skip to content

Instantly share code, notes, and snippets.

View chrsp's full-sized avatar

Charles Prado chrsp

  • Post Finance
  • Portugal
View GitHub Profile
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
@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
@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)