Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension CLLocation {
- var latlong:LatLng {
- return LatLng(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
- }
- }
- struct AlgoliaSetting {
- var id:String = ""
- var searchKey:String = ""
- var writeKey:String = ""
- }
- class AlgoliaSearch {
- ///Globle shared instance
- static let shared :AlgoliaSearch = AlgoliaSearch()
- private var query:Query?
- private var index:Index!
- private var client:Client! {
- didSet {
- self.index = self.client.index(withName: "Stocks")
- }
- }
- private var ongoingOperation:Operation?
- private var setting:AlgoliaSetting! {
- didSet {
- self.client = Client(appID: setting.id, apiKey: setting.searchKey)
- }
- }
- ///shows Kilometers. Default value 5KMs
- var radius:UInt = 5
- private init() {
- }
- func configure(_ setting:AlgoliaSetting) {
- self.setting = setting
- }
- }
- ///PART -2
- extension AlgoliaSearch {
- func cancelSearch() {
- self.query = nil
- }
- private func search(usingQuery q:Query, block_:((AlgoliaSearchResult?,Error?) -> Void)?) -> Operation {
- let op = self.index.search(q, completionHandler: { (response, error) in
- if let e = error {
- block_?(nil,e)
- } else if let response = response {
- do {
- let result = try JSONSchemaSupport().decode(using: response, toType: AlgoliaSearchResult.self)
- block_?(result,nil)
- }
- catch {
- block_?(nil,error)
- }
- /*if let data = try? JSONSerialization.data(withJSONObject: response, options: .prettyPrinted) {
- let json = String(data: data, encoding: .utf8)
- print(json)
- }*/
- }
- })
- return op
- }
- func showMoreResults(from lastResult:AlgoliaSearchResult?,callback block:((AlgoliaSearchResult?,Error?) -> Void)?) {
- guard let q = self.query, let result = lastResult else {
- block?(nil,nil)
- return
- }
- //FOR PAGINATION
- q.page = UInt(result.page) + 1
- if let location = LocationHelper.shared.currentLocation {
- q.aroundLatLng = location.latlong
- self.ongoingOperation = self.search(usingQuery: q, block_: block)
- }else {
- LocationHelper.shared.getWhenInUseLocation { (location, msg) in
- q.aroundLatLng = location!.latlong
- self.ongoingOperation = self.search(usingQuery: q, block_: block)
- }
- }
- }
- func search(_ text:String = "",_ radius:UInt = 5,_ lastResult:AlgoliaSearchResult?,callback block:((AlgoliaSearchResult?,Error?) -> Void)?) {
- if self.query == nil {
- self.query = Query(query: text)
- self.query?.aroundRadius = .explicit((radius * 1000))
- self.query?.hitsPerPage = 20
- self.query?.page = 0
- }
- if let q = self.query {
- //FOR PAGINATION
- if let result = lastResult {
- q.page = UInt(result.page) + 1
- }
- if let location = LocationHelper.shared.currentLocation {
- q.aroundLatLng = location.latlong
- self.ongoingOperation = self.search(usingQuery: q, block_: block)
- }else {
- LocationHelper.shared.getWhenInUseLocation { (location, msg) in
- guard let loc = location else { return }
- q.aroundLatLng = loc.latlong
- self.ongoingOperation = self.search(usingQuery: q, block_: block)
- }
- }
- }else {
- block?(nil,nil)
- }
- }
- }
Add Comment
Please, Sign In to add comment