shuklavatsal1992

AlgoliaSearch

Jul 8th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.74 KB | None | 0 0
  1. extension CLLocation {
  2.     var latlong:LatLng {
  3.         return LatLng(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
  4.     }
  5. }
  6.  
  7.  
  8. struct AlgoliaSetting {
  9.     var id:String = ""
  10.     var searchKey:String = ""
  11.     var writeKey:String = ""
  12. }
  13.  
  14.  
  15. class AlgoliaSearch {
  16.     ///Globle shared instance
  17.     static let shared :AlgoliaSearch = AlgoliaSearch()
  18.    
  19.     private var query:Query?
  20.    
  21.     private var index:Index!
  22.    
  23.     private var client:Client! {
  24.         didSet {
  25.             self.index = self.client.index(withName: "Stocks")
  26.         }
  27.     }
  28.    
  29.     private var ongoingOperation:Operation?
  30.    
  31.     private var setting:AlgoliaSetting! {
  32.         didSet {
  33.             self.client = Client(appID: setting.id, apiKey: setting.searchKey)
  34.         }
  35.     }
  36.    
  37.     ///shows Kilometers. Default value 5KMs
  38.     var radius:UInt = 5
  39.    
  40.     private init() {
  41.        
  42.     }
  43.    
  44.     func configure(_ setting:AlgoliaSetting) {
  45.         self.setting = setting
  46.     }
  47. }
  48.  
  49.  
  50. ///PART -2
  51. extension AlgoliaSearch {
  52.     func cancelSearch() {
  53.         self.query = nil
  54.     }
  55.    
  56.     private func search(usingQuery q:Query, block_:((AlgoliaSearchResult?,Error?) -> Void)?) -> Operation {
  57.         let op = self.index.search(q, completionHandler: { (response, error) in
  58.             if let e = error {
  59.                 block_?(nil,e)
  60.             } else if let response = response {
  61.                 do {
  62.                     let result = try JSONSchemaSupport().decode(using: response, toType: AlgoliaSearchResult.self)
  63.                     block_?(result,nil)
  64.                 }
  65.                 catch {
  66.                     block_?(nil,error)
  67.                 }
  68.                 /*if let data = try? JSONSerialization.data(withJSONObject: response, options: .prettyPrinted) {
  69.                     let json = String(data: data, encoding: .utf8)
  70.                     print(json)
  71.                 }*/
  72.             }
  73.         })
  74.         return op
  75.     }
  76.    
  77.     func showMoreResults(from lastResult:AlgoliaSearchResult?,callback block:((AlgoliaSearchResult?,Error?) -> Void)?) {
  78.         guard let q = self.query, let result = lastResult else {
  79.             block?(nil,nil)
  80.             return
  81.         }
  82.         //FOR PAGINATION
  83.         q.page = UInt(result.page) + 1
  84.        
  85.         if let location = LocationHelper.shared.currentLocation {
  86.             q.aroundLatLng = location.latlong
  87.             self.ongoingOperation = self.search(usingQuery: q, block_: block)
  88.         }else {
  89.             LocationHelper.shared.getWhenInUseLocation { (location, msg) in
  90.                 q.aroundLatLng = location!.latlong
  91.                 self.ongoingOperation = self.search(usingQuery: q, block_: block)
  92.             }
  93.         }
  94.     }
  95.    
  96.     func search(_ text:String = "",_ radius:UInt = 5,_ lastResult:AlgoliaSearchResult?,callback block:((AlgoliaSearchResult?,Error?) -> Void)?) {
  97.        
  98.         if self.query == nil {
  99.             self.query = Query(query: text)
  100.             self.query?.aroundRadius = .explicit((radius * 1000))
  101.             self.query?.hitsPerPage = 20
  102.             self.query?.page = 0
  103.         }
  104.        
  105.         if let q = self.query {
  106.            
  107.             //FOR PAGINATION
  108.             if let result = lastResult {
  109.                 q.page = UInt(result.page) + 1
  110.             }
  111.             if let location = LocationHelper.shared.currentLocation {
  112.                 q.aroundLatLng = location.latlong
  113.                 self.ongoingOperation = self.search(usingQuery: q, block_: block)
  114.             }else {
  115.                 LocationHelper.shared.getWhenInUseLocation { (location, msg) in
  116.                     guard let loc = location else { return }
  117.                     q.aroundLatLng = loc.latlong
  118.                     self.ongoingOperation = self.search(usingQuery: q, block_: block)
  119.                 }
  120.             }
  121.         }else {
  122.             block?(nil,nil)
  123.         }
  124.        
  125.     }
  126.    
  127. }
Add Comment
Please, Sign In to add comment