Skip to content

Commit

Permalink
feat: optimisticly update members balance
Browse files Browse the repository at this point in the history
Rough draft of updating group members balance after creating an expense or payment.
  • Loading branch information
Jan Czizikow committed Apr 26, 2020
1 parent 2a34e2e commit a00fce0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
22 changes: 15 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,36 +198,44 @@ class _MyAppState extends State<MyApp> {
child: Consumer<Api>(
builder: (_, api, __) => MultiProvider(
providers: [
ChangeNotifierProxyProvider2<Api, Auth, AccountProvider>(
ChangeNotifierProxyProvider<Auth, AccountProvider>(
create: (_) => AccountProvider(api),
update: (_, api, auth, account) {
update: (_, auth, account) {
if (auth.forceLogoutTimestamp != null) {
account.reset();
}
return account;
},
),
ChangeNotifierProxyProvider2<Api, Auth, GroupsProvider>(
ChangeNotifierProxyProvider<Auth, GroupsProvider>(
create: (_) => GroupsProvider(api),
update: (_, api, auth, groups) {
update: (_, auth, groups) {
if (auth.forceLogoutTimestamp != null) {
groups.reset();
}
groups.isAuthenticated =
auth.status == AuthStatus.AUTHENTICATED;
return groups;
}),
ChangeNotifierProxyProvider2<Api, Auth, InvitesProvider>(
ChangeNotifierProxyProvider<Auth, InvitesProvider>(
create: (_) => InvitesProvider(api),
update: (_, api, auth, invites) {
update: (_, auth, invites) {
if (auth.forceLogoutTimestamp != null) {
invites.reset();
}
return invites;
},
),
ChangeNotifierProvider(
ChangeNotifierProxyProvider2<Auth, GroupsProvider,
ExpensesProvider>(
create: (_) => ExpensesProvider(api),
update: (_, auth, groups, expenses) {
expenses.groupsProvider = groups;
if (auth.forceLogoutTimestamp != null) {
expenses.reset();
}
return expenses;
},
),
],
child: Selector2<ThemeProvider, Auth, Tuple2<ThemeData, int>>(
Expand Down
18 changes: 18 additions & 0 deletions lib/models/group.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:sliceit/models/member.dart';
import 'package:sliceit/models/share.dart';

class Group {
final String id;
Expand Down Expand Up @@ -49,4 +50,21 @@ class Group {
String memberFirstNameByUserId(String userId) {
return members.firstWhere((member) => member.userId == userId).firstName;
}

Member memberByUserId(String userId) {
return members.firstWhere((member) => member.userId == userId);
}

void optimisticBalanceUpdate(int total, List<Share> shares, String payerId) {
int diff;
// TODO: REFACTOR nested loop
for (Share share in shares) {
if (share.userId == payerId) {
diff = total - share.amount;
memberByUserId(share.userId).balance += diff;
} else {
memberByUserId(share.userId).balance -= share.amount;
}
}
}
}
26 changes: 22 additions & 4 deletions lib/providers/expenses.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:sliceit/models/expense.dart';
import 'package:sliceit/models/share.dart';
import 'package:sliceit/providers/base.dart';
import 'package:sliceit/providers/groups.dart';
import 'package:sliceit/services/api.dart';

class ExpensesProvider extends BaseProvider {
final Api _api;
final Map<String, List<Expense>> _expensesByGroupId = {};
GroupsProvider _groupsProvider;

ExpensesProvider(this._api);

set groupsProvider(GroupsProvider groupsProvider) {
_groupsProvider = groupsProvider;
}

List<Expense> byGroupId(String groupId) {
if (_expensesByGroupId.containsKey(groupId)) {
return _expensesByGroupId[groupId];
Expand Down Expand Up @@ -57,7 +64,7 @@ class ExpensesProvider extends BaseProvider {
@required String currency,
@required String date,
}) async {
print(shares);
assert(_groupsProvider != null);
status = Status.PENDING;
try {
final Expense expense = await _api.createExpense(
Expand All @@ -69,12 +76,16 @@ class ExpensesProvider extends BaseProvider {
currency: currency,
date: date,
);

if (_expensesByGroupId.containsKey(groupId)) {
_expensesByGroupId[groupId].insert(0, expense);
} else {
_expensesByGroupId[groupId] = [expense];
}
// TODO: Apply balance to the group!

_groupsProvider.selectedGroup
.optimisticBalanceUpdate(amount, shares, payerId);

status = Status.RESOLVED;
} catch (e) {
status = Status.REJECTED;
Expand All @@ -90,6 +101,7 @@ class ExpensesProvider extends BaseProvider {
@required String currency,
@required String date,
}) async {
assert(_groupsProvider != null);
status = Status.PENDING;
try {
final Expense payment = await _api.createPayment(
Expand All @@ -100,20 +112,26 @@ class ExpensesProvider extends BaseProvider {
currency: currency,
date: date,
);

if (_expensesByGroupId.containsKey(groupId)) {
_expensesByGroupId[groupId].insert(0, payment);
} else {
_expensesByGroupId[groupId] = [payment];
}
// TODO: Apply balance to the group!

_groupsProvider.selectedGroup.optimisticBalanceUpdate(
amount,
[Share(userId: from, amount: 0), Share(userId: to, amount: amount)],
from,
);

status = Status.RESOLVED;
} catch (e) {
status = Status.REJECTED;
rethrow;
}
}

// TODO: reset
void reset() {
_expensesByGroupId.clear();
}
Expand Down

0 comments on commit a00fce0

Please sign in to comment.