update flows

This commit is contained in:
JMARyA 2024-09-23 16:34:39 +02:00
parent d1eda26759
commit c7f07c3d0a
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 126 additions and 10 deletions

View file

@ -228,6 +228,10 @@ class API {
return FlowInfo(jsonDecode(await getRequest("$instance/flow/$id/info"))); return FlowInfo(jsonDecode(await getRequest("$instance/flow/$id/info")));
} }
Future<Flow> getFlow(String id) async {
return Flow(jsonDecode(await getRequest("$instance/flow/$id")));
}
Future<List<Flow>> getActiveFlowsOf(String id) async { Future<List<Flow>> getActiveFlowsOf(String id) async {
var res = jsonDecode(await getRequest("$instance/flow/$id/active")) var res = jsonDecode(await getRequest("$instance/flow/$id/active"))
as List<dynamic>; as List<dynamic>;

View file

@ -1,5 +1,6 @@
import 'package:cdb_ui/api.dart' as API; import 'package:cdb_ui/api.dart' as API;
import 'package:cdb_ui/pages/expandable_list.dart'; import 'package:cdb_ui/pages/expandable_list.dart';
import 'package:cdb_ui/pages/transaction.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class FlowsPage extends StatefulWidget { class FlowsPage extends StatefulWidget {
@ -160,38 +161,47 @@ class _FlowsPageState extends State<FlowsPage> {
} }
} }
class FlowInfoPage extends StatelessWidget { class FlowInfoPage extends StatefulWidget {
final API.FlowInfo info; final API.FlowInfo info;
const FlowInfoPage(this.info, {super.key}); const FlowInfoPage(this.info, {super.key});
@override
State<FlowInfoPage> createState() => _FlowInfoPageState();
}
class _FlowInfoPageState extends State<FlowInfoPage> {
void refresh() {
setState(() {});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar(title: Text(info.name)), appBar: AppBar(title: Text(widget.info.name)),
body: Column( body: Column(
children: [ children: [
// todo : ui improve // todo : ui improve
if (info.next != null) Text("Next: ${info.next}"), if (widget.info.next != null) Text("Next: ${widget.info.next}"),
if (info.depends.isNotEmpty) if (widget.info.depends.isNotEmpty)
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
const Text("Flow can use: "), const Text("Flow can use: "),
...info.depends.map((x) => Text(x)).toList(), ...widget.info.depends.map((x) => Text(x)).toList(),
], ],
), ),
if (info.produces?.isNotEmpty ?? false) if (widget.info.produces?.isNotEmpty ?? false)
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
const Text("Flow can produce: "), const Text("Flow can produce: "),
...info.produces!.map((x) => Text(x)).toList(), ...widget.info.produces!.map((x) => Text(x)).toList(),
], ],
), ),
const Divider(), const Divider(),
FutureBuilder( FutureBuilder(
future: API.API().getActiveFlowsOf(info.id), future: API.API().getActiveFlowsOf(widget.info.id),
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData) { if (!snapshot.hasData) {
return const CircularProgressIndicator(); return const CircularProgressIndicator();
@ -212,6 +222,96 @@ class FlowInfoPage extends StatelessWidget {
) )
], ],
), ),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => CreateFlowPage(widget.info, refresh)));
},
child: const Icon(Icons.add)),
);
}
}
class CreateFlowPage extends StatelessWidget {
final API.FlowInfo info;
final Function refresh;
Map<String, List<API.Transaction>> depends = {};
CreateFlowPage(this.info, this.refresh, {super.key});
void _create(BuildContext context) {
// todo : input handling
API.API().startFlow(info.id, input: []).then((flowID) {
refresh();
API.API().getFlow(flowID).then((flow) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => ActiveFlowPage(flow)));
});
});
}
void selectDependItems(BuildContext context, String itemVariant) {
var (item, variant) = API.itemVariant(itemVariant);
API.API().getInventoryOfVariant(item, variant).then((transactions) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return TransactionSelectPage(transactions, onSelect: (t) {
depends.putIfAbsent(itemVariant, () {
return [];
});
depends[itemVariant]!.add(t);
}, exclude: depends[itemVariant] ?? []);
},
));
});
}
@override
Widget build(BuildContext context) {
// todo : able to add transactions from depends
return Scaffold(
body: null,
);
}
}
class TransactionSelectPage extends StatelessWidget {
final Function(API.Transaction) onSelect;
final List<API.Transaction> selections;
final List<API.Transaction>? exclude;
const TransactionSelectPage(this.selections,
{super.key, required this.onSelect, this.exclude});
@override
Widget build(BuildContext context) {
var selectionList = [];
for (var s in selections) {
if (exclude?.any((x) => x.uuid == s.uuid) ?? false) {
continue;
}
selectionList.add(s);
}
return Scaffold(
appBar: AppBar(
title: const Text("Select a Transaction"),
),
body: ListView(
children: selections
.map((x) => TransactionCard(
x,
() {},
onLongPress: (x) {},
onTap: (t) {
onSelect(t);
Navigator.of(context).pop();
},
))
.toList()),
); );
} }
} }

View file

@ -19,7 +19,6 @@ class StatsPage extends StatelessWidget {
// todo : add global statistics // todo : add global statistics
// todo : demand stat // todo : demand stat
// todo : stat on origin + destination // todo : stat on origin + destination
// todo : add btn for scanning transaction
return Scaffold( return Scaffold(
body: FutureBuilder( body: FutureBuilder(

View file

@ -65,13 +65,21 @@ class TransactionPage extends StatelessWidget {
class TransactionCard extends StatelessWidget { class TransactionCard extends StatelessWidget {
final Transaction t; final Transaction t;
final Function refresh; final Function refresh;
final Function(Transaction)? onTap;
final Function(Transaction)? onLongPress;
const TransactionCard(this.t, this.refresh, {super.key}); const TransactionCard(this.t, this.refresh,
{this.onTap, this.onLongPress, super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return InkWell( return InkWell(
onTap: () { onTap: () {
if (onTap != null) {
onTap!(t);
return;
}
Navigator.of(context).push(MaterialPageRoute( Navigator.of(context).push(MaterialPageRoute(
builder: (context) { builder: (context) {
return ConsumePage(t, refresh); return ConsumePage(t, refresh);
@ -79,6 +87,11 @@ class TransactionCard extends StatelessWidget {
)); ));
}, },
onLongPress: () { onLongPress: () {
if (onLongPress != null) {
onLongPress!(t);
return;
}
Navigator.of(context).push(MaterialPageRoute( Navigator.of(context).push(MaterialPageRoute(
builder: (context) { builder: (context) {
return TransactionPage(t); return TransactionPage(t);