-
Notifications
You must be signed in to change notification settings - Fork 1
/
Todos.dart
395 lines (335 loc) · 9.77 KB
/
Todos.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
///
/// Copyright (C) 2018 Andrious Solutions
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Created 04 Nov 2018
import "dart:async" show Future;
import 'package:auth/auth.dart';
import 'package:flutter/material.dart'
show
AppLifecycleState,
FormState,
GlobalKey,
MaterialPageRoute,
Navigator,
ScaffoldState,
Text,
TextEditingController,
ThemeData,
Widget;
import 'package:intl/intl.dart' show DateFormat;
import 'package:workingmemory/src/model.dart' as m;
import 'package:workingmemory/src/view.dart';
import 'package:workingmemory/src/controller.dart';
final ThemeData theme = App.themeData;
class Controller extends ControllerMVC {
factory Controller() => _this ??= Controller._();
static Controller _this;
Controller._() : super() {
_model = m.Model();
_dataFields = ToDoEdit(this);
}
// External access to the Model component.
m.Model get model => _model;
m.Model _model;
/// Allow for easy access to 'the Controller' throughout the application.
static Controller get con => _this ?? Controller();
WorkingController get app => _app ??= WorkingController();
WorkingController _app;
void rebuild() => _this.refresh();
Future<List<Map<String, dynamic>>> requery() async{
var recs = await data.query();
refresh();
return recs;
}
Future<bool> initAsync() async {
bool init = await _model.initAsync();
List<Map<String, dynamic>> records = await data.query();
setAlarms(records);
_favIcons = await _model.listIcons();
return init;
}
ToDoEdit get data => _dataFields;
ToDoEdit _dataFields;
String get editKey => _editKey;
String _editKey;
String get listKey => _listKey;
String _listKey;
List<Map<String, dynamic>> get favIcons => _favIcons;
List<Map<String, dynamic>> _favIcons;
Map<String, String> get icons => _icons;
Map<String, String> _icons = m.Icons.code;
Future<bool> saveIcon(String icon) async {
data.icon = icon;
bool save = await _model.saveIcon(icon);
_favIcons = await _model.listIcons();
return save;
}
@override
void dispose() {
_model.dispose();
super.dispose();
}
void signIn() async {
// app.signIn();
signOut();
await Navigator.push(
context, MaterialPageRoute(builder: (context) => SignIn()));
// refresh();
}
void logOut() => app.logOut();
Future<void> signOut() => app.signOut();
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
/// Passing these possible values:
/// AppLifecycleState.paused (may enter the suspending state at any time)
/// AppLifecycleState.resumed
/// AppLifecycleState.inactive (may be paused at any time)
/// AppLifecycleState.suspending (Android only)
if (state == AppLifecycleState.resumed) {
// if(app.loggedIn)
// _model.sync();
}
}
Future<bool> save(Map<String, dynamic> data) => _model.save(data);
Future<bool> saveRec(
Map<String, dynamic> diffRec, Map<String, dynamic> oldRec) =>
save(newRec(diffRec, oldRec));
Map<String, dynamic> newRec(
Map<String, dynamic> diffRec, Map<String, dynamic> oldRec) {
Map<String, dynamic> newRec = Map();
if (oldRec == null) {
newRec.addAll(diffRec);
} else {
newRec.addAll(oldRec);
newRec.addEntries(diffRec.entries);
}
return newRec;
}
recordDump(FirebaseUser user) async {
if (user == null) return;
bool dump = await _model.recordDump();
if (dump) {
requery();
}
}
get defaultIcon => _model.defaultIcon;
void reSync() => _model.reSync();
bool itemsOrdered([bool ordered]) => _model.itemsOrdered(ordered);
void setAlarms(List<Map<String, dynamic>> list) async {
recs = list;
Iterator it = list.iterator;
String sDateTime;
DateTime time;
DateTime threshold = DateTime.now();
bool oneShot;
while (it.moveNext()) {
int id = it.current["rowid"];
if (id == null) continue;
sDateTime = it.current["DateTime"];
if (sDateTime == null) continue;
time = DateTime.parse(sDateTime);
if (time.isAfter(threshold)) {
// oneShot = await AlarmManager.oneShotAt(
// time,
// id,
// (int id) async {
// Iterator it = recs.iterator;
// int rowid;
// while (it.moveNext()) {
// rowid = it.current["rowid"];
// if (rowid == null) continue;
// if (rowid == id) {
// break;
// }
// }
// },
// exact: true,
// );
// if (!oneShot) {
// getError(Exception("AlarmManager.oneShotAt returned false."));
// break;
// }
// AlarmManager.cancel(id).then((cancel) {
// print(cancel);
// });
}
break;
}
}
static List<Map<String, dynamic>> recs;
}
//class ToDoEdit extends _ToDoList {
// bool hasName;
// TextEditingController changer;
// bool hasChanged = false;
//
//// final scaffoldKey = GlobalKey<ScaffoldState>();
//
// final formKey = GlobalKey<FormState>();
//
// void init([Map todo]) {
// this.todo = todo;
//
// hasName = this.todo?.isNotEmpty ?? false;
//
// if (hasName) {
// item = todo['Item'];
// dateTime = DateTime.tryParse(todo['DateTime']);
// icon = todo['Icon'];
// } else {
// item = ' ';
// icon = Controller().defaultIcon;
// }
//
// changer = TextEditingController(text: item);
// changer.addListener(() {
// hasChanged = changer.value.text != item;
// });
//
// dateTime = dateTime ?? DateTime.now();
// }
//
// Widget get title => Text(hasName ? item : 'New');
//
// Future<bool> onPressed() async {
// bool save = formKey.currentState.validate();
// if (save) {
// formKey.currentState.save();
// save = await this.save(
// {'Item': changer.text.trim(), 'DateTime': dateTime, 'Icon': icon},
// this.todo);
// await retrieve();
// }
// return save;
// }
//
// Future<bool> save(
// Map<String, dynamic> diffRec, Map<String, dynamic> oldRec) async {
// bool save = await Controller().saveRec(diffRec, oldRec);
// return save;
// }
//
// Future<bool> delete(Map<String, dynamic> data) =>
// _model.delete(data).then((delete) async {
// await Controller().data.query();
// return delete;
// });
//
// Future<bool> unDelete(Map<String, dynamic> data) =>
// _model.unDelete(data).then((un) async {
// await Controller().data.query();
// return un;
// });
//}
//
//typedef MapCallback = void Function(Map data);
//
//class _ToDoList extends _ToDoFields {
// _ToDoList() : super() {
// scaffoldKey = GlobalKey<ScaffoldState>();
// _model = m.Model();
// }
// final DateFormat dateFormat = DateFormat('EEEE, MMM dd h:mm a');
// GlobalKey<ScaffoldState> scaffoldKey;
// m.Model _model;
//
// List<Map<String, dynamic>> get items => _items;
// List<Map<String, dynamic>> _items = [];
//
// /// Retrieve the to-do items from the database
// Future<List<Map<String, dynamic>>> retrieve() async {
// _items = await _model.list();
// return _items;
// }
//}
class _ToDoFields {
Map todo;
String item;
String icon;
DateTime dateTime;
bool saveNeeded;
bool hasChanged;
}
class ToDoEdit extends DataFields {
ToDoEdit(this.con) {
_model = m.Model();
}
final Controller con;
m.Model get model => _model;
m.Model _model;
bool hasName;
TextEditingController changer;
bool hasChanged = false;
Map todo;
String item;
String icon;
DateTime dateTime;
bool saveNeeded;
final DateFormat dateFormat = DateFormat('EEEE, MMM dd h:mm a');
final scaffoldKey = GlobalKey<ScaffoldState>();
final formKey = GlobalKey<FormState>();
Widget get title => Text(hasName ? item : 'New');
void init([Map todo]) {
this.todo = todo;
hasName = this.todo?.isNotEmpty ?? false;
if (hasName) {
item = todo['Item'];
dateTime = DateTime.tryParse(todo['DateTime']);
icon = todo['Icon'];
} else {
item = ' ';
icon = con.defaultIcon;
}
changer = TextEditingController(text: item);
changer.addListener(() {
hasChanged = changer.value.text != item;
});
dateTime = dateTime ?? DateTime.now();
}
/// Retrieve the to-do items from the database
@override
Future<List<Map<String, dynamic>>> retrieve() => _model.list();
Future<bool> onPressed() async {
bool save = formKey.currentState.validate();
if (save) {
formKey.currentState.save();
save = await this.saveRec(
{'Item': changer.text.trim(), 'DateTime': dateTime, 'Icon': icon},
this.todo);
await query();
}
return save;
}
Future<bool> saveRec(
Map<String, dynamic> diffRec, Map<String, dynamic> oldRec) =>
save(con?.newRec(diffRec, oldRec));
@override
Future<bool> save(Map<String, dynamic> rec) => _model.save(rec);
@override
Future<bool> delete(Map<String, dynamic> rec) async {
bool delete = await _model.delete(rec);
await con?.data?.query();
return delete;
}
@override
Future<bool> undo(Map<String, dynamic> rec) async {
bool undo = await _model.unDelete(rec);
await con?.data?.query();
return undo;
}
Future<bool> favIcon() {
return Future.value(true);
}
}