Skip to content

Commit

Permalink
fix: add progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amaralkaff committed Nov 6, 2024
1 parent ed9dcec commit 7ca0c6e
Show file tree
Hide file tree
Showing 5 changed files with 649 additions and 252 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ app.*.map.json
# VSCode
.vscode/



# Xcode
/ios/

Expand Down
51 changes: 51 additions & 0 deletions lib/models/progress_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class ProgressData {
final double currentWeight;
final double totalCalories;
final int month;

ProgressData({
required this.currentWeight,
required this.totalCalories,
required this.month,
});

factory ProgressData.fromJson(Map<String, dynamic> json) {
return ProgressData(
currentWeight: _parseDouble(json['currentWeight']),
totalCalories: _parseDouble(json['totalCalories']),
month: json['month'] as int,
);
}

static double _parseDouble(dynamic value) {
if (value == null) return 0.0;
if (value is double) return value;
if (value is int) return value.toDouble();
if (value is String) return double.tryParse(value) ?? 0.0;
return 0.0;
}
}

class ProgressResponse {
final int statusCode;
final String averageCurrentWeight;
final List<ProgressData> data;

ProgressResponse({
required this.statusCode,
required this.averageCurrentWeight,
required this.data,
});

factory ProgressResponse.fromJson(Map<String, dynamic> json) {
return ProgressResponse(
statusCode: json['statusCode'] as int,
averageCurrentWeight: json['averageCurrentWeight'] as String,
data: (json['data'] as List)
.map((item) => ProgressData.fromJson(item as Map<String, dynamic>))
.toList(),
);
}

double get averageWeight => double.tryParse(averageCurrentWeight) ?? 0.0;
}
25 changes: 25 additions & 0 deletions lib/services/progress_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:developer';
import 'package:workout_ai/services/api_service.dart';
import 'package:workout_ai/models/progress_data.dart';

class ProgressService {
final APIService _api = APIService();

Future<ProgressResponse> getProgress() async {
try {
final response = await _api.get('progress');
log('Progress response: $response');

if (response['statusCode'] != 200) {
throw Exception(response['error'] ??
response['message'] ??
'Failed to fetch progress data');
}

return ProgressResponse.fromJson(response);
} catch (e) {
log('Error in getProgress: $e');
rethrow;
}
}
}
89 changes: 87 additions & 2 deletions lib/services/workout_info_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class WorkoutInfoService {
log('Workout info response: $response');

if (response['statusCode'] != 200) {
throw Exception(response['error'] ?? response['message'] ?? 'Failed to fetch workout info');
throw Exception(response['error'] ??
response['message'] ??
'Failed to fetch workout info');
}

final List<dynamic> workouts = response['data'] as List<dynamic>? ?? [];
Expand All @@ -31,13 +33,15 @@ class WorkoutInfo {
final double totalCalories;
final String id;
final String userId;
final int month;

const WorkoutInfo({
required this.woName,
required this.sumWo,
required this.totalCalories,
required this.id,
required this.userId,
required this.month,
});

factory WorkoutInfo.fromJson(Map<String, dynamic> json) {
Expand All @@ -47,6 +51,7 @@ class WorkoutInfo {
totalCalories: _parseDouble(json['totalCalories']),
id: json['_id']?.toString() ?? '',
userId: json['userId']?.toString() ?? '',
month: _parseInteger(json['month']),
);
}

Expand All @@ -65,4 +70,84 @@ class WorkoutInfo {
if (value is String) return double.tryParse(value) ?? 0.0;
return 0.0;
}
}
}

// lib/models/progress_data.dart
class ProgressData {
final double currentWeight;
final double totalCalories;
final int month;

ProgressData({
required this.currentWeight,
required this.totalCalories,
required this.month,
});

factory ProgressData.fromJson(Map<String, dynamic> json) {
return ProgressData(
currentWeight: _parseDouble(json['currentWeight']),
totalCalories: _parseDouble(json['totalCalories']),
month: json['month'] as int,
);
}

static double _parseDouble(dynamic value) {
if (value == null) return 0.0;
if (value is double) return value;
if (value is int) return value.toDouble();
if (value is String) return double.tryParse(value) ?? 0.0;
return 0.0;
}
}

class ProgressResponse {
final int statusCode;
final String averageCurrentWeight;
final List<ProgressData> data;

ProgressResponse({
required this.statusCode,
required this.averageCurrentWeight,
required this.data,
});

factory ProgressResponse.fromJson(Map<String, dynamic> json) {
return ProgressResponse(
statusCode: json['statusCode'] as int,
averageCurrentWeight: json['averageCurrentWeight'] as String,
data: (json['data'] as List)
.map((item) => ProgressData.fromJson(item as Map<String, dynamic>))
.toList(),
);
}

double get averageWeight => double.tryParse(averageCurrentWeight) ?? 0.0;

// Helper methods for data processing
List<ProgressData> get sortedByMonth {
final sorted = List<ProgressData>.from(data);
sorted.sort((a, b) => a.month.compareTo(b.month));
return sorted;
}

double get totalCaloriesBurned {
return data.fold(0.0, (sum, item) => sum + item.totalCalories);
}

double get weightChange {
if (data.isEmpty) return 0;
final sorted = sortedByMonth;
return sorted.last.currentWeight - sorted.first.currentWeight;
}

double get weightChangePercentage {
if (data.isEmpty) return 0;
final sorted = sortedByMonth;
final firstWeight = sorted.first.currentWeight;
final change = weightChange;
return (change / firstWeight * 100).abs();
}

bool get isWeightGain => weightChange > 0;
}
Loading

0 comments on commit 7ca0c6e

Please sign in to comment.