Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Search with Location
Browse files Browse the repository at this point in the history
  • Loading branch information
TorIsHere committed Jun 15, 2015
1 parent e5e3fba commit 653bc2b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
72 changes: 57 additions & 15 deletions benri/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll
self.locationManager.requestAlwaysAuthorization()
} else {
isInitiated = true
self.populateMenu(true, tags: nil)
self.populateMenu(true, tags: nil, location:locationService.locationToLonLat(locationService.getCurrentLocation()))
}
}

Expand All @@ -106,7 +106,7 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll
static var onceToken : dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
self.populateMenu(false, tags: nil)
self.populateMenu(false, tags: nil, location:self.locationService.locationToLonLat(self.locationService.getCurrentLocation()))
}
}

Expand Down Expand Up @@ -217,19 +217,21 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll
})
}

func populateMenu(isReset:Bool, tags: String?) -> Void {
func populateMenu(isReset:Bool, tags: String?, location: [CGFloat]?) -> Void {
if self.isPopulating {
return
}
if isReset {
self.currentLoadedIndex = 0
self.menuArray = []
}

println("location")
println(location)
isPopulating = true
var menuSVAPI:MenuSVAPI = MenuSVAPI()
if let searchTag = tags {
menuSVAPI.getMenuByTags(searchTag,
location:location,
start: self.currentLoadedIndex,
limit: self.populateLength,
successCallback: {(somejson) -> Void in
Expand Down Expand Up @@ -269,7 +271,8 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll
self.isPopulating = false
})
} else {
menuSVAPI.getMenu(self.currentLoadedIndex,
menuSVAPI.getMenu(location,
start:self.currentLoadedIndex,
limit: self.populateLength,
successCallback: {(somejson) -> Void in
if let json: AnyObject = somejson{
Expand Down Expand Up @@ -327,18 +330,43 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll
}
}

func castCGFloat()->(String)->CGFloat {
return { (invalue:String) -> CGFloat in
if let n = NSNumberFormatter().numberFromString(invalue) {
let result = CGFloat(n)
return result
}
return 0.0
}
}

func scrollViewDidScroll(scrollView: UIScrollView) {

if !isInitiated {
return

}

if scrollView.contentOffset.y + view.frame.size.height > scrollView.contentSize.height * 0.8 {
if let searchTag = const.getConst("search", key: "tag") {
self.populateMenu(false, tags: searchTag)
if let locationSearch = const.getConst("search", key: "location"){
let location:[CGFloat] = (split(locationSearch) {$0 == ","}).map(castCGFloat())
if let searchTag = const.getConst("search", key: "tag") {
self.populateMenu(false, tags: searchTag, location:location)
} else {
self.populateMenu(false, tags: nil, location:location)
}

} else {
self.populateMenu(false, tags: nil)
if let searchTag = const.getConst("search", key: "tag") {
self.populateMenu(false, tags: searchTag, location:nil)
} else {
self.populateMenu(false, tags: nil, location:nil)
}

}



}
let translation = scrollView.panGestureRecognizer.translationInView(scrollView.superview!)
if translation.y > 0 {
Expand Down Expand Up @@ -372,7 +400,9 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll
}
}*/
cell.setImageByURL(menu.imgURL)
cell.setMenuCell(menu.menuName, retaurantName: restuarant.name, distanceVal: 1.2, pointVal: 1, price: menu.price, address: restuarant.address)
let storeDistance = self.locationService.getDistanceFrom(restuarant.location)

cell.setMenuCell(menu.menuName, retaurantName: restuarant.name, distanceVal: storeDistance, pointVal: 1, price: menu.price, address: restuarant.address)
return cell
}

Expand All @@ -397,13 +427,25 @@ class FirstViewController: UIViewController, CLLocationManagerDelegate, UIScroll

func updateNotificationDiscoverSearch() {
// Mixapanel Track
if let searchTag = const.getConst("search", key: "picker") {
var mixPanelInstance:Mixpanel = Mixpanel.sharedInstance()
mixPanelInstance.track("Simulate Search Tag", properties: ["Tag" : searchTag])

const.setConst("search", key: "tag", value: searchTag)
self.populateMenu(true, tags: searchTag)
if let locationSearch = const.getConst("search", key: "location"){
let location:[CGFloat] = (split(locationSearch) {$0 == ","}).map(castCGFloat())
if let searchTag = const.getConst("search", key: "picker") {
var mixPanelInstance:Mixpanel = Mixpanel.sharedInstance()
mixPanelInstance.track("Simulate Search Tag", properties: ["Tag" : searchTag])

const.setConst("search", key: "tag", value: searchTag)
self.populateMenu(true, tags: searchTag, location:location)
}
} else {
if let searchTag = const.getConst("search", key: "picker") {
var mixPanelInstance:Mixpanel = Mixpanel.sharedInstance()
mixPanelInstance.track("Simulate Search Tag", properties: ["Tag" : searchTag])

const.setConst("search", key: "tag", value: searchTag)
self.populateMenu(true, tags: searchTag, location:nil)
}
}

const.deleteConst("search", key: "picker")
}

Expand Down
6 changes: 5 additions & 1 deletion benri/LocationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ class LocationService: NSObject {
private var currentLocality:String

override init() {
currentLocation = CLLocation(latitude: 35.6895, longitude: 139.6917)
currentLocation = CLLocation(latitude: 35.664122, longitude: 139.729426)
currentLocality = "Minato"
}

func getCurrentLocation() -> CLLocation {
return currentLocation
}

func locationToLonLat(location:CLLocation) -> [CGFloat] {
return [CGFloat(location.coordinate.longitude), CGFloat(location.coordinate.latitude)]
}

func getDistanceFrom(targetLocation:CLLocation) -> Double {
return currentLocation.distanceFromLocation(targetLocation) / 1000.0
}
Expand Down
15 changes: 11 additions & 4 deletions benri/MenuServerAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class MenuSVAPI {
}
}

func getMenu(start:Int, limit:Int, successCallback:(json:AnyObject?)->Void, errorCallback:()->Void){
func getMenu(location:[CGFloat]?, start:Int, limit:Int, successCallback:(json:AnyObject?)->Void, errorCallback:()->Void){

let params = [
var params:[String:AnyObject] = [
"start": String(start),
"limit": String(limit)
]
if let location = location {
params["geolocation"] = location
}

UIApplication.sharedApplication().networkActivityIndicatorVisible = true

Alamofire.request(.GET, apiBaseURL + apiEndPoint, parameters: params)
Expand All @@ -59,13 +63,16 @@ class MenuSVAPI {
}
}

func getMenuByTags(tags:String, start:Int, limit:Int, successCallback:(json:AnyObject?)->Void, errorCallback:()->Void){
func getMenuByTags(tags:String, location:[CGFloat]?,start:Int, limit:Int, successCallback:(json:AnyObject?)->Void, errorCallback:()->Void){

let params = [
var params:[String:AnyObject] = [
"tags" : tags,
"start": String(start),
"limit": String(limit)
]
if let location = location {
params["geolocation"] = location
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true

Alamofire.request(.GET, apiBaseURL + apiEndPoint, parameters: params)
Expand Down
3 changes: 3 additions & 0 deletions benri/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class SearchViewController: UIViewController, UIPickerViewDataSource, UIPickerVi
if sender.identifier == "selectLocationFromSearchSegueUnwind" {
// update location
locationLabel.text = searchLocation
let lonlatSearch = String(stringInterpolationSegment: searchLocationCoordinate.coordinate.longitude) + "," + String(stringInterpolationSegment: searchLocationCoordinate.coordinate.latitude)
println(lonlatSearch)
const.setConst("search", key: "location", value: lonlatSearch)
}
}

Expand Down

0 comments on commit 653bc2b

Please sign in to comment.