Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing non finalized transaction processing, and display name for apple #21

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/controllers/fetch/price_tables_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:how_much/custom_types.dart';
import 'package:how_much/util/firebase_init.dart';
import 'package:how_much/util/helper_funcs.dart';
import 'package:how_much/util/parsing.dart';

class PriceTablesController extends GetxController with WidgetsBindingObserver {
final _firebaseService = FirebaseService();
Expand Down Expand Up @@ -90,13 +91,25 @@ class PriceTablesController extends GetxController with WidgetsBindingObserver {
/// if provided, queries the given price table
/// if not, queries the current (the newest) price table
double? getPriceForAsset(AssetType assetType, String assetId,
{String priceTableDate = ""}) {
if (priceTableDate == "") {
{String priceTableDateString = ""}) {
if (priceTableDateString == "") {
return priceTables
.priceTableMap.values.last.data[assetType]?.entries[assetId];
} else {
return priceTables
.priceTableMap[priceTableDate]!.data[assetType]?.entries[assetId];
DateTime priceTableDateUTC = parseDateWithHour(priceTableDateString);
DateTime now = DateTime.now();
DateTime nowUTC = now.subtract(now.timeZoneOffset);
if (priceTableDateUTC.difference(nowUTC).inHours > 0) {
// last price table date is later than `now`, means we are dealing
// with a non-finalized snapshot, and this price table does not exist yet
// use the previous price table for now
// this can only happen when we are processing a non-finalized transaction
priceTableDateString = formatDateWithHour(
priceTableDateUTC.subtract(const Duration(hours: 6)));
}

return priceTables.priceTableMap[priceTableDateString]!.data[assetType]
?.entries[assetId];
}
}
}
17 changes: 12 additions & 5 deletions lib/controllers/fetch/transactions_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ class TransactionsController extends GetxController {

await _fetchPriceTablesWithDate(sortedKeys.first);

// dummy value that does not affect the computation (null would require additional checks)
DateTime lastCreatedSnapshotDate = DateTime.now();
// we use this value to compare how many snapshots were in between current
// and last transaction. Since the current transaction may not be finalized
// we need to add at least 6 hours to `now` to ensure this check will work
// correctly for the first time. We could have made this null, but then
// we would need additional null checks for every iteration.
DateTime lastCreatedSnapshotDate =
DateTime.now().add(const Duration(days: 1));

sortedTransactions.forEach((date, transactionData) {
// Extract transaction details
Expand Down Expand Up @@ -115,7 +120,7 @@ class TransactionsController extends GetxController {
Asset(
amount: 0,
price: _priceTablesController.getPriceForAsset(type, assetId,
priceTableDate: nextSnapshotDateString)!,
priceTableDateString: nextSnapshotDateString)!,
category: type.name,
);
}
Expand Down Expand Up @@ -147,8 +152,10 @@ class TransactionsController extends GetxController {
// so we should use the previous snapshot date, in order to fetch the correct price tables

// find the previous update time for that timestamp with rounding down
String previousUpdateTime =
getPreviousUpdateTime(DateTime.parse(timestamp));
// we subtract 6 for the case the transaction may not be finalized,
// if so, we will need the previous price table
String previousUpdateTime = getPreviousUpdateTime(
DateTime.parse(timestamp).subtract(const Duration(hours: 6)));

await _priceTablesController.fetchPriceTables(previousUpdateTime);
}
Expand Down
9 changes: 8 additions & 1 deletion lib/controllers/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,12 @@ class LoginController extends GetxController {
FirebaseAuth.instance.signOut();
}

String get displayName => _firebaseUser!.displayName!;
String get displayName {
try {
return " ${_firebaseUser!.displayName!}";
} catch (_) {
// cannot fetch display name if the auth provider is apple
return "";
}
}
}
2 changes: 1 addition & 1 deletion lib/controllers/snapshots_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class SnapshotsController extends GetxController {
assetMap.forEach((assetId, asset) {
double price = _priceTablesController.getPriceForAsset(
assetType, assetId,
priceTableDate: priceTableDateUtc)!;
priceTableDateString: priceTableDateUtc)!;
assets.typeMap[assetType]![assetId]!.price = price;
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/presentation/pages/main/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DashboardPage extends StatelessWidget {
DashboardPage({super.key});

final dateController = Get.find<DateController>();
final googleController = Get.find<LoginController>();
final loginController = Get.find<LoginController>();
final reportController = Get.find<ReportController>();
final navigationController = Get.find<NavigationController>();

Expand All @@ -41,7 +41,7 @@ class DashboardPage extends StatelessWidget {
Row(
children: [
Text(
"👋 Welcome ${googleController.displayName.split(" ")[0]}!",
"👋 Welcome${loginController.displayName.split(" ")[0]}!",
style: welcomeTextStyle),
const Spacer(),
CircleAvatar(
Expand Down
Loading