164 lines
5.6 KiB
Dart
164 lines
5.6 KiB
Dart
import 'package:checker/screens/faq.dart';
|
||
import 'package:checker/screens/purchase.dart';
|
||
import 'package:checker/screens/settings.dart';
|
||
import 'package:checker/screens/splash.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
|
||
import 'consts.dart';
|
||
import 'db.dart';
|
||
import 'network.dart';
|
||
import 'resources.dart';
|
||
import 'strings.dart';
|
||
|
||
// Канал для взаимодействия с кодом платформы.
|
||
const platform = const MethodChannel('com.dinect.checker/instance_id');
|
||
|
||
// Метод обеспечивает замену текущего объекта route новым.
|
||
pushRouteReplacement(BuildContext context, Widget widget) {
|
||
var route = new MaterialPageRoute<Null>(builder: (BuildContext context) => widget);
|
||
Navigator.of(context).pushReplacement(route);
|
||
}
|
||
|
||
pushRoute(BuildContext context, Widget widget) {
|
||
var route = new MaterialPageRoute<Null>(builder: (BuildContext context) => widget);
|
||
Navigator.of(context).push(route);
|
||
}
|
||
|
||
// Добавление route, с возможностью вернуться к предыдущему экрану.
|
||
faq(BuildContext context, bool returnToScanner) {
|
||
pushRoute(context, new FAQScreen(returnToScanner));
|
||
}
|
||
|
||
// В методе отправляется запрос на удаление токена кассы, очищаются SharedPreferences приложения.
|
||
logout(BuildContext context, SqliteHelper helper) async {
|
||
|
||
String token = await helper.getToken();
|
||
String locale = await helper.getLocale();
|
||
|
||
VoidCallback positiveCallback = () {
|
||
if (token != null) {
|
||
deleteToken(token, locale).then((response) {
|
||
helper.clear().then((result) {
|
||
helper.close().then((_) {
|
||
Navigator.of(context).pop();
|
||
Navigator.of(context).pop();
|
||
pushRouteReplacement(context, new SplashScreen()); // Запускаем регистрацию
|
||
});
|
||
});
|
||
}).catchError((error) {
|
||
print(error.toString());
|
||
});
|
||
} else {
|
||
Navigator.of(context).pop();
|
||
Navigator.of(context).pop();
|
||
}
|
||
};
|
||
|
||
showYesNoDialog(context, StringsLocalization.confirmation(), StringsLocalization.askChangeStore(), positiveCallback);
|
||
}
|
||
|
||
forceLogout(String token , BuildContext context) async {
|
||
|
||
deleteToken(token, 'ru').then((response) {
|
||
SqliteHelper helper = new SqliteHelper();
|
||
helper.open().then((_) {
|
||
helper.clear().then((_) {
|
||
helper.close().then((_) {
|
||
while (Navigator.of(context).canPop()) {
|
||
Navigator.of(context).pop();
|
||
}
|
||
pushRouteReplacement(context, new SplashScreen());
|
||
});
|
||
});
|
||
});
|
||
}).catchError((error) {
|
||
print(error.toString());
|
||
});
|
||
}
|
||
|
||
/// Запуск спецефичной для каждой платформы части приложения - сканера.
|
||
/// Может производиться с нескольких экранов (splash, finish_registration).
|
||
startScanner(BuildContext context, String app, SqliteHelper helper) async {
|
||
if (helper == null) {
|
||
helper = new SqliteHelper();
|
||
helper.open().then((_) {
|
||
startScanner(context, app, helper);
|
||
});
|
||
} else {
|
||
String token = await helper.getToken();
|
||
helper.close();
|
||
// Канал ловит вызовы методов из "нативной" части приложения.
|
||
// Могут быть вызваны либо logout либо faq, либо purchase.
|
||
if (token != null) {
|
||
platform.setMethodCallHandler((MethodCall call) async {
|
||
if (call.method == 'logout') {
|
||
forceLogout(token, context);
|
||
} else if (call.method == 'faq') {
|
||
faq(context, true);
|
||
} else if(call.method == 'settings') {
|
||
helper = new SqliteHelper();
|
||
helper.open().then((_) {
|
||
pushRoute(context, new SettingsScreen(helper, app, true));
|
||
});
|
||
} else {
|
||
String userString = call.arguments[0];
|
||
String card = call.arguments[1];
|
||
var route = new MaterialPageRoute<Null>(
|
||
builder: (BuildContext context) =>
|
||
new PurchaseScreen(
|
||
userString, card));
|
||
while (Navigator.of(context).canPop()) {
|
||
Navigator.of(context).pop();
|
||
}
|
||
Navigator.of(context).pushReplacement(route);
|
||
}
|
||
});
|
||
|
||
await platform.invokeMethod('startScanner', {
|
||
'token': token,
|
||
'url': url,
|
||
'appToken': appToken,
|
||
'color': Resources
|
||
.getPrimaryColor(app)
|
||
.value
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
// Запуск диалога с двумя кнопками
|
||
showYesNoDialog(BuildContext context, String title, String content, VoidCallback positiveCallback) {
|
||
showDialog(context: context, child: new AlertDialog(
|
||
title: new Text(title),
|
||
content: new Text(content),
|
||
actions: <Widget>[
|
||
new FlatButton(
|
||
child: new Text(StringsLocalization.no()),
|
||
onPressed: () {
|
||
Navigator.of(context).pop();
|
||
}
|
||
),
|
||
new FlatButton(
|
||
child: new Text(StringsLocalization.yes()),
|
||
onPressed: positiveCallback)]));
|
||
}
|
||
|
||
getCurrencyTitle(int code) {
|
||
switch(code) {
|
||
case 643: return StringsLocalization.nominativeRuble();
|
||
case 840: return StringsLocalization.nominativeDollar();
|
||
case 980: return StringsLocalization.nominativeHryvna();
|
||
case 978: return StringsLocalization.nominativeEuro();
|
||
case 398: return StringsLocalization.nominativeTenge();
|
||
}
|
||
}
|
||
|
||
getLocaleTitle(String code) {
|
||
switch(code) {
|
||
case 'ru': return 'Русский';
|
||
case 'en': return 'English';
|
||
case 'ua': return 'Український';
|
||
case 'es': return 'Español';
|
||
}
|
||
} |