Files
checker/lib/splash.dart

142 lines
5.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:intl/intl.dart';
import 'dart:async';
import 'dart:convert';
import 'common.dart';
import 'network.dart';
import 'consts.dart';
import 'registration.dart';
import 'finish_registration.dart';
import 'db.dart';
class SplashScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Появляется splash screen, проверяется токен.
new Future.delayed(const Duration(milliseconds: 500), () {
platform.invokeMethod('getLocale').then((locale) {
Intl.defaultLocale = locale;
print(Intl.defaultLocale);
showNextScreen(context);
});
});
return new Stack(children: <Widget>[getSplashBackground(), 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(logo_png, height: 112.0, width: 252.0),
new Image.asset(splash_text_png, height: 40.0, width: 240.0)]));
}
/// Возвращает контейнер, который содержит decoration с фоновым изображением.
getSplashBackground() {
return new Container(decoration:
new BoxDecoration(image:
new DecorationImage(image: new ExactAssetImage(splash_png), fit: BoxFit.cover)));
}
/// Запуск следующего экрана приложения.
showNextScreen(BuildContext context) async {
SqliteHelper helper = new SqliteHelper();
await helper.open();
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(context, statusResponse, helper);
}).catchError((error) {
helper.close().then((_) {
print(error.toString());
return false;
});
});
}
}
}
/// Обработка ответа.
/// В случае, если токен был удален может прийти active: false, либо 404.
/// Если токен не активен, попробовать создать его еще раз.
handleStatusResponse(BuildContext context, 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);
} else {
if (await platform.invokeMethod('isOnline')) {
_createToken(context, helper);
}
}
}
}
/// Отправляется запрос на создание токена.
///
/// Если вернулся код 409, значит такой токен уже существует и активирован.
/// Нужно направить пользователя на экран подтверждения активации.
///
/// Если вернулся код 200, значит токен был ранее удален и только что снова создался.
/// Нужно удалить его и направить пользователя на экран регистрации.
_createToken(BuildContext ctx, 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(ctx, new FinishRegistrationScreen());
} else if (response.statusCode == 201) {
clearToken(response, ctx, helper);
}
}).catchError((error) => print(error.toString()));
}
/// Очищаем бд, делаем запрос на удаление токена.
void clearToken(Response response, BuildContext ctx, SqliteHelper helper) {
helper.clear().then((_) {
Map parsedMap = JSON.decode(response.body);
deleteToken(parsedMap['token']).then((_) {
helper.close();
Navigator.of(ctx).pop(); // Убираем текущий route
pushRoute(ctx, new RegistrationScreen()); // Запускаем регистрацию
}).catchError((error) {
helper.close();
print(error.toString());
});
});
}
}