From b13b385cb75b5a2336a86f3facb5cf0534d40612 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 7 Oct 2024 21:48:48 +0200 Subject: [PATCH] update --- android/app/build.gradle | 3 +- android/app/src/main/AndroidManifest.xml | 3 +- lib/api.dart | 63 ++++++++++-------------- lib/main.dart | 48 +++++++++++++----- lib/pages/flow/end_flow_page.dart | 2 - lib/pages/stats.dart | 1 + lib/pages/supply.dart | 15 ++++-- lib/pages/transaction.dart | 9 ++-- pubspec.lock | 8 +-- pubspec.yaml | 2 +- 10 files changed, 84 insertions(+), 70 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 29d8a3d..9c1dbee 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -41,8 +41,7 @@ android { } defaultConfig { - // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "com.example.cdb_ui" + applicationId "de.hydrar.red.cdb" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. minSdkVersion flutter.minSdkVersion diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index cf5af51..b1ca99c 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + init() async { pref = await SharedPreferences.getInstance(); - instance = pref.getString("instance") ?? ""; + instance = pref!.getString("instance") ?? ""; } bool isInit() { - return pref.containsKey("token") && pref.containsKey("instance"); + if (pref == null) { + return false; + } + + return pref!.containsKey("token") && pref!.containsKey("instance"); + } + + bool isPrefetched() { + return items != null && locations != null && flowInfos != null; } void save(String instance, String token) { - pref.setString("instance", instance); - pref.setString("token", token); + pref!.setString("instance", instance); + pref!.setString("token", token); this.instance = instance; } @@ -60,7 +68,7 @@ class API { var resp = await http.get(Uri.parse(url), headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json; charset=UTF-8', - 'Token': pref.getString("token")! + 'Token': pref!.getString("token")! }); return utf8.decode(resp.bodyBytes); @@ -71,7 +79,7 @@ class API { headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json; charset=UTF-8', - 'Token': pref.getString("token")! + 'Token': pref!.getString("token")! }, body: jsonEncode(data)); @@ -163,7 +171,7 @@ class API { } // /supply - Future supplyItem(String item, String variant, String price, + Future supplyItem(String item, String variant, double price, String? origin, String? location, String? note) async { if (origin!.isEmpty) { origin = null; @@ -214,7 +222,7 @@ class API { } // /item///price_history? - Future> getPriceHistory(String item, String variant, + Future> getPriceHistory(String item, String variant, {String? origin}) async { var url = "$instance/item/$item/$variant/price_history"; @@ -224,10 +232,10 @@ class API { var resp = jsonDecode(await getRequest(url)) as List; - return resp.map((x) => Price(x)).toList(); + return resp.map((x) => x as double).toList(); } - Future getLatestPrice(String item, String variant, + Future getLatestPrice(String item, String variant, {String? origin}) async { var url = "$instance/item/$item/$variant/price_latest"; @@ -237,7 +245,7 @@ class API { var resp = jsonDecode(await getRequest(url)) as Map; - return Price(resp); + return resp as double; } // Flows @@ -386,31 +394,11 @@ class ItemVariant { } } -class Price { - late double value; - late String currency; - - Price(Map json) { - value = json["value"]; - currency = json["currency"]; - } - - Price.fromString(String value) { - var priceSplit = value.split(" "); - this.value = double.parse(priceSplit[0]); - currency = priceSplit[1]; - } - - String format() { - return "${value.toStringAsFixed(2)} $currency"; - } -} - class Transaction { late String uuid; late String item; late String variant; - late Price price; + late double price; String? origin; late int timestamp; ConsumeInfo? consumed; @@ -422,7 +410,7 @@ class Transaction { uuid = json["uuid"]; item = json["item"]; variant = json["variant"]; - price = Price(json["price"]); + price = json["price"]; origin = json["origin"]; timestamp = json["timestamp"]; expired = json["expired"]; @@ -431,12 +419,11 @@ class Transaction { location = json["location"] != null ? Location(json["location"]) : null; } - Transaction.inMemory(String itemID, String variantID, String price, + Transaction.inMemory(String itemID, String variantID, this.price, String? origin, Location? location, String? note) { uuid = ""; item = itemID; variant = variantID; - this.price = Price.fromString(price); origin = origin; timestamp = 0; consumed = null; @@ -448,12 +435,12 @@ class Transaction { class ConsumeInfo { late String destination; - late Price price; + late double price; late int timestamp; ConsumeInfo(Map json) { destination = json["destination"]; - price = Price(json["price"]); + price = json["price"]; timestamp = json["timestamp"]; } } diff --git a/lib/main.dart b/lib/main.dart index 1fb0955..a194491 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,27 +7,49 @@ import 'package:cdb_ui/pages/stats.dart'; import 'package:flutter/material.dart'; Future main() async { - await API().init(); - if (API().isInit()) { - await API().prefetch(); - } runApp(const MyApp()); } -class MyApp extends StatelessWidget { +class MyApp extends StatefulWidget { const MyApp({super.key}); + @override + State createState() => _MyAppState(); +} + +class _MyAppState extends State { + bool init = false; + + @override + void initState() { + super.initState(); + () async { + await API().init(); + if (API().isInit()) { + await API().prefetch(); + setState(() { + init = true; + }); + } + }(); + } + @override Widget build(BuildContext context) { return MaterialApp( - title: 'CDB', - theme: ThemeData( - colorScheme: ColorScheme.fromSeed( - seedColor: Colors.deepPurple, brightness: Brightness.dark), - useMaterial3: true, - ), - home: API().isInit() ? const MyHomePage() : const SetupPage(), - ); + title: 'CDB', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed( + seedColor: Colors.deepPurple, brightness: Brightness.dark), + useMaterial3: true, + ), + home: API().isInit() + ? (API().isPrefetched() + ? const MyHomePage() + : const Scaffold( + body: CircularProgressIndicator(), + )) + : const SetupPage()); } } diff --git a/lib/pages/flow/end_flow_page.dart b/lib/pages/flow/end_flow_page.dart index cf4061b..32f233d 100644 --- a/lib/pages/flow/end_flow_page.dart +++ b/lib/pages/flow/end_flow_page.dart @@ -1,5 +1,3 @@ -import 'dart:js_interop_unsafe'; - import 'package:cdb_ui/api.dart' as API; import 'package:cdb_ui/api.dart'; import 'package:cdb_ui/pages/supply.dart'; diff --git a/lib/pages/stats.dart b/lib/pages/stats.dart index e50b52e..91ddd66 100644 --- a/lib/pages/stats.dart +++ b/lib/pages/stats.dart @@ -24,6 +24,7 @@ class StatsPage extends StatelessWidget { // global origin / destinations return Scaffold( + appBar: AppBar(title: Text("Home"),), body: FutureBuilder( future: _fetchData(), builder: (context, snapshot) { diff --git a/lib/pages/supply.dart b/lib/pages/supply.dart index 18c3bb3..bb3dad3 100644 --- a/lib/pages/supply.dart +++ b/lib/pages/supply.dart @@ -53,7 +53,7 @@ class _SupplyPageState extends State { var t = SupplyForm( itemID: widget.item.id, variant: variant, - price: "${_priceController.text} €", + price: double.parse(_priceController.text), origin: _selectedOrigin, location: _selectedLocation, note: _noteController.text); @@ -66,8 +66,13 @@ class _SupplyPageState extends State { } API() - .supplyItem(widget.item.id, variant, "${_priceController.text} €", - _selectedOrigin, _selectedLocation, _noteController.text) + .supplyItem( + widget.item.id, + variant, + double.parse(_priceController.text), + _selectedOrigin, + _selectedLocation, + _noteController.text) .then((_) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Item added successfully!')), @@ -138,7 +143,7 @@ class _SupplyPageState extends State { ? await API() .getLatestPrice(widget.item.id, variant, origin: selection) - .then((x) => x.value.toStringAsFixed(2)) + .then((x) => x.toStringAsFixed(2)) : _priceController.text; setState(() { _priceController.text = price; @@ -293,7 +298,7 @@ class AutocompletedTextField extends StatelessWidget { class SupplyForm { final String itemID; final String variant; - final String price; + final double price; final String? origin; final String? location; final String note; diff --git a/lib/pages/transaction.dart b/lib/pages/transaction.dart index 1de2095..7faee85 100644 --- a/lib/pages/transaction.dart +++ b/lib/pages/transaction.dart @@ -165,7 +165,7 @@ class _TransactionPageState extends State { if (transaction.expired) const Text("Transaction is Expired!"), - IconText(Icons.money, transaction.price.format(), + IconText(Icons.money, transaction.price.toStringAsFixed(2), color: Colors.green), if (transaction.origin != null) @@ -181,7 +181,8 @@ class _TransactionPageState extends State { Text("Consumed on: ${tsFormat(transaction.consumed!.timestamp)}"), IconText(Icons.store, transaction.consumed!.destination, color: Colors.blue), - IconText(Icons.money, transaction.consumed!.price.format(), + IconText( + Icons.money, transaction.consumed!.price.toStringAsFixed(2), color: Colors.green), ] @@ -286,8 +287,7 @@ class TransactionCard extends StatelessWidget { const SizedBox( height: 10, ), - IconText(Icons.money, - "${t.price.value.toStringAsFixed(2)} ${t.price.currency}", + IconText(Icons.money, "${t.price.toStringAsFixed(2)} €", color: Colors.green), if (t.origin != null) ...[ const SizedBox(height: 8), @@ -328,6 +328,7 @@ class IconText extends StatelessWidget { Text( text, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), + overflow: TextOverflow.fade, ), ], ); diff --git a/pubspec.lock b/pubspec.lock index 8ec77fd..8626fab 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -124,10 +124,10 @@ packages: dependency: "direct main" description: name: http - sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938" + sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.2.2" http_parser: dependency: transitive description: @@ -417,10 +417,10 @@ packages: dependency: transitive description: name: web - sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27" + sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb url: "https://pub.dev" source: hosted - version: "0.5.1" + version: "1.1.0" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c19fe54..7370483 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: cdb_ui -description: A new Flutter project. +description: Economic Database. # The following line prevents the package from being accidentally published to # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev