diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 7652a55b84..639f526311 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -20,6 +20,7 @@ Breaking changes can still occur in minor versions, if the alternative is to not * `AutocompleteToolbar` has a new `itemBuilder` initializer. * `AutocompleteToolbarItem` is a new view that replicates a native autocomplete item. * `AutocompleteToolbarItemText` is a new view that replicates the text of a native autocomplete item. +* `KeyboardAction` has a new `shouldApplyAutocompleteSuggestion` property. * `KeyboardLocale` now implementes `Identifiable`. * `KeyboardLocale` has new `flag`, `id` and `localeIdentifier` properties. * `KeyboardInputViewController` has a new `autocompleteSuggestionProvider` property. diff --git a/Sources/KeyboardKit/Actions/KeyboardAction+Autocomplete.swift b/Sources/KeyboardKit/Actions/KeyboardAction+Autocomplete.swift new file mode 100644 index 0000000000..9e9859e577 --- /dev/null +++ b/Sources/KeyboardKit/Actions/KeyboardAction+Autocomplete.swift @@ -0,0 +1,26 @@ +// +// KeyboardAction+Actions.swift +// KeyboardKit +// +// Created by Daniel Saidi on 2021-03-18. +// Copyright © 2021 Daniel Saidi. All rights reserved. +// + +import Foundation + +public extension KeyboardAction { + + /** + Whether or not the action, when triggered, should apply + autocomplete suggestions where `isAutocomplete` is true. + */ + var shouldApplyAutocompleteSuggestion: Bool { + switch self { + case .character(let char): return char.isWordDelimiter + case .newLine: return true + case .return: return true + case .space: return true + default: return false + } + } +} diff --git a/Sources/KeyboardKit/_Deprecated/KeyboardLocale+Deprecated.swift b/Sources/KeyboardKit/_Deprecated/KeyboardLocale+Deprecated.swift index dc4fe23eb3..9a8ef21389 100644 --- a/Sources/KeyboardKit/_Deprecated/KeyboardLocale+Deprecated.swift +++ b/Sources/KeyboardKit/_Deprecated/KeyboardLocale+Deprecated.swift @@ -2,6 +2,6 @@ import Foundation public extension KeyboardLocale { - @available(*, deprecated, renamed: "identifier") + @available(*, deprecated, renamed: "id") var key: String { id } } diff --git a/Tests/KeyboardKitTests/Actions/KeyboardAction+AliasesTests.swift b/Tests/KeyboardKitTests/Actions/KeyboardAction+AliasesTests.swift index 63f38305d7..dfab623c9d 100644 --- a/Tests/KeyboardKitTests/Actions/KeyboardAction+AliasesTests.swift +++ b/Tests/KeyboardKitTests/Actions/KeyboardAction+AliasesTests.swift @@ -1,5 +1,5 @@ // -// KeyboardActionRowTest.swift +// KeyboardAction+AliasesTests.swift // KeyboardKit // // Created by Daniel Saidi on 2019-07-04. @@ -10,7 +10,7 @@ import Quick import Nimble import KeyboardKit -class KeyboardActionRowTest: QuickSpec { +class KeyboardAction_AliasesTests: QuickSpec { override func spec() { diff --git a/Tests/KeyboardKitTests/Actions/KeyboardAction+AutocompleteTests.swift b/Tests/KeyboardKitTests/Actions/KeyboardAction+AutocompleteTests.swift new file mode 100644 index 0000000000..529c3478a2 --- /dev/null +++ b/Tests/KeyboardKitTests/Actions/KeyboardAction+AutocompleteTests.swift @@ -0,0 +1,40 @@ +// +// KeyboardAction_AutocompleteTests.swift +// KeyboardKit +// +// Created by Daniel Saidi on 2021-03-18. +// Copyright © 2021 Daniel Saidi. All rights reserved. +// + +import Quick +import Nimble +import KeyboardKit + +class KeyboardAction_AutocompleteTests: QuickSpec { + + override func spec() { + + var actions: [KeyboardAction]! + var delimiterActions: [KeyboardAction]! + + beforeEach { + actions = KeyboardAction.testActions + delimiterActions = String.wordDelimiters.map { .character($0) } + delimiterActions.forEach { actions.append($0) } + } + + describe("should apply autocomplete suggestion") { + + it("is true for word and sentence ending actions") { + var expected = delimiterActions! + expected.append(.newLine) + expected.append(.return) + expected.append(.space) + + actions.forEach { + expect($0.shouldApplyAutocompleteSuggestion).to(equal(expected.contains($0))) + } + } + } + } +}