def __init__(self, active_exercise, screen, **kwargs): super(ExerciseGrid, self).__init__(**kwargs) self.active_exercise = active_exercise self.base_exercise = active_exercise.base_exercise self.reps = active_exercise.reps self.sets = active_exercise.sets self.weight = active_exercise.weight self.rest_time = active_exercise.rest_time self.main_grid = GridLayout(cols=1) self.screen = screen self.height = 190 grid = GridLayout(cols=1, padding=[10, 10], spacing=[0, 10], size_hint=[1, None]) title_grid = GridLayout(cols=2, size_hint=[1, None], height=40) grid.add_widget(title_grid) KivyButton(grid=title_grid, md_bg_color=L_GREEN, font_size=15, on_release_action=None, size_hint=[1 / 2, None], height=40, text=f"{self.base_exercise.name}") self.rest_title = KivyLabel(title_grid, font_size=25, halign="right", size_hint=[1 / 2, None], height=40) exercise_info_grid = GridLayout(cols=2, size_hint=[1, None], height=40) rep_text = f"{self.sets} sets of {self.reps} reps" if self.base_exercise.each_side: rep_text += " each side" weight_button = KivyButton(grid=exercise_info_grid, md_bg_color=L_GREEN, font_size=15, on_release_action=None, size_hint=[1 / 2, None], height=40, text=f"{self.weight} kg") weight_button.bind(on_release=lambda weight_button: screen.change_weight_action(weight_button)) KivyLabel(grid=exercise_info_grid, font_size=30, halign="center", size_hint=[1 / 2, None], height=40, text=rep_text) grid.add_widget(exercise_info_grid) self.exercise_row = ExerciseRow(self.sets, self.reps, self.rest_time, self, self.screen) grid.add_widget(self.exercise_row) self.add_widget(grid)
class ActiveGymScreen(Screen): change_weight = False weight_canvas = None selected_exercise = None weight_input = None info_label = None save_button = None routine_changed = False previous_routine = None def __init__(self, app, **kwargs): super(ActiveGymScreen, self).__init__(**kwargs) self.app = app def start_routine(self, routine): self.ids.active_routine_grid.clear_widgets() self.ids.title.text = routine.name self.all_grids = [] self.routine = routine if self.weight_canvas is not None: self.canvas.remove(self.weight_canvas) for set in self.routine.exercises: if isinstance(set, SuperSet): grid = SuperSetGrid(set, self) self.all_grids.append(grid) self.ids.active_routine_grid.add_widget(grid) elif isinstance(set, Exercise): grid = ExerciseGrid(set, self) self.all_grids.append(grid) self.ids.active_routine_grid.add_widget(grid) KivyButton(grid=self.ids.active_routine_grid, md_bg_color=L_GREEN, font_size=15, on_release_action=self.finish_routine, size_hint=[1, None], height=100, text="Finish Routine") def change_weight_action(self, exercise_button, *args): if not self.change_weight: self.ids.main_scroll.do_scroll_y = False self.change_weight = True self.weight_canvas = InstructionGroup() self.weight_canvas.add(Color(0, 0, 0, 0.4)) self.background = Rectangle(rectangle=(0, 0, 0, 0)) self.weight_canvas.add(self.background) self.weight_canvas.add(Color(1, 1, 1, 1)) self.weight_rect = Rectangle(rectangle=(0, 0, 0, 0)) self.weight_canvas.add(self.weight_rect) self.canvas.add(self.weight_canvas) self.weight_input = IntegerInput(self, "Weight(kg)", 0, 500, True, exercise_button.exercise.weight, None, size_hint=[1 / 3, None], height=100, pos_hint={"x": 1 / 3, "center_y": 4 / 9}) label_text = "" if exercise_button.exercise.base_exercise.equipment == "Dumbbell": label_text = "The weight for each dumbbell. " elif exercise_button.exercise.base_exercise.equipment == "Barbell": label_text = "The total weight of the barbell (usually 20kg/45lb) and the plates on either side" self.info_label = KivyLabel(grid=self, font_size=20, halign="center", size_hint=[3 / 4, 1 / 3], pos_hint={"center_x": 1 / 2, "top": 3 / 4}, color=(0, 0, 0, 1), text=label_text) self.save_button = KivyButton(grid=self, md_bg_color=L_GREEN, font_size=25, on_release_action=None, size_hint=[9 / 20, 1 / 10], pos_hint={"x": 11 / 40, "y": 11 / 40}, text="Save Weight") self.save_button.bind(on_release=lambda _: self.save_weight(self.weight_input.text, exercise_button)) self.update_bg() self.bind(size=self.update_bg, pos=self.update_bg) self.bind(on_touch_up=self.weight_touch_up) def update_bg(self, *args): self.background.size, self.background.pos = [self.width, self.height], [0, 0] self.weight_rect.size, self.weight_rect.pos = [self.width * 3 / 4, self.height / 2], [self.width / 8, self.height / 4] def weight_touch_up(self, *args): if not self.width / 4 < args[1].x < self.width * (3 / 4) or not self.height / 4 < args[1].y < self.height * ( 3 / 4): self.unbind(on_touch_up=self.weight_touch_up) def change_weight_bool(self, *args): self.ids.main_scroll.do_scroll_y = True self.change_weight = False def save_weight(self, exercise_text, exercise_button): self.hide_weight_change() try: float(exercise_text) except ValueError: return exercise_button.text = f"{float(exercise_text)} kg" self.previous_routine = self.routine for exercise_set in self.routine.exercises: if isinstance(exercise_set, SuperSet): for exercise in exercise_set.super_set_exercises: if exercise.base_exercise.number == exercise_button.exercise.base_exercise.number: pass exercise.weight = float(exercise_text) elif isinstance(exercise_set, Exercise): if exercise_set.base_exercise.number == exercise_button.exercise.base_exercise.number: pass exercise_set.weight = float(exercise_text) save_new_routine("files/gym_files/saved_routines.pkl", [self.routine.name, self.routine.exercises], self.previous_routine) self.routine_changed = True def finish_routine(self): self.previous_routine = self.routine all_data = [] for set_of_exercises in self.all_grids: if isinstance(set_of_exercises, SuperSetGrid): superset_data = ["Superset", [set_of_exercises.sets, set_of_exercises.rest_time], []] for active_exercise in set_of_exercises.active_exercises: superset_data[2].append([active_exercise, []]) for row in set_of_exercises.all_rows: for i in range(len(row.all_buttons)): superset_data[2][i][1].append(row.all_buttons[i].reps) all_data.append(superset_data) elif isinstance(set_of_exercises, ExerciseGrid): exercise_data = ["Exercise", set_of_exercises.active_exercise, []] for button in set_of_exercises.exercise_row.all_buttons: exercise_data[2].append(button.reps) all_data.append(exercise_data) def progression_calculator(exercise, reps_completed, total_reps): def weight_calculator(weight, progression): if 0.0 <= float(weight) < 10.0: weight = round(weight) + progression * 1 if weight < 0.0: weight = 0.0 elif weight > 10.0: weight = 10.0 elif 10.0 <= float(weight) < 20.0: weight = round(weight) + progression * 2 if weight < 10.0: weight = 10.0 elif weight > 20.0: weight = 20.0 elif float(weight) >= 20.0: if exercise.base_exercise.equipment == "Dumbbell": weight += 2.0 elif exercise.base_exercise.equipment == "Barbell" or "Body-weight": weight += 2.5 return float(weight) progress = 0 if (reps_completed / total_reps) * 100 == 100.0: progress = 1 elif 75.0 <= (reps_completed / total_reps) * 100 < 100.0: progress = 0 elif 0.0 <= (reps_completed / total_reps) * 100 < 75.0: progress = -1 exercise.reps = exercise.reps + progress if exercise.reps > exercise.max_reps: exercise.weight = weight_calculator(exercise.weight, 1) exercise.reps = exercise.min_reps elif exercise.reps < exercise.min_reps: exercise.weight = weight_calculator(exercise.weight, -1) exercise.reps = exercise.max_reps return exercise new_routine_exercises = [] for data in all_data: if data[0] == "Superset": new_exercises = [] for exercise in data[2]: reps_completed = 0 for active_reps in exercise[1]: if active_reps is None: active_reps = 0 reps_completed += active_reps new_exercises.append( progression_calculator(exercise[0], reps_completed, exercise[0].reps * data[1][0])) new_routine_exercises.append(SuperSet(new_exercises, [data[1][0], data[1][1]])) elif data[0] == "Exercise": reps_completed = 0 for active_reps in data[2]: if active_reps is None: active_reps = 0 reps_completed += active_reps new_routine_exercises.append( progression_calculator(data[1], reps_completed, data[1].reps * data[1].sets)) save_new_routine("files/gym_files/saved_routines.pkl", [self.routine.name, new_routine_exercises], self.previous_routine) screen = self.app.screen_action("gym_screen", "down", "R/T") screen.start() def hide_weight_change(self): self.unbind(size=self.update_bg, pos=self.update_bg) if self.weight_canvas is not None: self.canvas.remove(self.weight_canvas) self.weight_canvas = None self.change_weight = False if self.weight_input is not None: self.remove_widget(self.weight_input) if self.info_label is not None: self.remove_widget(self.info_label) if self.save_button is not None: self.remove_widget(self.save_button) Clock.schedule_once(self.change_weight_bool, 0.5) def gym_screen(self): if not self.change_weight: screen = self.app.screen_action("gym_screen", "down", "R/T") screen.start() def home_screen(self): if not self.change_weight: self.app.screen_action(selected_screen_name="home_screen", direction="down", action="R/T").start() def open_nav_drawer(self): if not self.change_weight: self.ids.nav_drawer.set_state()
def __init__(self, active_super_set, screen, **kwargs): super(SuperSetGrid, self).__init__(**kwargs) self.active_exercises = active_super_set.super_set_exercises self.sets = active_super_set.sets self.rest_time = active_super_set.rest_time self.screen = screen self.height = 225 + 60 * self.sets self.all_rows = [] grid = GridLayout(cols=1, size_hint=[1, None], height=250 + 60 * self.sets, spacing=[0, 10], padding=[10, 10]) title_grid = GridLayout(cols=2, size_hint=[1, None], height=40) KivyLabel(grid=title_grid, font_size=30, halign="center", size_hint=[1 / 2, None], text="Super-Set", height=40) self.rest_title = KivyLabel(grid=title_grid, font_size=30, halign="right", size_hint=[1 / 2, None], height=40) grid.add_widget(title_grid) exercise_button_grid = GridLayout(rows=1, cols=len(self.active_exercises), size_hint=[1, None], height=50, spacing=[10, 0]) exercise_weight = GridLayout(rows=1, cols=len(self.active_exercises), size_hint=[1, None], height=30, spacing=[10, 0]) exercise_reps = GridLayout(rows=1, cols=len(self.active_exercises), size_hint=[1, None], height=30, spacing=[10, 0]) rep_data = [] weight_data = [] for exercise in self.active_exercises: rep_data.append(exercise.reps) weight_data.append(exercise.weight) new_name = [] for word in exercise.base_exercise.name.split(" "): new_name.append(word.capitalize()) button = KivyButton(grid=exercise_button_grid, md_bg_color=L_GREEN, font_size=15, size_hint=[1 / len(self.active_exercises), None], text="\n".join(new_name), height=30) button.bind(on_release=lambda exercise_button: self.exercise_info(exercise_button.exercise)) button = KivyButton(grid=exercise_weight, md_bg_color=L_GREEN, font_size=15, size_hint=[1 / len(self.active_exercises), None], text=f"{exercise.weight} kg", height=30) button.exercise = exercise button.bind(on_release=lambda weight_button: screen.change_weight_action(weight_button)) rep_text = f"{exercise.reps} reps" if exercise.base_exercise.each_side: rep_text += " each side" KivyLabel(grid=exercise_reps, font_size=20, halign="center", size_hint=[1 / len(self.active_exercises), None], height=30, text=rep_text) grid.add_widget(exercise_button_grid) grid.add_widget(exercise_weight) grid.add_widget(exercise_reps) self.add_widget(grid) active_set_grid = GridLayout(rows=self.sets * 2, size_hint=[1, None], padding=[0, 0]) grid.add_widget(active_set_grid) for i in range(self.sets): KivyLabel(grid=active_set_grid, halign="center", size_hint=[1, None], height=10) row = SuperSetRow(active_set_grid, rep_data, self.rest_time, self, i) self.all_rows.append(row)