api cache

This commit is contained in:
JMARyA 2024-09-26 09:50:53 +02:00
parent 82d258f74a
commit 5687efae4f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 54 additions and 11 deletions

View file

@ -4,12 +4,16 @@ import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
// todo : api errors
// todo : api caching
class API {
late SharedPreferences pref;
static final API _instance = API._internal();
// cache
List<Item>? items;
Map<String, Location>? locations;
Map<String, FlowInfo>? flowInfos;
factory API() {
return _instance;
}
@ -57,9 +61,14 @@ class API {
// /items
Future<List<Item>> getItems() async {
if (items != null) {
return items!;
}
var resp = jsonDecode(await getRequest("$instance/items"));
var lst = resp["items"] as List<dynamic>;
return lst.map((x) => Item(x)).toList();
items = lst.map((x) => Item(x)).toList();
return items!;
}
Future<GlobalItemStat> getGlobalItemStat() async {
@ -82,12 +91,21 @@ class API {
}
Future<Map<String, Location>> getLocations() async {
if (locations != null) {
return locations!;
}
var resp = jsonDecode(await getRequest("$instance/locations"))
as Map<String, dynamic>;
return resp.map((key, value) => MapEntry(key, Location(value)));
locations = resp.map((key, value) => MapEntry(key, Location(value)));
return locations!;
}
Future<Item> getItem(String item) async {
if (items != null) {
return items!.firstWhere((x) => x.id == item);
}
return Item(jsonDecode(await getRequest("$instance/item/$item")));
}
@ -219,14 +237,23 @@ class API {
// /flows
Future<Map<String, FlowInfo>> getFlows() async {
if (flowInfos != null) {
return flowInfos!;
}
var resp =
jsonDecode(await getRequest("$instance/flows")) as Map<String, dynamic>;
return resp.map((key, value) => MapEntry(key, FlowInfo(value)));
flowInfos = resp.map((key, value) => MapEntry(key, FlowInfo(value)));
return flowInfos!;
}
// /flow/<id>/info
Future<FlowInfo> getFlowInfo(String id) async {
if (flowInfos != null) {
return flowInfos![id]!;
}
return FlowInfo(jsonDecode(await getRequest("$instance/flow/$id/info")));
}
@ -288,6 +315,10 @@ class API {
}
Future<Location> getLocation(String id) async {
if (locations != null) {
return locations![id]!;
}
var resp = jsonDecode(await getRequest("$instance/location/$id"));
return Location(resp);
}

View file

@ -62,7 +62,6 @@ class _ActiveFlowPageState extends State<ActiveFlowPage> {
}
},
icon: const Icon(Icons.stop)),
// todo : continue next flow
if (widget.info.next != null)
IconButton(
onPressed: () {

View file

@ -3,7 +3,6 @@ import 'package:cdb_ui/pages/flow/active_flow_page.dart';
import 'package:cdb_ui/pages/transaction.dart';
import 'package:flutter/material.dart';
class CreateFlowPage extends StatefulWidget {
final API.FlowInfo info;
final Function refresh;
@ -77,13 +76,30 @@ class _CreateFlowPageState extends State<CreateFlowPage> {
}).toList());
}
Widget flowContinuation() {
return Row(
children: [
Card(
child: Text(widget.previousFlow!.kind),
),
const Text(" -> "),
Card(
child: Text(widget.info.name),
)
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Create new ${widget.info.name} Flow")),
body: Column(
children: [
// todo : flow continuation overview
if (widget.previousFlow != null) ...[
flowContinuation(),
const Divider()
],
buildInputSelection(context),
const Divider(),
Card(
@ -99,4 +115,4 @@ class _CreateFlowPageState extends State<CreateFlowPage> {
),
);
}
}
}

View file

@ -78,7 +78,6 @@ class _EndFlowWithProduceState extends State<EndFlowWithProduce> {
@override
Widget build(BuildContext context) {
// todo : show end screen with produce
return Scaffold(
appBar: AppBar(
title: Text("End ${widget.info.name} Flow"),
@ -87,7 +86,6 @@ class _EndFlowWithProduceState extends State<EndFlowWithProduce> {
children: [
...addProduceButtons(),
const Divider(),
// todo : add produced list
...produces.map((x) {
return TransactionCard(
x.transaction(locations),

View file

@ -50,7 +50,6 @@ class _SupplyPageState extends State<SupplyPage> {
_formKey.currentState!.save();
if (widget.onCreate != null) {
// todo : create shadow transaction
var t = SupplyForm(
itemID: widget.item.id,
variant: variant,