fix async nav + refactor

This commit is contained in:
JMARyA 2024-09-16 09:42:55 +02:00
parent 689fa13a7e
commit bd054b0a9f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 52 additions and 44 deletions

View file

@ -253,9 +253,9 @@ class API {
return resp.map((x) => MinItem(x)).toList(); return resp.map((x) => MinItem(x)).toList();
} }
Future<void> moveTransaction(String id, String new_location) async { Future<void> moveTransaction(String id, String newLocation) async {
jsonDecode(await postRequest( jsonDecode(await postRequest(
"$instance/transaction/$id/move", {"to": new_location})); "$instance/transaction/$id/move", {"to": newLocation}));
} }
Future<Location> getLocation(String id) async { Future<Location> getLocation(String id) async {
@ -360,11 +360,11 @@ class ConsumeInfo {
class ItemVariantStat { class ItemVariantStat {
late int amount; late int amount;
late double total_price; late double totalPrice;
ItemVariantStat(Map<String, dynamic> json) { ItemVariantStat(Map<String, dynamic> json) {
amount = json["amount"]; amount = json["amount"];
total_price = json["total_price"]; totalPrice = json["total_price"];
} }
} }
@ -388,11 +388,11 @@ class Location {
conditions = json["conditions"] != null ? (json["conditions"]) : null; conditions = json["conditions"] != null ? (json["conditions"]) : null;
} }
String full_name_path(Map<String, Location> locations) { String fullNamePath(Map<String, Location> locations) {
var name = this.name; var name = this.name;
if (parent != null) { if (parent != null) {
name = "${locations[parent!]!.full_name_path(locations)} / $name"; name = "${locations[parent!]!.fullNamePath(locations)} / $name";
} }
return name; return name;
@ -408,10 +408,10 @@ class LocationCondition {
} }
class MinItem { class MinItem {
late String item_variant; late String itemVariant;
late int need; late int need;
MinItem(Map<String, dynamic> json) { MinItem(Map<String, dynamic> json) {
item_variant = json["item_variant"]; itemVariant = json["item_variant"];
need = json["need"]; need = json["need"];
} }
} }

View file

@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
seedColor: Colors.deepPurple, brightness: Brightness.dark), seedColor: Colors.deepPurple, brightness: Brightness.dark),
useMaterial3: true, useMaterial3: true,
), ),
home: API().isInit() ? MyHomePage() : SetupPage(), home: API().isInit() ? const MyHomePage() : const SetupPage(),
); );
} }
} }
@ -41,7 +41,7 @@ class StatsPage extends StatelessWidget {
future: _fetchData(), future: _fetchData(),
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData) { if (!snapshot.hasData) {
return CircularProgressIndicator(); return const CircularProgressIndicator();
} }
var data = snapshot.data!; var data = snapshot.data!;
@ -50,15 +50,17 @@ class StatsPage extends StatelessWidget {
return Column( return Column(
children: [ children: [
if (min.isNotEmpty) ListTile(title: Text("Items under Minimum")), if (min.isNotEmpty)
const ListTile(title: Text("Items under Minimum")),
...min.map((item) { ...min.map((item) {
return ListTile( return ListTile(
title: Text( title: Text(
"Item ${item.item_variant} under minimum. Needs ${item.need} more."), "Item ${item.itemVariant} under minimum. Needs ${item.need} more."),
); );
}).toList(), }).toList(),
if (expired.isNotEmpty) ListTile(title: Text("Expired Items")), if (expired.isNotEmpty)
const ListTile(title: Text("Expired Items")),
// Mapping expired list to widgets // Mapping expired list to widgets
...expired.map((item) { ...expired.map((item) {
@ -80,7 +82,7 @@ class FlowsPage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// todo : add locations tree view // todo : add locations tree view
return Scaffold(); return const Scaffold();
} }
} }
@ -90,7 +92,7 @@ class LocationsPage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// todo : add locations tree view // todo : add locations tree view
return Scaffold(); return const Scaffold();
} }
} }
@ -133,7 +135,12 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int pageIndex = 0; int pageIndex = 0;
List<Widget> pages = [StatsPage(), ItemsPage(), FlowsPage(), LocationsPage()]; List<Widget> pages = [
const StatsPage(),
const ItemsPage(),
const FlowsPage(),
const LocationsPage()
];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -169,11 +176,9 @@ class ItemCard extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return InkWell( return InkWell(
onTap: () async { onTap: () {
var itemInfo = await API().getItem(item); API().getItem(item).then((itemInfo) => Navigator.push(context,
MaterialPageRoute(builder: (context) => ItemView(item: itemInfo))));
Navigator.push(context,
MaterialPageRoute(builder: (context) => ItemView(item: itemInfo)));
}, },
child: Row(children: [ child: Row(children: [
/*Image.network( /*Image.network(

View file

@ -24,18 +24,19 @@ class _ConsumePageState extends State<ConsumePage> {
super.initState(); super.initState();
} }
Future<void> _consume() async { void _consume() {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();
await API() API()
.consumeItem(widget.transaction, _selectedDestination, "${_price}"); .consumeItem(widget.transaction, _selectedDestination, "$_price")
.then((_) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item consumed successfully!')), const SnackBar(content: Text('Item consumed successfully!')),
); );
Navigator.of(context).pop(); Navigator.of(context).pop();
widget.refresh(); widget.refresh();
});
} }
} }

View file

@ -75,7 +75,7 @@ class _ItemViewState extends State<ItemView> {
children: [ children: [
Text("Amount: ${stat.amount}"), Text("Amount: ${stat.amount}"),
Text( Text(
"Total Cost: ${stat.total_price.toStringAsFixed(2)}") "Total Cost: ${stat.totalPrice.toStringAsFixed(2)}")
], ],
); );
}, },
@ -125,7 +125,7 @@ class TransactionCard extends StatelessWidget {
onTap: () { onTap: () {
Navigator.of(context).push(MaterialPageRoute( Navigator.of(context).push(MaterialPageRoute(
builder: (context) { builder: (context) {
return ConsumePage(t.uuid, t.item, t.variant, this.refresh); return ConsumePage(t.uuid, t.item, t.variant, refresh);
}, },
)); ));
}, },

View file

@ -6,7 +6,7 @@ class SetupPage extends StatefulWidget {
const SetupPage({super.key}); const SetupPage({super.key});
@override @override
_SetupPageState createState() => _SetupPageState(); State<SetupPage> createState() => _SetupPageState();
} }
class _SetupPageState extends State<SetupPage> { class _SetupPageState extends State<SetupPage> {

View file

@ -25,18 +25,20 @@ class _SupplyPageState extends State<SupplyPage> {
variant = widget.item.variants.keys.first; variant = widget.item.variants.keys.first;
} }
Future<void> _supply() async { void _supply() {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();
await API().supplyItem(widget.item.name, variant, "${_price}", API()
_selectedOrigin, _selectedLocation); .supplyItem(widget.item.name, variant, "$_price", _selectedOrigin,
_selectedLocation)
ScaffoldMessenger.of(context).showSnackBar( .then((_) {
const SnackBar(content: Text('Item added successfully!')), ScaffoldMessenger.of(context).showSnackBar(
); const SnackBar(content: Text('Item added successfully!')),
Navigator.of(context).pop(); );
widget.refresh(); Navigator.of(context).pop();
widget.refresh();
});
} }
} }
@ -165,8 +167,8 @@ class _SupplyPageState extends State<SupplyPage> {
.map<DropdownMenuItem<String>>((id) { .map<DropdownMenuItem<String>>((id) {
return DropdownMenuItem<String>( return DropdownMenuItem<String>(
value: id, value: id,
child: Text( child:
locations[id]!.full_name_path(locations)), Text(locations[id]!.fullNamePath(locations)),
); );
}).toList(), }).toList(),
onSaved: (value) { onSaved: (value) {