diff --git a/lib/main.dart b/lib/main.dart index 638879c6e..e7741b187 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -30,7 +30,7 @@ class MyApp extends StatelessWidget { ChangeNotifierProxyProvider( create: null, // TODO: Create is required but it can be null?? update: (context, value, previous) => WorkoutPlans( - auth.token, + auth, previous == null ? [] : previous.items, ), ) diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index 334d04bd4..66cc4c2b2 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -29,6 +29,10 @@ class Auth with ChangeNotifier { // return null; } + String get serverUrl { + return _serverUrl; + } + // String get userId { // return _userId; // } diff --git a/lib/providers/workout_plans.dart b/lib/providers/workout_plans.dart index 182ad44e1..17a7c448e 100644 --- a/lib/providers/workout_plans.dart +++ b/lib/providers/workout_plans.dart @@ -2,14 +2,21 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; +import 'package:wger/providers/auth.dart'; import 'package:wger/providers/workout_plan.dart'; class WorkoutPlans with ChangeNotifier { - static const workoutPlansUrl = 'http://10.0.2.2:8000/api/v2/workout/'; - List _entries = []; - final String authToken; + static const workoutPlansUrl = '/api/v2/workout/'; - WorkoutPlans(this.authToken, this._entries); + String _url; + List _entries = []; + Auth _auth; + + WorkoutPlans(Auth auth, List entries) { + this._auth = auth; + this._entries = entries; + this._url = auth.serverUrl + workoutPlansUrl; + } List get items { return [..._entries]; @@ -21,8 +28,8 @@ class WorkoutPlans with ChangeNotifier { Future fetchAndSetWorkouts() async { final response = await http.get( - workoutPlansUrl, - headers: {'Authorization': 'Token $authToken'}, + _url, + headers: {'Authorization': 'Token ${_auth.token}'}, ); final extractedData = json.decode(response.body) as Map; @@ -49,7 +56,7 @@ class WorkoutPlans with ChangeNotifier { Future addProduct(WorkoutPlan product) async { final productsUrl = - 'https://flutter-shop-a2335.firebaseio.com/products.json?auth=$authToken'; + 'https://flutter-shop-a2335.firebaseio.com/products.json?auth=${_auth.token}'; try { final response = await http.post( productsUrl, @@ -76,7 +83,7 @@ class WorkoutPlans with ChangeNotifier { final prodIndex = _entries.indexWhere((element) => element.id == id); if (prodIndex >= 0) { final url = - 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=$authToken'; + 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}'; await http.patch(url, body: json.encode({ 'description': newProduct.description, @@ -88,7 +95,7 @@ class WorkoutPlans with ChangeNotifier { Future deleteProduct(String id) async { final url = - 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=$authToken'; + 'https://flutter-shop-a2335.firebaseio.com/products/$id.json?auth=${_auth.token}'; final existingProductIndex = _entries.indexWhere((element) => element.id == id); var existingProduct = _entries[existingProductIndex];