用于现代 swift 应用程序开发的开源定义和相关扩展库

斯威夫特MVI

SwiftMVI 是一个开源库,其中包含用于现代 swift 应用程序开发的定义和相关扩展。

概述

SwiftMVI 提供以下功能:

  • 轻量级和可扩展:从单个减速器到复杂功能的广泛可能实现
  • 能够发布事件
  • 一个简单但非常有效的 API 将联合发布者绑定到化简器和发布者。

有关当前体系结构的架构

目标是一组用于存档结构化数据流的协议,只需最少的工作量。 与其他 MVI 实现相比,存在一些重要差异。 首先,在 SwiftMVI 状态下是一个可变的,因此化简器不返回任何甚至没有效果的东西。化简器可以使用协议及其 API 进行连接。可以使用方法连接现有的联合发布者,并且可以将实现的发布者作为发布服务器实例插入。在任何使用 .ObservableObjectProcessing.bindFeatureEventReducer

安装

您可以使用 Swift Package Manager 来集成库,方法是在 Package.swift 文件中添加以下依赖项或直接在 Xcode 中添加:

.Package(url: "https://github.com/xtro/SwiftMVI.git", majorVersion: 1)

用法

要使用 SwiftMVI 构建 UI 功能,您需要定义一些对域进行建模的类型和值:

  • ReducibleState: A type that describes the state of your UI.
  • Intent: A type that represents all of the actions that can happen in your feature, such as user actions, notifications, event sources and more.
  • IntentReducer: A function that handles intents and processes them over time.
  • Processing: Enable processing functionalities in a feature.

As a basic example, consider a UI that shows a number along with “+” and “−” buttons that increment and decrement the number.

Here we need to define a type for the feature’s state, which consists of an integer for the current count:

import SwiftMVI

class Feature: ObservableObject, ReducibleState {
    class State {
        var count = 0
    }
    var state = State()
}

We also need to define a type for the feature’s intents, there are two intents one for the increase and one for decrease:

extension Feature: IntentReducer {
    enum Intent {
        case increment
        case decrement
    }
}

And then we implement the reduce method which is responsible for handling the behaviour of the feature. In this example to change the state we need to add protocol and call :Processing.state

extension Feature: IntentReducer, Processing {
    enum Intent { ... }
    func reduce(intent: Intent) {
        switch intent {
        case .increment:
            state {
                $0.count += 1
            }
        case .decrement:
            state {
                $0.count -= 1
            }
        }
    }
}

And then finally we define the view that displays the feature, adding as or and call by passing its :StateObjectObservedObjectIntent

struct FeatureView: View {
    @StateObject var feature = Feature()
    
    var body: some View {
        VStack {
            HStack {
                Button("") { feature(.decrement) }
                Text("\(feature.state.count)")
                Button("+") { feature(.increment) }
            }
        }
    }
}

Sponsors

SwiftMVI is an MIT-licensed open-source project with its ongoing development made possible entirely by the support of awesome backers. If you’d like to join them, please consider sponsoring this development.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

This library is released under the MIT license. See LICENSE for details.

GitHub

点击跳转