Skip to content

Instantly share code, notes, and snippets.

@swift2931
swift2931 / Bonfire.swift
Last active December 2, 2023 10:33
networking for SwiftUI
import SwiftUI
import Combine
protocol Service {
static var mods: [String: (inout URLRequest) -> Void] {get set}
var baseURL: String {get set}
func config(_ pat: String, _ mod: @escaping (inout URLRequest) -> Void)
func decorated(_ absURL: String, _ req: URLRequest) -> URLRequest
func match(_ pat: String, _ absURL: String) -> Bool
func makeRequest(_ relativeURL: String) -> URLRequest
//
// Redux.swift
// Elf
//
// Created by JimLai on 2019/8/18.
// Copyright © 2019 JimLai. All rights reserved.
//
import SwiftUI
@swift2931
swift2931 / MJS.swift
Created June 1, 2019 04:02
MJ extension for Siesta
extension TypedContentAccessors {
var mj: MJ {
return MJ(entityForTypedContentAccessors?.content as Any)
}
}
import UIKit
struct Rx<T> {
var tx: ((T) -> ())?
var val: T {
didSet {
tx?(val)
}
}
@swift2931
swift2931 / Tx.swift
Created January 30, 2019 05:27
Alternative to RxSwift
import UIKit
protocol Redux: class {
func action<V> (_ action: @escaping (V, Self) -> ()) -> (V) -> ()
}
extension Redux where Self: UIViewController {
func action<V> (_ action: @escaping (V, Self) -> ()) -> (V) -> () {
return { [weak self] v in
if let vc = self {
action(v, vc)
import UIKit
func dp(_ any: Any...) {
print(any)
}
protocol Redux: class {
func action(_ f: String) -> (Self) -> ()
func effect(_ f: String) -> Self
func effect(_ desc: String, _ cls: () -> ()) -> Self
@swift2931
swift2931 / creating view controller from storyboard
Last active January 26, 2019 10:47
Initialize a view controller with its view defined in storyboard
iOS development builds around storyboard.
my opinionated approach is to keep only 1 view controller in each storyboard.
use it only as the view of controller, but do most of the control in code.
you may wonder why not just using XIB?
first it requires you overriding loadView(), which is some overhead
@swift2931
swift2931 / Future.swift
Created January 26, 2019 05:31
A future promise without inheritance
import UIKit
struct Future<U> {
var u: U? {
didSet {
promise?(u)
}
}
var promise: ((U?) -> ())?
func then<V>(_ action: @escaping (U?, Future<V>) -> ()) -> Future<V> {