From d64d773afef29a4498d52b79527d895897768694 Mon Sep 17 00:00:00 2001 From: dreautall <109872040+dreautall@users.noreply.github.com> Date: Sat, 9 Sep 2023 17:25:52 +0000 Subject: [PATCH] HomeMain: reduce API calls for category overview Previously, a separate API call was made for every category. Now, only two API calls are made overall (Expense & Income in Categories). --- lib/pages/home/main.dart | 74 ++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/lib/pages/home/main.dart b/lib/pages/home/main.dart index 0e5b130b..850843fb 100644 --- a/lib/pages/home/main.dart +++ b/lib/pages/home/main.dart @@ -41,7 +41,7 @@ class _HomeMainState extends State final Map lastMonthsEarned = {}; final Map lastMonthsSpent = {}; List overviewChartData = []; - final Map catChartData = {}; + final List catChartData = []; final Map budgetInfos = {}; final List possibleChartColors = [ @@ -311,33 +311,54 @@ class _HomeMainState extends State final DateTime now = DateTime.now().toLocal().clearTime(); - final Response respCatData = await api.v1CategoriesGet(); - if (!respCatData.isSuccessful || respCatData.body == null) { + catChartData.clear(); + + final Response respCatIncomeData = + await api.v1InsightIncomeCategoryGet( + start: DateFormat('yyyy-MM-dd', 'en_US').format(now.copyWith(day: 1)), + end: DateFormat('yyyy-MM-dd', 'en_US').format(now), + ); + final Response respCatExpenseData = + await api.v1InsightExpenseCategoryGet( + start: DateFormat('yyyy-MM-dd', 'en_US').format(now.copyWith(day: 1)), + end: DateFormat('yyyy-MM-dd', 'en_US').format(now), + ); + + if (!respCatExpenseData.isSuccessful || respCatExpenseData.body == null) { if (context.mounted) { throw Exception( - S - .of(context) - .errorAPIInvalidResponse(respCatData.error?.toString() ?? ""), + S.of(context).errorAPIInvalidResponse( + respCatExpenseData.error?.toString() ?? ""), ); } else { throw Exception( - "[nocontext] Invalid API response: ${respCatData.error}", + "[nocontext] Invalid API response: ${respCatExpenseData.error}", ); } } - for (CategoryRead e in respCatData.body!.data) { - final Response respCat = await api.v1CategoriesIdGet( - id: e.id, - start: DateFormat('yyyy-MM-dd', 'en_US').format(now.copyWith(day: 1)), - end: DateFormat('yyyy-MM-dd', 'en_US').format(now), - ); - - if (!respCat.isSuccessful || respCat.body == null) { + Map catIncomes = {}; + for (InsightGroupEntry cat + in respCatIncomeData.body ?? []) { + if (cat.id?.isEmpty ?? true) { continue; } + catIncomes[cat.id!] = cat.differenceFloat ?? 0; + } - catChartData[respCat.body!.data.id] = respCat.body!.data.attributes; + for (InsightGroupEntry cat in respCatExpenseData.body!) { + if (cat.id?.isEmpty ?? true) { + continue; + } + double amount = cat.differenceFloat ?? 0; + if (catIncomes.containsKey(cat.id)) { + amount += catIncomes[cat.id]!; + } + // Don't add "positive" categories, we want to show expenses + if (amount >= 0) { + continue; + } + catChartData.add(cat.copyWith(differenceFloat: amount)); } return true; @@ -1252,7 +1273,7 @@ class CategoryChart extends StatelessWidget { required this.possibleChartColors, }); - final Map catChartData; + final List catChartData; final List possibleChartColors; @override @@ -1261,24 +1282,17 @@ class CategoryChart extends StatelessWidget { CurrencyRead defaultCurrency = context.read().defaultCurrency; - catChartData.forEach((_, Category e) { - double sum = 0; - if (e.spent == null) { - return; - } - for (CategorySpent f in e.spent!) { - sum += double.tryParse(f.sum ?? "") ?? 0; - } - if (sum == 0) { - return; + for (InsightGroupEntry e in catChartData) { + if ((e.name?.isEmpty ?? true) || e.differenceFloat == 0) { + continue; } data.add( LabelAmountChart( - e.name, - sum, + e.name!, + e.differenceFloat ?? 0, ), ); - }); + } data.sort((LabelAmountChart a, LabelAmountChart b) => a.amount.compareTo(b.amount));