cdb_ui/lib/pages/flow/flows_page.dart

176 lines
4.9 KiB
Dart
Raw Permalink Normal View History

2024-09-24 16:00:13 +00:00
import 'package:cdb_ui/api.dart' as API;
import 'package:cdb_ui/pages/expandable_list.dart';
2024-09-26 18:20:39 +00:00
import 'package:cdb_ui/pages/flow/active_flow_page.dart';
2024-09-24 16:00:13 +00:00
import 'package:cdb_ui/pages/flow/flow_info_page.dart';
2024-10-08 08:11:18 +00:00
import 'package:cdb_ui/pages/supply.dart';
2024-09-24 16:00:13 +00:00
import 'package:flutter/material.dart';
class FlowsPage extends StatefulWidget {
const FlowsPage({super.key});
@override
State<FlowsPage> createState() => _FlowsPageState();
}
class _FlowsPageState extends State<FlowsPage> {
int tabSelection = 0;
Map<String, API.FlowInfo>? flowInfos;
@override
void initState() {
super.initState();
2024-09-26 19:35:28 +00:00
flowInfos = API.API().getFlows();
2024-09-24 16:00:13 +00:00
}
Widget flowTile(BuildContext context, API.FlowInfo x) {
return ListTile(
title: Text(x.name),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => FlowInfoPage(x),
));
},
);
}
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());
});
}
Widget listAllFlowInfos(
BuildContext context, Map<String, API.FlowInfo> infos) {
return ListView(
children: infos.values.map((x) {
return flowTile(context, x);
}).toList());
}
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-26 19:35:28 +00:00
items.add(ExpandableListItem(
body: flows, header: Text(API.API().getItem(key).name)));
2024-09-24 16:00:13 +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-26 19:35:28 +00:00
items.add(ExpandableListItem(
body: flows, header: Text(API.API().getItem(key).name)));
2024-09-24 16:00:13 +00:00
}
return ExpandableList(items);
}
@override
Widget build(BuildContext context) {
if (flowInfos == null) {
return const CircularProgressIndicator();
}
return DefaultTabController(
length: 4,
child: Scaffold(
2024-09-26 18:20:39 +00:00
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("..."),
},
floatingActionButton: FloatingActionButton(
2024-10-08 08:11:18 +00:00
onPressed: () async {
2024-09-26 18:20:39 +00:00
// scan flow code
2024-10-08 08:11:18 +00:00
var code = await scanQRCode(context, title: "Scan Flow Code");
API.API().getFlow(code!).then((flow) {
var info = API.API().getFlowInfo(flow.kind);
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return ActiveFlowPage(flow, info);
},
));
});
2024-09-26 18:20:39 +00:00
},
child: const Icon(Icons.qr_code),
),
));
2024-09-24 16:00:13 +00:00
}
2024-09-25 08:49:21 +00:00
}