0xLet / SwiftUIKit

📱 UIKit code that is fun to write
https://0xlet.github.io/SwiftUIKit/
MIT License
74 stars 6 forks source link

Add UIView.later... #164

Closed 0xLeif closed 4 years ago

0xLeif commented 4 years ago

UIView.later POC

Example Usage

import UIKit
import SwiftUIKit
import LaterView

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.embed {
            UIView.later { later in
                later
                    .fetch(url: URL(string: "https://jsonplaceholder.typicode.com/todos/1")!)
                    .flatMap { data, response, error in
                        if let error = error {
                            return later.main {
                                VStack {
                                    [
                                        Image(.red),
                                        Label("Error: \(error)")
                                    ]
                                }
                            }
                        }

                        guard let data = data else {
                            return later.main {
                                List(defaultCellHeight: 256) {
                                    [
                                        Label("Response: \(String(describing: response))")
                                            .number(ofLines: 100)
                                    ]
                                }
                            }
                        }

                        return later.main {
                            Label("\(String(data: data, encoding: .utf8) ?? "-1")")
                                .number(ofLines: 10)
                                .padding()
                        }
                }
            }
        }
    }
}