Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // TGStationSearchWorker.swift
- // TicketCleverGo
- //
- // Created by Anh Phan Tran on 06/08/2018.
- // Copyright © 2018 The Distance. All rights reserved.
- //
- import Foundation
- import Promises
- protocol TGStationSearchWorkerProtocol {
- func getStations(for searchString: String, onlyRecent: Bool) -> Promise<[TGStation]>
- }
- class TGStationSearchWorker: TGStationSearchWorkerProtocol {
- var localDBService: TGLocalDBServiceProtocol
- init(localDBService: TGLocalDBServiceProtocol) {
- self.localDBService = localDBService
- }
- func sort(_ stations: [TGStation], with key: String) -> [TGStation] {
- var crsStations = [TGStation]()
- var name1stWordStations = [TGStation]()
- var name2ndWordStations = [TGStation]()
- var name3rdWordStations = [TGStation]()
- var remainingStations = [TGStation]()
- for station in stations {
- if key.hasPrefix(station.crs, caseInsensitive: false) {
- crsStations.append(station)
- } else if station.name.word(at: 0).hasPrefix(key, caseInsensitive: false) {
- name1stWordStations.append(station)
- } else if station.name.word(at: 1).hasPrefix(key, caseInsensitive: false) {
- name2ndWordStations.append(station)
- } else if station.name.word(at: 2).hasPrefix(key, caseInsensitive: false) {
- name3rdWordStations.append(station)
- } else {
- remainingStations.append(station)
- }
- }
- try? name1stWordStations.sort(by: self.stationNameAlphabeticalOrderSort)
- try? name2ndWordStations.sort(by: self.stationNameAlphabeticalOrderSort)
- try? name3rdWordStations.sort(by: self.stationNameAlphabeticalOrderSort)
- try? remainingStations.sort(by: self.stationNameAlphabeticalOrderSort)
- return crsStations + name1stWordStations + name2ndWordStations + name3rdWordStations + remainingStations
- }
- func stationNameAlphabeticalOrderSort(lhs: TGStation, rhs: TGStation) throws -> Bool {
- return lhs.name < rhs.name
- }
- func getStations(for key: String, onlyRecent: Bool) -> Promise<[TGStation]> {
- if key.isEmpty {
- let recentStations = TGLocalData.shared.recentStations
- if onlyRecent {
- return Promise(Array(recentStations))
- } else {
- return self.localDBService.fetchStations().then { stations -> [TGStation] in
- if !recentStations.isEmpty {
- var returningStations = stations
- if let mostRecentStation = recentStations.first, let index = returningStations.index(of: mostRecentStation) {
- returningStations.remove(at: index)
- returningStations.insert(mostRecentStation, at: 0)
- }
- if recentStations.count >= 2 {
- let secondMostRecentStation = returningStations[1]
- if let index = returningStations.index(of: secondMostRecentStation) {
- returningStations.remove(at: index)
- returningStations.insert(secondMostRecentStation, at: 1)
- }
- }
- return returningStations
- } else {
- return stations
- }
- }
- }
- } else if key.count < 3 {
- let predicate = NSPredicate(format: "crs BEGINSWITH[cd] %@ OR name CONTAINS[cd] %@", key, key)
- if onlyRecent {
- let filtered = TGLocalData.shared.recentStations.filter(predicate).sorted(byKeyPath: "name", ascending: true)
- return Promise(Array(filtered))
- } else {
- return self.localDBService.queryStations(with: predicate)
- }
- } else {
- // 1. CRS/Name Filter:
- return Promise<[TGStation]>() { fullfill, reject in
- let predicate = NSPredicate(format: "crs BEGINSWITH[cd] %@ OR name CONTAINS[cd] %@", key, key)
- if onlyRecent {
- let filtered = TGLocalData.shared.recentStations.filter(predicate)
- fullfill(Array(filtered))
- } else {
- self.localDBService.queryStations(with: predicate).then { filtered in
- fullfill(filtered)
- }
- }
- }
- // 2. Plugin suggestions:
- .then { filtered -> [TGStation] in
- let sortedStations = self.sort(filtered, with: key)
- var results = sortedStations
- var offset = 0
- for (index, station) in sortedStations.enumerated() {
- for suggestion in station.searchSuggestions.reversed() {
- if let suggestedStation = self.localDBService.getStation(withCrs: suggestion) {
- if !results.contains(suggestedStation) {
- results.insert(suggestedStation, at: index + 1 + offset)
- offset += 1
- }
- }
- }
- }
- return results
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement