174 lines
5.8 KiB
Dart
174 lines
5.8 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:http/http.dart';
|
||
import 'package:intl/intl.dart';
|
||
import 'dart:convert';
|
||
import 'dart:async';
|
||
import 'package:checker/common.dart';
|
||
import 'package:checker/network.dart';
|
||
import 'package:checker/consts.dart';
|
||
import 'package:checker/resources.dart';
|
||
import 'package:checker/db.dart';
|
||
import 'package:checker/base_state.dart';
|
||
import 'package:checker/screens/registration.dart';
|
||
import 'package:checker/screens/finish_registration.dart';
|
||
|
||
class SplashScreen extends StatefulWidget {
|
||
@override State createState() => new _SplashScreenState();
|
||
}
|
||
|
||
class _SplashScreenState extends BaseState<SplashScreen> {
|
||
|
||
@override Widget getMainWidget() {
|
||
return getScreenContent();
|
||
}
|
||
|
||
@override void onStart() {
|
||
helper.getSettings().then((info) {
|
||
if (info == null) {
|
||
platform.invokeMethod('getLocale').then((locale) {
|
||
Intl.defaultLocale = locale;
|
||
platform.invokeMethod('getCurrency').then((currency) {
|
||
helper.createAppInfo(locale, currency).then((_) {
|
||
showNext();
|
||
});
|
||
});
|
||
});
|
||
} else {
|
||
showNext();
|
||
}
|
||
});
|
||
}
|
||
|
||
void showNext() {
|
||
new Future.delayed(const Duration(milliseconds: 1000), () {
|
||
showNextScreen();
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget getScreenContent() {
|
||
return app == null
|
||
? getBackground()
|
||
: new Stack(
|
||
children: <Widget>[
|
||
getBackground(),
|
||
getLogo(),
|
||
new Align(
|
||
alignment: FractionalOffset.bottomRight,
|
||
child: new Container(
|
||
margin: new EdgeInsets.only(
|
||
right: 11.0,
|
||
bottom: 5.0),
|
||
child: new Image.asset(powered_by_dinect_splash_png,
|
||
height: 16.0,
|
||
width: 122.0)))]);
|
||
}
|
||
|
||
/// Возвращает столбец с логотипом приложения и текстом под ним.
|
||
/// Столбец занимает не все доступное пространство, а необходимый минимум в центре экрана.
|
||
getLogo() {
|
||
return new Center(
|
||
child: new Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: <Widget>[
|
||
new Image.asset(
|
||
Resources.getLogo(app),
|
||
height: 112.0,
|
||
width: 252.0),
|
||
new Image.asset(
|
||
splash_text_png,
|
||
height: 40.0,
|
||
width: 240.0)]));
|
||
}
|
||
|
||
/// Запуск следующего экрана приложения.
|
||
showNextScreen() async {
|
||
|
||
String token = await helper.getToken();
|
||
|
||
// В случае, если в приложении отсутствует токен,
|
||
// необходимо запустить регистрацию кассы.
|
||
if (token == null) {
|
||
await helper.close();
|
||
pushRoute(context, new RegistrationScreen());
|
||
} else {
|
||
if (await platform.invokeMethod('isOnline')) {
|
||
checkTokenStatus(token).then((statusResponse) {
|
||
handleStatusResponse(statusResponse, helper);
|
||
}).catchError((error) {
|
||
helper.close().then((_) {
|
||
print(error.toString());
|
||
return false;
|
||
});
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Обработка ответа.
|
||
/// В случае, если токен был удален может прийти active: false, либо 404.
|
||
/// Если токен не активен, попробовать создать его еще раз.
|
||
handleStatusResponse(var statusResponse, SqliteHelper helper) async {
|
||
int code = statusResponse.statusCode;
|
||
print('resp: ${code}');
|
||
|
||
if (code == 404) {
|
||
helper.clear().then((result) {
|
||
helper.close().then((_) {
|
||
pushRoute(context, new RegistrationScreen());
|
||
});
|
||
});
|
||
} else {
|
||
|
||
Map status = JSON.decode(statusResponse.body);
|
||
bool active = status['active'] == null ? false : status['active'];
|
||
|
||
if (active) {
|
||
startScanner(context, app, helper);
|
||
} else {
|
||
if (await platform.invokeMethod('isOnline')) {
|
||
_createToken(helper);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Отправляется запрос на создание токена.
|
||
///
|
||
/// Если вернулся код 409, значит такой токен уже существует и активирован.
|
||
/// Нужно направить пользователя на экран подтверждения активации.
|
||
///
|
||
/// Если вернулся код 200, значит токен был ранее удален и только что снова создался.
|
||
/// Нужно удалить его и направить пользователя на экран регистрации.
|
||
_createToken(SqliteHelper helper) async {
|
||
|
||
String merchantID = await helper.getMerchantID();
|
||
String posID = await helper.getPosID();
|
||
|
||
createToken(merchantID, posID).then((response) {
|
||
if (response.statusCode == 409) {
|
||
helper.close();
|
||
pushRoute(context, new FinishRegistrationScreen());
|
||
} else if (response.statusCode == 201) {
|
||
clearToken(response, helper);
|
||
}
|
||
}).catchError((error) => print(error.toString()));
|
||
}
|
||
|
||
/// Очищаем бд, делаем запрос на удаление токена.
|
||
void clearToken(Response response, SqliteHelper helper) {
|
||
|
||
helper.clear().then((_) {
|
||
Map parsedMap = JSON.decode(response.body);
|
||
deleteToken(parsedMap['token']).then((_) {
|
||
helper.close();
|
||
Navigator.of(context).pop(); // Убираем текущий route
|
||
pushRoute(context, new RegistrationScreen()); // Запускаем регистрацию
|
||
}).catchError((error) {
|
||
helper.close();
|
||
print(error.toString());
|
||
});
|
||
});
|
||
}
|
||
}
|