Skip to content

Commit

Permalink
store_utils: add fp wrappers for Box
Browse files Browse the repository at this point in the history
  • Loading branch information
marfavi committed Feb 3, 2024
1 parent efba5a9 commit d74d4dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
22 changes: 22 additions & 0 deletions lib/core/store_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,25 @@ extension HiveFP on HiveInterface {
);
}
}

extension HiveBoxFP<E> on Box<E> {
/// [Task] wrapper around [put].
Task<Unit> putAsTask(dynamic key, E value) {
return Task(() => put(key, value)).map((_) => unit);
}

/// [Task] wrapper around [delete].
Task<Unit> deleteAsTask(dynamic key) {
return Task(() => delete(key)).map((_) => unit);
}

/// [Task] wrapper around [clear].
Task<Unit> clearAsTask() {
return Task(() => clear()).map((_) => unit);
}

/// [TaskOption] wrapper around [get].
TaskOption<E> getAsTaskOption(dynamic key) {
return TaskOption(() async => Option.fromNullable(get(key)));
}
}
9 changes: 4 additions & 5 deletions lib/features/ticket/domain/usecases/consume_ticket.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:coffeecard/core/errors/failures.dart';
import 'package:coffeecard/core/store_utils.dart';
import 'package:coffeecard/features/receipt/domain/entities/receipt.dart';
import 'package:coffeecard/features/ticket/data/datasources/ticket_remote_data_source.dart';
import 'package:fpdart/fpdart.dart';
Expand All @@ -22,10 +23,8 @@ class ConsumeTicket {
int productId,
int menuItemId,
) {
return TaskEither(() async {
final cache = await Hive.openBox<int>('lastUsedMenuItemByProductId');
await cache.put(productId, menuItemId);
return const Right(unit);
});
return Hive.openBoxAsTask<int>('lastUsedMenuItemByProductId')
.flatMap((box) => box.putAsTask(productId, menuItemId))
.toTaskEither<Failure>();
}
}
1 change: 1 addition & 0 deletions lib/features/ticket/domain/usecases/load_tickets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LoadTickets {
final getCachedMenuItemId = TaskEither<Failure, int>(() async {
final cache = await Hive.openBox<int>('lastUsedMenuItemByProductId');
return Either.fromNullable(
// TODO(marfavi): Use getAsTaskOption instead.
cache.get(ticket.product.id),
() => const LocalStorageFailure('No last used menu item found'),
);
Expand Down
26 changes: 16 additions & 10 deletions lib/src/authentication/repository.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:coffeecard/core/store_utils.dart';
import 'package:coffeecard/features/authentication.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hive_flutter/hive_flutter.dart';
Expand All @@ -11,25 +12,30 @@ class AuthenticationRepository {
required this.logger,
});

/// The [Box] used to store the [AuthenticationInfo].
///
/// Should be encrypted using [HiveFP.openEncryptedBox].
final Box<AuthenticationInfo> store;
final Logger logger;

/// Store the authentication info in an encrypted Hive box.
Task<Unit> saveAuthenticationInfo(AuthenticationInfo info) {
return Task(() => store.put(_authenticationInfoKey, info))
.map((_) => logger.d('Authentication info saved'))
.map((_) => unit);
return store
.putAsTask(_authenticationInfoKey, info)
.andThen(() => _logMessage('Authentication info saved.'));
}

TaskOption<AuthenticationInfo> getAuthenticationInfo() {
return TaskOption.fromNullable(store.get(_authenticationInfoKey));
return store.getAsTaskOption(_authenticationInfoKey);
}

Task<Unit> clearAuthenticationInfo() {
return Task(() async {
await store.clear();
logger.d('Authentication info cleared');
return unit;
});
return store
.clearAsTask()
.andThen(() => _logMessage('Authentication info cleared.'));
}

Task<Unit> _logMessage(String message) => Task(() async {
logger.d(message);
return unit;
});
}

0 comments on commit d74d4dc

Please sign in to comment.