• This repository has been archived on 02/Oct/2022
  • Stars
    star
    45
  • Rank 624,037 (Top 13 %)
  • Language
    Swift
  • Created almost 4 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Auto-binding for RxSwift inspired by SwiftUI

RxUI

RxUI is inspired by SwiftUI. RxUI goal is to improve the developer experience of using RxSwift by allowing you to concentrate on the business logic instead of the low-level reactive code.

  • You can express your business logic in a natural way using plain Swift properties and methods
  • It makes it much easier to debug your views and view models. You can set breakpoints and query any of your view model state.
  • Itโ€™s beginner-friendly. You donโ€™t need to learn combineLatest, withLatestFrom and other complex stateful operators to use it. You don't need flatMap to send a network request.
  • It is more efficient because you avoid creating massive observable chains

WARNING This is proof of concept.

RxObservableObject

You can think of RxObservableObject and RxPublished as analogs of SwiftUI ObservableObject and Published.

final class LoginViewModel: RxObservableObject {
    @RxPublished var email = ""
    @RxPublished var password = ""
    @RxPublished private(set) var isLoading = false

    var title: String {
        "Welcome, \(email)"
    }

    var isLoginButtonEnabled: Bool {
        isInputValid && !isLoading
    }

    private var isInputValid: Bool {
        !email.isEmpty && !password.isEmpty
    }

    func login() {
        isLoading = true
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
            self.isLoading = false
        }
    }
}

Each RxObservableObject has objectWillChange relay. The relay is generated automatically and is automatically bound to all properties marked with @RxPublished property wrapper. This all happens in runtime using reflection and associated objects.

RxView

RxView is an analog of a SwiftUI View. There is, however, one crucial difference. In UIKit, views are expensive, can't recreate them each time. The is reflected in RxView design.

final class LoginViewController: UIViewController, RxView {
    private let model = LoginViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        // ... add views on screen ...

        disposeBag.insert(
            emailTextField.rx.text.bind(to: model.$email),
            passwordTextField.rx.text.bind(to: model.$password),
            loginButton.rx.tap.subscribe(onNext: model.login)
        )

        bind(model) // Automatically registers for update
    }

    // Called automatically when model changes, but no more frequently than
    // once per render cycle.
    func refreshView() {
        titleLabel.text = model.title
        model.isLoading ? spinner.startAnimating() : spinner.stopAnimating()
        loginButton.isEnabled = model.isLoginButtonEnabled
    }
}

When you call bind() method that accepts RxObservableObject it automatically registers for its updates (objectWillChange property). When the object is changed, refreshView() is called automatically. RxView hooks into the display system such that refreshView called only once per one render cycle.

License

RxUI is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

Nuke

Image loading system
Swift
8,112
star
2

Pulse

Network logger for Apple platforms
Swift
6,307
star
3

DFImageManager

Image loading, processing, caching and preheating
Objective-C
1,180
star
4

Get

Web API client built using async/await
Swift
941
star
5

Preheat

Automates prefetching of content in UITableView and UICollectionView
Swift
629
star
6

PulsePro

A macOS app for viewing logs from Pulse
Swift
482
star
7

Align

Intuitive and powerful Auto Layout library
Swift
350
star
8

Future

Streamlined Future<Value, Error> implementation
Swift
317
star
9

FetchImage

Makes it easy to download images using Nuke and display them in SwiftUI apps
Swift
212
star
10

Regex

Open source regex engine
Swift
211
star
11

Arranged

Open source replacement of UIStackView for iOS 8 (100% layouts supported)
Swift
208
star
12

VPN

Sample custom VPN client/server in Swift
Swift
182
star
13

Formatting

Swift
179
star
14

DFCache

Composite LRU cache with fast metadata using UNIX extended file attributes
Objective-C
162
star
15

RxNuke

RxSwift extensions for Nuke
Swift
148
star
16

CreateAPI

Delightful code generator for OpenAPI specs
Swift
142
star
17

SwiftSQL

Swift API for SQLite
Swift
131
star
18

ThreeColumnNavigation

A minimal example of three-column navigation for iPad and macOS using SwiftUI
Swift
127
star
19

Stacks

A micro UIStackView convenience API inspired by SwiftUI
Swift
73
star
20

Nuke-FLAnimatedImage-Plugin

FLAnimatedImage plugin for Nuke
Swift
53
star
21

Nuke-Alamofire-Plugin

Alamofire plugin for Nuke
Swift
40
star
22

NukeDemo

Nuke Demo
Swift
34
star
23

DFJPEGTurbo

Objective-C libjpeg-turbo wrapper
C
33
star
24

ImagePublisher

Combine publishers for Nuke
Swift
25
star
25

NukeUI

Lazy image loading for Apple platforms: SwiftUI, UIKit, AppKit
Swift
19
star
26

articles

Articles for kean.github.io
19
star
27

URLQueryEncoder

URL query encoder with support for all OpenAPI serialization options
Swift
17
star
28

NukeBuilder

A fun and convenient way to use Nuke
Swift
14
star
29

ScrollViewPrefetcher

Prefetching for SwiftUI
Swift
14
star
30

PulseLogHandler

SwiftLog Extension for Pulse
Swift
12
star
31

HTTPHeaders

Parsing Simple HTTP Headers
Swift
11
star
32

OctoKit

GitHub API client built with Fuse
Swift
8
star
33

PulseApps

Base Pulse macOS and iOS apps and a few demo projects
Swift
7
star
34

kean

1
star