Skip to content

Commit

Permalink
[Feature] Custom display name support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhenry authored Jan 17, 2023
1 parent 01fb795 commit 940401d
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 186 deletions.
4 changes: 2 additions & 2 deletions Demo/DemoTests/MockConfigs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ struct MockConfigs: XConfigsSpec {
@XConfig(key: "width", defaultValue: 320, group: .feature2)
var width: Double

@XConfig(key: "accountType", defaultValue: .guest, group: .feature3)
@XConfig(key: "accountType", displayName: "Account Type", defaultValue: .guest, group: .feature3)
var accountType: AccountType

@XConfig(key: "contact", defaultValue: .default, group: .feature3)
@XConfig(key: "contact", displayName: "Contact", defaultValue: .default, group: .feature3)
var contact: Contact
}

Expand Down
8 changes: 4 additions & 4 deletions Demo/DemoTests/SnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ final class SnapshotTests: XCTestCase {
}

func testInputValueViewController() throws {
let vc = InputValueViewController(viewModel: .init(model: .init(key: "Hello", value: "World"))).wrapInsideNavVC()
let vc = InputValueViewController(viewModel: .init(model: .init(key: "Hello", value: "World", displayName: "Hello"))).wrapInsideNavVC()
assertSnapshot(matching: vc, as: .image)
}

func testInputValueViewControllerJSON() throws {
let vc = InputValueViewController(viewModel: .init(model: .init(key: "JSON", value: "{\"name\":\"Kel\", \"city\": \"Melbourne\" }"))).wrapInsideNavVC()
let vc = InputValueViewController(viewModel: .init(model: .init(key: "JSON", value: "{\"name\":\"Kel\", \"city\": \"Melbourne\" }", displayName: "Contact"))).wrapInsideNavVC()
assertSnapshot(matching: vc, as: .image)
}

func testInputValueViewControllerURL() throws {
let vc = InputValueViewController(viewModel: .init(model: .init(key: "URL", value: "https://google.com"))).wrapInsideNavVC()
let vc = InputValueViewController(viewModel: .init(model: .init(key: "URL", value: "https://google.com", displayName: "URL"))).wrapInsideNavVC()
assertSnapshot(matching: vc, as: .image)
}

func testOptionViewController() throws {
let choices = [1, 2, 3, 4].map { "Value\($0)" }.map { Choice(displayName: $0, value: $0) }
let vc = OptionViewController(viewModel: .init(model: .init(key: "Name", value: "Value1", choices: choices))).wrapInsideNavVC()
let vc = OptionViewController(viewModel: .init(model: .init(key: "Name", value: "Value1", choices: choices, displayName: "Name"))).wrapInsideNavVC()
assertSnapshot(matching: vc, as: .image(precision: 0.95))
}

Expand Down
346 changes: 173 additions & 173 deletions Demo/DemoTests/XConfigsTests.swift

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Sources/XConfigs/Models/InputModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import Foundation
struct TextInputModel: Hashable {
let key: String
let value: String
let displayName: String
}
1 change: 1 addition & 0 deletions Sources/XConfigs/Models/OptionSelectionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ struct OptionSelectionModel: Hashable {
let key: String
let value: String
let choices: [Choice]
let displayName: String
}
1 change: 1 addition & 0 deletions Sources/XConfigs/Models/ToggleModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import Foundation
struct ToggleModel: Hashable {
let key: String
let value: Bool
let displayName: String
}
4 changes: 3 additions & 1 deletion Sources/XConfigs/PropertyWrappers/XConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import Foundation
@propertyWrapper
public struct XConfig<Value: RawStringValueRepresentable>: ConfigInfo {
public let key: String
public let displayName: String?
public let defaultValue: Value
public let group: XConfigGroup

public var wrappedValue: Value {
defaultConfigUseCase.get(for: key, defaultValue: defaultValue, group: group)
}

public init(key: String, defaultValue: Value, group: XConfigGroup = .default) {
public init(key: String, displayName: String? = nil, defaultValue: Value, group: XConfigGroup = .default) {
self.key = key
self.displayName = displayName
self.defaultValue = defaultValue
self.group = group
}
Expand Down
1 change: 1 addition & 0 deletions Sources/XConfigs/Protocols/ConfigInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation

public protocol ConfigInfo {
var configKey: String { get }
var displayName: String? { get }
var configValue: RawStringValueRepresentable { get }

var group: XConfigGroup { get }
Expand Down
6 changes: 3 additions & 3 deletions Sources/XConfigs/ViewControllers/XConfigsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class XConfigsViewController: UITableViewController {
switch item {
case let .toggle(vm):
let cell = tableView.dequeueCell(UIViewTableWrapperCell<ToggleView>.self, for: indexPath)
cell.configure(with: (vm.key, vm.value))
cell.configure(with: (vm.displayName, vm.value))
cell.mainView.valueChangedPublisher
.map { KeyValue(key: vm.key, value: $0) }
.bind(to: self.updateValueSubject)
Expand All @@ -27,11 +27,11 @@ final class XConfigsViewController: UITableViewController {
return cell
case let .textInput(vm):
let cell = tableView.dequeueCell(UIViewTableWrapperCell<KeyValueView>.self, for: indexPath)
cell.configure(with: (vm.key, vm.value))
cell.configure(with: (vm.displayName, vm.value))
return cell
case let .optionSelection(vm):
let cell = tableView.dequeueCell(UIViewTableWrapperCell<KeyValueView>.self, for: indexPath)
cell.configure(with: (vm.key, vm.value))
cell.configure(with: (vm.displayName, vm.value))
return cell
case let .actionButton(title, _):
let cell = tableView.dequeueCell(UIViewTableWrapperCell<ActionView>.self, for: indexPath)
Expand Down
7 changes: 4 additions & 3 deletions Sources/XConfigs/ViewModels/XConfigsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ struct XConfigsViewModel: ViewModelType {
let key = info.configKey
switch info.configValue {
case let val as Bool:
return .toggle(.init(key: key, value: val))
return .toggle(.init(key: key, value: val, displayName: info.displayName ?? info.configKey))
case let val as any CaseIterable & RawStringValueRepresentable:
return .optionSelection(.init(
key: key,
value: (val as? CustomStringConvertible)?.description ?? val.rawString,
choices: val.allChoices
choices: val.allChoices,
displayName: info.displayName ?? info.configKey
))
default:
return .textInput(.init(key: key, value: info.configValue.rawString))
return .textInput(.init(key: key, value: info.configValue.rawString, displayName: info.displayName ?? info.configKey))
}
}
}
Expand Down

0 comments on commit 940401d

Please sign in to comment.