cdb_ui/lib/pages/flow.dart

332 lines
9 KiB
Dart
Raw Normal View History

2024-09-20 06:52:56 +00:00
import 'package:cdb_ui/api.dart' as API;
2024-09-23 09:35:37 +00:00
import 'package:cdb_ui/pages/expandable_list.dart';
2024-09-23 14:34:39 +00:00
import 'package:cdb_ui/pages/transaction.dart';
2024-09-19 06:46:48 +00:00
import 'package:flutter/material.dart';
2024-09-23 09:35:37 +00:00
class FlowsPage extends StatefulWidget {
2024-09-19 06:46:48 +00:00
const FlowsPage({super.key});
2024-09-23 09:35:37 +00:00
@override
State<FlowsPage> createState() => _FlowsPageState();
}
class _FlowsPageState extends State<FlowsPage> {
int tabSelection = 0;
Map<String, API.FlowInfo>? flowInfos;
@override
void initState() {
super.initState();
API.API().getFlows().then(
(value) {
setState(() {
flowInfos = value;
});
},
);
}
Widget flowTile(BuildContext context, API.FlowInfo x) {
return ListTile(
title: Text(x.name),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => FlowInfoPage(x),
));
},
);
}
2024-09-23 09:43:34 +00:00
Widget listFlowInfoByActive(
BuildContext context, Map<String, API.FlowInfo> infos) {
return FutureBuilder(future: () async {
var included = [];
for (var key in infos.keys) {
var active = await API.API().getActiveFlowsOf(key);
if (active.isNotEmpty) {
included.add(infos[key]);
}
}
return included;
}(), builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var included = snapshot.data!;
return ListView(
children: included.map((x) => flowTile(context, x)).toList());
});
}
2024-09-23 08:31:40 +00:00
Widget listAllFlowInfos(
BuildContext context, Map<String, API.FlowInfo> infos) {
return ListView(
children: infos.values.map((x) {
2024-09-23 09:35:37 +00:00
return flowTile(context, x);
2024-09-23 08:31:40 +00:00
}).toList());
}
2024-09-23 09:35:37 +00:00
Widget listFlowInfoByProduced(
BuildContext context, Map<String, API.FlowInfo> infos) {
Map<String, List<API.FlowInfo>> producedMapping = {};
for (var f in infos.values) {
for (var produces in f.produces ?? []) {
var item = API.itemVariant(produces).$1;
producedMapping.putIfAbsent(item, () {
return [];
});
producedMapping[item]!.add(f);
}
}
List<ExpandableListItem> items = [];
for (var key in producedMapping.keys) {
var flows = Column(
children: producedMapping[key]!.map((x) {
return flowTile(context, x);
}).toList());
2024-09-23 18:19:30 +00:00
items.add(ExpandableListItem(body: flows, header: Text(key)));
2024-09-23 09:35:37 +00:00
}
return ExpandableList(items);
}
Widget listFlowInfoByDependant(
BuildContext context, Map<String, API.FlowInfo> infos) {
Map<String, List<API.FlowInfo>> dependsMapping = {};
for (var f in infos.values) {
for (var produces in f.depends) {
var item = API.itemVariant(produces).$1;
// todo : add only if item is in inventory
dependsMapping.putIfAbsent(item, () {
return [];
});
dependsMapping[item]!.add(f);
}
}
List<ExpandableListItem> items = [];
for (var key in dependsMapping.keys) {
var flows = Column(
children: dependsMapping[key]!.map((x) {
return flowTile(context, x);
}).toList());
2024-09-23 18:19:30 +00:00
items.add(ExpandableListItem(body: flows, header: Text(key)));
2024-09-23 09:35:37 +00:00
}
return ExpandableList(items);
}
2024-09-19 06:46:48 +00:00
@override
Widget build(BuildContext context) {
2024-09-23 09:35:37 +00:00
if (flowInfos == null) {
return const CircularProgressIndicator();
}
2024-09-23 11:57:08 +00:00
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
title: const Text("Flows"),
bottom: TabBar(
tabs: const [
Tab(text: "All"),
Tab(text: "Produces"),
Tab(text: "Depends"),
Tab(text: "Active")
],
onTap: (value) {
setState(() {
tabSelection = value;
});
},
)),
body: switch (tabSelection) {
0 => listAllFlowInfos(context, flowInfos!),
1 => listFlowInfoByProduced(context, flowInfos!),
2 => listFlowInfoByDependant(context, flowInfos!),
3 => listFlowInfoByActive(context, flowInfos!),
_ => const Text("..."),
}));
2024-09-19 06:46:48 +00:00
}
}
2024-09-23 14:34:39 +00:00
class FlowInfoPage extends StatefulWidget {
2024-09-20 06:52:56 +00:00
final API.FlowInfo info;
2024-09-19 06:46:48 +00:00
2024-09-23 09:35:37 +00:00
const FlowInfoPage(this.info, {super.key});
2024-09-19 06:46:48 +00:00
2024-09-23 14:34:39 +00:00
@override
State<FlowInfoPage> createState() => _FlowInfoPageState();
}
class _FlowInfoPageState extends State<FlowInfoPage> {
void refresh() {
setState(() {});
}
2024-09-19 06:46:48 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
2024-09-23 14:34:39 +00:00
appBar: AppBar(title: Text(widget.info.name)),
2024-09-19 06:46:48 +00:00
body: Column(
children: [
// todo : ui improve
2024-09-23 14:34:39 +00:00
if (widget.info.next != null) Text("Next: ${widget.info.next}"),
if (widget.info.depends.isNotEmpty)
2024-09-19 06:46:48 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Text("Flow can use: "),
2024-09-23 14:34:39 +00:00
...widget.info.depends.map((x) => Text(x)).toList(),
2024-09-19 06:46:48 +00:00
],
),
2024-09-23 14:34:39 +00:00
if (widget.info.produces?.isNotEmpty ?? false)
2024-09-19 06:46:48 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Text("Flow can produce: "),
2024-09-23 14:34:39 +00:00
...widget.info.produces!.map((x) => Text(x)).toList(),
2024-09-19 06:46:48 +00:00
],
),
const Divider(),
FutureBuilder(
2024-09-23 14:34:39 +00:00
future: API.API().getActiveFlowsOf(widget.info.id),
2024-09-19 06:46:48 +00:00
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var data = snapshot.data!;
2024-09-23 18:19:30 +00:00
return Expanded(
child: ListView(
children: data
.map((x) => ListTile(
title: Text(x.id),
onTap: () =>
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ActiveFlowPage(x),
))))
.toList()),
);
2024-09-19 06:46:48 +00:00
},
)
],
),
2024-09-23 14:34:39 +00:00
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()),
2024-09-19 06:46:48 +00:00
);
}
}
2024-09-20 06:52:56 +00:00
class ActiveFlowPage extends StatelessWidget {
late API.Flow flow;
ActiveFlowPage(this.flow);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [],
));
}
}