def add_friend(self, friend_id): friend_id = friend_id.replace("\n", "") # Make sure friend id was a number otherwise it's invalid try: int_friend_id = int(friend_id) except: # Friend id had some letters in it when it should just be a number self.root.ids['add_friend_screen'].ids[ 'add_friend_label'].text = "Friend ID should be a number." return # Make sure they aren't adding themselves if friend_id == self.my_friend_id: self.root.ids['add_friend_screen'].ids[ 'add_friend_label'].text = "You can't add yourself as a friend." return # Make sure this is not someone already in their friends list if friend_id in self.friends_list.split(","): self.root.ids['add_friend_screen'].ids[ 'add_friend_label'].text = "This user is already in your friend's list." return # Query database and make sure friend_id exists check_req = requests.get( 'https://friendly-fitness.firebaseio.com/.json?orderBy="my_friend_id"&equalTo=' + friend_id) data = check_req.json() if data == {}: # If it doesn't, display it doesn't in the message on the add friend screen self.root.ids['add_friend_screen'].ids[ 'add_friend_label'].text = "Invalid friend ID" else: # Requested friend ID exists key = list(data.keys())[0] #new_friend_id = data[key]['my_friend_id'] # Add friend id to friends list and patch new friends list self.friends_list += ",%s" % friend_id patch_data = '{"friends": "%s"}' % self.friends_list patch_req = requests.patch( "https://friendly-fitness.firebaseio.com/%s.json?auth=%s" % (self.local_id, self.id_token), data=patch_data) # Add new friend banner in friends list screen if friend_id in self.nicknames.keys(): friend_id_text = "[u]" + self.nicknames[friend_id] + "[/u]" else: friend_id_text = "[u]Friend ID: " + friend_id + "[/u]" friend_banner = FriendBanner(friend_id=friend_id, friend_id_text=friend_id_text) self.root.ids['friends_list_screen'].ids[ 'friends_list_grid'].add_widget(friend_banner) # Inform them they added a friend successfully self.root.ids['add_friend_screen'].ids[ 'add_friend_label'].text = "Friend ID %s added successfully." % friend_id print(self.nicknames)
def on_start(self): # Populate friends list grid friends_list_array = self.friends_list.split(",") for friend_id in friends_list_array: friend_id = friend_id.replace(" ", "") if friend_id == "": continue if friend_id in self.nicknames.keys(): friend_id_text = "[u]" + self.nicknames[friend_id] + "[/u]" else: friend_id_text = "[u]Friend ID: " + friend_id + "[/u]" friend_banner = FriendBanner(friend_id=friend_id, friend_id_text=friend_id_text) self.root.ids['friends_list_screen'].ids['friends_list_grid'].add_widget(friend_banner) self.change_screen("home_screen", "None")
def on_start(self): # Display the ads if platform == 'ios': from pyobjus import autoclass self.banner_ad = autoclass('adSwitch').alloc().init() # Choose the correct time icon to show based on the current hour of day now = datetime.now() hour = now.hour if hour <= 6: self.root.ids['time_indicator1'].opacity = 1 elif hour <= 12: self.root.ids['time_indicator2'].opacity = 1 elif hour <= 18: self.root.ids['time_indicator3'].opacity = 1 else: self.root.ids['time_indicator4'].opacity = 1 # Set the current day, month, and year in the add workout section day, month, year = now.day, now.month, now.year self.root.ids.add_workout_screen.ids.month_input.text = str(month) self.root.ids.add_workout_screen.ids.day_input.text = str(day) self.root.ids.add_workout_screen.ids.year_input.text = str(year) # Populate avatar grid avatar_grid = self.root.ids['change_avatar_screen'].ids['avatar_grid'] for root_dir, folders, files in walk("icons/avatars"): for f in files: img = ImageButtonSelectable(source="icons/avatars/" + f, on_release=partial( self.change_avatar, f)) avatar_grid.add_widget(img) # Populate workout image grid workout_image_grid = self.root.ids['add_workout_screen'].ids[ 'workout_image_grid'] for root_dir, folders, files in walk("icons/workouts"): for f in files: if '.png' in f: img = ImageButton(source="icons/workouts/" + f, on_release=partial( self.update_workout_image, f)) workout_image_grid.add_widget(img) try: # Try to read the persistent signin credentials (refresh token) with open(self.refresh_token_file, 'r') as f: refresh_token = f.read() # Use refresh token to get a new idToken id_token, local_id = self.my_firebase.exchange_refresh_token( refresh_token) self.local_id = local_id self.id_token = id_token # Get database data print("LOCAL ID IS", local_id) print("https://friendly-fitness.firebaseio.com/" + local_id + ".json?auth=" + id_token) result = requests.get("https://friendly-fitness.firebaseio.com/" + local_id + ".json?auth=" + id_token) data = json.loads(result.content.decode()) print("id token is", id_token) print(result.ok) print("DATA IS", data) self.my_friend_id = data['my_friend_id'] friend_id_label = self.root.ids['settings_screen'].ids[ 'friend_id_label'] friend_id_label.text = "Friend ID: " + str(self.my_friend_id) # Get and update avatar image avatar_image = self.root.ids['avatar_image'] avatar_image.source = "icons/avatars/" + data['avatar'] # Get friends list self.friends_list = data['friends'] # Get nicknames try: print(data['nicknames']) for i, friend_id in enumerate(self.friends_list.split(",")): if friend_id: print(i, friend_id) self.nicknames[friend_id] = data['nicknames'][i] except: self.nicknames = data.get('nicknames', {}) # Populate friends list grid friends_list_array = self.friends_list.split(",") for friend_id in friends_list_array: friend_id = friend_id.replace(" ", "") if friend_id == "": continue try: nicknames = list(self.friends_list.keys()) except: nicknames = self.nicknames if friend_id in nicknames: friend_id_text = "[u]" + self.nicknames[friend_id] + "[/u]" else: friend_id_text = "[u]Friend ID: " + friend_id + "[/u]" friend_banner = FriendBanner(friend_id=friend_id, friend_id_text=friend_id_text) self.root.ids['friends_list_screen'].ids[ 'friends_list_grid'].add_widget(friend_banner) # Get and update streak label streak_label = self.root.ids['home_screen'].ids['streak_label'] #streak_label.text = str(data['streak']) + " Day Streak" # Thisis updated if there are workouts # Set the images in the add_workout_screen banner_grid = self.root.ids['home_screen'].ids['banner_grid'] workouts = data['workouts'] if workouts != "": workout_keys = list(workouts.keys()) streak = helperfunctions.count_workout_streak(workouts) if str(streak) == 0: streak_label.text = "0 Day Streak. Go workout!" else: streak_label.text = str(streak) + " Day Streak!" # Sort workouts by date then reverse (we want youngest dates at the start) workout_keys.sort(key=lambda value: datetime.strptime( workouts[value]['date'], "%m/%d/%Y")) workout_keys = workout_keys[::-1] for workout_key in workout_keys: workout = workouts[workout_key] # Populate workout grid in home screen W = WorkoutBanner(workout_image=workout['workout_image'], description=workout['description'], type_image=workout['type_image'], number=workout['number'], units=workout['units'], likes=workout['likes'], date=workout['date']) banner_grid.add_widget(W) self.change_screen("home_screen", "None") except Exception as e: traceback.print_exc() pass
def add_friend(self, friend_id): friend_id = friend_id.replace("\n", "") # Make sure friend id was a number otherwise it's invalid try: int_friend_id = int(friend_id) except: # Friend id had some letters in it when it should just be a number self.show_popup("ID harus berupa angka", "number_only") return # Make sure they aren't adding themselves if friend_id == self.my_friend_id: self.show_popup( "Maaf, anda tidak dapat menambahkan ID anda sendiri", "own_id") return # Make sure this is not someone already in their friends list if friend_id in self.friends_list.split(","): self.show_popup("Teman dengan ID " + friend_id + " sudah ada", "duplicated") return # Query database and make sure friend_id exists check_req = requests.get( 'https://fitnessapp-kivy.firebaseio.com/.json?orderBy="my_friend_id"&equalTo="' + friend_id + '"') data = check_req.json() if data == {}: # If it doesn't, display it doesn't in the message on the add friend screen self.show_popup("ID teman tidak ditemukan", "not_found") else: # Requested friend ID exists key = list(data.keys())[0] # new_friend_id = data[key]['my_friend_id'] # Add friend id to friends list and patch new friends list self.friends_list += ",%s" % friend_id patch_data = '{"friends": "%s"}' % self.friends_list patch_req = requests.patch( "https://fitnessapp-kivy.firebaseio.com/" + self.local_id + ".json?auth=" + self.id_token, data=patch_data) if patch_req.ok == True: self.show_popup( "Teman dengan ID " + friend_id + " berhasil ditambahkan", "friend_added") self.root.ids['add_friend_screen'].ids[ 'add_friend_input'].text = "" else: self.show_popup("Maaf, ada kesalahan pada sistem", "error_adding") # Add new friend banner in friends list screen if friend_id in self.nicknames.keys(): friend_id_text = "[u]" + self.nicknames[friend_id] + "[/u]" else: friend_id_text = "[u]Friend ID: " + friend_id + "[/u]" friend_banner = FriendBanner(friend_id=friend_id, friend_id_text=friend_id_text) self.root.ids['friends_list_screen'].ids[ 'friends_list_grid'].add_widget(friend_banner)
def on_start(self): # Choose the correct time icon to show based on the current hour of day now = datetime.now() hour = now.hour if hour <= 6: self.root.ids['time_indicator1'].opacity = 1 elif hour <= 12: self.root.ids['time_indicator2'].opacity = 1 elif hour <= 18: self.root.ids['time_indicator3'].opacity = 1 else: self.root.ids['time_indicator4'].opacity = 1 # Set the current day, month, and year in the add workout section day, month, year = now.day, now.month, now.year self.root.ids.add_workout_screen.ids.month_input.text = str(month) self.root.ids.add_workout_screen.ids.day_input.text = str(day) self.root.ids.add_workout_screen.ids.year_input.text = str(year) # Populate avatar grid avatar_grid = self.root.ids['change_avatar_screen'].ids['avatar_grid'] for root_dir, folders, files in walk("icons/avatars"): for f in files: img = ImageButton(source="icons/avatars/" + f, on_release=partial(self.change_avatar, f)) avatar_grid.add_widget(img) # Populate workout image grid workout_image_grid = self.root.ids['add_workout_screen'].ids[ 'workout_image_grid'] for root_dir, folders, files in walk("icons/workouts"): for f in files: img = ImageButton(source="icons/workouts/" + f, on_release=partial(self.update_workout_image, f)) workout_image_grid.add_widget(img) try: # Try to read the persisten signin credentials (refresh token) with open("refresh_token.txt", "r") as f: refresh_token = f.read() # use refresh_token to get a new idToken self.id_token, self.local_id = self.my_firebase.exchange_refresh_token( refresh_token) # Get database data result = requests.get("https://fitnessapp-kivy.firebaseio.com/" + self.local_id + ".json?auth=" + self.id_token) data = json.loads(result.content.decode()) # Update avatar image avatar_image = self.root.ids['avatar_image'] avatar_image.source = "icons/avatars/" + data['avatar'] # Update streak label label_streak = self.root.ids['home_screen'].ids['streak_label'] # Friend List self.friends_list = data['friends'] # Get nicknames try: print(data['nicknames']) for i, friend_id in enumerate(self.friends_list.split(",")): if friend_id: print(i, friend_id) self.nicknames[friend_id] = data['nicknames'][i] except: self.nicknames = data.get('nicknames', {}) # Populate friends list grid friends_list_array = self.friends_list.split(",") for friend_id in friends_list_array: friend_id = friend_id.replace(" ", "") if friend_id == "": continue try: nicknames = list(self.friends_list.keys()) except: nicknames = self.nicknames if friend_id in nicknames: friend_id_text = "[u]" + self.nicknames[friend_id] + "[/u]" else: friend_id_text = "[u]Friend ID: " + friend_id + "[/u]" friend_banner = FriendBanner(friend_id=friend_id, friend_id_text=friend_id_text) self.root.ids['friends_list_screen'].ids[ 'friends_list_grid'].add_widget(friend_banner) # Update id label self.my_friend_id = data['my_friend_id'] friend_id_label = self.root.ids['settings_screen'].ids[ 'friend_id_label'] friend_id_label.text = "ID: " + str(data['my_friend_id']) # Get data workouts banner_grid = self.root.ids['home_screen'].ids['banner_grid'] workouts = data['workouts'] if workouts != "": workout_keys = list(workouts.keys()) streak = helperfunctions.count_workout_streak(workouts) if str(streak) == "0": label_streak.text = "[b]0 Hari berturut-turut, latihan sekarang![/b]" else: label_streak.text = "[b]" + str( streak) + " Hari berturut-turut[/b]" # Sort workouts by date then reverse (we want youngest dates at the start) workout_keys.sort(key=lambda value: datetime.strptime( workouts[value]['date'], "%m/%d/%Y")) workout_keys = workout_keys[::-1] for workout_key in workout_keys: workout = workouts[workout_key] # Populate workout grid in home screen W = WorkoutsBanner(workout_image=workout['workout_image'], description=workout['description'], type_image=workout['type_image'], number=float(workout['number']), units=workout['units'], likes=workout['likes'], date=workout['date']) banner_grid.add_widget(W) self.change_screen("home_screen", "none") except Exception as e: print(e) pass