229 lines
7.2 KiB
Dart
229 lines
7.2 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 'package:image_picker/image_picker.dart';
|
||
|
||
import 'db.dart';
|
||
import 'dart:convert';
|
||
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(SqliteHelper helper, String app, BuildContext context,
|
||
bool returnToScanner) {
|
||
pushRoute(context, new FAQScreen(helper, app, returnToScanner));
|
||
}
|
||
|
||
// В методе отправляется запрос на удаление токена кассы, очищаются SharedPreferences приложения.
|
||
logout(BuildContext context, SqliteHelper helper) async {
|
||
String token = await helper.getToken();
|
||
// String locale = await helper.getLocale();
|
||
|
||
VoidCallback positiveCallback = () {
|
||
if (token != null) {
|
||
getDeleteTokenRequest(token).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 {
|
||
getDeleteTokenRequest(token).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();
|
||
// Канал ловит вызовы методов из "нативной" части приложения.
|
||
// Могут быть вызваны либо exit либо faq, либо purchase.
|
||
if (token != null) {
|
||
platform.setMethodCallHandler((MethodCall call) async {
|
||
print('flutter handler');
|
||
print(call.method);
|
||
|
||
if (call.method == 'findUser') {
|
||
|
||
var userResponse;
|
||
String cardPhone = call.arguments[0];
|
||
|
||
try {
|
||
switch (call.arguments[1]) {
|
||
case 'card':
|
||
userResponse = await getUserByCard(cardPhone,token);
|
||
break;
|
||
case 'phone':
|
||
userResponse = await getUserByPhone(cardPhone,token);
|
||
break;
|
||
}
|
||
|
||
} catch (error) {
|
||
print(error.toString());
|
||
}
|
||
|
||
List<Map> users;
|
||
|
||
|
||
try{
|
||
users = JSON.decode(userResponse.body);
|
||
} catch (error) {
|
||
print(error);
|
||
}
|
||
|
||
if (users.length > 0) {
|
||
return users[0];
|
||
} else {
|
||
startScanner(context, app, helper);
|
||
throw new FlutterError("Users not found");
|
||
}
|
||
|
||
|
||
} else if (call.method == 'faq') {
|
||
faq(helper, app, context, true);
|
||
} else if (call.method == 'settings') {
|
||
pushRoute(context, new SettingsScreen(helper, app, true));
|
||
} else {
|
||
String userString;
|
||
|
||
if (call.arguments[0] is String) {
|
||
userString = call.arguments[0];
|
||
} else {
|
||
userString = JSON.encode(call.arguments[0]);
|
||
}
|
||
|
||
print(userString);
|
||
|
||
String card = call.arguments[1];
|
||
print('$userString, $card');
|
||
var route = new MaterialPageRoute<Null>(
|
||
builder: (BuildContext context) =>
|
||
new PurchaseScreen(helper, app, userString, card));
|
||
while (Navigator.of(context).canPop()) {
|
||
Navigator.of(context).pop();
|
||
}
|
||
Navigator.of(context).pushReplacement(route);
|
||
}
|
||
});
|
||
|
||
Map<String, String> args = StringsLocalization.strings;
|
||
args.addAll({
|
||
'token': token,
|
||
'url': await platform.invokeMethod('getEndpoint'),
|
||
'appToken': await platform.invokeMethod('getAppToken'),
|
||
'locale': StringsLocalization.localeCode,
|
||
'color': Resources.getPrimaryColor(app).value.toString()
|
||
});
|
||
|
||
platform.invokeMethod('startScanner', args);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Запуск диалога с двумя кнопками
|
||
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';
|
||
}
|
||
}
|
||
|
||
// Добавил вызов, что-бы AOT компилер не выкинул либу.
|
||
getImage() async {
|
||
return await ImagePicker.pickImage();
|
||
}
|