aboutsummaryrefslogtreecommitdiff
path: root/lib/views/week_view.dart
blob: 8ea43a4548f60d8bfd2919ccc073bc8bac47a1cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import 'dart:convert';

import 'package:flutter/material.dart';

import 'package:kulinar_app/models/recipe_class.dart';
import 'package:kulinar_app/widgets/error_widgets.dart';
import 'package:kulinar_app/widgets/recipe_card_widget.dart';
import 'package:kulinar_app/widgets/custom_drawer_widget.dart';
import 'package:kulinar_app/models/data/recipe_data_class.dart';
import 'package:kulinar_app/models/data/settings_data_class.dart';

import 'package:http/http.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class WeekView extends StatefulWidget {
  const WeekView({Key? key}) : super(key: key);

  @override
  _WeekViewState createState() => _WeekViewState();
}

class _WeekViewState extends State<WeekView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(AppLocalizations.of(context)!.category5)),
      drawer: CustomDrawer(initalIndex: 2),
      body: FutureBuilder(
        future: _getWeeklyRecipes(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState != ConnectionState.done) return LinearProgressIndicator();
          if (snapshot.hasError || !snapshot.hasData) return NetworkContentError(refreshCallback: _retry);

          return RefreshIndicator(
            onRefresh: () async {
              setState(() {});
            },
            child: ListView(
              children: _buildRecipeCardsFromString(snapshot.data.body),
            ),
          );
        },
      ),
    );
  }

  Future<Response?> _getWeeklyRecipes() async {
    Map<String, String> _headers = {"Content-Type": "application/json; charset=UTF-8"};

    try {
      return await Future.delayed(Duration(milliseconds: 500), () {
        print(SettingsData.settings["serverURL"]);
        return get(Uri.https(SettingsData.settings["serverURL"]!, "/weekly"), headers: _headers);
      });
    } catch (e) {
      print(e);
      return null;
    }
  }

  List<Widget> _buildRecipeCardsFromString(String string) {
    List<Widget> _list = [];
    RecipeData.remoteRecipeList.clear();

    for (dynamic _entry in jsonDecode(string)) {
      Recipe _recipe = Recipe.fromJson(jsonEncode(_entry));
      RecipeData.remoteRecipeList.add(_recipe);
      _list.add(RecipeCard(remote: true, recipe: _recipe));
    }

    return _list;
  }

  Future<Null> _retry() async {
    await Future.delayed(Duration(milliseconds: 300));

    setState(() {});

    return null;
  }
}