From 669dff82d457067adc0928b8353ece9f36ed6f48 Mon Sep 17 00:00:00 2001 From: doman Date: Sat, 29 Jul 2023 20:55:32 +0200 Subject: [PATCH] pawlacz --- lib/client.dart | 13 ++++++++++--- lib/models/diary.dart | 23 ++++++++++++++++++++++- lib/models/entry.dart | 2 +- lib/models/product.dart | 2 +- lib/screens/login.dart | 5 ++--- lib/screens/main.dart | 27 +++++++++++++++++++++------ lib/widgets/diary.dart | 1 + 7 files changed, 58 insertions(+), 15 deletions(-) diff --git a/lib/client.dart b/lib/client.dart index 3fd8c2e..5064377 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -20,16 +20,19 @@ class ApiClient { } } - Map headers() { + Map headers({bool forGet = false}) { if (token == null) { throw Exception('Not logged in'); } final headers = { - 'Content-Type': 'application/json', 'Accept': 'application/json', }; + if (!forGet) { + headers['Content-Type'] = 'application/json'; + } + if (token != null) { headers['Authorization'] = 'Bearer $token'; } @@ -40,7 +43,7 @@ class ApiClient { Future> get(String path) async { final response = await httpClient.get( Uri.parse('$baseUrl$path'), - headers: headers(), + headers: headers(forGet: true), ); if (response.statusCode != 200) { @@ -137,4 +140,8 @@ class ApiClient { refreshToken = response['refresh_token'] as String; window.localStorage['refreshToken'] = refreshToken!; } + + Future> getDiary() async { + return await get("/diary?date=2023-07-29"); + } } diff --git a/lib/models/diary.dart b/lib/models/diary.dart index f9b3ed7..6de827b 100644 --- a/lib/models/diary.dart +++ b/lib/models/diary.dart @@ -2,9 +2,30 @@ import 'package:fooder_web/models/meal.dart'; class Diary { + final int id; + final DateTime date; final List meals; + final double calories; + final double protein; + final double carb; + final double fat; - const Diary({ + Diary({ + required this.id, + required this.date, required this.meals, + required this.calories, + required this.protein, + required this.carb, + required this.fat, }); + + Diary.fromJson(Map map): + id = map['id'] as int, + date = DateTime.parse(map['date']), + meals = [], + calories = map['calories'] as double, + protein = map['protein'] as double, + carb = map['carb'] as double, + fat = map['fat'] as double; } diff --git a/lib/models/entry.dart b/lib/models/entry.dart index aa335a3..31d9cfc 100644 --- a/lib/models/entry.dart +++ b/lib/models/entry.dart @@ -11,7 +11,7 @@ class Entry { final double carb; - const Entry({ + Entry({ required this.id, required this.grams, required this.product, diff --git a/lib/models/product.dart b/lib/models/product.dart index 38370bc..d05366f 100644 --- a/lib/models/product.dart +++ b/lib/models/product.dart @@ -6,7 +6,7 @@ class Product { final double carb; final double fat; - const Product({ + Product({ required this.id, required this.name, required this.calories, diff --git a/lib/screens/login.dart b/lib/screens/login.dart index d236658..916084d 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -43,7 +43,7 @@ class _LoginScreen extends State { } void popMeDady() { - Navigator.push( + Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) => MainScreen(apiClient: widget.apiClient), @@ -64,6 +64,7 @@ class _LoginScreen extends State { showError(e.toString()); } } + @override void initState () { super.initState(); @@ -71,8 +72,6 @@ class _LoginScreen extends State { } Future _asyncInitState() async { - super.initState(); - try { await widget.apiClient.refresh(); showText("Welcome back!"); diff --git a/lib/screens/main.dart b/lib/screens/main.dart index a9ae60c..e044e35 100644 --- a/lib/screens/main.dart +++ b/lib/screens/main.dart @@ -16,16 +16,31 @@ class MainScreen extends BasedScreen { class _MainScreen extends State { Diary? diary; + @override + void initState () { + super.initState(); + _asyncInitState().then((value) => null); + } + + Future _asyncInitState() async { + var diaryMap = await widget.apiClient.getDiary(); + setState(() { + diary = Diary.fromJson(diaryMap); + }); + } + @override Widget build(BuildContext context) { var content; + var title = "FOODER"; if (diary != null) { - content = Container( - constraints: const BoxConstraints(maxWidth: 600), - padding: const EdgeInsets.all(10), - child: DiaryWidget(diary: diary!), - ); + content = Container( + constraints: const BoxConstraints(maxWidth: 600), + padding: const EdgeInsets.all(10), + child: DiaryWidget(diary: diary!), + ); + title = "FOODER - ${diary!.date.year}-${diary!.date.month}-${diary!.date.day}"; } else { content = const CircularProgressIndicator(); } @@ -33,7 +48,7 @@ class _MainScreen extends State { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: Text("FOODER"), + title: Text(title), ), body: Center( child: content, diff --git a/lib/widgets/diary.dart b/lib/widgets/diary.dart index 66d41fb..9447abd 100644 --- a/lib/widgets/diary.dart +++ b/lib/widgets/diary.dart @@ -20,6 +20,7 @@ class DiaryWidget extends StatelessWidget { MealWidget( meal: meal, ), + Text(diary.date.toString()), ], ), );