Skip to content

Commit

Permalink
[chore] docs (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry authored Jun 29, 2023
1 parent 12c4995 commit ba16f1e
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 1 deletion.
44 changes: 44 additions & 0 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Documentation

on:
push:
branches:
- "main"

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: true

jobs:
build:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}documentation/xconfigs
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Generate Documentation
run: |
swift package --allow-writing-to-directory ./docs \
generate-documentation --target XConfigs \
--disable-indexing \
--transform-for-static-hosting \
--output-path ./docs \
--hosting-base-path XConfigs
- name: Setup Pages
uses: actions/configure-pages@v3

- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: './docs'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Package.resolved
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let package = Package(
.package(url: "https://github.com/ra1028/DiffableDataSources", from: "0.5.0"),
.package(url: "https://github.com/michaelhenry/Prettier.swift", from: "1.1.1"),
.package(url: "https://github.com/raspu/Highlightr", from: "2.1.2"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
],
targets: [
.target(
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [How to use](#how-to-use)
- [Documentation](#-documentation)
- [Example](#example)
- [Other Related](#other-related)
- [License](#license)
Expand Down Expand Up @@ -54,6 +55,10 @@ Eg.
#endif
```

## 📄 Documentation

Please refer to [XConfigs's docs](https://michaelhenry.github.io/XConfigs/documentation/xconfigs/).

## Example

Similar with logger tool such as [swift-log](https://github.com/apple/swift-log), You can simply create a single global variable or just a singleton, as long as the it conforms to [XConfigSpec](Sources/XConfigs/Protocols/XConfigsSpec.swift)ification and then use the `@XConfig` property wrapper inside it.
Expand Down
1 change: 1 addition & 0 deletions Sources/XConfigs/Protocols/ConfigInfo.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

/// A protocol for parsing a configuration spec file (`XConfigsSpec`).
public protocol ConfigInfo {
var configKey: String { get }
var displayName: String? { get }
Expand Down
1 change: 1 addition & 0 deletions Sources/XConfigs/Protocols/InAppConfigUpdateDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

/// A delegate that listen to the key-value changed done inside the in-app config screen.
public protocol InAppConfigUpdateDelegate {
func configWillUpdate(key: String, value: RawStringValueRepresentable, store: KeyValueStore)
}
1 change: 1 addition & 0 deletions Sources/XConfigs/Protocols/KeyValueProvider.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

/// A protocol for providing key value.
public protocol KeyValueProvider {
func get<Value: RawStringValueRepresentable>(for key: String) -> Value?
}
1 change: 1 addition & 0 deletions Sources/XConfigs/Protocols/KeyValueStore.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

/// A protocol for storing key values when the in-app modification is enabled.
public protocol KeyValueStore {
func get<Value: RawStringValueRepresentable>(for key: String) -> Value?
func set<Value: RawStringValueRepresentable>(value: Value, for key: String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

/// A protocol for representing any object or data type into a string or vise versa via `rawString` property.
public protocol RawStringValueRepresentable: Codable {
var rawString: String { get }

Expand Down
1 change: 1 addition & 0 deletions Sources/XConfigs/Services/LogicHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public struct ValueWithPermission<Value> {
}
}

/// A protocol that handles the actual logic for the configuration.
public protocol XConfigsLogicHandler {
func handle<Value: RawStringValueRepresentable>(isInAppModificationEnabled: Bool, key: String, defaultValue: Value, group: XConfigGroup, keyValueProvider: KeyValueProvider, keyValueStore: KeyValueStore?) -> ValueWithPermission<Value>
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/XConfigs/UseCases/XConfigUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
public class XConfigUseCase {
private let isInAppModificationEnabledKey = "XConfigs.Debug.isInAppModificationEnabled"

/// Determine if the in-app modification is enabled.
var isInAppModificationEnabled: Bool {
get {
keyValueStore?.get(for: isInAppModificationEnabledKey) ?? false
Expand All @@ -19,7 +20,7 @@ public class XConfigUseCase {
private let logicHandler: XConfigsLogicHandler
private let updateDelegate: InAppConfigUpdateDelegate?

// To update the local kv store and remote kv provider, please use the assigned method for it.
/// To update the local kv store and remote kv provider, please use the assigned method for it.
init(spec: XConfigsSpec.Type, keyValueProvider: KeyValueProvider, logicHandler: XConfigsLogicHandler, keyValueStore: KeyValueStore?, updateDelegate: InAppConfigUpdateDelegate?) {
configsSpec = spec
self.keyValueProvider = keyValueProvider
Expand All @@ -28,22 +29,26 @@ public class XConfigUseCase {
self.updateDelegate = updateDelegate
}

/// Get the information from the ConfigSpec.
func getConfigs() -> [ConfigInfo] {
let instance = configsSpec.init()
let mirror = Mirror(reflecting: instance)
return mirror.children.compactMap { $0.value as? ConfigInfo }
}

/// Get the Value of a particular key.
func get<Value: RawStringValueRepresentable>(for key: String, defaultValue: Value, group: XConfigGroup) -> ValueWithPermission<Value> {
logicHandler.handle(isInAppModificationEnabled: isInAppModificationEnabled, key: key, defaultValue: defaultValue, group: group, keyValueProvider: keyValueProvider, keyValueStore: keyValueStore)
}

/// Set the Value of a particular key.
func set<Value: RawStringValueRepresentable>(value: Value, for key: String) {
guard isInAppModificationEnabled, let store = keyValueStore else { return }
updateDelegate?.configWillUpdate(key: key, value: value, store: store)
keyValueStore?.set(value: value, for: key)
}

/// Reset the store (needed for the in-app modification).
func reset() {
guard isInAppModificationEnabled else { return }
getConfigs().forEach {
Expand Down
3 changes: 3 additions & 0 deletions Sources/XConfigs/XConfigs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
internal var defaultConfigUseCase: XConfigUseCase!

public enum XConfigs {
/// Use this method the configure the XConfigs into your app.
public static func configure(
with spec: XConfigsSpec.Type,
keyValueProvider: KeyValueProvider,
Expand All @@ -32,6 +33,8 @@ public enum XConfigs {
try vc.present(configsViewController().wrapInsideNavVC(), animated: animated, completion: nil)
}
#endif

/// A method to allow in-app modification or not.
public static func setInAppModification(enable: Bool) throws {
guard defaultConfigUseCase.keyValueStore != nil else { throw ConfigError.inAppModificationIsNotAllowed }
defaultConfigUseCase.isInAppModificationEnabled = enable
Expand Down

0 comments on commit ba16f1e

Please sign in to comment.