This commit is contained in:
doman 2023-07-29 20:55:32 +02:00
parent cccdc04dee
commit 669dff82d4
7 changed files with 58 additions and 15 deletions

View file

@ -20,16 +20,19 @@ class ApiClient {
} }
} }
Map<String, String> headers() { Map<String, String> headers({bool forGet = false}) {
if (token == null) { if (token == null) {
throw Exception('Not logged in'); throw Exception('Not logged in');
} }
final headers = { final headers = {
'Content-Type': 'application/json',
'Accept': 'application/json', 'Accept': 'application/json',
}; };
if (!forGet) {
headers['Content-Type'] = 'application/json';
}
if (token != null) { if (token != null) {
headers['Authorization'] = 'Bearer $token'; headers['Authorization'] = 'Bearer $token';
} }
@ -40,7 +43,7 @@ class ApiClient {
Future<Map<String, dynamic>> get(String path) async { Future<Map<String, dynamic>> get(String path) async {
final response = await httpClient.get( final response = await httpClient.get(
Uri.parse('$baseUrl$path'), Uri.parse('$baseUrl$path'),
headers: headers(), headers: headers(forGet: true),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
@ -137,4 +140,8 @@ class ApiClient {
refreshToken = response['refresh_token'] as String; refreshToken = response['refresh_token'] as String;
window.localStorage['refreshToken'] = refreshToken!; window.localStorage['refreshToken'] = refreshToken!;
} }
Future<Map<String, dynamic>> getDiary() async {
return await get("/diary?date=2023-07-29");
}
} }

View file

@ -2,9 +2,30 @@ import 'package:fooder_web/models/meal.dart';
class Diary { class Diary {
final int id;
final DateTime date;
final List<Meal> meals; final List<Meal> 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.meals,
required this.calories,
required this.protein,
required this.carb,
required this.fat,
}); });
Diary.fromJson(Map<String, dynamic> map):
id = map['id'] as int,
date = DateTime.parse(map['date']),
meals = <Meal>[],
calories = map['calories'] as double,
protein = map['protein'] as double,
carb = map['carb'] as double,
fat = map['fat'] as double;
} }

View file

@ -11,7 +11,7 @@ class Entry {
final double carb; final double carb;
const Entry({ Entry({
required this.id, required this.id,
required this.grams, required this.grams,
required this.product, required this.product,

View file

@ -6,7 +6,7 @@ class Product {
final double carb; final double carb;
final double fat; final double fat;
const Product({ Product({
required this.id, required this.id,
required this.name, required this.name,
required this.calories, required this.calories,

View file

@ -43,7 +43,7 @@ class _LoginScreen extends State<LoginScreen> {
} }
void popMeDady() { void popMeDady() {
Navigator.push( Navigator.pushReplacement(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => MainScreen(apiClient: widget.apiClient), builder: (context) => MainScreen(apiClient: widget.apiClient),
@ -64,6 +64,7 @@ class _LoginScreen extends State<LoginScreen> {
showError(e.toString()); showError(e.toString());
} }
} }
@override @override
void initState () { void initState () {
super.initState(); super.initState();
@ -71,8 +72,6 @@ class _LoginScreen extends State<LoginScreen> {
} }
Future<void> _asyncInitState() async { Future<void> _asyncInitState() async {
super.initState();
try { try {
await widget.apiClient.refresh(); await widget.apiClient.refresh();
showText("Welcome back!"); showText("Welcome back!");

View file

@ -16,9 +16,23 @@ class MainScreen extends BasedScreen {
class _MainScreen extends State<MainScreen> { class _MainScreen extends State<MainScreen> {
Diary? diary; Diary? diary;
@override
void initState () {
super.initState();
_asyncInitState().then((value) => null);
}
Future<void> _asyncInitState() async {
var diaryMap = await widget.apiClient.getDiary();
setState(() {
diary = Diary.fromJson(diaryMap);
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var content; var content;
var title = "FOODER";
if (diary != null) { if (diary != null) {
content = Container( content = Container(
@ -26,6 +40,7 @@ class _MainScreen extends State<MainScreen> {
padding: const EdgeInsets.all(10), padding: const EdgeInsets.all(10),
child: DiaryWidget(diary: diary!), child: DiaryWidget(diary: diary!),
); );
title = "FOODER - ${diary!.date.year}-${diary!.date.month}-${diary!.date.day}";
} else { } else {
content = const CircularProgressIndicator(); content = const CircularProgressIndicator();
} }
@ -33,7 +48,7 @@ class _MainScreen extends State<MainScreen> {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text("FOODER"), title: Text(title),
), ),
body: Center( body: Center(
child: content, child: content,

View file

@ -20,6 +20,7 @@ class DiaryWidget extends StatelessWidget {
MealWidget( MealWidget(
meal: meal, meal: meal,
), ),
Text(diary.date.toString()),
], ],
), ),
); );