-
Notifications
You must be signed in to change notification settings - Fork 1
/
working_memory_app.dart
226 lines (174 loc) · 5.62 KB
/
working_memory_app.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
///
/// 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 23 Jun 2018
///
import 'dart:async' show Future;
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show FlutterExceptionHandler;
import 'package:mvc_application/controller.dart' as a show runApp;
import 'package:workingmemory/src/model.dart'
show CloudDB, FireBaseDB, RemoteConfig; //, RemoteConfigValue;
import 'package:workingmemory/src/view.dart' show ReportErrorHandler, showBox;
import 'package:package_info/package_info.dart' show PackageInfo;
import 'package:workingmemory/src/controller.dart'
show AppController, Controller;
import 'package:auth/auth.dart' show Auth, FirebaseUser, GoogleSignInAccount;
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
runApp(
Widget app, {
FlutterExceptionHandler handler,
ErrorWidgetBuilder builder,
ReportErrorHandler reportError,
}) {
// Supply Firebase Crashlytics
Crashlytics crash = Crashlytics.instance;
handler ??= crash.recordFlutterError;
reportError ??= crash.recordError;
crash.enableInDevMode = true;
a.runApp(app, handler: handler, builder: builder, reportError: reportError);
}
class WorkingController extends AppController {
factory WorkingController() => _this ??= WorkingController._();
static WorkingController _this;
//
// /// Allow for easy access to 'the Controller' throughout the application.
// static WorkingMemoryApp get con => _this;
WorkingController._() {
_auth = Auth(listener: _logInUser);
_con = Controller();
_remoteConfig = RemoteConfig();
}
/// Provide the sign in and the loading database info.
@override
Future<bool> initAsync() async {
super.initAsync();
await signIn();
await _remoteConfig.initAsync();
await _con.initAsync();
return true;
}
Controller _con;
bool _loggedIn;
Auth _auth;
RemoteConfig get config => _remoteConfig;
RemoteConfig _remoteConfig;
void didChangeAppLifecycleState(AppLifecycleState state) {
FireBaseDB.didChangeAppLifecycleState(state);
CloudDB.didChangeAppLifecycleState(state);
}
@override
void dispose() {
_con.dispose();
_auth?.dispose();
_remoteConfig.dispose();
super.dispose();
}
bool get loggedIn => _loggedIn;
// logout and refresh
void logOut() async {
await signOut();
rebuild();
}
void _logInUser(dynamic user) {
//
if (user != null) {
userStamp();
}
Crashlytics.instance.setUserEmail(_auth.email);
Crashlytics.instance.setUserIdentifier(_auth.displayName);
Crashlytics.instance.setUserName(_auth.displayName);
}
// 'disconnect' from Firebase
Future<void> signOut() => _auth.signOut().then(_logInUser);
Future<bool> signIn() async {
_loggedIn = await signInSilently();
if (!_loggedIn) _loggedIn = await signInAnonymously();
if(_auth.isAnonymous){
_auth.listener = _con.recordDump;
}
return _loggedIn;
}
Future<bool> signInAnonymously() => _auth.signInAnonymously();
Future<bool> signInSilently() => _auth.signInSilently();
Future<bool> signInWithFacebook() => _auth.signInWithFacebook();
// List<String> items = App.packageName.split(".");
Future<bool> signInWithTwitter() async {
//
PackageInfo info = await PackageInfo.fromPlatform();
List<String> items = info.packageName.split(".");
String one = await _remoteConfig.getStringed(items[0]);
if (one.isEmpty) {
return false;
}
String two = await _remoteConfig.getStringed(items[1]);
if (two.isEmpty) {
return false;
}
bool signIn = await _auth
.signInWithTwitter(
key: one,
secret: two,
)
.catchError((error) {
getError(error);
});
if (!signIn) {
Exception ex = _auth.getError();
showBox(text: ex.toString(), context: context);
}
return signIn;
}
Future<bool> signInEmailPassword(BuildContext context) async {
//
String email = "";
String password = "";
bool signIn = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
if (!signIn) {
Exception ex = _auth.getError();
showBox(text: ex.toString(), context: context);
}
return signIn;
}
Future<bool> signInWithGoogle() async {
bool signIn = await _auth.signInWithGoogle();
if (!signIn) {
Exception ex = _auth.getError();
showBox(text: ex.toString(), context: context);
}
await rebuild();
return signIn;
}
// Stamp the user information to the firebase database.
void userStamp() => FireBaseDB().userStamp();
Future<void> rebuild() async {
_loggedIn = await _auth.isLoggedIn();
_con.refresh();
// Pops only if on the stack and not on the first screen.
if (_con.context != null) Navigator.of(_con.context).maybePop();
}
String get uid => _auth.uid;
get email => _auth.email;
get name => _auth.displayName;
get provider => _auth.providerId;
get isNewUser => _auth.isNewUser;
get isAnonymous => _auth.isAnonymous;
get photo => _auth.photoUrl;
get token => _auth.accessToken;
get tokenId => _auth.idToken;
}