Skip to content

Commit b47a661

Browse files
committed
New main kluster fetch
1 parent f16161a commit b47a661

File tree

5 files changed

+74
-22
lines changed

5 files changed

+74
-22
lines changed

Cluster.xcworkspace/xcuserdata/mafellows.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,21 @@
1919
landmarkType = "5">
2020
</BreakpointContent>
2121
</BreakpointProxy>
22+
<BreakpointProxy
23+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
24+
<BreakpointContent
25+
shouldBeEnabled = "Yes"
26+
ignoreCount = "0"
27+
continueAfterRunningActions = "No"
28+
filePath = "Cluster/HomeViewController.swift"
29+
timestampString = "478164263.175424"
30+
startingColumnNumber = "9223372036854775807"
31+
endingColumnNumber = "9223372036854775807"
32+
startingLineNumber = "98"
33+
endingLineNumber = "98"
34+
landmarkName = "fetchKlusters()"
35+
landmarkType = "5">
36+
</BreakpointContent>
37+
</BreakpointProxy>
2238
</Breakpoints>
2339
</Bucket>

Cluster/HomeViewController.swift

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class HomeViewController: UIViewController {
2323
//MARK: - UICollectionViewDataSource
2424
private var klusters = [PFObject]()
2525
var locationManager = CLLocationManager()
26+
var currentGeoPoint: PFGeoPoint?
2627

2728
//MARK: - Change Status Bar to White
2829
override func preferredStatusBarStyle() -> UIStatusBarStyle {
@@ -50,6 +51,8 @@ class HomeViewController: UIViewController {
5051
menuButton.addTarget(self.revealViewController(), action: "revealToggle:", forControlEvents: UIControlEvents.TouchUpInside)
5152
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
5253
}
54+
55+
self.calculateCurrentLocation()
5356
}
5457

5558
override func viewWillAppear(animated: Bool) {
@@ -74,6 +77,14 @@ class HomeViewController: UIViewController {
7477
self.presentViewController(loginVC, animated: true, completion: nil)
7578
}
7679

80+
private func calculateCurrentLocation() {
81+
PFGeoPoint.geoPointForCurrentLocationInBackground({ (geoPoint, error) -> Void in
82+
if (error == nil) {
83+
self.currentGeoPoint = geoPoint
84+
}
85+
})
86+
}
87+
7788
@IBAction func searchButtonPressed(sender: AnyObject) {
7889
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
7990
let searchController = storyboard.instantiateViewControllerWithIdentifier("KlusterSearchController") as! KlusterSearchController
@@ -82,12 +93,28 @@ class HomeViewController: UIViewController {
8293
}
8394

8495
private func fetchKlusters() {
85-
KlusterDataSource.fetchKlustersForUser { (object, error) -> Void in
86-
if (error != nil) {
87-
print("Error: %@", error?.localizedDescription)
88-
} else {
89-
self.klusters = object as! [PFObject]
90-
self.collectionView.reloadData()
96+
97+
// This is incredibly gross and should be refactored
98+
if (self.currentGeoPoint != nil) {
99+
let params = ["latitude" : self.currentGeoPoint!.latitude,
100+
"longitude" : self.currentGeoPoint!.longitude]
101+
102+
KlusterDataSource.fetchMainKlusters(params as [NSObject : AnyObject]) { (objects, error) -> Void in
103+
if (error != nil) {
104+
print("Error: %@", error?.localizedDescription)
105+
} else {
106+
self.klusters = objects as! [PFObject]
107+
self.collectionView.reloadData()
108+
}
109+
}
110+
} else {
111+
KlusterDataSource.fetchMainKlusters(nil) { (objects, error) -> Void in
112+
if (error != nil) {
113+
print("Error: %@", error?.localizedDescription)
114+
} else {
115+
self.klusters = objects as! [PFObject]
116+
self.collectionView.reloadData()
117+
}
91118
}
92119
}
93120
}
@@ -108,10 +135,14 @@ extension HomeViewController : UICollectionViewDataSource
108135
let cellIdentifier = "Kluster Cell"
109136
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! KlusterCollectionViewCell
110137

138+
let user = PFUser.currentUser()
111139
let k = Kluster.init(object: self.klusters[indexPath.item])
112140
cell.kluster = k
113141
cell.joinKlusterButton.tag = indexPath.row
114142
cell.joinKlusterButton.addTarget(self, action: "joinKluster:", forControlEvents: UIControlEvents.TouchUpInside)
143+
cell.joinKlusterButton.hidden = k.isCreator(user)
144+
145+
cell.distanceLabel.text = k.distanceToKluster(self.currentGeoPoint)
115146

116147
// Moved the PFImageView loading out of the cell
117148
cell.featuredImageView.tag = indexPath.row
@@ -161,8 +192,7 @@ extension HomeViewController : CLLocationManagerDelegate {
161192

162193
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
163194
if (status == .AuthorizedWhenInUse) {
164-
self.createKlusterButton.enabled = true
165-
self.locationManager.startUpdatingLocation()
195+
self.calculateCurrentLocation()
166196
}
167197
}
168198
}

Cluster/Kluster.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Kluster
2222
var featuredImageFile: PFFile!
2323
var distanceString = ""
2424
var memberRelation: PFRelation!
25+
var creator: PFUser?
2526

2627
// init(id: String, title: String, description: String, distance: String, featuredImage: UIImage!)
2728
init(object: PFObject!) {
@@ -33,20 +34,20 @@ class Kluster
3334
self.featuredImageFile = object.objectForKey("photo") as! PFFile
3435
self.numberOfMembers = object.objectForKey("memberCount") as! Int
3536
self.memberRelation = object.relationForKey("members")
37+
self.creator = object.objectForKey("creatorId") as? PFUser
38+
3639
// TODO: Calculate distance string
3740
}
3841

39-
// MARK: - Private
40-
//
41-
// static func createKlusters() -> [Kluster]
42-
// {
43-
// return [
44-
// Kluster(id: "r1", title: "Miami Clique", description: "We love backpack and adventures! We walked to Antartica yesterday, and camped with so me cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", distance: "7 miles", featuredImage: UIImage(named: "1")!),
45-
// Kluster(id: "r2", title: "Romance Novels", description: "We love romantic stories. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", distance: "9 miles", featuredImage: UIImage(named: "2")!),
46-
// Kluster(id: "r3", title: "iOS Dev", description: "Create beautiful apps. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", distance: "10 miles", featuredImage: UIImage(named: "3")!),
47-
// Kluster(id: "r4", title: "Race", description: "Cars and aircrafts and boats and sky. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", distance: "11 miles",featuredImage: UIImage(named: "5")!),
48-
// Kluster(id: "r5", title: "Personal Development", description: "Meet life with full presence. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", distance: "12 miles",featuredImage: UIImage(named: "1")!),
49-
// Kluster(id: "r6", title: "Reading News", description: "Get up to date with breaking-news. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", distance: "15 miles",featuredImage: UIImage(named: "2")!),
50-
// ]
51-
// }
42+
internal func isCreator(user: PFUser!) -> Bool {
43+
return user.objectId == self.creator?.objectId
44+
}
45+
46+
internal func distanceToKluster(point: PFGeoPoint?) -> String {
47+
if (point == nil) {
48+
return ""
49+
}
50+
51+
return String(format: "%.0fmi", point!.distanceInMilesTo(self.location))
52+
}
5253
}

Cluster/KlusterDataSource.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class KlusterDataSource: NSObject {
2323
}
2424
}
2525

26+
class func fetchMainKlusters(params: [NSObject : AnyObject]?, completion: PFIdResultBlock) -> Void {
27+
PFCloud.callFunctionInBackground("fetchMainKlusters", withParameters: params) { (object, error) -> Void in
28+
completion(object, error)
29+
}
30+
}
31+
2632
class func fetchKlustersForUser(completion:PFIdResultBlock) -> Void {
2733
PFCloud.callFunctionInBackground("fetchKlustersForUser", withParameters: nil) { (object, error) -> Void in
2834
completion(object, error)

Cluster/NewKlusterViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class NewKlusterViewController: UIViewController, UIImagePickerControllerDelegat
106106
actionSheet.addAction(currentLocationAction)
107107

108108
let pickLocation = UIAlertAction.init(title: "Pick A Location", style: .Default) { (action) -> Void in
109-
self.presentViewController(actionSheet, animated: true, completion: nil)
110109
let storyboard = UIStoryboard.init(name: "Map", bundle: nil)
111110
let mapController = storyboard.instantiateInitialViewController() as! UINavigationController
112111
// let locationController = mapController.childViewControllers.first as! LocationSelectViewController

0 commit comments

Comments
 (0)