import 'package:cdb_ui/api.dart' as API; import 'package:cdb_ui/pages/expandable_list.dart'; import 'package:flutter/material.dart'; class FlowsPage extends StatefulWidget { const FlowsPage({super.key}); @override State createState() => _FlowsPageState(); } class _FlowsPageState extends State { int tabSelection = 0; Map? 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), )); }, ); } Widget listAllFlowInfos( BuildContext context, Map infos) { return ListView( children: infos.values.map((x) { return flowTile(context, x); }).toList()); } Widget listFlowInfoByProduced( BuildContext context, Map infos) { Map> 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 items = []; for (var key in producedMapping.keys) { var flows = Column( children: producedMapping[key]!.map((x) { return flowTile(context, x); }).toList()); items.add(ExpandableListItem(body: flows, header: Text(key))); } return ExpandableList(items); } Widget listFlowInfoByDependant( BuildContext context, Map infos) { Map> 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 items = []; for (var key in dependsMapping.keys) { var flows = Column( children: dependsMapping[key]!.map((x) { return flowTile(context, x); }).toList()); items.add(ExpandableListItem(body: flows, header: Text(key))); } return ExpandableList(items); } @override Widget build(BuildContext context) { // todo : list currently active if (flowInfos == null) { return const CircularProgressIndicator(); } return Scaffold( appBar: AppBar( title: const Text("Flows"), bottom: TabBar( tabs: const [Text("All"), Text("Produces"), Text("Depends")], onTap: (value) { setState(() { tabSelection = value; }); }, )), body: switch (tabSelection) { 0 => listAllFlowInfos(context, flowInfos!), 1 => listFlowInfoByProduced(context, flowInfos!), 2 => listFlowInfoByDependant(context, flowInfos!), _ => const Text("..."), }); } } class FlowInfoPage extends StatelessWidget { final API.FlowInfo info; const FlowInfoPage(this.info, {super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(info.name)), body: Column( children: [ // todo : ui improve if (info.next != null) Text("Next: ${info.next}"), if (info.depends.isNotEmpty) Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ const Text("Flow can use: "), ...info.depends.map((x) => Text(x)).toList(), ], ), if (info.produces?.isNotEmpty ?? false) Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ const Text("Flow can produce: "), ...info.produces!.map((x) => Text(x)).toList(), ], ), const Divider(), FutureBuilder( future: API.API().getActiveFlowsOf(info.id), builder: (context, snapshot) { if (!snapshot.hasData) { return const CircularProgressIndicator(); } var data = snapshot.data!; return ListView( children: data .map((x) => ListTile( title: Text(x.id), onTap: () => Navigator.of(context).push(MaterialPageRoute( builder: (context) => ActiveFlowPage(x), )))) .toList()); }, ) ], ), ); } } class ActiveFlowPage extends StatelessWidget { late API.Flow flow; ActiveFlowPage(this.flow); @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [], )); } }