mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 04:57:37 +01:00
Start saving all exercises, muscles, etc to a cache
This commit is contained in:
@@ -5,6 +5,7 @@ import 'package:wger/locale/locales.dart';
|
||||
import 'package:wger/providers/exercises.dart';
|
||||
import 'package:wger/providers/workout_plans.dart';
|
||||
import 'package:wger/screens/auth_screen.dart';
|
||||
import 'package:wger/screens/dashboard.dart';
|
||||
import 'package:wger/screens/home_tabs_screen.dart';
|
||||
import 'package:wger/screens/nutrition_screen.dart';
|
||||
import 'package:wger/screens/splash_screen.dart';
|
||||
@@ -47,10 +48,10 @@ class MyApp extends StatelessWidget {
|
||||
)
|
||||
],
|
||||
child: MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
title: 'wger',
|
||||
theme: wgerTheme,
|
||||
home: auth.isAuth
|
||||
? HomeTabsScreen()
|
||||
? DashboardScreen()
|
||||
: FutureBuilder(
|
||||
future: auth.tryAutoLogin(),
|
||||
builder: (ctx, authResultSnapshot) =>
|
||||
@@ -59,6 +60,7 @@ class MyApp extends StatelessWidget {
|
||||
: AuthScreen(),
|
||||
),
|
||||
routes: {
|
||||
DashboardScreen.routeName: (ctx) => DashboardScreen(),
|
||||
HomeTabsScreen.routeName: (ctx) => HomeTabsScreen(),
|
||||
WeightScreen.routeName: (ctx) => WeightScreen(),
|
||||
WorkoutPlansScreen.routeName: (ctx) => WorkoutPlansScreen(),
|
||||
|
||||
@@ -2,20 +2,39 @@ import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:wger/models/exercises/category.dart';
|
||||
import 'package:wger/models/exercises/equipment.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/exercises/muscle.dart';
|
||||
import 'package:wger/providers/auth.dart';
|
||||
|
||||
class Exercises with ChangeNotifier {
|
||||
static const exercisesUrl = '/api/v2/exercise/?language=1&limit=1000';
|
||||
static const exerciseCommentUrl = '/api/v2/exercisecomment/';
|
||||
static const categoriesUrl = '/api/v2/exercisecategory/';
|
||||
static const musclesUrl = '/api/v2/muscle/';
|
||||
static const equipmentUrl = '/api/v2/equipment/';
|
||||
|
||||
String _url;
|
||||
String _urlExercises;
|
||||
String _urlExercisesComment;
|
||||
String _urlCategories;
|
||||
String _urlMuscles;
|
||||
String _urlEquipment;
|
||||
List<Exercise> _entries = [];
|
||||
List<Category> _categories = [];
|
||||
List<Muscle> _muscles = [];
|
||||
List<Equipment> _equipment = [];
|
||||
Auth _auth;
|
||||
|
||||
Exercises(Auth auth, List<Exercise> entries) {
|
||||
this._auth = auth;
|
||||
this._entries = entries;
|
||||
this._url = auth.serverUrl + exercisesUrl;
|
||||
this._urlExercises = auth.serverUrl + exercisesUrl;
|
||||
this._urlExercisesComment = auth.serverUrl + exerciseCommentUrl;
|
||||
this._urlCategories = auth.serverUrl + categoriesUrl;
|
||||
this._urlMuscles = auth.serverUrl + musclesUrl;
|
||||
this._urlEquipment = auth.serverUrl + equipmentUrl;
|
||||
}
|
||||
|
||||
List<Exercise> get items {
|
||||
@@ -26,9 +45,55 @@ class Exercises with ChangeNotifier {
|
||||
return _entries.firstWhere((exercise) => exercise.id == id);
|
||||
}
|
||||
|
||||
Future<void> fetchAndSetCategories() async {
|
||||
final response = await http.get(_urlCategories);
|
||||
final categories = json.decode(response.body) as Map<String, dynamic>;
|
||||
try {
|
||||
for (final category in categories['results']) {
|
||||
_categories.add(Category.fromJson(category));
|
||||
}
|
||||
} catch (error) {
|
||||
throw (error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> fetchAndSetMuscles() async {
|
||||
final response = await http.get(_urlMuscles);
|
||||
final muscles = json.decode(response.body) as Map<String, dynamic>;
|
||||
try {
|
||||
for (final muscle in muscles['results']) {
|
||||
_muscles.add(Muscle.fromJson(muscle));
|
||||
}
|
||||
} catch (error) {
|
||||
throw (error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> fetchAndSetEquipment() async {
|
||||
final response = await http.get(_urlEquipment);
|
||||
final equipments = json.decode(response.body) as Map<String, dynamic>;
|
||||
try {
|
||||
for (final equipment in equipments['results']) {
|
||||
_equipment.add(Equipment.fromJson(equipment));
|
||||
}
|
||||
} catch (error) {
|
||||
throw (error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> fetchAndSetExercises() async {
|
||||
// TODO: Load exercises from cache
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (prefs.containsKey('exerciseData')) {
|
||||
final exerciseData = json.decode(prefs.getString('exerciseData'));
|
||||
}
|
||||
|
||||
await fetchAndSetCategories();
|
||||
await fetchAndSetMuscles();
|
||||
await fetchAndSetEquipment();
|
||||
|
||||
final response = await http.get(
|
||||
_url,
|
||||
_urlExercises,
|
||||
headers: <String, String>{'Authorization': 'Token ${_auth.token}'},
|
||||
);
|
||||
final extractedData = json.decode(response.body) as Map<String, dynamic>;
|
||||
@@ -40,20 +105,28 @@ class Exercises with ChangeNotifier {
|
||||
|
||||
try {
|
||||
for (final entry in extractedData['results']) {
|
||||
loadedExercises.add(Exercise(
|
||||
id: entry['id'],
|
||||
uuid: entry['uudid'],
|
||||
name: entry['name'],
|
||||
description: entry['description'],
|
||||
category: entry['category'],
|
||||
creationDate: DateTime.parse(entry['creation_date']),
|
||||
muscles: entry['muscles'],
|
||||
musclesSecondary: entry['muscles_secondary'],
|
||||
equipment: entry['equipment'],
|
||||
));
|
||||
entry['category'] = _categories.firstWhere((cat) => cat.id == entry['category']).toJson();
|
||||
entry['muscles'] = entry['muscles']
|
||||
.map((e) => _muscles.firstWhere((muscle) => muscle.id == e).toJson())
|
||||
.toList();
|
||||
entry['muscles_secondary'] = entry['muscles_secondary']
|
||||
.map((e) => _muscles.firstWhere((muscle) => muscle.id == e).toJson())
|
||||
.toList();
|
||||
entry['equipment'] = entry['equipment']
|
||||
.map((e) => _equipment.firstWhere((equipment) => equipment.id == e).toJson())
|
||||
.toList();
|
||||
loadedExercises.add(Exercise.fromJson(entry));
|
||||
}
|
||||
|
||||
_entries = loadedExercises;
|
||||
|
||||
final exerciseData = {
|
||||
'date': DateTime.now().toIso8601String(),
|
||||
'expiresIn': DateTime.now().add(Duration(days: 7)).toIso8601String(),
|
||||
'exercises': _entries.map((e) => e.toJson()).toList()
|
||||
};
|
||||
prefs.setString('exerciseData', json.encode(exerciseData));
|
||||
|
||||
notifyListeners();
|
||||
} catch (error) {
|
||||
throw (error);
|
||||
|
||||
@@ -4,7 +4,7 @@ import 'package:wger/screens/weight_screen.dart';
|
||||
import 'package:wger/screens/workout_plans_screen.dart';
|
||||
|
||||
class HomeTabsScreen extends StatefulWidget {
|
||||
static const routeName = '/dashboard';
|
||||
static const routeName = '/dashboard2';
|
||||
|
||||
@override
|
||||
_HomeTabsScreenState createState() => _HomeTabsScreenState();
|
||||
@@ -48,7 +48,7 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> {
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
icon: Icon(Icons.fastfood),
|
||||
icon: Icon(Icons.restaurant),
|
||||
label: 'Nutrition',
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
|
||||
@@ -3,7 +3,6 @@ import 'package:provider/provider.dart';
|
||||
import 'package:wger/locale/locales.dart';
|
||||
import 'package:wger/models/workouts/workout_plan.dart';
|
||||
import 'package:wger/providers/workout_plans.dart';
|
||||
import 'package:wger/widgets/app_drawer.dart';
|
||||
import 'package:wger/widgets/workouts/workout_plan_detail.dart';
|
||||
|
||||
class WorkoutPlanScreen extends StatefulWidget {
|
||||
@@ -38,7 +37,7 @@ class _WorkoutPlanScreenState extends State<WorkoutPlanScreen> {
|
||||
|
||||
return Scaffold(
|
||||
appBar: getAppBar(),
|
||||
drawer: AppDrawer(),
|
||||
//drawer: AppDrawer(),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {},
|
||||
child: const Icon(Icons.play_arrow),
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/locale/locales.dart';
|
||||
import 'package:wger/providers/auth.dart';
|
||||
import 'package:wger/screens/dashboard.dart';
|
||||
import 'package:wger/screens/nutrition_screen.dart';
|
||||
import 'package:wger/screens/weight_screen.dart';
|
||||
import 'package:wger/screens/workout_plans_screen.dart';
|
||||
|
||||
class AppDrawer extends StatelessWidget {
|
||||
@override
|
||||
@@ -13,28 +18,49 @@ class AppDrawer extends StatelessWidget {
|
||||
automaticallyImplyLeading: false,
|
||||
),
|
||||
Divider(),
|
||||
ListTile(
|
||||
leading: Icon(Icons.home),
|
||||
title: Text(AppLocalizations.of(context).labelDashboard),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pushReplacementNamed(DashboardScreen.routeName);
|
||||
},
|
||||
),
|
||||
Divider(),
|
||||
ListTile(
|
||||
leading: Icon(Icons.shop),
|
||||
title: Text('Training'),
|
||||
onTap: () => Navigator.of(context).pushReplacementNamed('/'),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pushReplacementNamed(WorkoutPlansScreen.routeName);
|
||||
},
|
||||
),
|
||||
Divider(),
|
||||
ListTile(
|
||||
leading: Icon(Icons.fastfood),
|
||||
title: Text('Nutrition'),
|
||||
onTap: () => Navigator.of(context).pushReplacementNamed('/'),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pushReplacementNamed(NutritionScreen.routeName);
|
||||
},
|
||||
),
|
||||
Divider(),
|
||||
ListTile(
|
||||
leading: Icon(Icons.bar_chart),
|
||||
title: Text('Weight'),
|
||||
onTap: () => Navigator.of(context).pushReplacementNamed('/'),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pushReplacementNamed(WeightScreen.routeName);
|
||||
},
|
||||
),
|
||||
Divider(),
|
||||
ListTile(
|
||||
leading: Icon(Icons.edit),
|
||||
title: Text('Options'),
|
||||
onTap: () => Navigator.of(context).pushReplacementNamed('/'),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pushReplacementNamed(WeightScreen.routeName);
|
||||
},
|
||||
),
|
||||
Divider(),
|
||||
ListTile(
|
||||
|
||||
Reference in New Issue
Block a user