Skip to content

Commit

Permalink
Bug fix of Unplaned Workout
Browse files Browse the repository at this point in the history
  • Loading branch information
lingchao2016 committed Oct 31, 2016
1 parent a92f69a commit 55c79ba
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 756 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ fastlane/report.xml
fastlane/screenshots

demos/

GymBuddyUp/Base.lproj/Invitation.storyboard
63 changes: 49 additions & 14 deletions GymBuddyUp/API/Discover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,68 @@ class Discover {
// 4. for each workout
var invites = [Invite]()
let getInvitesTaskGrp = dispatch_group_create()


//let getInvitesTaskGrp2 = dispatch_group_create()
userFriendsRef.child((User.currentUser?.userId)!).queryOrderedByChild("is_friend").queryEqualToValue(1).observeSingleEventOfType(.Value, withBlock: {(snapshot) in

for friend in snapshot.children {
dispatch_group_enter(getInvitesTaskGrp)
// get this friend's active workout
let friendUid = (friend as! FIRDataSnapshot).key

dispatch_group_enter(getInvitesTaskGrp)

userWorkoutInviteRef.child(friendUid).queryOrderedByChild("workout_time").queryStartingAtValue(NSDate().timeIntervalSince1970).observeSingleEventOfType(.Value, withBlock: { (userWorkoutInviteRef) in
let invite = userWorkoutInviteRef.valueForKey("invite") as! String
print("friendUid :::::" + friendUid)
let getInvitesTaskGrp1 = dispatch_group_create()
userWorkoutInviteRef.child(friendUid).queryOrderedByChild("workout_time").observeSingleEventOfType(.Value, withBlock: { (userWorkoutInviteRef) in
let obj = userWorkoutInviteRef.value as! NSDictionary

for (key, value) in obj {
dispatch_group_enter(getInvitesTaskGrp1)
let inviteid = (value as! NSDictionary)["invite"] as! String
print("invite :::::" + inviteid)
// now get this invite object
workoutInviteRef.child(invite).queryOrderedByChild("access").queryStartingAtValue(1).observeSingleEventOfType(.Value, withBlock: { (inviteSnapshot) in
let inviteData = inviteSnapshot.value as! [String: AnyObject]
if let invite = Invite(JSON: inviteData){
if invite.isAvailable != false {
invites.append(invite)
workoutInviteRef.child(inviteid).observeSingleEventOfType(.Value, withBlock: { (inviteSnapshot) in
if(inviteSnapshot.value is NSNull){
print("invite is null")
}else{
var inviteData = inviteSnapshot.value as! [String: AnyObject]
inviteData["id"] = key
if let invite = Invite(JSON: inviteData){
if invite.isAvailable != false && invite.accessLevel != 2 {
print("invite is not null " + invite.id)
invites.append(invite)
}
}

}
}

dispatch_group_leave(getInvitesTaskGrp1)
})
/*workoutInviteRef.child(inviteidArray[0]).queryOrderedByChild("access").queryEqualToValue(1).observeSingleEventOfType(.Value, withBlock: { (inviteSnapshot) in
if(inviteSnapshot.value is NSNull){

}else{
let inviteData = inviteSnapshot.value as! [String: AnyObject]

if let invite = Invite(JSON: inviteData){
if invite.isAvailable != false {
invites.append(invite)
}
}
}

dispatch_group_leave(getInvitesTaskGrp2)

})*/
}
dispatch_group_notify(getInvitesTaskGrp1, dispatch_get_main_queue()) {
dispatch_group_leave(getInvitesTaskGrp)
})
}

})
}

dispatch_group_notify(getInvitesTaskGrp, dispatch_get_main_queue()) {
completion(invites, nil)
completion(invites, nil)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions GymBuddyUp/API/Gym.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Gym: NSObject, Mappable {
}

override init () {
self.name = "test gym"
self.name = ""
self.placeid = "-1"
self.address = "XXX University Dr, College Station"
self.address = ""
self.location = CLLocation(latitude: 30.563, longitude: -96.311)
}

Expand Down
49 changes: 48 additions & 1 deletion GymBuddyUp/API/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,54 @@ class Library {
completion(plans: nil, error: error)
}
}


class func getSinglePlanByMidId(catID: Int, completion: (plans: Plan?, error:NSError?) -> Void) {
print("getSinglePlanByMidId " + String(catID))

var plan = Plan()
midCategoryRef.child("\(catID)/system_plans").observeSingleEventOfType(.Value, withBlock: {
(snapshot) in

let getPlanTaskGrp1 = dispatch_group_create()

for planSnapshot in snapshot.children {
let planId = (planSnapshot as! FIRDataSnapshot).key
print("inside getSinglePlanByMidId " + planId)
dispatch_group_enter(getPlanTaskGrp1)

if let isMultiple = (planSnapshot as! FIRDataSnapshot).value as? Bool {
if(!isMultiple){
getPlanById(planId, completion: { (_plan, error) in
if _plan != nil {
print("getting plan", planId)
plan = _plan!
dispatch_group_leave(getPlanTaskGrp1)
}else{
dispatch_group_leave(getPlanTaskGrp1)
}
})

}else{
dispatch_group_leave(getPlanTaskGrp1)
}
}else{
dispatch_group_leave(getPlanTaskGrp1)
}

}

dispatch_group_notify(getPlanTaskGrp1, dispatch_get_main_queue()) {
print("leave getPlanTaskGrp1")
completion(plans: plan, error: nil)
}

}) {
(error) in
completion(plans: nil, error: error)
}

}

private class func getExercisesByPlanId(planId: String, completion: (exercises: [Exercise]?, error: NSError?) -> Void) {
planDetailRef.child(planId).observeSingleEventOfType(.Value, withBlock: {
(snapshot) in
Expand Down
4 changes: 2 additions & 2 deletions GymBuddyUp/API/ScheduledWorkout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ScheduledWorkout {
//assign this plan as user's today plan
//replace if plan exists

class func addWorkoutToCalendar(planId: String, startDate: NSDate, endDate: NSDate? = stringToDate("9999-12-31"), recur: Int = 0, completion: (NSError?) -> Void) {
class func addWorkoutToCalendar(planId: String, startDate: NSDate, endDate: NSDate? = stringToDate("9999-12-31"), recur: Int = 0, completion: (workoutId: String, NSError?) -> Void) {
let planId = planId
let newWorkoutRef = workoutCalendarRef.child(User.currentUser!.userId).childByAutoId()

Expand All @@ -115,7 +115,7 @@ class ScheduledWorkout {
// If workout is not recurring, set end_date same as start_date, else use specified end_date. if specified enddate is nil, use 9999-12-31.

newWorkoutRef.setValue(data) { (error, ref) in
completion(error)
completion(workoutId: newWorkoutRef.key, error)
}
}

Expand Down
4 changes: 2 additions & 2 deletions GymBuddyUp/API/Tracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Tracking {

}

class func trackedPlanOnSave(scheduledWorkout: String, planId: String, startTime: NSDate, endTime: NSDate, trackedItems: [TrackedItem],
class func trackedPlanOnSave(scheduledWorkout: String, planId: String, startTime: NSDate, endTime: NSDate, trackedItems: [TrackedItem], gym: Gym,
completion: (NSError?) -> Void) {

print("=========== Inside trackedPlanOnSave"+User.currentUser!.userId)
Expand All @@ -89,7 +89,7 @@ class Tracking {
subdata["scheduled_workout"] = scheduledWorkout
subdata["start_time"] = dateToInt(startTime)
subdata["end_time"] = dateToInt(endTime)

subdata["gym"] = gym.toDictionary()
for index in 0 ..< trackedItems.count {
print("=========== Inside trackedItems"+String(index))
var exercise_details = [String: AnyObject]()
Expand Down
Loading

0 comments on commit 55c79ba

Please sign in to comment.