Sort shortcuts by phrase

This commit is contained in:
Davide De Rosa 2019-03-27 20:55:50 +01:00
parent 5ee18071b0
commit f0d0eb5ed3
1 changed files with 16 additions and 3 deletions

View File

@ -29,7 +29,7 @@ import IntentsUI
import Passepartout_Core
@available(iOS 12, *)
private struct ShortcutWrapper {
private struct ShortcutWrapper: Comparable {
let phrase: String
let intentDescription: String?
@ -43,6 +43,18 @@ private struct ShortcutWrapper {
original: vs
)
}
// MARK: Equatable
static func ==(lhs: ShortcutWrapper, rhs: ShortcutWrapper) -> Bool {
return lhs.phrase == rhs.phrase
}
// MARK: Comparable
static func <(lhs: ShortcutWrapper, rhs: ShortcutWrapper) -> Bool {
return lhs.phrase < rhs.phrase
}
}
@available(iOS 12, *)
@ -112,6 +124,7 @@ class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutV
private func handleShortcuts(_ shortcuts: [INVoiceShortcut]) {
wrappers = shortcuts.map { ShortcutWrapper.from($0) }
wrappers?.sort()
reloadModel()
tableView.reloadData()
}
@ -183,9 +196,8 @@ class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutV
// MARK: ShortcutsAddViewControllerDelegate
func shortcutAddController(_ controller: ShortcutsAddViewController, voiceShortcut: INVoiceShortcut) {
// TODO: sort voice shortcuts by?
wrappers?.append(ShortcutWrapper.from(voiceShortcut))
wrappers?.sort()
reloadModel()
tableView.reloadData()
@ -210,6 +222,7 @@ class ShortcutsEditViewController: UITableViewController, INUIEditVoiceShortcutV
}
editedIndexPath = nil
wrappers?[indexPath.row] = ShortcutWrapper.from(voiceShortcut)
wrappers?.sort()
dismiss(animated: true) {
self.tableView.reloadData()