pawlacz
This commit is contained in:
		
							parent
							
								
									cccdc04dee
								
							
						
					
					
						commit
						669dff82d4
					
				
					 7 changed files with 58 additions and 15 deletions
				
			
		| 
						 | 
				
			
			@ -20,16 +20,19 @@ class ApiClient {
 | 
			
		|||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Map<String, String> headers() {
 | 
			
		||||
  Map<String, String> 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<Map<String, dynamic>> 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<Map<String, dynamic>> getDiary() async {
 | 
			
		||||
    return await get("/diary?date=2023-07-29");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,9 +2,30 @@ import 'package:fooder_web/models/meal.dart';
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
class Diary {
 | 
			
		||||
  final int id;
 | 
			
		||||
  final DateTime date;
 | 
			
		||||
  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.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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,7 +11,7 @@ class Entry {
 | 
			
		|||
  final double carb;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  const Entry({
 | 
			
		||||
  Entry({
 | 
			
		||||
    required this.id,
 | 
			
		||||
    required this.grams,
 | 
			
		||||
    required this.product,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,7 +6,7 @@ class Product {
 | 
			
		|||
  final double carb;
 | 
			
		||||
  final double fat;
 | 
			
		||||
 | 
			
		||||
  const Product({
 | 
			
		||||
  Product({
 | 
			
		||||
    required this.id,
 | 
			
		||||
    required this.name,
 | 
			
		||||
    required this.calories,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,7 +43,7 @@ class _LoginScreen extends State<LoginScreen> {
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  void popMeDady() {
 | 
			
		||||
    Navigator.push(
 | 
			
		||||
    Navigator.pushReplacement(
 | 
			
		||||
      context,
 | 
			
		||||
      MaterialPageRoute(
 | 
			
		||||
        builder: (context) => MainScreen(apiClient: widget.apiClient),
 | 
			
		||||
| 
						 | 
				
			
			@ -64,6 +64,7 @@ class _LoginScreen extends State<LoginScreen> {
 | 
			
		|||
      showError(e.toString());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  void initState () {
 | 
			
		||||
    super.initState();
 | 
			
		||||
| 
						 | 
				
			
			@ -71,8 +72,6 @@ class _LoginScreen extends State<LoginScreen> {
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _asyncInitState() async {
 | 
			
		||||
    super.initState();
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      await widget.apiClient.refresh();
 | 
			
		||||
      showText("Welcome back!");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,16 +16,31 @@ class MainScreen extends BasedScreen {
 | 
			
		|||
class _MainScreen extends State<MainScreen> {
 | 
			
		||||
  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
 | 
			
		||||
  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<MainScreen> {
 | 
			
		|||
    return Scaffold(
 | 
			
		||||
      appBar: AppBar(
 | 
			
		||||
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
 | 
			
		||||
        title: Text("FOODER"),
 | 
			
		||||
        title: Text(title),
 | 
			
		||||
      ),
 | 
			
		||||
      body: Center(
 | 
			
		||||
        child: content,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,7 @@ class DiaryWidget extends StatelessWidget {
 | 
			
		|||
            MealWidget(
 | 
			
		||||
              meal: meal,
 | 
			
		||||
              ),
 | 
			
		||||
            Text(diary.date.toString()),
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue