2024-09-20 07:24:22 +00:00
|
|
|
import 'package:cdb_ui/api.dart';
|
2024-09-21 15:15:12 +00:00
|
|
|
import 'package:cdb_ui/pages/consume.dart';
|
2024-09-20 07:24:22 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-09-21 15:15:12 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2024-09-23 06:55:40 +00:00
|
|
|
import 'package:qr_bar_code/qr/qr.dart';
|
2024-09-24 14:19:15 +00:00
|
|
|
import 'package:qr_bar_code_scanner_dialog/qr_bar_code_scanner_dialog.dart';
|
2024-09-20 07:24:22 +00:00
|
|
|
|
2024-09-24 14:19:15 +00:00
|
|
|
class TransactionPage extends StatefulWidget {
|
2024-09-23 06:55:40 +00:00
|
|
|
final Transaction transaction;
|
2024-09-23 07:03:40 +00:00
|
|
|
final Function? refresh;
|
2024-09-20 07:24:22 +00:00
|
|
|
|
2024-09-23 07:03:40 +00:00
|
|
|
const TransactionPage(this.transaction, {this.refresh, super.key});
|
2024-09-20 07:24:22 +00:00
|
|
|
|
2024-09-24 14:19:15 +00:00
|
|
|
@override
|
|
|
|
State<TransactionPage> createState() => _TransactionPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _TransactionPageState extends State<TransactionPage> {
|
2024-09-26 18:20:39 +00:00
|
|
|
late Transaction transaction;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
transaction = widget.transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> reload() async {
|
|
|
|
if (widget.refresh != null) {
|
|
|
|
widget.refresh!();
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateTransaction = await API().getTransaction(transaction.uuid);
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
transaction = updateTransaction;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-20 07:24:22 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-09-24 12:55:14 +00:00
|
|
|
appBar: AppBar(
|
2024-09-24 14:19:15 +00:00
|
|
|
title: Text(widget.transaction.item),
|
2024-09-24 12:55:14 +00:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
2024-09-25 08:49:21 +00:00
|
|
|
onPressed: () {
|
|
|
|
API().getLocations().then((locations) {
|
|
|
|
List<String> locationList = locations.keys.toList();
|
|
|
|
String? selectedLocationID;
|
|
|
|
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
title: const Text('Move Transaction'),
|
|
|
|
content: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
DropdownButton<String>(
|
|
|
|
value: selectedLocationID,
|
|
|
|
onChanged: (value) {
|
|
|
|
selectedLocationID = value!;
|
|
|
|
API()
|
|
|
|
.moveTransaction(widget.transaction.uuid,
|
|
|
|
selectedLocationID!)
|
|
|
|
.then((x) {
|
|
|
|
Navigator.of(context).pop();
|
2024-09-24 14:19:15 +00:00
|
|
|
});
|
2024-09-25 08:49:21 +00:00
|
|
|
|
2024-09-24 14:19:15 +00:00
|
|
|
setState(() {});
|
2024-09-24 12:55:14 +00:00
|
|
|
},
|
2024-09-25 08:49:21 +00:00
|
|
|
items: locationList
|
|
|
|
.map<DropdownMenuItem<String>>((locationID) {
|
|
|
|
return DropdownMenuItem<String>(
|
|
|
|
value: locationID,
|
|
|
|
child: Text(locations[locationID]!.name),
|
|
|
|
);
|
|
|
|
}).toList(),
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
API().getLocations().then((locations) {
|
|
|
|
QrBarCodeScannerDialog()
|
|
|
|
.getScannedQrBarCode(
|
|
|
|
context: context,
|
|
|
|
onCode: (code) {
|
|
|
|
// library is retarded
|
|
|
|
code = code!.replaceFirst(
|
|
|
|
"Code scanned = ", "");
|
|
|
|
if (!locations
|
|
|
|
.containsKey(code)) {
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
.showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(
|
|
|
|
'The location $code does not exist.')),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
API()
|
|
|
|
.moveTransaction(
|
|
|
|
widget.transaction.uuid,
|
|
|
|
selectedLocationID!)
|
|
|
|
.then(
|
|
|
|
(x) {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.qr_code))
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2024-09-24 12:55:14 +00:00
|
|
|
},
|
|
|
|
icon: const Icon(Icons.move_up))
|
|
|
|
],
|
|
|
|
),
|
2024-09-26 18:20:39 +00:00
|
|
|
body: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
QRCode(
|
|
|
|
data: transaction.uuid,
|
|
|
|
size: 128,
|
|
|
|
eyeStyle: const QREyeStyle(color: Colors.white),
|
|
|
|
dataModuleStyle: const QRDataModuleStyle(color: Colors.white),
|
|
|
|
semanticsLabel: "Transaction UUID",
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 16.0,
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
// todo : human names
|
|
|
|
Text(
|
|
|
|
"${transaction.item}",
|
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.bold, fontSize: 28),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"${transaction.variant}",
|
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontSize: 20,
|
|
|
|
color: Colors.grey),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 8.0,
|
|
|
|
),
|
|
|
|
Text("Added: ${tsFormat(transaction.timestamp)}"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const Divider(),
|
|
|
|
const SizedBox(
|
|
|
|
height: 12.0,
|
|
|
|
),
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
if (transaction.expired) const Text("Transaction is Expired!"),
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
IconText(Icons.money, transaction.price.format(),
|
|
|
|
color: Colors.green),
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
if (transaction.origin != null)
|
|
|
|
IconText(Icons.store, transaction.origin!, color: Colors.blue),
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
if (transaction.location != null)
|
|
|
|
IconText(Icons.location_city, transaction.location!.name),
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
if (transaction.note != null) Text(transaction.note!),
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
if (transaction.consumed != null) ...[
|
|
|
|
const Divider(),
|
|
|
|
Text("Consumed on: ${tsFormat(transaction.consumed!.timestamp)}"),
|
|
|
|
IconText(Icons.store, transaction.consumed!.destination,
|
|
|
|
color: Colors.blue),
|
|
|
|
IconText(Icons.money, transaction.consumed!.price.format(),
|
|
|
|
color: Colors.green),
|
|
|
|
]
|
2024-09-23 06:55:40 +00:00
|
|
|
|
2024-09-26 18:20:39 +00:00
|
|
|
// todo : chart with price history
|
|
|
|
],
|
|
|
|
),
|
2024-09-20 07:24:22 +00:00
|
|
|
),
|
2024-09-26 18:20:39 +00:00
|
|
|
floatingActionButton: transaction.consumed == null
|
|
|
|
? FloatingActionButton(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(
|
|
|
|
builder: (context) {
|
|
|
|
return ConsumePage(transaction, reload);
|
|
|
|
},
|
|
|
|
));
|
2024-09-23 07:03:40 +00:00
|
|
|
},
|
2024-09-26 18:20:39 +00:00
|
|
|
child: const Icon(Icons.receipt_long))
|
|
|
|
: null,
|
2024-09-20 07:24:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 15:15:12 +00:00
|
|
|
|
|
|
|
class TransactionCard extends StatelessWidget {
|
|
|
|
final Transaction t;
|
|
|
|
final Function refresh;
|
2024-09-23 14:34:39 +00:00
|
|
|
final Function(Transaction)? onTap;
|
|
|
|
final Function(Transaction)? onLongPress;
|
2024-09-21 15:15:12 +00:00
|
|
|
|
2024-09-23 14:34:39 +00:00
|
|
|
const TransactionCard(this.t, this.refresh,
|
|
|
|
{this.onTap, this.onLongPress, super.key});
|
2024-09-21 15:15:12 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return InkWell(
|
|
|
|
onTap: () {
|
2024-09-23 14:34:39 +00:00
|
|
|
if (onTap != null) {
|
|
|
|
onTap!(t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-21 15:15:12 +00:00
|
|
|
Navigator.of(context).push(MaterialPageRoute(
|
|
|
|
builder: (context) {
|
2024-09-23 07:03:40 +00:00
|
|
|
return ConsumePage(t, refresh);
|
2024-09-21 15:15:12 +00:00
|
|
|
},
|
|
|
|
));
|
|
|
|
},
|
2024-09-22 20:15:43 +00:00
|
|
|
onLongPress: () {
|
2024-09-23 14:34:39 +00:00
|
|
|
if (onLongPress != null) {
|
|
|
|
onLongPress!(t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-22 20:15:43 +00:00
|
|
|
Navigator.of(context).push(MaterialPageRoute(
|
|
|
|
builder: (context) {
|
|
|
|
return TransactionPage(t);
|
|
|
|
},
|
|
|
|
));
|
|
|
|
},
|
2024-09-21 15:15:12 +00:00
|
|
|
child: Card(
|
|
|
|
color: t.expired ? Colors.red[100] : Colors.black,
|
|
|
|
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
|
|
|
elevation: 4,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
t.item,
|
|
|
|
style: const TextStyle(fontSize: 16),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 4,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
t.variant,
|
|
|
|
style: TextStyle(fontSize: 14, color: Colors.grey[400]),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-09-25 16:14:42 +00:00
|
|
|
if (t.timestamp != 0)
|
|
|
|
Text(
|
|
|
|
tsFormat(t.timestamp),
|
|
|
|
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
|
|
|
|
),
|
2024-09-21 15:15:12 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
if ((t.note ?? "").isNotEmpty) ...[
|
|
|
|
const SizedBox(
|
2024-09-23 18:19:30 +00:00
|
|
|
height: 4,
|
|
|
|
),
|
|
|
|
Text(t.note!)
|
2024-09-21 15:15:12 +00:00
|
|
|
],
|
2024-09-23 18:19:30 +00:00
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
2024-09-21 15:15:12 +00:00
|
|
|
IconText(Icons.money,
|
|
|
|
"${t.price.value.toStringAsFixed(2)} ${t.price.currency}",
|
|
|
|
color: Colors.green),
|
|
|
|
if (t.origin != null) ...[
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
IconText(Icons.store, t.origin!, color: Colors.blue)
|
|
|
|
],
|
2024-09-23 06:42:58 +00:00
|
|
|
if (t.location != null) ...[
|
|
|
|
const SizedBox(
|
|
|
|
height: 8,
|
|
|
|
),
|
2024-09-23 18:19:30 +00:00
|
|
|
IconText(Icons.location_city, t.location!.name)
|
2024-09-23 06:42:58 +00:00
|
|
|
]
|
2024-09-21 15:15:12 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String tsFormat(int ts) {
|
|
|
|
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(ts * 1000);
|
|
|
|
return DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
class IconText extends StatelessWidget {
|
|
|
|
final IconData icon;
|
|
|
|
final String text;
|
|
|
|
final Color? color;
|
|
|
|
|
|
|
|
const IconText(this.icon, this.text, {super.key, this.color});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
Icon(icon, size: 18, color: color),
|
|
|
|
const SizedBox(width: 6),
|
|
|
|
Text(
|
|
|
|
text,
|
|
|
|
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 16:00:13 +00:00
|
|
|
|
|
|
|
class TransactionSelectPage extends StatelessWidget {
|
|
|
|
final Function(Transaction) onSelect;
|
|
|
|
final List<Transaction> selections;
|
|
|
|
final List<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);
|
|
|
|
}
|
|
|
|
|
2024-09-24 16:14:51 +00:00
|
|
|
if (selectionList.isEmpty) {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(content: Text('No Transactions to select')),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-24 16:00:13 +00:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text("Select a Transaction"),
|
|
|
|
),
|
|
|
|
body: ListView(
|
|
|
|
children: selectionList
|
|
|
|
.map((x) => TransactionCard(
|
|
|
|
x,
|
|
|
|
() {},
|
|
|
|
onLongPress: (x) {},
|
|
|
|
onTap: (t) {
|
|
|
|
onSelect(t);
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.toList()),
|
|
|
|
);
|
|
|
|
}
|
2024-09-25 08:49:21 +00:00
|
|
|
}
|