2023-12-29 21:39:54 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:intl/intl.dart';
|
2023-11-22 15:09:05 +01:00
|
|
|
import 'package:prasule/api/category.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:prasule/api/wallet.dart';
|
2024-01-08 18:21:31 +01:00
|
|
|
import 'package:prasule/api/wallet_manager.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:prasule/main.dart';
|
|
|
|
import 'package:prasule/pw/platformbutton.dart';
|
|
|
|
import 'package:prasule/pw/platformroute.dart';
|
|
|
|
import 'package:prasule/util/drawer.dart';
|
|
|
|
import 'package:prasule/util/graphs.dart';
|
|
|
|
import 'package:prasule/views/settings/settings.dart';
|
|
|
|
import 'package:prasule/views/setup.dart';
|
2023-12-25 19:03:52 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Shows data from a [Wallet] in graphs
|
2023-11-21 20:23:14 +01:00
|
|
|
class GraphView extends StatefulWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Shows data from a [Wallet] in graphs
|
2023-11-21 20:23:14 +01:00
|
|
|
const GraphView({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<GraphView> createState() => _GraphViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _GraphViewState extends State<GraphView> {
|
|
|
|
var _selectedDate = DateTime.now();
|
|
|
|
Wallet? selectedWallet;
|
|
|
|
List<Wallet> wallets = [];
|
|
|
|
String? locale;
|
2023-12-29 21:39:54 +01:00
|
|
|
Set<String> yearlyBtnSet = {"monthly"};
|
|
|
|
Set<String> graphTypeSet = {"expense", "income"};
|
2023-11-21 20:23:14 +01:00
|
|
|
bool get yearly => yearlyBtnSet.contains("yearly");
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
locale ??= Localizations.localeOf(context).languageCode;
|
|
|
|
}
|
|
|
|
|
2023-11-22 15:09:05 +01:00
|
|
|
List<double> generateChartData(EntryType type) {
|
2024-01-08 15:38:31 +01:00
|
|
|
final d = _selectedDate.add(const Duration(days: 31));
|
2023-12-29 21:39:54 +01:00
|
|
|
final data = List<double>.filled(
|
2024-01-08 15:38:31 +01:00
|
|
|
yearly ? 12 : DateTime(d.year, d.month, 0).day,
|
2023-12-29 21:39:54 +01:00
|
|
|
0,
|
|
|
|
);
|
2023-11-22 15:09:05 +01:00
|
|
|
if (selectedWallet == null) return [];
|
2023-11-21 20:23:14 +01:00
|
|
|
for (var i = 0; i < data.length; i++) {
|
2023-12-29 21:39:54 +01:00
|
|
|
final entriesForRange = selectedWallet!.entries.where(
|
|
|
|
(element) =>
|
|
|
|
((!yearly)
|
|
|
|
? element.date.month == _selectedDate.month &&
|
|
|
|
element.date.year == _selectedDate.year &&
|
|
|
|
element.date.day == i + 1
|
|
|
|
: element.date.month == i + 1 &&
|
|
|
|
element.date.year == _selectedDate.year) &&
|
|
|
|
element.type == type,
|
|
|
|
);
|
2023-11-21 20:23:14 +01:00
|
|
|
var sum = 0.0;
|
2023-12-29 21:39:54 +01:00
|
|
|
for (final e in entriesForRange) {
|
2023-11-21 20:23:14 +01:00
|
|
|
sum += e.data.amount;
|
|
|
|
}
|
|
|
|
data[i] = sum;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
Future<void> loadWallet() async {
|
2023-11-21 20:23:14 +01:00
|
|
|
wallets = await WalletManager.listWallets();
|
|
|
|
if (wallets.isEmpty && mounted) {
|
2023-12-29 21:39:54 +01:00
|
|
|
unawaited(
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushReplacement(platformRoute((c) => const SetupView())),
|
|
|
|
);
|
2023-11-21 20:23:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedWallet = wallets.first;
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
2023-12-25 19:03:52 +01:00
|
|
|
int? chartType;
|
2023-11-21 20:23:14 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
loadWallet();
|
2023-12-25 19:03:52 +01:00
|
|
|
SharedPreferences.getInstance().then((s) {
|
|
|
|
chartType = s.getInt("monthlygraph") ?? 2;
|
|
|
|
setState(() {});
|
|
|
|
});
|
2023-11-21 20:23:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-01-08 15:38:31 +01:00
|
|
|
floatingActionButton: Tooltip(
|
|
|
|
message: AppLocalizations.of(context).changeDate,
|
|
|
|
child: PlatformButton(
|
|
|
|
style: ButtonStyle(
|
|
|
|
backgroundColor: MaterialStateProperty.all(
|
|
|
|
Theme.of(context).colorScheme.primary,
|
|
|
|
),
|
|
|
|
foregroundColor: MaterialStateProperty.all(
|
|
|
|
Theme.of(context).colorScheme.onPrimary,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
text: yearly
|
|
|
|
? DateFormat.y(locale).format(_selectedDate)
|
|
|
|
: DateFormat.yMMMM(locale).format(_selectedDate),
|
|
|
|
onPressed: () async {
|
|
|
|
final firstDate = (selectedWallet!.entries
|
|
|
|
..sort(
|
|
|
|
(a, b) => a.date.compareTo(b.date),
|
|
|
|
))
|
|
|
|
.first
|
|
|
|
.date;
|
|
|
|
final newDate = await showDatePicker(
|
|
|
|
context: context,
|
|
|
|
initialDate: DateTime(
|
|
|
|
_selectedDate.year,
|
|
|
|
_selectedDate.month,
|
|
|
|
),
|
|
|
|
firstDate: firstDate,
|
|
|
|
lastDate: DateTime.now(),
|
|
|
|
initialEntryMode: yearly
|
|
|
|
? DatePickerEntryMode.input
|
|
|
|
: DatePickerEntryMode.calendar,
|
|
|
|
initialDatePickerMode:
|
|
|
|
yearly ? DatePickerMode.year : DatePickerMode.day,
|
|
|
|
);
|
|
|
|
if (newDate == null) return;
|
|
|
|
_selectedDate = newDate;
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: DropdownButton<int>(
|
|
|
|
value:
|
|
|
|
(selectedWallet == null) ? -1 : wallets.indexOf(selectedWallet!),
|
|
|
|
items: [
|
|
|
|
...wallets.map(
|
|
|
|
(e) => DropdownMenuItem(
|
|
|
|
value: wallets.indexOf(
|
|
|
|
e,
|
|
|
|
),
|
|
|
|
child: Text(e.name),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
DropdownMenuItem(
|
|
|
|
value: -1,
|
2023-12-25 19:03:52 +01:00
|
|
|
child: Text(AppLocalizations.of(context).newWallet),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
],
|
|
|
|
onChanged: (v) async {
|
|
|
|
if (v == null || v == -1) {
|
|
|
|
await Navigator.of(context).push(
|
|
|
|
platformRoute(
|
|
|
|
(c) => const SetupView(
|
|
|
|
newWallet: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
wallets = await WalletManager.listWallets();
|
|
|
|
logger.i(wallets.length);
|
|
|
|
selectedWallet = wallets.last;
|
|
|
|
setState(() {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedWallet = wallets[v];
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
PopupMenuButton(
|
|
|
|
itemBuilder: (context) => [
|
2023-12-25 19:03:52 +01:00
|
|
|
AppLocalizations.of(context).settings,
|
2023-12-29 21:39:54 +01:00
|
|
|
AppLocalizations.of(context).about,
|
2023-11-21 20:23:14 +01:00
|
|
|
].map((e) => PopupMenuItem(value: e, child: Text(e))).toList(),
|
|
|
|
onSelected: (value) {
|
2023-12-25 19:03:52 +01:00
|
|
|
if (value == AppLocalizations.of(context).settings) {
|
2024-01-08 15:38:31 +01:00
|
|
|
Navigator.of(context)
|
|
|
|
.push(
|
2023-12-29 21:39:54 +01:00
|
|
|
platformRoute(
|
|
|
|
(context) => const SettingsView(),
|
2023-11-21 20:23:14 +01:00
|
|
|
),
|
2024-01-08 15:38:31 +01:00
|
|
|
)
|
|
|
|
.then((value) async {
|
|
|
|
selectedWallet =
|
|
|
|
await WalletManager.loadWallet(selectedWallet!.name);
|
|
|
|
final s = await SharedPreferences.getInstance();
|
|
|
|
chartType = s.getInt("monthlygraph") ?? 2;
|
|
|
|
setState(() {});
|
|
|
|
});
|
2023-12-25 19:03:52 +01:00
|
|
|
} else if (value == AppLocalizations.of(context).about) {
|
2023-11-21 20:23:14 +01:00
|
|
|
showAboutDialog(
|
2023-12-29 21:39:54 +01:00
|
|
|
context: context,
|
|
|
|
applicationLegalese: AppLocalizations.of(context).license,
|
|
|
|
applicationName: "Prašule",
|
|
|
|
);
|
2023-11-21 20:23:14 +01:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
drawer: makeDrawer(context, 2),
|
|
|
|
body: SingleChildScrollView(
|
|
|
|
child: Center(
|
2023-12-25 19:03:52 +01:00
|
|
|
child: (selectedWallet == null)
|
|
|
|
? const CircularProgressIndicator(
|
|
|
|
strokeWidth: 5,
|
|
|
|
)
|
|
|
|
: SizedBox(
|
2023-12-25 19:54:30 +01:00
|
|
|
width: MediaQuery.of(context).size.width,
|
2023-12-25 19:03:52 +01:00
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
SegmentedButton<String>(
|
|
|
|
segments: [
|
|
|
|
ButtonSegment<String>(
|
|
|
|
value: "expense",
|
|
|
|
label: Text(AppLocalizations.of(context).expenses),
|
|
|
|
),
|
|
|
|
ButtonSegment<String>(
|
|
|
|
value: "income",
|
|
|
|
label: Text(AppLocalizations.of(context).income),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
selected: graphTypeSet,
|
|
|
|
multiSelectionEnabled: true,
|
|
|
|
onSelectionChanged: (selection) {
|
|
|
|
graphTypeSet = selection;
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 5,
|
|
|
|
),
|
|
|
|
SegmentedButton<String>(
|
|
|
|
segments: [
|
|
|
|
ButtonSegment<String>(
|
|
|
|
value: "yearly",
|
|
|
|
label: Text(AppLocalizations.of(context).yearly),
|
|
|
|
),
|
|
|
|
ButtonSegment<String>(
|
|
|
|
value: "monthly",
|
|
|
|
label: Text(AppLocalizations.of(context).monthly),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
selected: yearlyBtnSet,
|
|
|
|
onSelectionChanged: (selection) async {
|
|
|
|
yearlyBtnSet = selection;
|
2023-12-29 21:39:54 +01:00
|
|
|
final s = await SharedPreferences.getInstance();
|
|
|
|
chartType = yearly
|
2023-12-25 19:03:52 +01:00
|
|
|
? (s.getInt("yearlygraph") ?? 1)
|
|
|
|
: (s.getInt("monthlygraph") ?? 2);
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(height: 5),
|
|
|
|
Container(
|
|
|
|
decoration: BoxDecoration(
|
2023-12-29 21:39:54 +01:00
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
color:
|
|
|
|
Theme.of(context).colorScheme.secondaryContainer,
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
child: Padding(
|
2023-12-29 21:39:54 +01:00
|
|
|
padding: const EdgeInsets.all(8),
|
2023-12-25 19:03:52 +01:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width * 0.9,
|
2024-01-08 15:38:31 +01:00
|
|
|
height:
|
|
|
|
MediaQuery.of(context).size.height * 0.35,
|
2023-12-25 19:03:52 +01:00
|
|
|
child: (chartType == null)
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: (chartType == 1)
|
|
|
|
? ExpensesBarChart(
|
|
|
|
currency: selectedWallet!.currency,
|
|
|
|
date: _selectedDate,
|
|
|
|
locale: locale ?? "en",
|
|
|
|
yearly: yearly,
|
|
|
|
expenseData: (graphTypeSet
|
|
|
|
.contains("expense"))
|
|
|
|
? generateChartData(
|
2023-12-29 21:39:54 +01:00
|
|
|
EntryType.expense,
|
|
|
|
)
|
2023-12-25 19:03:52 +01:00
|
|
|
: [],
|
|
|
|
incomeData: (graphTypeSet
|
|
|
|
.contains("income"))
|
|
|
|
? generateChartData(
|
2023-12-29 21:39:54 +01:00
|
|
|
EntryType.income,
|
|
|
|
)
|
2023-12-25 19:03:52 +01:00
|
|
|
: [],
|
|
|
|
)
|
2024-01-08 15:38:31 +01:00
|
|
|
: Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: ExpensesLineChart(
|
|
|
|
currency:
|
|
|
|
selectedWallet!.currency,
|
|
|
|
date: _selectedDate,
|
|
|
|
locale: locale ?? "en",
|
|
|
|
yearly: yearly,
|
|
|
|
expenseData: (graphTypeSet
|
|
|
|
.contains("expense"))
|
|
|
|
? generateChartData(
|
|
|
|
EntryType.expense,
|
|
|
|
)
|
|
|
|
: [],
|
|
|
|
incomeData: (graphTypeSet
|
|
|
|
.contains("income"))
|
|
|
|
? generateChartData(
|
|
|
|
EntryType.income,
|
|
|
|
)
|
|
|
|
: [],
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2024-01-08 15:38:31 +01:00
|
|
|
const SizedBox(
|
|
|
|
height: 25,
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
color:
|
|
|
|
Theme.of(context).colorScheme.secondaryContainer,
|
|
|
|
),
|
|
|
|
width: MediaQuery.of(context).size.width * 0.95,
|
|
|
|
height: MediaQuery.of(context).size.height * 0.35,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: CategoriesPieChart(
|
|
|
|
symbol: selectedWallet!.currency.symbol,
|
|
|
|
entries: selectedWallet!.entries,
|
|
|
|
categories: selectedWallet!.categories,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
],
|
2023-11-21 20:23:14 +01:00
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|