ObservableThemeKit 1.0.0

ObservableThemeKit 1.0.0

Lukasz Sliwinski 维护。



  • 作者
  • Łukasz Śliwiński

ObservableThemeKit Logo

Swift 5.0 Version Platform Carthage Compatible SPM

ObservableThemeKit

ObservableThemeKit 框架允许轻松地对应用程序进行主题化。它使用面向协议的编程、属性包装和可观察模式,这使得可以针对需求定制主题规范的所有方面。

特性

  • 使用属性包装,轻松访问定义的主题
  • 允许观察主题样式的更改(可以是任何东西,这取决于您,例如 浅色深色 的过渡)
  • 您可以定义任何用于主题的样式(CSS样式表)

示例

示例应用程序是查看 ObservableThemeKit 最好的方式。只需打开 ObservableThemeKit.xcodeproj 并运行 Example 方案。

游乐场

示例应用程序之外的游乐场允许快速检查框架的用法。只需打开 ObservableThemeKit.xcworkspace 并从 Xcode 的 项目导航器 中选择 ObservableThemeKitPlayground

CocoaPods

ObservableThemeKit可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中

pod 'ObservableThemeKit'

Carthage

Carthage是一个去中心化的依赖管理器,用于构建您的依赖项并向您提供二进制框架。

要使用Carthage将ObservableThemeKit集成到您的Xcode项目中,请在您的Cartfile中指定它

github "nonameplum/ObservableThemeKit"

运行carthage update来构建框架,并将构建的ObservableThemeKit.framework拖入您的Xcode项目。

在您应用程序目标的“构建阶段”设置选项卡中,点击“+”图标,选择“新建运行脚本阶段”,并添加在Carthage入门步骤4、5和6中提到的框架路径

Swift Package Manager

要使用Apple的Swift Package Manager进行集成,请将以下内容作为依赖项添加到您的Package.swift

dependencies: [
    .package(url: "https://github.com/nonameplum/ObservableThemeKit.git", from: "1.0.0")
]

或者,导航到您的Xcode项目,选择Swift Packages,然后点击“+”图标以搜索ObservableThemeKit

手动

如果您不打算使用上述任何依赖管理器,您可以手动将ObservableThemeKit集成到项目中。只需将“源”文件夹拖入您的Xcode项目即可。

使用方法

最好的方法是查看示例应用程序Playground

该框架的主要概念是围绕Theme协议。

public protocol Theme {
    associatedtype Style
    init(stylesheet: Style)
    static var `default`: Self { get }
    static var stylesheet: Observable<Style> { get }
}

目标是为主题提供一种Style,它将被各种主题所使用,例如

struct AppStylesheet {
    let accentColor: UIColor
}

然后您可以这样声明第一个主题

struct ViewTheme: Theme {
    static let `default`: ViewController.ViewTheme = .init(stylesheet: AppStylesheet())
    static let stylesheet: Observable = Observable(AppStylesheet())
  
    let labelColor: UIColor

    init(stylesheet: AppStylesheet) {
        self.labelColor = stylesheet.accentColor
    }
}

最后一步是使用ObservableTheme 属性包装器 使用主题,这样您就有权在任何地方使用主题,例如

class ViewController: UIViewController {
		@ObservableTheme var theme: ViewTheme
}

stylesheet发生改变时,可以观察theme

class ViewController: UIViewController {
		@ObservableTheme var theme: ViewTheme
    override func viewDidLoad() {
        super.viewDidLoad()

        self.$theme.observe(
            owner: self,
            handler: { (owner, _) in
                owner.setupAppearance()
            }
        )
    }
}

ObservableTheme提供了projectedValue,它是一个Observable

重要的是要提到,ViewTheme实现了Theme协议,这意味着该struct必须提供

defaultstylesheet static属性和构造函数init

它被ObservableTheme用来实例化主题,观察stylesheet的变化,并在每次改变时实例化新的主题,并通过提到的可观察的projectedValue将其放回。

因为在大多数情况下,这样的主题声明没有任何意义,尤其是stylesheet属性

struct ViewTheme: Theme {
    static let `default`: ViewController.ViewTheme = .init(stylesheet: AppStylesheet())
    static let stylesheet: Observable = Observable(AppStylesheet())
		...
}

如果您发现声明默认的stylesheet实现有益,可以在extension中声明

extension Theme {
    /// Default stylesheet for the convenience
    static var stylesheet: Observable<Stylesheet> {
        return AppStylesheet.shared
    }
}

有了这个,然后您可以声明一个主题

struct ViewTheme: Theme {
    static let `default`: ViewController.ViewTheme = .init(stylesheet: Self.stylesheet.wrappedValue)
		...
}

但是,您将在示例中发现,有非常多提供defaultstylesheet的方法。这取决于您,它可能是单例、全局变量或任何满足您需求的解决方案。

贡献

非常欢迎贡献🙌

许可

ObservableThemeKit
Copyright (c) 2020 plum [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.