return purchases screen
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:checker/resources.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -10,7 +11,6 @@ import 'package:checker/screens/faq.dart';
|
||||
import 'package:checker/strings.dart';
|
||||
import 'package:checker/db.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
abstract class BaseState<T extends StatefulWidget> extends State<T> {
|
||||
|
||||
@@ -78,7 +78,14 @@ abstract class BaseState<T extends StatefulWidget> extends State<T> {
|
||||
child: getMenuItem(help_png, StringsLocalization.help())
|
||||
));
|
||||
|
||||
if (Theme.of(context).platform != TargetPlatform.iOS) {
|
||||
if(showReturnScreen()) {
|
||||
menuItemList.add(new PopupMenuItem(
|
||||
value: 3,
|
||||
child: getMenuItem(exit_png, StringsLocalization.returnLabel())
|
||||
));
|
||||
}
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
menuItemList.add(new PopupMenuItem(
|
||||
value: 2,
|
||||
child: getMenuItem(exit_png, StringsLocalization.exit())
|
||||
@@ -95,6 +102,8 @@ abstract class BaseState<T extends StatefulWidget> extends State<T> {
|
||||
];
|
||||
}
|
||||
|
||||
bool showReturnScreen() => false;
|
||||
|
||||
void onOptionsItemClick(int index) {
|
||||
switch (index) {
|
||||
case 0: {
|
||||
@@ -119,9 +128,14 @@ abstract class BaseState<T extends StatefulWidget> extends State<T> {
|
||||
platform.invokeMethod('finish');
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
openReturnScreen();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void openReturnScreen() {}
|
||||
|
||||
/// Возвращает пункт меню (Картинка с текстом)
|
||||
Widget getMenuItem(String image, String text) {
|
||||
return new Row(children: [
|
||||
|
||||
@@ -106,3 +106,33 @@ getEndpoint() async {
|
||||
getToken() async {
|
||||
return await platform.invokeMethod('getAppToken');
|
||||
}
|
||||
|
||||
Future<Response> getUserPurchases(String token, int userId, int page) async {
|
||||
|
||||
var headers = {
|
||||
'DM-Authorization': 'dmapptoken ${await getToken()}',
|
||||
'Authorization': 'dmtoken $token',
|
||||
'Accept-Language': StringsLocalization.localeCode
|
||||
};
|
||||
|
||||
|
||||
var finalEndpoint = "${await getEndpoint()}users/$userId/purchases/?returned=false&page=$page";
|
||||
print(finalEndpoint);
|
||||
|
||||
return httpClient.get(finalEndpoint, headers: headers);
|
||||
}
|
||||
|
||||
Future<Response> returnPurchase(String token, int userId, int purchaseId) async {
|
||||
|
||||
var headers = {
|
||||
'DM-Authorization': 'dmapptoken ${await getToken()}',
|
||||
'Authorization': 'dmtoken $token',
|
||||
'Accept-Language': StringsLocalization.localeCode
|
||||
};
|
||||
|
||||
|
||||
var finalEndpoint = "${await getEndpoint()}users/$userId/purchases/$purchaseId";
|
||||
print(finalEndpoint);
|
||||
|
||||
return httpClient.delete(finalEndpoint, headers: headers);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:checker/base/base_screen.dart';
|
||||
import 'package:checker/db.dart';
|
||||
import 'package:checker/screens/return.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'dart:convert';
|
||||
@@ -127,6 +128,16 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
||||
return listView;
|
||||
}
|
||||
|
||||
bool showReturnScreen() => true;
|
||||
|
||||
void openReturnScreen() {
|
||||
Future.delayed(const Duration(milliseconds: 200), () {
|
||||
Route route = MaterialPageRoute<String>(builder: (BuildContext context) =>
|
||||
ReturnScreen(helper, app, user['id'], user['bonus']), fullscreenDialog: true);
|
||||
Navigator.push(context, route);
|
||||
});
|
||||
}
|
||||
|
||||
getBonusInputField() {
|
||||
var bonusTextField = new TextField(
|
||||
keyboardType: TextInputType.number,
|
||||
|
||||
186
lib/screens/return.dart
Normal file
186
lib/screens/return.dart
Normal file
@@ -0,0 +1,186 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:checker/base/base_screen.dart';
|
||||
import 'package:checker/base/base_state.dart';
|
||||
import 'package:checker/db.dart';
|
||||
import 'package:checker/strings.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:checker/network.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class ReturnScreen extends BaseScreen {
|
||||
final int userId;
|
||||
final int bonus;
|
||||
|
||||
ReturnScreen(SqliteHelper helper, String app, this.userId, this.bonus) : super(helper, app);
|
||||
|
||||
@override
|
||||
_ReturnScreenState createState() => _ReturnScreenState<ReturnScreen>(helper, app);
|
||||
}
|
||||
|
||||
class _ReturnScreenState<T> extends BaseState<ReturnScreen> {
|
||||
static DateFormat dateFormat = DateFormat('d MMM yy');
|
||||
|
||||
ScrollController _controller;
|
||||
List _purchases = [];
|
||||
int _bonus;
|
||||
int _page = 1;
|
||||
bool _fullLoaded = false;
|
||||
bool _loading = false;
|
||||
|
||||
|
||||
_ReturnScreenState(SqliteHelper helper, String app) : super(helper, app);
|
||||
|
||||
bool isAutomaticallyImplyLeading() => true;
|
||||
|
||||
@override
|
||||
Widget getScreenContent() => ListView.separated(
|
||||
itemBuilder: (context, index) {
|
||||
final TextStyle style = Theme.of(context).textTheme.subhead;
|
||||
if(index > 0) {
|
||||
final item = _purchases[index - 1];
|
||||
final DateTime date = DateTime.parse(item['date']);
|
||||
final String formattedDate = dateFormat.format(date);
|
||||
return GestureDetector(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Text(formattedDate, style: style),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(item['sum_total'], style: style),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(item['sum_bonus'].toString(), style: style),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
onTap: () => _confirmReturnPurchase(item, formattedDate),
|
||||
);
|
||||
} else {
|
||||
final TextStyle headerStyle = style.copyWith(color: Colors.grey);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Container(),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(StringsLocalization.sum(), style: headerStyle),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(StringsLocalization.points(), style: headerStyle),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
separatorBuilder: (_, index) => index == 0
|
||||
? Container()
|
||||
: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Divider(height: 1,),
|
||||
),
|
||||
itemCount: _purchases.length > 0 ? _purchases.length + 1 : _purchases.length,
|
||||
controller: _controller);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => getMainWidget();
|
||||
|
||||
@override
|
||||
String getTitle() => StringsLocalization.returnLabel();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
setState(() {
|
||||
loading = true;
|
||||
});
|
||||
_loadUserPurchases();
|
||||
_bonus = widget.bonus;
|
||||
_controller = ScrollController();
|
||||
_controller.addListener(_scrollListener);
|
||||
}
|
||||
|
||||
void _scrollListener() {
|
||||
if (_controller.offset >= _controller.position.maxScrollExtent &&
|
||||
!_controller.position.outOfRange) {
|
||||
_loadUserPurchases();
|
||||
}
|
||||
}
|
||||
|
||||
void _loadUserPurchases() async {
|
||||
if(!_fullLoaded && !_loading) {
|
||||
_loading = true;
|
||||
setState(() => loading = true);
|
||||
String token = await helper.getToken();
|
||||
Response response = await getUserPurchases(token, widget.userId, _page);
|
||||
final responseJson = json.decode(response.body);
|
||||
List purchases = responseJson['results'];
|
||||
_fullLoaded = _page == responseJson['pages'];
|
||||
setState(() {
|
||||
loading = false;
|
||||
_purchases.addAll(purchases);
|
||||
});
|
||||
_page++;
|
||||
_loading = false;
|
||||
setState(() => loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
void _confirmReturnPurchase(Map purchase, String formattedDate) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) =>
|
||||
AlertDialog(
|
||||
title: Text(StringsLocalization.confirmation()),
|
||||
content: Text(StringsLocalization.returnConfirmation(
|
||||
purchase['sum_total'], formattedDate, _bonus + purchase['sum_bonus'])),
|
||||
actions: <Widget>[
|
||||
FlatButton(
|
||||
child: Text(StringsLocalization.no()),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
FlatButton(
|
||||
child: Text(StringsLocalization.yes()),
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
_returnPurchase(purchase, formattedDate);
|
||||
}
|
||||
)
|
||||
],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void _returnPurchase(Map purchase, String formattedDate) async {
|
||||
String token = await helper.getToken();
|
||||
final Response response = await returnPurchase(token, widget.userId, purchase['id']);
|
||||
if(response.statusCode == 204) {
|
||||
_bonus += purchase['sum_bonus'];
|
||||
setState(() {
|
||||
_purchases.removeWhere((p) => p['id'] == purchase['id']);
|
||||
});
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) =>
|
||||
AlertDialog(
|
||||
title: Text(StringsLocalization.returnConfirmed()),
|
||||
content: Text(StringsLocalization.returnConfirmedContent(purchase['sum_total'], formattedDate)),
|
||||
actions: <Widget>[
|
||||
FlatButton(
|
||||
child: Text(StringsLocalization.yes()),
|
||||
onPressed: () => Navigator.pop(context)
|
||||
)
|
||||
],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,6 +125,11 @@ class StringsLocalization {
|
||||
String trimmedVal = val.substring(0, val.length - 3);
|
||||
return sprintf(strings['payment_complite'], [val, declineCurrency(int.parse(trimmedVal), code)]);
|
||||
}
|
||||
static String returnConfirmation(String sum, String date, int points) =>
|
||||
sprintf(strings['return_purchase_content'], [sum, date, points.toString()]);
|
||||
|
||||
static String returnConfirmedContent(String sum, String date) =>
|
||||
sprintf(strings['return_confirmation_content'], [sum, date]);
|
||||
|
||||
static String registration() => strings['registration'];
|
||||
static String usage() => strings['usage'];
|
||||
@@ -218,4 +223,7 @@ class StringsLocalization {
|
||||
static String joysMinus() => strings['joys_minus'];
|
||||
static String joysHint() => strings['joys_hint'];
|
||||
static String phone() => strings['phone'];
|
||||
static String returnLabel() => strings['returnLabel'];
|
||||
static String returnConfirmed() => strings['return_confirmation'];
|
||||
static String points() => strings['points'];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user