Newer
Older
import '../config.dart';
import 'error-alert.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:developer';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_web_browser/flutter_web_browser.dart';
import 'dart:convert';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_app_auth_wrapper/flutter_app_auth_wrapper.dart';
import 'dart:io';
import 'package:uni_links/uni_links.dart';
Markin Igor
committed
enum AppMode {
Default,
Authorization,
}
enum Screen {
App,
OAuth,
Dashboard
}
Future<Widget> initApplication() async {
Uri initialUri;
try {
initialUri = await getInitialUri();
} on PlatformException {
initialUri = null;
} on FormatException {
initialUri = null;
}
final prefs = await SharedPreferences.getInstance();
final host = prefs.getString('host');
return new MainApp(initialUri: initialUri, initialHost: host);
Widget build(BuildContext context) {
return MaterialApp(
title: 'Vereign',
theme: ThemeData(
primaryColor: Color(0xFFd51d32),
fontFamily: "Arial",
textTheme: TextTheme(
button: TextStyle(color: Colors.white, fontSize: 18.0),
title: TextStyle(color: Colors.red))),
home: new SplashScreen(
)
);
}
}
class MainApp extends StatefulWidget {
MainApp({ @required this.initialUri, this.initialHost });
@override
_MainAppState createState() => _MainAppState();
Markin Igor
committed
/// "Default" when you open app, "Authorization" when app initiated
/// from third party app
AppMode _appMode;
// Url of the app which invoked OAuth
String _invokerURL;
String _host = Config.appFlavor == Flavor.DEVELOPMENT ? Config.HOSTS[0] : Config.DEFAULT_APP_HOST;
Screen _currentScreen = Screen.App;
@override
initState() {
super.initState();
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
// Set up app host
if (widget.initialHost != null) {
setState(() {
_host = widget.initialHost;
});
}
// Subscribe to authorization events
FlutterAppAuthWrapper.eventStream().listen((data) async {
var token = json.decode(data.toString())["access_token"];
setScreen(Screen.App);
log("Open $_invokerURL");
try {
await launch("$_invokerURL?token=$token&host=$_host");
} catch (e) {
log("Error launching url $_invokerURL");
}
}, onError: (error) {
log("Err $error");
String errorMessage = error.message;
String errorDetails = error.details;
if (
// Handle cancellation for Android
errorDetails == '{"type":0,"code":1,"errorDescription":"User cancelled flow"}' ||
// Handle cancellation for iOS
errorMessage.toLowerCase().contains("the operation couldn")
) {
if (Platform.isAndroid) {
// Show only for android, because iOS will show the same alert as
// was for Auth request
openVereign();
} else {
setScreen(Screen.App);
}
} else {
showErrorAlert(context, Platform.isAndroid ? errorDetails : errorMessage, openVereign);
setScreen(Screen.App);
}
});
initUniLinks();
}
@override
dispose() {
if (_sub != null) _sub.cancel();
super.dispose();
}
Future<Null> initUniLinks() async {
_sub = getUriLinksStream().listen((Uri uri) {
}, onError: (err) {
log('got err: $err');
});
}
Markin Igor
committed
_appMode = AppMode.Authorization;
Markin Igor
committed
_appMode = AppMode.Default;
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
setScreen(Screen screen) {
setState(() {
_currentScreen = screen;
});
}
openVereign() {
setScreen(Screen.Dashboard);
FlutterWebBrowser.openWebPage(url: _host, androidToolbarColor: Color(0xFFd51d32));
}
startOAuth() {
if (_currentScreen == Screen.OAuth) {
return;
}
setScreen(Screen.OAuth);
var params = Config.getOAuthParams(host: _host);
FlutterAppAuthWrapper.startAuth(
AuthConfig(
clientId: params["clientId"],
clientSecret: params["clientSecret"],
redirectUrl: params["redirectUrl"],
state: "login",
prompt: "consent",
endpoint: AuthEndpoint(
auth: params["authEndpoint"], token: params["tokenEndpoint"]),
scopes: [
"user_account_status",
"user_territory",
"user_profile"
],
),
);
}
setHost(String host) {
setState(() {
_host = host;
});
}
return new Scaffold(
appBar: new AppBar(title: Text("Vereign")),
body: Home(
mode: _appMode,
host: _host,
setHost: setHost,
openDashboardClick: openVereign,
authorizeClick: startOAuth,