This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Toggle.swift | |
| // Switch | |
| // | |
| // Created by Maciek Grzybowski on 28.07.2017. | |
| // Copyright © 2017 ncreated. All rights reserved. | |
| // | |
| import RxSwift | |
| import RxCocoa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func testItEmitsPredictionsResult() { | |
| let disposeBag = DisposeBag() | |
| let provider = MockAutocompleteProvider() | |
| let autocomplete = Autocomplete(provider: provider) | |
| let input = SimulatedInput() | |
| let mockPrediction = Prediction(predictedText: "Italy", | |
| matchingRange: ("It".startIndex..<"It".endIndex)) | |
| let expectation = self.expectation(description: "will emit result") | |
| let (result, _) = autocomplete.autocomplete(text: input.text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SearchViewController: UITableViewController { | |
| @IBOutlet weak var searchBar: UISearchBar! | |
| @IBOutlet var noResultsView: UIView! | |
| @IBOutlet var errorView: UIView! | |
| @IBOutlet weak var errorMessageLabel: UILabel! | |
| private let disposeBag = DisposeBag() | |
| private let autocomplete = Autocomplete(provider: CountryAutocompleteProvider()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let autocomplete = Autocomplete(provider: CountryAutocompleteProvider()) | |
| let (results, isLoading) = autocomplete.autocomplete(text: inputText) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func autocomplete(text: Observable<String>) -> (result: Driver<AutocompleteResult>, | |
| isBusy: Driver<Bool>) { | |
| let provider = self.provider | |
| let resultAndLoading: Driver<(result: AutocompleteResult?, loading: Bool)> = text | |
| .flatMapLatest { query -> Observable<(result: AutocompleteResult?, loading: Bool)> in | |
| provider | |
| .autocomplete(text: query) | |
| .map { (result: .predictions($0), loading: false) } | |
| .catchError { .just((result: .error($0), loading: false)) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func autocomplete(text: Observable<String>) -> (result: Driver<AutocompleteResult>, isBusy: Driver<Bool>) { | |
| let provider = self.provider | |
| let isLoadingSubject = PublishSubject<Bool>() | |
| let result: Driver<AutocompleteResult> = text | |
| .do(onNext: { _ in isLoadingSubject.onNext(true) }) | |
| .flatMapLatest { query -> Observable<AutocompleteResult> in | |
| provider | |
| .autocomplete(text: query) | |
| .map { .predictions($0) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func autocomplete(text: Observable<String>) -> (result: Driver<AutocompleteResult>, | |
| isBusy: Driver<Bool>) { | |
| let provider = self.provider | |
| let result: Driver<AutocompleteResult> = text | |
| .flatMapLatest { query in | |
| provider | |
| .autocomplete(text: query) | |
| .map { AutocompleteResult.predictions($0) } | |
| .catchError { .just(AutocompleteResult.error($0)) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class Autocomplete { | |
| // MARK: - Properties | |
| private let provider: AutocompleteProvider | |
| // MARK: - Initializers | |
| init(provider: AutocompleteProvider) { | |
| self.provider = provider |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import RxSwift | |
| private let countries = ["Afghanistan", "Albania", /* all the countries */ "Zambia", "Zimbabwe"] | |
| final class CountryAutocompleteProvider: AutocompleteProvider { | |
| func autocomplete(text: String) -> Observable<[Prediction]> { | |
| func predictionsFor(prefix: String) -> [Prediction] { | |
| return countries | |
| .flatMap { country in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Prediction { | |
| let predictedText: String | |
| let matchingRange: Range<String.Index> | |
| } |