mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 01:41:05 +01:00
Add exercise providers
This commit is contained in:
25
lib/providers/exercise.dart
Normal file
25
lib/providers/exercise.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class Exercise with ChangeNotifier {
|
||||
final int id;
|
||||
final String uuid;
|
||||
final DateTime creation_date;
|
||||
final String name;
|
||||
final String description;
|
||||
final String category;
|
||||
final List<String> muscles;
|
||||
final List<String> muscles_secondary;
|
||||
final List<String> equipment;
|
||||
|
||||
Exercise({
|
||||
this.id,
|
||||
this.uuid,
|
||||
this.creation_date,
|
||||
this.name,
|
||||
this.description,
|
||||
this.category,
|
||||
this.muscles,
|
||||
this.muscles_secondary,
|
||||
this.equipment,
|
||||
});
|
||||
}
|
||||
63
lib/providers/exercises.dart
Normal file
63
lib/providers/exercises.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:wger/providers/auth.dart';
|
||||
|
||||
import 'exercise.dart';
|
||||
|
||||
class Exercises with ChangeNotifier {
|
||||
static const exercisesUrl = '/api/v2/exercise/?language=1&limit=1000';
|
||||
|
||||
String _url;
|
||||
List<Exercise> _entries = [];
|
||||
Auth _auth;
|
||||
|
||||
Exercises(Auth auth, List<Exercise> entries) {
|
||||
this._auth = auth;
|
||||
this._entries = entries;
|
||||
this._url = auth.serverUrl + exercisesUrl;
|
||||
}
|
||||
|
||||
List<Exercise> get items {
|
||||
return [..._entries];
|
||||
}
|
||||
|
||||
Exercise findById(int id) {
|
||||
return _entries.firstWhere((workoutPlan) => workoutPlan.id == id);
|
||||
}
|
||||
|
||||
Future<void> fetchAndSetExercises() async {
|
||||
final response = await http.get(
|
||||
_url,
|
||||
headers: <String, String>{'Authorization': 'Token ${_auth.token}'},
|
||||
);
|
||||
final extractedData = json.decode(response.body) as Map<String, dynamic>;
|
||||
|
||||
final List<Exercise> loadedExercises = [];
|
||||
if (loadedExercises == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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'],
|
||||
creation_date: DateTime.parse(entry['creation_date']),
|
||||
muscles: entry['muscles'],
|
||||
muscles_secondary: entry['muscles_secondary'],
|
||||
equipment: entry['equipment'],
|
||||
));
|
||||
}
|
||||
|
||||
_entries = loadedExercises;
|
||||
notifyListeners();
|
||||
} catch (error) {
|
||||
throw (error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user