aboutsummaryrefslogtreecommitdiffstats
path: root/screens/exercises.py
blob: d9a9cdad194d4ea583958eaa63425ebd7dd4ab6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import sqlite3

import models
import ui


def manage_workout_exercises(conn: sqlite3.Connection) -> None:
    while True:
        ui.clear_screen()
        ui.print_header("Manage Workout Exercises")
        print("1. List Exercises")
        print("2. Add Exercise")
        print("3. Edit Exercise")
        print("4. Delete Exercise")
        print("5. Back")
        choice = input("\n> ").strip()
        if choice == "1":
            _list_workout_exercises(conn)
        elif choice == "2":
            _add_workout_exercise(conn)
        elif choice == "3":
            _edit_workout_exercise(conn)
        elif choice == "4":
            _delete_workout_exercise(conn)
        elif choice == "5":
            break


def _list_workout_exercises(conn: sqlite3.Connection, pause: bool = True) -> None:
    exercises = models.list_workout_exercises(conn)
    ui.print_header("All Workout Exercises")
    ui.print_table(
        ["ID", "Name", "BW?", "Note"],
        [
            [
                str(e["id"]),
                e["name"],
                "Yes" if e["bw_relative"] else "No",
                e["note"] or "",
            ]
            for e in exercises
        ],
    )
    if pause:
        ui.pause()


def _add_workout_exercise(conn: sqlite3.Connection) -> None:
    name = ui.prompt_str("Name: ")
    assert name is not None
    bw_relative = ui.confirm("Is weight relative to body weight?")
    note = ui.prompt_str("Note (optional): ", required=False)
    try:
        models.add_workout_exercise(conn, name, bw_relative, note)
        print(f'Exercise "{name}" added.')
    except sqlite3.IntegrityError:
        print(f'Exercise "{name}" already exists.')
    ui.pause()


def _edit_workout_exercise(conn: sqlite3.Connection) -> None:
    _list_workout_exercises(conn, pause=False)
    eid = ui.prompt_int("\nExercise ID to edit: ")
    assert eid is not None
    ex = models.get_workout_exercise(conn, eid)
    if not ex:
        print("Exercise not found.")
        return
    print(f'Editing "{ex["name"]}" (leave blank to keep current value)')
    name = ui.prompt_str(f"Name [{ex['name']}]: ", required=False)
    bw_cur = "Yes" if ex["bw_relative"] else "No"
    print(f"BW-relative [{bw_cur}]: ", end="")
    bw_input = input().strip().lower()
    if bw_input in ("y", "yes"):
        bw_relative: bool | None = True
    elif bw_input in ("n", "no"):
        bw_relative = False
    else:
        bw_relative = None  # keep current
    note = ui.prompt_str(f"Note [{ex['note'] or ''}]: ", required=False)
    try:
        models.update_workout_exercise(conn, eid, name, bw_relative, note)
        print("Exercise updated.")
    except sqlite3.IntegrityError:
        print(f'An exercise named "{name}" already exists.')
    ui.pause()


def _delete_workout_exercise(conn: sqlite3.Connection) -> None:
    _list_workout_exercises(conn, pause=False)
    eid = ui.prompt_int("\nExercise ID to delete: ")
    assert eid is not None
    ex = models.get_workout_exercise(conn, eid)
    if not ex:
        print("Exercise not found.")
        return
    if not ui.confirm(f'Delete "{ex["name"]}"?'):
        return
    try:
        models.delete_workout_exercise(conn, eid)
        print("Exercise deleted.")
    except sqlite3.IntegrityError:
        print(
            f'Cannot delete "{ex["name"]}" — it is used in existing sessions or templates.'
        )
    ui.pause()