Skip to content

Commit

Permalink
Use new input set provider in UIKit demo
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Dec 1, 2020
1 parent 7e77c2d commit 72a3829
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 42 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
@@ -1,6 +1,7 @@
disabled_rules:
- function_body_length
- function_parameter_count
- identifier_name
- statement_position
- trailing_whitespace
- line_length
Expand Down
Expand Up @@ -6,7 +6,7 @@
"repositoryURL": "https://github.com/danielsaidi/KeyboardKit",
"state": {
"branch": "layout",
"revision": "b005323a1c2bad175e6869aedeeddb522f94d68f",
"revision": "33917a4c75265a1755f944048439e296f7cfb684",
"version": null
}
},
Expand Down
5 changes: 1 addition & 4 deletions KeyboardKitDemoKeyboard/KeyboardViewController+Setup.swift
Expand Up @@ -34,11 +34,8 @@ private extension KeyboardViewController {
}

func setupAlphabeticKeyboard(for state: KeyboardShiftState) {
let keyboard = AlphabeticKeyboard(uppercased: state.isUppercased, in: self)
let keyboard = AlphabeticKeyboard(isUppercased: state.isUppercased, in: self)
let rows = buttonRows(for: keyboard.actions, distribution: .fillProportionally)
let locale = Locale(identifier: "sv-SE")
let a = locale.languageCode

keyboardStackView.addArrangedSubviews(rows)
}

Expand Down
32 changes: 14 additions & 18 deletions KeyboardKitDemoKeyboard/Keyboards/AlphabeticKeyboard.swift
Expand Up @@ -9,13 +9,16 @@
import KeyboardKit

/**
This demo keyboard mimicks an English alphabetic keyboard.
This demo keyboard mimicks an alphabetic keyboard, and will
use the input set provider in the view controller's context.
*/
struct AlphabeticKeyboard: DemoKeyboard {

init(uppercased: Bool, in viewController: KeyboardViewController) {
init(isUppercased: Bool, in viewController: KeyboardViewController) {
let provider = viewController.context.inputSetProvider
actions = AlphabeticKeyboard.actions(
uppercased: uppercased,
for: provider.alphabeticInputSet,
isUppercased: isUppercased,
in: viewController)
}

Expand All @@ -25,33 +28,26 @@ struct AlphabeticKeyboard: DemoKeyboard {
private extension AlphabeticKeyboard {

static func actions(
uppercased: Bool,
for set: AlphabeticKeyboardInputSet,
isUppercased: Bool,
in viewController: KeyboardViewController) -> KeyboardActionRows {
KeyboardActionRows(characters: characters(uppercased: uppercased))
.addingSideActions(uppercased: uppercased)
let inputRows = set.inputRows
let inputs = isUppercased ? inputRows.uppercased() : inputRows
return KeyboardActionRows(characters: isUppercased ? inputs.uppercased() : inputs)
.addingSideActions(isUppercased: isUppercased)
.appending(bottomActions(leftmost: switchAction, for: viewController))
}

static let characters: [[String]] = [
["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"],
["a", "s", "d", "f", "g", "h", "j", "k", "l"],
["z", "x", "c", "v", "b", "n", "m"]
]

static func characters(uppercased: Bool) -> [[String]] {
uppercased ? characters.uppercased() : characters
}

static var switchAction: KeyboardAction {
.keyboardType(.numeric)
}
}

private extension Sequence where Iterator.Element == KeyboardActionRow {

func addingSideActions(uppercased: Bool) -> [Iterator.Element] {
func addingSideActions(isUppercased: Bool) -> [Iterator.Element] {
var result = map { $0 }
result[2].insert(uppercased ? .shift(currentState: .uppercased) : .shift(currentState: .lowercased), at: 0)
result[2].insert(isUppercased ? .shift(currentState: .uppercased) : .shift(currentState: .lowercased), at: 0)
result[2].insert(.none, at: 1)
result[2].append(.none)
result[2].append(.backspace)
Expand Down
20 changes: 10 additions & 10 deletions KeyboardKitDemoKeyboard/Keyboards/NumericKeyboard.swift
Expand Up @@ -9,31 +9,31 @@
import KeyboardKit

/**
This demo keyboard mimicks an English numeric keyboard.
This demo keyboard mimicks a numeric keyboard, and will use
the input set provider in the view controller's context.
*/
struct NumericKeyboard: DemoKeyboard {

init(in viewController: KeyboardViewController) {
actions = type(of: self).actions(in: viewController)
let provider = viewController.context.inputSetProvider
actions = type(of: self).actions(
for: provider.numericInputSet,
in: viewController)
}

let actions: KeyboardActionRows
}

private extension NumericKeyboard {

static func actions(in viewController: KeyboardViewController) -> KeyboardActionRows {
KeyboardActionRows(characters: characters)
static func actions(
for set: NumericKeyboardInputSet,
in viewController: KeyboardViewController) -> KeyboardActionRows {
KeyboardActionRows(characters: set.inputRows)
.addingSideActions()
.appending(bottomActions(leftmost: switchAction, for: viewController))
}

static let characters: [[String]] = [
["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
["-", "/", ":", ";", "(", ")", "$", "&", "@", "\""],
[".", ",", "?", "!", "´"]
]

static var switchAction: KeyboardAction {
.keyboardType(.alphabetic(.lowercased))
}
Expand Down
17 changes: 8 additions & 9 deletions KeyboardKitDemoKeyboard/Keyboards/SymbolicKeyboard.swift
Expand Up @@ -14,26 +14,25 @@ import KeyboardKit
struct SymbolicKeyboard: DemoKeyboard {

init(in viewController: KeyboardViewController) {
actions = type(of: self).actions(in: viewController)
let provider = viewController.context.inputSetProvider
actions = type(of: self).actions(
for: provider.symbolicInputSet,
in: viewController)
}

let actions: KeyboardActionRows
}

private extension SymbolicKeyboard {

static func actions(in viewController: KeyboardViewController) -> KeyboardActionRows {
KeyboardActionRows(characters: characters)
static func actions(
for set: SymbolicKeyboardInputSet,
in viewController: KeyboardViewController) -> KeyboardActionRows {
KeyboardActionRows(characters: set.inputRows)
.addingSideActions()
.appending(bottomActions(leftmost: switchAction, for: viewController))
}

static var characters: [[String]] = [
["[", "]", "{", "}", "#", "%", "^", "*", "+", "="],
["_", "\\", "|", "~", "<", ">", "", "£", "¥", ""],
[".", ",", "?", "!", "´"]
]

static var switchAction: KeyboardAction {
.keyboardType(.alphabetic(.lowercased))
}
Expand Down

0 comments on commit 72a3829

Please sign in to comment.