From 946d2ada41f239df88635270b8aa3cea8fa3e670 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Mon, 24 Jun 2024 14:08:02 +0300 Subject: [PATCH] fix: some fixes in example app --- lib/interfaces/items.dart | 57 ++++++++++--------- lib/mobile_example.dart | 92 +++++++++++++++++-------------- lib/pages/order_confirmation.dart | 1 + lib/tools/history.dart | 7 ++- web/js/demo-js-interop.js | 10 +++- web/payment.html | 51 ----------------- 6 files changed, 95 insertions(+), 123 deletions(-) delete mode 100644 web/payment.html diff --git a/lib/interfaces/items.dart b/lib/interfaces/items.dart index 7607088..e58edf6 100644 --- a/lib/interfaces/items.dart +++ b/lib/interfaces/items.dart @@ -166,13 +166,14 @@ class GymHistoryItem { final String date; final String sum; final String photo; + final String timestamp; - GymHistoryItem({ - required this.id, - required this.date, - required this.sum, - required this.photo, - }); + GymHistoryItem( + {required this.id, + required this.date, + required this.sum, + required this.photo, + required this.timestamp}); factory GymHistoryItem.fromRawJson(String str) => GymHistoryItem.fromJson(json.decode(str)); @@ -184,12 +185,14 @@ class GymHistoryItem { date: json["date"], sum: json["sum"], photo: json["photo"], + timestamp: json["timestamp"], ); Map toJson() => { "id": id, "date": date, "sum": sum, + "timestamp": timestamp, "photo": photo, }; } @@ -201,19 +204,20 @@ class GymHistoryItemDetail { String? payUrl; final String receiver; final String email; + final String timestamp; final String address; final List providers; - GymHistoryItemDetail({ - required this.id, - required this.date, - required this.sum, - this.payUrl, - required this.providers, - required this.receiver, - required this.email, - required this.address, - }); + GymHistoryItemDetail( + {required this.id, + required this.date, + required this.sum, + this.payUrl, + required this.providers, + required this.receiver, + required this.email, + required this.address, + required this.timestamp}); factory GymHistoryItemDetail.fromRawJson(String str) => GymHistoryItemDetail.fromJson(json.decode(str)); @@ -222,16 +226,16 @@ class GymHistoryItemDetail { factory GymHistoryItemDetail.fromJson(Map json) => GymHistoryItemDetail( - id: json["id"], - date: json["date"], - sum: json["sum"], - receiver: json["receiver"], - email: json["email"], - address: json["address"], - payUrl: json["pay_url"] as String?, - providers: List.from(json["providers"] - .map((x) => GymHistoryItemDetailProvider.fromJson(x))), - ); + id: json["id"], + date: json["date"], + sum: json["sum"], + receiver: json["receiver"], + email: json["email"], + address: json["address"], + payUrl: json["pay_url"] as String?, + providers: List.from(json["providers"] + .map((x) => GymHistoryItemDetailProvider.fromJson(x))), + timestamp: json["timestamp"]); Map toJson() => { "id": id, @@ -241,6 +245,7 @@ class GymHistoryItemDetail { "providers": List.from(providers.map((x) => x.toJson())), "receiver": receiver, "email": email, + "timestamp": timestamp, "address": address, }; } diff --git a/lib/mobile_example.dart b/lib/mobile_example.dart index 077dc3c..7d0c2a5 100644 --- a/lib/mobile_example.dart +++ b/lib/mobile_example.dart @@ -53,32 +53,49 @@ Widget getDrawer(BuildContext context) => Drawer( children: [ const DrawerHeader(child: Text('Drawer Header')), ListTile( - leading: const Icon(Icons.home), - title: const Text('Home'), - onTap: () => Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const ExampleMainPage(), - ), - ), - ), - ListTile( - leading: const Icon(Icons.sell), - title: const Text('Club 2'), - onTap: () => Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => ChangeNotifierProvider( - create: (_) => GymLinkProvider(), - child: Consumer( - builder: (_, value, __) => const ExampleClub2Page(), + leading: const Icon(Icons.home), + title: const Text('Home'), + onTap: () { + Future.microtask(() async { + final prefs = await SharedPreferences.getInstance(); + prefs.remove('token'); + prefs.remove('history'); + prefs.remove('cart'); + prefs.remove('detail_history'); + }); + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (context) => const ExampleMainPage(), ), - ), - ), - ), - ), + ); + }), + ListTile( + leading: const Icon(Icons.sell), + title: const Text('Club 2'), + onTap: () { + Future.microtask(() async { + final prefs = await SharedPreferences.getInstance(); + prefs.remove('token'); + prefs.remove('history'); + prefs.remove('cart'); + prefs.remove('detail_history'); + }); + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (context) => ChangeNotifierProvider( + create: (_) => GymLinkProvider(), + child: Consumer( + builder: (_, value, __) => const ExampleClub2Page(), + ), + ), + ), + ); + }), ListTile( leading: const Icon(Icons.search), title: const Text('Example page'), - onTap: () => Navigator.of(context).push(MaterialPageRoute( + onTap: () => + Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => const ExampleSecondPage(), )), ), @@ -143,26 +160,25 @@ class _ExamplePageState extends State { ), resizeToAvoidBottomInset: false, drawer: getDrawer(context), - body: const Column( + body: Column( children: [ - Text('test'), - Expanded( + IconButton( + icon: const Icon(Icons.colorize), + onPressed: () { + context.read().changeTheme( + Random().nextInt(0xffffff + 1), + blackTheme: Random().nextBool()); + }, + ), + const Expanded( child: MyApp(), ), - SizedBox( + const SizedBox( height: 20, ), - Text('Bottom text') + const Text('Bottom text') ], ), - floatingActionButton: IconButton( - icon: const Icon(Icons.search), - onPressed: () { - context.read().changeTheme( - Random().nextInt(0xffffff + 1), - blackTheme: Random().nextBool()); - }, - ), ); } } @@ -231,12 +247,6 @@ class _ExampleClub2PageState extends State { Text('Bottom text') ], ), - floatingActionButton: IconButton( - icon: const Icon(Icons.search), - onPressed: () { - // context.read().changeTheme(0xFFAABCAB); - }, - ), ); } } diff --git a/lib/pages/order_confirmation.dart b/lib/pages/order_confirmation.dart index 56d3a55..b8b38f6 100644 --- a/lib/pages/order_confirmation.dart +++ b/lib/pages/order_confirmation.dart @@ -118,6 +118,7 @@ class _OrderConfirmationPageState extends State { sum: totalPrice.toString(), date: '', providers: providers, + timestamp: DateTime.now().millisecondsSinceEpoch.toString(), ); await addToHistory(order); } diff --git a/lib/tools/history.dart b/lib/tools/history.dart index 9f6f168..7e74598 100644 --- a/lib/tools/history.dart +++ b/lib/tools/history.dart @@ -11,8 +11,7 @@ Future> getHistory() async { for (var historyItem in jsonDecode(historyString) as List) { history.add(GymHistoryItem.fromJson(historyItem)); } - history.sort((a, b) => b.id.compareTo(a.id)); - history = history.reversed.toList(); + history.sort((a, b) => b.timestamp.compareTo(a.timestamp)); return history; } @@ -50,7 +49,8 @@ Future addToHistory(GymHistoryItemDetail item) async { "receiver": item.receiver, "email": item.email, "address": item.address, - "providers": providers + "providers": providers, + "timestamp": DateTime.now().millisecondsSinceEpoch.toString(), }; final detailHistoryItem = GymHistoryItemDetail.fromJson(json); detailHistory.add(detailHistoryItem); @@ -59,6 +59,7 @@ Future addToHistory(GymHistoryItemDetail item) async { id: detailHistoryItem.id, photo: detailHistoryItem.providers[0].items[0].photo, sum: detailHistoryItem.sum, + timestamp: detailHistoryItem.timestamp, )); prefs.setString('history', jsonEncode(history)); prefs.setString('detail_history', jsonEncode(detailHistory)); diff --git a/web/js/demo-js-interop.js b/web/js/demo-js-interop.js index fd87528..82177f9 100644 --- a/web/js/demo-js-interop.js +++ b/web/js/demo-js-interop.js @@ -31,10 +31,16 @@ } const btn = document.getElementById('token'); - btn.addEventListener('click', () => getToken('eeb42dcb-8e5b-4f21-825a-3fc7ada43445')); + btn.addEventListener('click', () => { + localStorage.clear(); + getToken('eeb42dcb-8e5b-4f21-825a-3fc7ada43445'); + }); const btn2 = document.getElementById('token2'); - btn2.addEventListener('click', () => getToken('a8622a61-3142-487e-8db8-b6aebd4f04aa')); + btn2.addEventListener('click', () => { + localStorage.clear(); + getToken('a8622a61-3142-487e-8db8-b6aebd4f04aa'); + }); let colorChangeBtnRed = document.getElementById('colorChangeBtnRed'); colorChangeBtnRed.addEventListener('click', function () { diff --git a/web/payment.html b/web/payment.html deleted file mode 100644 index 402b097..0000000 --- a/web/payment.html +++ /dev/null @@ -1,51 +0,0 @@ - - - - - Прием платежа с помощью виджета ЮKassa - - - - - - Ниже отобразится платежная форма. Если вы еще не создавали платеж и не передавали токен для инициализации виджета, появится сообщение об ошибке. - - -
- - Данные банковской карты для оплаты в тестовом магазине: - - - номер — 5555 5555 5555 4477 - - срок действия — 01/30 (или другая дата, больше текущей) - - CVC — 123 (или три любые цифры) - - код для прохождения 3-D Secure — 123 (или три любые цифры) - - Другие тестовые банковские карты - - - -