203 lines
5.8 KiB
Dart
203 lines
5.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:checker/base/base_screen.dart';
|
|
import 'package:checker/db.dart';
|
|
import 'package:checker/screens/purchase.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:http/http.dart';
|
|
import 'dart:core';
|
|
|
|
import 'package:checker/resources.dart';
|
|
import 'package:checker/strings.dart';
|
|
import 'package:checker/common.dart';
|
|
import 'package:checker/consts.dart';
|
|
import 'package:checker/base/base_state.dart';
|
|
import 'package:checker/network.dart';
|
|
|
|
/// Экран ввода суммы покупки
|
|
class PurchaseSumScreen extends BaseScreen {
|
|
PurchaseSumScreen(helper, app, this.token) : super(helper, app);
|
|
|
|
final String token;
|
|
|
|
@override
|
|
State createState() =>
|
|
new PurchaseSumScreenState<PurchaseSumScreen>(helper, app);
|
|
}
|
|
|
|
class PurchaseSumScreenState<T> extends BaseState<PurchaseSumScreen> {
|
|
TextEditingController _sumController = new TextEditingController();
|
|
|
|
bool isAutomaticallyImplyLeading() => false;
|
|
|
|
PurchaseSumScreenState(SqliteHelper helper, String app) : super(helper, app);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_subscribe();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext ctx) {
|
|
return getMainWidget();
|
|
}
|
|
|
|
@override
|
|
Widget getScreenContent() => Column(
|
|
children: <Widget>[
|
|
getHintLabel(),
|
|
getInputField(),
|
|
wrapButton(
|
|
_getScreenMargins(24.0),
|
|
getScanButton(context, StringsLocalization.scan(),
|
|
Resources.getPrimaryColor(app)))
|
|
],
|
|
);
|
|
|
|
EdgeInsets _getScreenMargins(double top) {
|
|
double side = 42.0;
|
|
return new EdgeInsets.only(top: top, left: side, right: side);
|
|
}
|
|
|
|
Widget getScanButton(BuildContext context, String title, Color textColor) {
|
|
return new Container(
|
|
height: buttonHeight,
|
|
child: new FlatButton(
|
|
child: new Text(title, style: new TextStyle(color: textColor)),
|
|
onPressed: () => _startScanner()),
|
|
decoration: new BoxDecoration(
|
|
border: new Border.all(
|
|
color: Resources.getButtonColor(app), width: 1.0),
|
|
borderRadius: new BorderRadius.all(new Radius.circular(4.0))));
|
|
}
|
|
|
|
void _startScanner() {
|
|
platform.invokeMethod('getEndpoint').then((url) {
|
|
platform.invokeMethod('getAppToken').then((appToken) {
|
|
Map<String, String> args = StringsLocalization.strings;
|
|
args['token'] = widget.token;
|
|
args['url'] = url;
|
|
args['appToken'] = appToken;
|
|
args['localeCode'] = StringsLocalization.localeCode;
|
|
args['color'] = Resources.getPrimaryColor(app).value.toString();
|
|
platform.invokeMethod('startScanner', args).then((result) {
|
|
_processResult(result);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
String getTitle() {
|
|
return StringsLocalization.sum();
|
|
}
|
|
|
|
@override
|
|
getHintString() {
|
|
return StringsLocalization.sum();
|
|
}
|
|
|
|
@override
|
|
getTextWidget() {
|
|
return new TextField(
|
|
keyboardType: TextInputType.number,
|
|
decoration: new InputDecoration.collapsed(
|
|
hintText: getHintString(),
|
|
hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0)),
|
|
controller: _sumController,
|
|
onSubmitted: (String text) {
|
|
setState(() {
|
|
_sumController.text = _parseSum(text);
|
|
});
|
|
},
|
|
textAlign: TextAlign.center);
|
|
}
|
|
|
|
String _cleanupNumber(String text) {
|
|
String tmp = text
|
|
.replaceAll(' ', '')
|
|
.replaceAll('-', '')
|
|
.replaceAll(',', '.')
|
|
.replaceAll('..', '.');
|
|
|
|
while (tmp.indexOf('..') != -1) {
|
|
tmp = tmp.replaceAll('..', '.');
|
|
}
|
|
return tmp;
|
|
}
|
|
|
|
String _parseSum(String input) {
|
|
num sumTotal = 0.0;
|
|
String text = _cleanupNumber(input);
|
|
|
|
try {
|
|
sumTotal = num.parse(text);
|
|
} catch (exception) {
|
|
print(exception);
|
|
try {
|
|
int idx = text.indexOf('.');
|
|
String integerPart = text.substring(0, idx);
|
|
String fractionalPart = text.substring(idx + 1, text.length);
|
|
if (fractionalPart.length > 2) {
|
|
fractionalPart = fractionalPart.substring(0, 2);
|
|
}
|
|
return '$integerPart.$fractionalPart';
|
|
} catch (exception) {
|
|
print(exception);
|
|
}
|
|
}
|
|
print(sumTotal.toStringAsFixed(2));
|
|
return sumTotal.toStringAsFixed(2);
|
|
}
|
|
|
|
void _processResult(result) {
|
|
if (result is List) {
|
|
final String user = result[0] as String;
|
|
final String card = result[1] as String;
|
|
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
|
|
String sum = _parseSum(_sumController.text);
|
|
_sumController.text = "";
|
|
return PurchaseScreen(helper, app, user, card, sum);
|
|
}));
|
|
}
|
|
}
|
|
|
|
void _subscribe() async {
|
|
platform.setMethodCallHandler((MethodCall call) async {
|
|
if (call.method == 'findUser') {
|
|
try {
|
|
Response userResponse;
|
|
|
|
switch (call.arguments[1]) {
|
|
case 'card':
|
|
userResponse = await getUserByCard(call.arguments[0], widget.token);
|
|
break;
|
|
case 'phone':
|
|
userResponse = await getUserByPhone(call.arguments[0], widget.token);
|
|
break;
|
|
}
|
|
|
|
if (userResponse != null) {
|
|
print('I have user in method handler!');
|
|
List<dynamic> users = json.decode(userResponse.body);
|
|
if (users.length > 0) {
|
|
return json.encode(users[0]);
|
|
} else {
|
|
throw new FlutterError("Users not found");
|
|
}
|
|
} else {
|
|
throw new FlutterError("Users not found");
|
|
}
|
|
} catch (error) {
|
|
print(error.toString());
|
|
throw new FlutterError("Users not found");
|
|
}
|
|
} else if (call.method == 'scanSuccess') {
|
|
_processResult(call.arguments);
|
|
}
|
|
});
|
|
}
|
|
}
|