Skip to content

Commit

Permalink
use cm and lbs for precision
Browse files Browse the repository at this point in the history
  • Loading branch information
youngbryanyu committed Mar 6, 2024
1 parent 39c97fa commit 67a3b60
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 28 deletions.
92 changes: 65 additions & 27 deletions frontend/lib/features/onboarding/pages/enter_biometrics_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@ class EnterBiometricsPage extends ConsumerStatefulWidget {
}

class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
// TODO: set all values initially to "select, then enforce that they must be selected"
String selectedSex = 'Male';
DateTime? selectedBirthday;
String selectedUnit = 'Metric';
int selectedHeight = 0; // Default in cm for metric
int selectedWeight = 0; // Default in kg for metric
String selectedActivityLevel = 'Sedentary';
String selectedWeightGoal = 'Maintain';

// Define lists for the picker data
final List<String> sexOptions = ['Male', 'Female', 'Other'];
final List<String> unitOptions = ['Metric', 'Imperial'];
// Define lists for the picker data.
// TODO: move these somewhere to integrate with User schema (check backend too)
final List<String> sexOptions = ['Male', 'Female'];
final List<String> unitOptions = ['Imperial', 'Metric'];
final List<String> activityLevelOptions = [
'Sedentary',
'Lightly Active',
Expand All @@ -38,6 +30,15 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
];
final List<String> weightGoalOptions = ['Lose', 'Gain', 'Maintain'];

// TODO: set all values initially to "select, then enforce that they must be selected"
String selectedSex = 'Select';
DateTime? selectedBirthday;
String selectedUnit = 'Imperial';
int selectedHeight = -1; // Default in cm for more precision over in
int selectedWeight = -1; // Default in lbs for more precision over kg
String selectedActivityLevel = 'Select';
String selectedWeightGoal = 'Select';

void signOutUser(WidgetRef ref) async {
FirebaseAuth.instance.signOut();
ref.read(authPageStateProvider.notifier).state = AuthPageState.login;
Expand Down Expand Up @@ -102,16 +103,25 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
),
buildCustomListTile(
'Height',
formatHeight(selectedHeight,
selectedUnit), // Use the helper function for formatting
selectedHeight == -1
? 'Select'
: formatHeight(
selectedHeight,
selectedUnit,
), // Use the helper function for formatting
() {
showHeightPicker(context);
},
context,
),
buildCustomListTile(
'Weight',
'$selectedWeight ${selectedUnit == "Metric" ? "kg" : "lbs"}',
selectedWeight == -1
? 'Select'
: formatWeight(
selectedWeight,
selectedUnit,
),
() {
showWeightPicker(context);
},
Expand Down Expand Up @@ -174,6 +184,16 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
}
}

String formatWeight(int weightLbs, String unit) {
if (unit == "Imperial") {
return "$weightLbs lbs";
} else {
final int weightKg = (weightLbs / 2.2).round();

return "$weightKg kg";
}
}

// TODO: move this into another file
void showPicker(BuildContext context, String title, List<String> options,
ValueChanged<String> onSelectedItemChanged, String initialValue) {
Expand Down Expand Up @@ -287,15 +307,18 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
List<int> inchesOptions =
List.generate(12, (index) => index); // 0-11 inches

int selectedIndex = selectedHeight;
// Convert selectedHeight to feet and inches if Imperial
int feet = 0;
int inches = 0;
if (selectedUnit == 'Imperial') {
double totalInches = selectedHeight / 2.54; // Convert cm to inches
feet = totalInches ~/ 12;
inches = (totalInches % 12).round();
inches = (totalInches.round() % 12).round();
}

// TODO create helper for conversion

showModalBottomSheet(
context: context,
builder: (BuildContext context) {
Expand Down Expand Up @@ -324,11 +347,14 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
onPressed: () {
Navigator.of(context).pop();
// Convert feet and inches back to cm and set selectedHeight
if (selectedUnit == 'Imperial') {
selectedHeight =
(((feet * 12) + inches) * 2.54).round();
}
setState(() {});
setState(() {
if (selectedUnit == 'Imperial') {
selectedHeight = (((feet * 12) + inches) * 2.54)
.round(); // convert to CM
} else {
selectedHeight = selectedIndex; // use CM
}
});
},
),
],
Expand Down Expand Up @@ -384,12 +410,15 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
backgroundColor:
Theme.of(context).colorScheme.background,
itemExtent: 32.0,
scrollController: FixedExtentScrollController(
initialItem: selectedIndex,
),
onSelectedItemChanged: (int index) {
// Assuming you have a method to convert cm to the selectedHeight
selectedHeight = index + 1; // cm starts at 1
selectedIndex =
index; // Temporarily store new index
},
children: List.generate(
500, (index) => index + 1) // Up to 500 cm
501, (index) => index) // Up to 500 cm
.map((cm) => Center(
child: Text('$cm cm',
style: Theme.of(context)
Expand All @@ -412,8 +441,10 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
void showWeightPicker(BuildContext context) {
int maxValue =
selectedUnit == 'Metric' ? 500 : 1100; // Max weight in kg or lbs
List<int> weightOptions = List.generate(maxValue, (index) => index + 1);
int selectedIndex = selectedWeight - 1; // Adjust based on zero indexing
List<int> weightOptions = List.generate(maxValue, (index) => index);
int selectedIndex = selectedUnit == 'Imperial'
? selectedWeight
: (selectedWeight / 2.2).round(); // convert lbs to kg for metric

showModalBottomSheet(
context: context,
Expand Down Expand Up @@ -443,7 +474,12 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
onPressed: () {
Navigator.of(context).pop();
setState(() {
selectedWeight = weightOptions[selectedIndex];
if (selectedUnit == 'Metric') {
selectedWeight = (selectedIndex * 2.2)
.round(); // convert kg to lbs
} else {
selectedWeight = selectedIndex; // already in lbs
}
});
},
),
Expand Down Expand Up @@ -479,6 +515,8 @@ class _EnterBiometricsPageState extends ConsumerState<EnterBiometricsPage> {
}
}

// TODO: find make sure max values for weight and height conform to backend constraints

// TODO: move to own component class
Widget buildCustomListTile(
String label,
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/features/onboarding/pages/welcome_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WelcomePage extends ConsumerWidget {

/* Next page button */
SmallRoundButton(
title: "Next",
title: "Continue",
onPressed: () {
ref.read(onboardingPageStateProvider.notifier).state =
OnboardingPageState.enterBiometrics;
Expand Down

0 comments on commit 67a3b60

Please sign in to comment.