Refactor "all" logic to avoid using context

This commit is contained in:
Gianfranco Gasbarri 2019-09-10 23:06:48 +01:00
parent 855a7bd911
commit 2a7d89a5f6
3 changed files with 61 additions and 37 deletions

View File

@ -1,6 +1,5 @@
import 'package:built_collection/built_collection.dart';
import 'package:charts_common/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:invoiceninja_flutter/constants.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/redux/dashboard/dashboard_state.dart';
@ -27,20 +26,20 @@ class ChartMoneyData {
final double amount;
}
var memoizedChartInvoices = memo5((BuildContext context,
var memoizedChartInvoices = memo5((BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> invoiceMap,
BuiltMap<int, ClientEntity> clientMap) =>
chartInvoices(
context: context,
currencyMap: currencyMap,
company: company,
settings: settings,
invoiceMap: invoiceMap,
clientMap: clientMap));
List<ChartDataGroup> chartInvoices({
BuildContext context,
BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> invoiceMap,
@ -87,9 +86,9 @@ List<ChartDataGroup> chartInvoices({
// Handle "All"
if (settings.currencyId == kCurrencyAll &&
currencyId != company.currencyId) {
amount *= getExchangeRate(context,
amount *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
balance *= getExchangeRate(context,
balance *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
}
@ -138,20 +137,20 @@ List<ChartDataGroup> chartInvoices({
return data;
}
var memoizedChartQuotes = memo5((BuildContext context,
var memoizedChartQuotes = memo5((BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> quoteMap,
BuiltMap<int, ClientEntity> clientMap) =>
chartQuotes(
context: context,
currencyMap: currencyMap,
company: company,
settings: settings,
quoteMap: quoteMap,
clientMap: clientMap));
List<ChartDataGroup> chartQuotes({
BuildContext context,
BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> quoteMap,
@ -201,7 +200,7 @@ List<ChartDataGroup> chartQuotes({
// Handle "All"
if (settings.currencyId == kCurrencyAll &&
currencyId != company.currencyId) {
amount *= getExchangeRate(context,
amount *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
}
@ -260,17 +259,17 @@ List<ChartDataGroup> chartQuotes({
return data;
}
var memoizedChartPayments = memo6((BuildContext context,
var memoizedChartPayments = memo6((BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> invoiceMap,
BuiltMap<int, ClientEntity> clientMap,
BuiltMap<int, PaymentEntity> paymentMap) =>
chartPayments(
context, company, settings, invoiceMap, clientMap, paymentMap));
currencyMap, company, settings, invoiceMap, clientMap, paymentMap));
List<ChartDataGroup> chartPayments(
BuildContext context,
BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> invoiceMap,
@ -316,9 +315,9 @@ List<ChartDataGroup> chartPayments(
// Handle "All"
if (settings.currencyId == kCurrencyAll &&
currencyId != company.currencyId) {
completedAmount *= getExchangeRate(context,
completedAmount *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
refunded *= getExchangeRate(context,
refunded *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
}
@ -368,7 +367,7 @@ List<ChartDataGroup> chartPayments(
}
List<ChartDataGroup> chartTasks(
BuildContext context,
BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, TaskEntity> taskMap,
@ -421,7 +420,7 @@ List<ChartDataGroup> chartTasks(
// Handle "All"
if (settings.currencyId == kCurrencyAll &&
currencyId != company.currencyId) {
amount *= getExchangeRate(context,
amount *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
}
@ -484,18 +483,18 @@ List<ChartDataGroup> chartTasks(
return data;
}
var memoizedChartTasks = memo7((BuildContext context,
var memoizedChartTasks = memo7((BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, TaskEntity> taskMap,
BuiltMap<int, InvoiceEntity> invoiceMap,
BuiltMap<int, ProjectEntity> projectMap,
BuiltMap<int, ClientEntity> clientMap) =>
chartTasks(context, company, settings, taskMap, invoiceMap, projectMap,
chartTasks(currencyMap, company, settings, taskMap, invoiceMap, projectMap,
clientMap));
List<ChartDataGroup> chartExpenses(
BuildContext context,
BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> invoiceMap,
@ -542,7 +541,7 @@ List<ChartDataGroup> chartExpenses(
// Handle "All"
if (settings.currencyId == kCurrencyAll &&
currencyId != company.currencyId) {
amount *= getExchangeRate(context,
amount *= getExchangeRateWithMap(currencyMap,
fromCurrencyId: currencyId, toCurrencyId: company.currencyId);
}
@ -614,9 +613,9 @@ List<ChartDataGroup> chartExpenses(
return data;
}
var memoizedChartExpenses = memo5((BuildContext context,
var memoizedChartExpenses = memo5((BuiltMap<int, CurrencyEntity> currencyMap,
CompanyEntity company,
DashboardUIState settings,
BuiltMap<int, InvoiceEntity> invoiceMap,
BuiltMap<int, ExpenseEntity> expenseMap) =>
chartExpenses(context, company, settings, invoiceMap, expenseMap));
chartExpenses(currencyMap, company, settings, invoiceMap, expenseMap));

View File

@ -14,6 +14,7 @@ import 'package:invoiceninja_flutter/ui/dashboard/dashboard_chart.dart';
import 'package:invoiceninja_flutter/ui/dashboard/dashboard_vm.dart';
import 'package:invoiceninja_flutter/utils/formatting.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';
import 'package:invoiceninja_flutter/utils/money.dart';
class DashboardPanels extends StatelessWidget {
const DashboardPanels({
@ -185,13 +186,17 @@ class DashboardPanels extends StatelessWidget {
final isLoaded = viewModel.state.invoiceState.isLoaded;
final settings = viewModel.dashboardUIState;
final state = viewModel.state;
final currentData = memoizedChartInvoices(context, state.selectedCompany,
settings, state.invoiceState.map, state.clientState.map);
final currentData = memoizedChartInvoices(
getCurrencyMap(context),
state.selectedCompany,
settings,
state.invoiceState.map,
state.clientState.map);
List<ChartDataGroup> previousData;
if (settings.enableComparison) {
previousData = memoizedChartInvoices(
context,
getCurrencyMap(context),
state.selectedCompany,
settings.rebuild((b) => b..offset += 1),
state.invoiceState.map,
@ -211,7 +216,7 @@ class DashboardPanels extends StatelessWidget {
final settings = viewModel.dashboardUIState;
final state = viewModel.state;
final currentData = memoizedChartPayments(
context,
getCurrencyMap(context),
state.selectedCompany,
settings,
state.invoiceState.map,
@ -221,7 +226,7 @@ class DashboardPanels extends StatelessWidget {
List<ChartDataGroup> previousData;
if (settings.enableComparison) {
previousData = memoizedChartPayments(
context,
getCurrencyMap(context),
state.selectedCompany,
settings.rebuild((b) => b..offset += 1),
state.invoiceState.map,
@ -241,13 +246,17 @@ class DashboardPanels extends StatelessWidget {
final settings = viewModel.dashboardUIState;
final state = viewModel.state;
final isLoaded = state.quoteState.isLoaded;
final currentData = memoizedChartQuotes(context, state.selectedCompany,
settings, state.quoteState.map, state.clientState.map);
final currentData = memoizedChartQuotes(
getCurrencyMap(context),
state.selectedCompany,
settings,
state.quoteState.map,
state.clientState.map);
List<ChartDataGroup> previousData;
if (settings.enableComparison) {
previousData = memoizedChartQuotes(
context,
getCurrencyMap(context),
state.selectedCompany,
settings.rebuild((b) => b..offset += 1),
state.quoteState.map,
@ -268,7 +277,7 @@ class DashboardPanels extends StatelessWidget {
final isLoaded = state.taskState.isLoaded;
final currentData = memoizedChartTasks(
context,
getCurrencyMap(context),
state.selectedCompany,
settings,
state.taskState.map,
@ -279,7 +288,7 @@ class DashboardPanels extends StatelessWidget {
List<ChartDataGroup> previousData;
if (settings.enableComparison) {
previousData = memoizedChartTasks(
context,
getCurrencyMap(context),
state.selectedCompany,
settings.rebuild((b) => b..offset += 1),
state.taskState.map,
@ -300,13 +309,17 @@ class DashboardPanels extends StatelessWidget {
final settings = viewModel.dashboardUIState;
final state = viewModel.state;
final isLoaded = state.expenseState.isLoaded;
final currentData = memoizedChartExpenses(context, state.selectedCompany,
settings, state.invoiceState.map, state.expenseState.map);
final currentData = memoizedChartExpenses(
getCurrencyMap(context),
state.selectedCompany,
settings,
state.invoiceState.map,
state.expenseState.map);
List<ChartDataGroup> previousData;
if (settings.enableComparison) {
previousData = memoizedChartExpenses(
context,
getCurrencyMap(context),
state.selectedCompany,
settings.rebuild((b) => b..offset += 1),
state.invoiceState.map,

View File

@ -1,12 +1,24 @@
import 'package:built_collection/built_collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:invoiceninja_flutter/constants.dart';
import 'package:invoiceninja_flutter/data/models/static/currency_model.dart';
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
double getExchangeRate(BuildContext context,
{int fromCurrencyId, int toCurrencyId}) {
final currencyMap = getCurrencyMap(context);
return getExchangeRateWithMap(currencyMap,
fromCurrencyId: fromCurrencyId, toCurrencyId: toCurrencyId);
}
BuiltMap<int, CurrencyEntity> getCurrencyMap(BuildContext context) {
final state = StoreProvider.of<AppState>(context).state;
final currencyMap = state.staticState.currencyMap;
return state.staticState.currencyMap;
}
double getExchangeRateWithMap(BuiltMap<int, CurrencyEntity> currencyMap,
{int fromCurrencyId, int toCurrencyId}) {
final fromCurrency = currencyMap[fromCurrencyId];
final toCurrency = currencyMap[toCurrencyId];
// TODO replace with data from server