Вынес работу с сетью и общие методы в отдельные файлы
This commit is contained in:
163
lib/main.dart
163
lib/main.dart
@@ -1,169 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'splash.dart';
|
||||
import 'registration.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'purchase.dart';
|
||||
|
||||
/// Главный класс приложения.
|
||||
/// Здесь распоосложены константы и некоторые методы, которые могут вызываться с разных экранов приложения.
|
||||
|
||||
// Serious constants
|
||||
const String intUrl = 'https://pos-api-int.dinect.com/20130701/';
|
||||
const String intToken = '9fec83cdca38c357e6b65dbb17514cdd36bf2a08';
|
||||
|
||||
// Hints
|
||||
const String merchantIDHint = 'ID магазина';
|
||||
const String posIDHint = 'Номер кассы';
|
||||
|
||||
// Assets
|
||||
const String logo_png = 'assets/registration_logo.png';
|
||||
const String splash_png = 'assets/splash.png';
|
||||
const String logout_png = 'assets/logout.png';
|
||||
const String activate_token_bg_png = 'assets/activate_token_message_background.png';
|
||||
const String active_token_bg_png = 'assets/active_token_message_background.png';
|
||||
const String expansion_icon_png = 'assets/expansion_icon.png';
|
||||
const String powered_by_dinect_splash_png = 'assets/powered_by_dinect_splash.png';
|
||||
const String powered_by_dinect_png = 'assets/powered_by_dinect.png';
|
||||
// Colors
|
||||
const Color primaryColor = const Color(0xffeb0004);
|
||||
const Color greyTextColor = const Color(0xffa5a5a5);
|
||||
const Color textBorderColor = const Color(0xffcfd8dc);
|
||||
const Color tokenActiveTextColor = const Color(0xff1f5a1f);
|
||||
const Color tokenActivateTextColor = const Color(0xff4e3a19);
|
||||
const Color greenBackground = const Color(0xff8ae28a);
|
||||
const Color faqGrey = const Color(0xff5b5b5b);
|
||||
const Color faqTitlesColor = const Color(0xff404040);
|
||||
// Dimens
|
||||
const double verticalMargin = 28.0;
|
||||
const double buttonVerticalMargin = 42.0;
|
||||
const double buttonHeight = 48.0;
|
||||
const double iconHeight = 20.0;
|
||||
|
||||
const platform = const MethodChannel('com.dinect.checker/instance_id');
|
||||
|
||||
// HttpClient
|
||||
final httpClient = createHttpClient();
|
||||
|
||||
/// Токен кассы. Инициализируется при регистрации.
|
||||
String token;
|
||||
import 'consts.dart';
|
||||
|
||||
/// Точка входа в приложение.
|
||||
void main() {
|
||||
runApp(new Checker());
|
||||
}
|
||||
|
||||
/// Проверка статуса токена. Токен может быть активирован, либо не активирован.
|
||||
checkTokenStatus(BuildContext context) async {
|
||||
return httpClient.get(intUrl + 'tokens/' + token + '?_dmapptoken=' + intToken);
|
||||
}
|
||||
|
||||
createToken(String merchantId) async {
|
||||
|
||||
String url = intUrl + 'tokens/?_dmapptoken=' + intToken;
|
||||
|
||||
String posID = await platform.invokeMethod('getPosID');
|
||||
print('posID: ${posID}');
|
||||
|
||||
print('merchantId: ${merchantId}');
|
||||
|
||||
// Поле description - необязательное.
|
||||
var body = {
|
||||
'merchant_shop': merchantId,
|
||||
'pos': posID,
|
||||
};
|
||||
|
||||
return httpClient.post(url, body: body);
|
||||
}
|
||||
|
||||
/// Запуск спецефичной для каждой платформы части приложения - сканера.
|
||||
/// Может производиться с нескольких экранов (splash, finish_registration).
|
||||
startScanner(BuildContext context) async {
|
||||
|
||||
// Канал ловит вызовы методов из "нативной" части приложения.
|
||||
// Могут быть вызваны либо logaut либо faq, либо purchase.
|
||||
if (token != null) {
|
||||
platform.setMethodCallHandler((MethodCall call) async {
|
||||
|
||||
if (call.method == 'foo') {
|
||||
logout(context);
|
||||
} else {
|
||||
|
||||
var card = call.arguments;
|
||||
|
||||
print('card: ' + card);
|
||||
String url = 'http://pos-api-int.dinect.com/20130701/users/?auto=${card}';
|
||||
print('url: ' + url);
|
||||
|
||||
var headers = {
|
||||
'DM-Authorization': 'dmapptoken 9fec83cdca38c357e6b65dbb17514cdd36bf2a08',
|
||||
'Authorization': 'dmtoken ${token}'
|
||||
};
|
||||
|
||||
httpClient.get(url, headers: headers).then((response) {
|
||||
|
||||
print(response.body);
|
||||
|
||||
List usersList = JSON.decode(response.body);
|
||||
print('usersList.length: ${usersList.length}');
|
||||
if (usersList.length > 0) {
|
||||
pushRoute(context, new PurchaseScreen(usersList[0], card));
|
||||
}
|
||||
|
||||
}).catchError((error) {
|
||||
print(error.toString());
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
await platform.invokeMethod('startScanner');
|
||||
}
|
||||
|
||||
logout(BuildContext context) {
|
||||
showDialog(context: context, child: new AlertDialog(
|
||||
title: new Text('Подтверждение'),
|
||||
content: new Text('Вы действительно хотите выйти и ввести другой номер магазина?'),
|
||||
actions: <Widget>[
|
||||
new FlatButton(
|
||||
child: new Text('Нет'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
),
|
||||
new FlatButton(
|
||||
child: new Text('Да'),
|
||||
onPressed: () {
|
||||
if (token != null) {
|
||||
String url = intUrl + 'tokens/' + token + '?_dmapptoken=' + intToken;
|
||||
print(url);
|
||||
httpClient.delete(url).then((response) async {
|
||||
print(response.body);
|
||||
await platform.invokeMethod('removeKeys');
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pop();
|
||||
pushRoute(context, new RegistrationScreen());
|
||||
}).catchError((error) {
|
||||
print(error.toString());
|
||||
});
|
||||
} else {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
)]));
|
||||
|
||||
}
|
||||
|
||||
/// Навигация по приложению.
|
||||
/// widget - следующий экран приложения.
|
||||
pushRoute(BuildContext context, Widget widget) {
|
||||
var route = new MaterialPageRoute<Null>(builder: (BuildContext context) => widget);
|
||||
Navigator.of(context).pushReplacement(route);
|
||||
}
|
||||
|
||||
class Checker extends StatelessWidget {
|
||||
@override Widget build(BuildContext context) {
|
||||
return new MaterialApp(title: "AutoClub",
|
||||
@@ -173,6 +16,4 @@ class Checker extends StatelessWidget {
|
||||
accentColor: primaryColor
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user