Sort shortcuts by phrase

This commit is contained in:
Davide De Rosa 2019-03-27 20:55:50 +01:00
parent 5ee18071b0
commit f0d0eb5ed3

View File

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