aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/utility_icon_row_widget.dart
blob: a09c7339fe1df87d2066d845c9a40f1ddd7ae9d1 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import 'package:flutter/material.dart';

import 'package:kulinar_app/constants.dart';
import 'package:kulinar_app/models/recipe_class.dart';
import 'package:kulinar_app/models/data/recipe_data_class.dart';
import 'package:kulinar_app/models/data/settings_data_class.dart';

class UtilityIconRow extends StatefulWidget {
  const UtilityIconRow({
    Key? key,
    this.remote = false,
    required this.unsavedRecipe,
    required this.isEditModeEnabled,
    required this.pickImageCallback,
    required this.removeImageCallback,
    required this.removeRecipeCallback,
    required this.downloadRecipeCallback,
    required this.cacheUnsavedRecipeCallback,
  }) : super(key: key);

  final bool remote;
  final bool isEditModeEnabled;
  final Recipe unsavedRecipe;
  final Function pickImageCallback;
  final Function removeImageCallback;
  final Function removeRecipeCallback;
  final Function downloadRecipeCallback;
  final Function cacheUnsavedRecipeCallback;

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

class UtilityIconRowState extends State<UtilityIconRow> {
  final _isInputSourceCamera = SettingsData.settings["photoSource"] == "0" ? true : false;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 10.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 6.0),
            child: _buildButtonAddImage(),
          ),
          Padding(
            padding: const EdgeInsets.only(right: 6.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                _buildIconRating(context),
                _buildIconFavorite(),
                // _buildIconCalculate(),
                // _buildIconShare(),
                // _buildIconImportExport(context),
              ],
            ),
          ),
        ],
      ),
    );
  }

  IconButton _buildButtonAddImage() {
    Icon pickImageIcon;
    Icon removeImageIcon;
    bool recipeHasImage = widget.unsavedRecipe.image == null;

    if (_isInputSourceCamera) {
      pickImageIcon = const Icon(Icons.add_a_photo_rounded);
      removeImageIcon = const Icon(Icons.no_photography_rounded);
    } else {
      pickImageIcon = const Icon(Icons.add_photo_alternate_rounded);
      removeImageIcon = const Icon(Icons.image_not_supported_rounded);
    }

    void onPress() {
      if (recipeHasImage) {
        widget.pickImageCallback();
      } else {
        widget.removeImageCallback();
      }
    }

    return IconButton(
      icon: recipeHasImage ? pickImageIcon : removeImageIcon,
      onPressed: widget.isEditModeEnabled ? null : onPress,
    );
  }

  Widget _buildIconRating(BuildContext context) {
    void onPress() {
      widget.unsavedRecipe.updateRating();
      widget.cacheUnsavedRecipeCallback();
      RecipeData.save();

      setState(() {});
    }

    return Row(
      children: [
        Padding(
          padding: const EdgeInsets.only(top: 2.0),
          child: Text(widget.unsavedRecipe.rating.toString(), style: cDefaultTextStyle),
        ),
        IconButton(
          iconSize: 28.0,
          icon: const Icon(Icons.star_border_rounded),
          onPressed: !widget.remote ? onPress : null,
        ),
      ],
    );
  }

  Widget _buildIconFavorite() {
    Icon icon = const Icon(Icons.favorite_border_rounded);
    Color color = Colors.black;

    if (widget.unsavedRecipe.favorite) {
      icon = const Icon(Icons.favorite_rounded);
      color = Colors.red;
    }

    void onPress() {
      widget.unsavedRecipe.toggleFavorite();
      widget.cacheUnsavedRecipeCallback();
      RecipeData.save();

      setState(() {});
    }

    return IconButton(
      icon: icon,
      color: color,
      onPressed: !widget.remote ? onPress : null,
    );
  }

  /*
    IF REMOTE:
      DOWNLOAD

    IF REMOTE AND LOCAL:
      GO TO LOCAL

    IF LOCAL:
      UPLOAD
  Widget _buildIconImportExport(BuildContext context) {
    if (widget.unsavedRecipe.isListed(remote: true)) {
      if (widget.unsavedRecipe.isListed()) {
        return IconButton(
          icon: Icon(Icons.get_app),
          onPressed: !widget.isEditingAllowedCallback() ? () => widget.downloadRecipeCallback(context, widget.unsavedRecipe) : null,
        );
      } else {
        return IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: !widget.isEditingAllowedCallback() ? () => widget.downloadRecipeCallback(context, widget.unsavedRecipe) : null,
        );
      }
    } else {
      return IconButton(
        icon: Icon(Icons.publish),
        onPressed: !widget.isEditingAllowedCallback() ? () => _uploadRecipe(context) : null,
      );
    }
  }
  */
}