|
| 1 | +import UIKit |
| 2 | + |
| 3 | +enum EditorKind { |
| 4 | + case add |
| 5 | + case edit(original: String) |
| 6 | +} |
| 7 | + |
| 8 | +enum ListAction { |
| 9 | + case add |
| 10 | + case shuffle |
| 11 | + case xchgShuffle |
| 12 | +} |
| 13 | + |
| 14 | +protocol WordListChildBuilders { |
| 15 | + func makeEditor(_ kind: EditorKind, _ response: @escaping (String?) -> Void) -> UIViewController |
| 16 | + func makeActions(_ response: @escaping (ListAction) -> Void) -> UIViewController |
| 17 | +} |
| 18 | + |
| 19 | +struct WordListBuilder: WordListChildBuilders { |
| 20 | + func make(navigation: UINavigationController) -> UIViewController { |
| 21 | + let flowController = WordListFlowController(navigation: navigation, builders: self) |
| 22 | + let viewModel = WordListViewModel(routing: flowController) |
| 23 | + let viewController = WordListViewController(viewModel: viewModel) |
| 24 | + return viewController |
| 25 | + } |
| 26 | + |
| 27 | + func makeEditor(_ kind: EditorKind, _ response: @escaping (String?) -> Void) -> UIViewController { |
| 28 | + let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert) |
| 29 | + |
| 30 | + switch kind { |
| 31 | + case .add: |
| 32 | + alert.title = "Add new words" |
| 33 | + alert.message = "Separate words with comma or space." |
| 34 | + alert.addTextField { _ in } |
| 35 | + |
| 36 | + case let .edit(original): |
| 37 | + alert.title = "Enter the replacement word" |
| 38 | + alert.addTextField { $0.text = original } |
| 39 | + } |
| 40 | + |
| 41 | + let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in response(nil) }) |
| 42 | + alert.addAction(cancel) |
| 43 | + |
| 44 | + let commit = UIAlertAction(title: "Confirm", |
| 45 | + style: .default, |
| 46 | + handler: { _ in response(alert.textFields![0].text ?? "") }) |
| 47 | + alert.addAction(commit) |
| 48 | + |
| 49 | + alert.preferredAction = commit |
| 50 | + |
| 51 | + return alert |
| 52 | + } |
| 53 | + |
| 54 | + func makeActions(_ response: @escaping (ListAction) -> Void) -> UIViewController { |
| 55 | + let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) |
| 56 | + |
| 57 | + let create = UIAlertAction(title: "Add", style: .default, handler: { _ in response(.add) }) |
| 58 | + actionSheet.addAction(create) |
| 59 | + |
| 60 | + let shuffle = UIAlertAction(title: "Shuffle", style: .default, handler: { _ in response(.shuffle) }) |
| 61 | + actionSheet.addAction(shuffle) |
| 62 | + |
| 63 | + let xchgShuffle = UIAlertAction(title: "Exchange and Shuffle", style: .default, handler: { _ in response(.xchgShuffle) }) |
| 64 | + actionSheet.addAction(xchgShuffle) |
| 65 | + |
| 66 | + let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in }) |
| 67 | + actionSheet.addAction(cancel) |
| 68 | + |
| 69 | + return actionSheet |
| 70 | + } |
| 71 | +} |
0 commit comments