If the app is still loading user data, show a loading indicator
*/
if (loading) {
return (
);
}
/**
If no user is logged in, prompt the user to log in
/
if (!user) {
return (
<Text style={{ color: theme.colors.text }}>No user data available. Please log in.
{/ Assume there's a Login button or navigation here */}
);
}
/**
Render the user's profile information along with the workout heatmap
/
return (
<ScrollView
contentContainerStyle={[styles.container, { backgroundColor: theme.colors.background }]}
>
{/ User Information Section with Hamburger Menu /}
{user.name ? (
<Title style={[styles.title, { color: theme.colors.text }]}>{user.name}
) : (
<Title style={[styles.title, { color: theme.colors.text }]}>User ID: {user.id}
)}
{user.email && (
<Caption style={[styles.caption, { color: theme.colors.text }]}>{user.email}
)}
{/ Hamburger Menu Button */}
<TouchableOpacity
onPress={() => navigation.navigate('Settings')}
accessibilityLabel="Open Settings"
accessibilityRole="button"
const styles = StyleSheet.create({
container: {
flexGrow: 1,
padding: 20,
},
userInfoSection: {
paddingHorizontal: 20,
paddingVertical: 20, // Adjusted padding for alignment
flexDirection: 'row', // Align items horizontally
justifyContent: 'space-between', // Space between user info and button
alignItems: 'center', // Vertically center the items
backgroundColor: 'transparent',
},
title: {
fontSize: 26,
fontWeight: 'bold',
},
caption: {
fontSize: 16,
lineHeight: 16,
marginTop: 5,
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default ProfileScreen;
// frontend/Components/WorkoutHeatmap.js
Assistant
import React, { useState, useEffect, useContext } from 'react';
import {
View,
StyleSheet,
Dimensions,
TouchableOpacity,
Text,
ActivityIndicator,
Modal,
ScrollView,
Alert,
} from 'react-native';
import Svg, { Rect, Text as SvgText, G, Circle } from 'react-native-svg';
import moment from 'moment';
import { useTheme } from 'react-native-paper';
import { Picker } from '@react-native-picker/picker';
import { AuthContext } from '../context/AuthContext';
import { api } from '../services/api'; // Import the centralized Axios instance
const { width } = Dimensions.get('window');
const CELL_SIZE = 30;
const CELL_MARGIN = 4;
const NUM_COLUMNS = 7; // Days of the week
// Handler for month navigation
const changeMonth = (direction) => {
if (direction === 'prev') {
setCurrentMonth(currentMonth.clone().subtract(1, 'month'));
} else if (direction === 'next') {
setCurrentMonth(currentMonth.clone().add(1, 'month'));
}
};
// Function to get color based on category and data
const getColorForDate = (date, category) => {
const dateStr = date.format('YYYY-MM-DD');
const data = workoutData[category]?.[dateStr];
code
if (!data) {
return theme.colors.disabled; // No activity
}
switch (category) {
case 'Cardio':
if (data.duration <= 20) return '#AEDFF7';
if (data.duration <= 40) return '#5CB1F6';
return '#2A6EBB';
case 'StrengthTraining':
if (data.volume <= 100) return '#C2FFD8';
if (data.volume <= 200) return '#6EE7B7';
return '#118D6E';
case 'Flexibility':
if (data.duration <= 20) return '#D7B0E0';
if (data.duration <= 40) return '#A569BD';
return '#6A1B9A';
default:
return theme.colors.disabled;
}
};
// Function to check if a date has a PR
const isPR = (dateStr) => {
for (let category of selectedCategories) {
if (workoutData[category]?.[dateStr]?.PR) {
return true;
}
}
return false;
};
// Function to filter data based on selected muscle group
const filterDataByMuscleGroup = (category, dateStr) => {
if (selectedMuscleGroup === 'All') return true;
if (category !== 'StrengthTraining') return true; // Only StrengthTraining has muscle groups
return workoutData[category][dateStr]?.muscleGroup === selectedMuscleGroup;
};
// Function to accumulate data for a specific date across selected categories
const getAggregatedData = (date) => {
const dateStr = date.format('YYYY-MM-DD');
const aggregated = [];
// Helper function to get category color for tooltip indicators
const getCategoryColor = (category) => {
switch (category) {
case 'Cardio':
return '#5CB1F6';
case 'StrengthTraining':
return '#6EE7B7';
case 'Flexibility':
return '#A569BD';
default:
return theme.colors.primary;
}
};
// Helper function to format metrics text based on category
const getMetricsText = (entry) => {
switch (entry.category) {
case 'Cardio':
return Duration: ${entry.data.duration}m, Calories: ${entry.data.calories};
case 'StrengthTraining':
return Volume: ${entry.data.volume}, Reps: ${entry.data.reps}, Sets: ${entry.data.sets}, Weight: ${entry.data.weight}kg, Calories: ${entry.data.calories};
case 'Flexibility':
return Duration: ${entry.data.duration}m, Calories: ${entry.data.calories};
default:
return '';
}
};
To ensure that the Workout Heatmap Calendar is always displayed on the Profile Screen, even when no workouts are detected, you'll need to make a few adjustments both in the ProfileScreen.js and WorkoutHeatmap.js components. Here's a comprehensive guide to achieving this:
Ensure WorkoutHeatmap is Always Rendered:
Your current ProfileScreen.js already includes the <WorkoutHeatmap /> component unconditionally, which is correct. However, it's essential to verify that there are no additional conditions elsewhere that might hide this component based on workout data.
Current Structure:
jsx
return (
<ScrollView
contentContainerStyle={[styles.container, { backgroundColor: theme.colors.background }]}
>
{/* User Information Section */}
<View style={styles.userInfoSection}>
{/* User Info */}
</View>
{/* Workout Heatmap Section */}
<WorkoutHeatmap />
</ScrollView>
);
Handle Empty Workout Data Gracefully in WorkoutHeatmap:
Within the WorkoutHeatmap component, you need to ensure that the heatmap renders even when there's no workout data. Currently, the heatmap is displayed with cells colored based on whether there's data, defaulting to theme.colors.disabled when no data is present. This behavior is correct, but you can enhance user experience by adding a message indicating that no workouts were detected for the selected month.
Steps to Modify WorkoutHeatmap.js:
a. Determine If There’s Any Workout Data:
Add a helper function or variable to check if all workout categories are empty for the current month.
b. Display a Message When No Workouts Are Present:
Conditionally render a message informing the user that no workouts were detected, while still displaying the heatmap.
c. Ensure Cells Are Rendered Appropriately:
The existing logic already handles coloring cells based on data presence. Ensure that this logic remains intact.
Modified WorkoutHeatmap.js:
jsx
Key Changes Explained:
a. hasWorkouts Function:
This function checks if there's any workout data present across all selected categories. It returns true if at least one workout entry exists; otherwise, it returns false.
b. Conditional Rendering of "No Workouts" Message:
After the loading state is resolved (!loading), the component checks if there are any workouts using the hasWorkouts function. If no workouts are detected, it renders a Text component informing the user.
jsx
{!hasWorkouts() && (
<Text style={styles.noDataText}>No workouts detected for this month.</Text>
)}
c. Ensuring the Heatmap is Always Rendered:
The heatmap (<Svg>...</Svg>) is always rendered inside the <ScrollView>, regardless of whether workout data exists. When there's no data, all cells default to theme.colors.disabled, providing a consistent user interface.
Enhancements and Accessibility:
a. Accessibility Labels:
Ensure that all interactive elements have appropriate accessibilityLabel and accessibilityRole props. This improves usability for users relying on screen readers.
b. User Feedback:
Providing feedback, such as a "No workouts detected" message, helps users understand the state of their data and encourages further interaction.
Testing the Implementation:
After making the above changes, thoroughly test the following scenarios to ensure the heatmap behaves as expected:
a. User with Multiple Workouts:
Verify that the heatmap accurately reflects workout data with appropriately colored cells.
b. User with No Workouts:
Ensure that the heatmap is still displayed with all cells in the disabled color and the "No workouts detected" message is visible.
c. Category and Muscle Group Filters:
Test toggling categories and selecting different muscle groups to confirm that the heatmap updates correctly.
d. Accessibility:
Use accessibility tools to ensure that screen readers can interpret the components correctly.
Final Thoughts:
By implementing these changes, the Workout Heatmap Calendar will consistently appear on the Profile Screen, providing users with a continuous visual representation of their workout activity—or lack thereof. This approach enhances the user experience by maintaining interface consistency and offering clear feedback regarding workout data.