aboutsummaryrefslogtreecommitdiff
path: root/lib/views/main_view.dart
blob: 3db3e970231423a51d77c55d9d8b710dff67bce3 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:kulinar_app/constants.dart';
import 'package:kulinar_app/views/file_info.dart';
import 'package:kulinar_app/views/recipe_view.dart';
import 'package:kulinar_app/models/recipe_class.dart';
import 'package:kulinar_app/widgets/error_widgets.dart';
import 'package:kulinar_app/widgets/toastbar_widget.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/widgets/recipe_search_delegate.dart';
import 'package:kulinar_app/widgets/page_route_transitions.dart';

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

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

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

class MainViewState extends State<MainView> with SingleTickerProviderStateMixin, WidgetsBindingObserver {
  static const platform = MethodChannel("com.davidpenkowoj.kulinar.openfile");

  TabController? _tabController;
  BuildContext? _toastyContext;

  @override
  void initState() {
    super.initState();
    getOpenFileUrl();
    WidgetsBinding.instance.addObserver(this);
    _tabController = TabController(length: 2, vsync: this, initialIndex: 1);
    // TODO: Make initialIndex a setting and maybe remember during app use
  }

  @override
  void dispose() {
    super.dispose();
    _tabController!.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      getOpenFileUrl();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context, _tabController!),
      drawer: const CustomDrawer(initialIndex: 0),
      floatingActionButton: _buildFloatingActionButton(),
      body: Builder(builder: (BuildContext context) {
        _toastyContext = context;

        return TabBarView(
          controller: _tabController,
          children: [
            _buildListView(RecipeData.recipeList.where((element) => element.rating > 0).toList()),
            _buildListView(RecipeData.recipeList.where((element) => element.rating == 0).toList()),
          ],
        );
      }),
    );
  }

  void getOpenFileUrl() async {
    dynamic url = await platform.invokeMethod("getOpenFileUrl");

    if (url != null) {
      setState(() {
        Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (BuildContext context) => FileInfo(filePath: url)), (_) => false);
      });
    }
  }

  void showToastCallback(String content, String actionLabel, Function actionCallback) {
    ToastBar.showToastBar(_toastyContext!, content, actionLabel: actionLabel, actionCallback: actionCallback);
  }

  Widget _buildFloatingActionButton() {
    return FloatingActionButton(
      child: const Icon(Icons.add, color: cIconColor),
      onPressed: () async {
        await Navigator.push(
          context,
          SlideFromBottomRoute(child: const RecipeView()),
        );

        setState(() {});
      },
    );
  }

  Widget _buildListView(List<Recipe> filteredRecipeList) {
    if (filteredRecipeList.isEmpty) return const NoContentError();

    return ListView.builder(
      itemCount: filteredRecipeList.length,
      itemBuilder: (context, index) => RecipeCard(recipe: filteredRecipeList[index], redrawCallback: redrawMainView, showToastCallback: showToastCallback),
    );
  }

  PreferredSizeWidget _buildAppBar(BuildContext context, TabController tabController) {
    return AppBar(
      title: Text(AppLocalizations.of(context)!.category1),
      actions: [
        IconButton(
          icon: const Icon(
            Icons.search,
          ),
          onPressed: () async {
            await showSearch(
              context: context,
              delegate: RecipeSearch(),
            );

            setState(() {});
          },
        ),
      ],
      bottom: TabBar(
        controller: tabController,
        tabs: [
          Tab(child: Text(AppLocalizations.of(context)!.category2, style: cSubTitleStyle)),
          Tab(child: Text(AppLocalizations.of(context)!.category3, style: cSubTitleStyle)),
        ],
      ),
    );
  }

  Future<void> redrawMainView() async {
    setState(() {});
  }
}