-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_state.dart
197 lines (166 loc) · 5.65 KB
/
app_state.dart
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import 'package:flutter/material.dart';
import 'flutter_flow/request_manager.dart';
import 'backend/backend.dart';
import 'backend/api_requests/api_manager.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:csv/csv.dart';
import 'flutter_flow/flutter_flow_util.dart';
class FFAppState extends ChangeNotifier {
static final FFAppState _instance = FFAppState._internal();
factory FFAppState() {
return _instance;
}
FFAppState._internal() {
initializePersistedState();
}
Future initializePersistedState() async {
secureStorage = FlutterSecureStorage();
_favorite = (await secureStorage.getStringList('ff_favorite'))
?.map((path) => path.ref)
.toList() ??
_favorite;
_cart = (await secureStorage.getStringList('ff_cart'))
?.map((path) => path.ref)
.toList() ??
_cart;
_cartsum = await secureStorage.getDouble('ff_cartsum') ?? _cartsum;
_color = await secureStorage.getString('ff_color') ?? _color;
}
void update(VoidCallback callback) {
callback();
notifyListeners();
}
late FlutterSecureStorage secureStorage;
List<DocumentReference> _favorite = [];
List<DocumentReference> get favorite => _favorite;
set favorite(List<DocumentReference> _value) {
_favorite = _value;
secureStorage.setStringList(
'ff_favorite', _value.map((x) => x.path).toList());
}
void deleteFavorite() {
secureStorage.delete(key: 'ff_favorite');
}
void addToFavorite(DocumentReference _value) {
_favorite.add(_value);
secureStorage.setStringList(
'ff_favorite', _favorite.map((x) => x.path).toList());
}
void removeFromFavorite(DocumentReference _value) {
_favorite.remove(_value);
secureStorage.setStringList(
'ff_favorite', _favorite.map((x) => x.path).toList());
}
void removeAtIndexFromFavorite(int _index) {
_favorite.removeAt(_index);
secureStorage.setStringList(
'ff_favorite', _favorite.map((x) => x.path).toList());
}
List<DocumentReference> _cart = [];
List<DocumentReference> get cart => _cart;
set cart(List<DocumentReference> _value) {
_cart = _value;
secureStorage.setStringList('ff_cart', _value.map((x) => x.path).toList());
}
void deleteCart() {
secureStorage.delete(key: 'ff_cart');
}
void addToCart(DocumentReference _value) {
_cart.add(_value);
secureStorage.setStringList('ff_cart', _cart.map((x) => x.path).toList());
}
void removeFromCart(DocumentReference _value) {
_cart.remove(_value);
secureStorage.setStringList('ff_cart', _cart.map((x) => x.path).toList());
}
void removeAtIndexFromCart(int _index) {
_cart.removeAt(_index);
secureStorage.setStringList('ff_cart', _cart.map((x) => x.path).toList());
}
double _cartsum = 0;
double get cartsum => _cartsum;
set cartsum(double _value) {
_cartsum = _value;
secureStorage.setDouble('ff_cartsum', _value);
}
void deleteCartsum() {
secureStorage.delete(key: 'ff_cartsum');
}
int _size = 0;
int get size => _size;
set size(int _value) {
_size = _value;
}
String _color = '';
String get color => _color;
set color(String _value) {
_color = _value;
secureStorage.setString('ff_color', _value);
}
void deleteColor() {
secureStorage.delete(key: 'ff_color');
}
bool _searchonof = false;
bool get searchonof => _searchonof;
set searchonof(bool _value) {
_searchonof = _value;
}
String _searchString = '';
String get searchString => _searchString;
set searchString(String _value) {
_searchString = _value;
}
final _usersManager = FutureRequestManager<ApiCallResponse>();
Future<ApiCallResponse> users({
String? uniqueQueryKey,
bool? overrideCache,
required Future<ApiCallResponse> Function() requestFn,
}) =>
_usersManager.performRequest(
uniqueQueryKey: uniqueQueryKey,
overrideCache: overrideCache,
requestFn: requestFn,
);
void clearUsersCache() => _usersManager.clear();
void clearUsersCacheKey(String? uniqueKey) =>
_usersManager.clearRequest(uniqueKey);
}
LatLng? _latLngFromString(String? val) {
if (val == null) {
return null;
}
final split = val.split(',');
final lat = double.parse(split.first);
final lng = double.parse(split.last);
return LatLng(lat, lng);
}
extension FlutterSecureStorageExtensions on FlutterSecureStorage {
void remove(String key) => delete(key: key);
Future<String?> getString(String key) async => await read(key: key);
Future<void> setString(String key, String value) async =>
await write(key: key, value: value);
Future<bool?> getBool(String key) async => (await read(key: key)) == 'true';
Future<void> setBool(String key, bool value) async =>
await write(key: key, value: value.toString());
Future<int?> getInt(String key) async =>
int.tryParse(await read(key: key) ?? '');
Future<void> setInt(String key, int value) async =>
await write(key: key, value: value.toString());
Future<double?> getDouble(String key) async =>
double.tryParse(await read(key: key) ?? '');
Future<void> setDouble(String key, double value) async =>
await write(key: key, value: value.toString());
Future<List<String>?> getStringList(String key) async =>
await read(key: key).then((result) {
if (result == null || result.isEmpty) {
return null;
}
return CsvToListConverter()
.convert(result)
.first
.map((e) => e.toString())
.toList();
});
Future<void> setStringList(String key, List<String> value) async =>
await write(key: key, value: ListToCsvConverter().convert([value]));
}