Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
woodycatliu / ReactiveProgramming.md
Last active March 1, 2022 07:12
What is Reactive Programming in Swift ?

響應式編程 Reactive Programming

此篇將以 Swift 的角度去簡化 RP 理論的解釋

對於 iOS/OS 開發前端,這兩年很頻繁的會聽到這個名詞。
尤其是 Apple 推出 SwiftUI 與 Combine 以後,似乎 RxSwift 已經變成不是選修條件,越來越多公司在職缺條件中 Combine 或 RxSwift 是必要條件。
註:Combine/ RXSwift 是 RP 的高階封裝框架。

什麼是響應式編程(RP) ?

相信很多人 Google 之後會更加模糊,尤其是閱讀完 Wikipedia 艱深又空洞的理論介紹後,只看到一堆專業術語跟更多沒見過的名詞。

@woodycatliu
woodycatliu / ReadMe.md
Last active March 7, 2022 07:28
Countdown Timer Publisher (Swift5.2)

Countdown Timer Publisher (Swift5.2)

In the past we had to set the countdown by Timer.TimePublisher.

We maybe coding to like bellow.

let timer = Timer.publish(every: 0.01, on: .main, in: .common)
    .autoconnect()
 .map { _ in return 0.01 }
@woodycatliu
woodycatliu / ParseTCA2.md
Last active February 23, 2022 08:00
Refactor to make it a more complete and llke TCA

Redux and TCA Learning Record 2


Pervious ParseTCA, I created some encapsulation to reproduce how TCA to implement in binding to ViewStore and Store.

Today, I will try to refactor them to more complete, like composable action and flexibility.

Step 1.

Refocator Reducer

// refactor after: 
@woodycatliu
woodycatliu / ParseTCA.md
Last active February 23, 2022 08:02
How does implement of Store and ViewStore in TCA

Redux and TCA Learning Record 1


We can use Store and compose a new ViewStore to binding a smaller UI Block in subView or subViewController. How does TCA implements in binding each other ?

I crate some code to reproduce it.

Implement

@woodycatliu
woodycatliu / TCA_zh.md
Created February 19, 2022 09:56 — forked from sh3l6orrr/TCA_zh.md
TCA

The Composable Architecture (可组装架构)

The Composable Architecture (简写为TCA) 让你用统一、便于理解的方式来搭建应用程序,它兼顾了组装,测试,以及功效。你可以在 SwiftUI,UIKit,以及其他框架,和任何苹果的平台(iOS、macOS、tvOS、和 watchOS)上使用 TCA。

@woodycatliu
woodycatliu / Extension.swift
Created February 11, 2022 06:13
Easy way to safe in userDefault within Combine flow
import Combine
extension Publisher where Self.Output: Encodable {
func safeInUserDefaultFirst(_ key: String) -> AnyPublisher<Self.Output, Self.Failure> {
return flatMap { (output)-> Just<Self.Output> in
let data = try? JSONEncoder().encode(output)
UserDefaults.standard.set(data, forKey: key)
return Just(output)
}.eraseToAnyPublisher()
//
// KitProviderLayout.swift
//
// Created by Woody on 2022/2/11.
//
import UIKit
#if canImport(SwiftUI) && DEBUG
class ViewLayout {
@woodycatliu
woodycatliu / KitProviderLayout.swift
Last active February 14, 2022 08:42
UIKit_UIViewPreviewProvider
//
// KitProviderLayout.swift
//
// Created by Woody on 2022/2/11.
//
import UIKit
#if canImport(SwiftUI) && DEBUG
class ViewLayout {
@woodycatliu
woodycatliu / OptimizationImageView.swift
Last active January 28, 2022 06:48
Optimize the UIImageView
// DownsampingImageView
//
// Created by Woody on 2022/1/28.
//
import UIKit
fileprivate var WidthScale: (_ r: CGFloat, _ l: CGFloat)-> CGFloat = {
return Scale($0, $1)
}
@woodycatliu
woodycatliu / Combine_Extension_AssignFunctioin_Readme.md
Last active April 10, 2023 06:17
Swift Combine extension for support assign a function or closure

Swift 5.3 Combine

With RxSwift, it can support bind to a function/closure to more readability.

Example

textField.rx.text.orEmpty.asObservable()
            .subscribeOn(MainScheduler.instance)
            .bind(onNext: receiveText(_:))
 .disposed(by: disBag)