This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Purpose Migrating from Shared Preferences to Hive. https://concordium.atlassian.net/browse/CBW-1474 ## Changes * Migrated from Shared Preferences to Hive * Renamed from `<Prefix>Service` to `<Prefix>Provider` for `StorageProvider` to follow the naming conventions of the Bloc architecture. https://bloclibrary.dev/#/architecture?id=data-provider * Renamed pipeline to just `ci.yml` since test step has been added. ## Checklist - [x] My code follows the style of this project. - [x] The code compiles without warnings. - [x] I have performed a self-review of the changes. - [x] I have documented my code, in particular the intent of the hard-to-understand areas.
- Loading branch information
Showing
18 changed files
with
468 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
part 'accepted_terms_and_conditions.g.dart'; | ||
|
||
/// Version of the Terms & Conditions accepted by the user. | ||
@HiveType(typeId: 1) | ||
class AcceptedTermsAndConditions { | ||
static const table = "accepted_terms_and_conditions"; | ||
|
||
@HiveField(0) | ||
final String version; | ||
@HiveField(1) | ||
final DateTime acceptedAt; | ||
|
||
AcceptedTermsAndConditions({required this.version, required this.acceptedAt}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'package:concordium_wallet/entities/accepted_terms_and_conditions.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
/// Service for interacting with [Hive]. | ||
class StorageProvider { | ||
final LazyBox<AcceptedTermsAndConditions> _acceptedTermsAndConditionBox; | ||
|
||
const StorageProvider._(this._acceptedTermsAndConditionBox); | ||
|
||
static Future<StorageProvider> init() async { | ||
await Hive.initFlutter(); | ||
_registerAdapters(); | ||
await _openBoxes(); | ||
|
||
return StorageProvider._(Hive.lazyBox<AcceptedTermsAndConditions>(AcceptedTermsAndConditions.table)); | ||
} | ||
|
||
/// Register all adapters needed for typed boxes. | ||
static void _registerAdapters() { | ||
Hive.registerAdapter(AcceptedTermsAndConditionsAdapter()); | ||
Hive.registerAdapter(PreciseDateTimeAdapter(), override: true, internal: true); | ||
} | ||
|
||
/// Opens all boxes asynchronously. | ||
static Future<void> _openBoxes() async { | ||
final atcFuture = Hive.openLazyBox<AcceptedTermsAndConditions>(AcceptedTermsAndConditions.table); | ||
await Future.wait([atcFuture]); | ||
} | ||
|
||
LazyBox<AcceptedTermsAndConditions> get acceptedTermsAndConditionBox => _acceptedTermsAndConditionBox; | ||
} | ||
|
||
/// A bit modified DateTimeWithTimezoneAdapter (https://github.com/isar/hive/blob/470473ffc1ba39f6c90f31ababe0ee63b76b69fe/hive/lib/src/adapters/date_time_adapter.dart#L25) | ||
/// This adapter is relevant because by default, [Hive] only stores datetimes down to millisecond precision. | ||
/// It's derived from issue in link and proposed as a solution (https://github.com/isar/hive/issues/474#issuecomment-730562545). | ||
/// The type ID needs to be 18 as it's required to overwrite the existing one registered. (https://github.com/isar/hive/blob/470473ffc1ba39f6c90f31ababe0ee63b76b69fe/hive/lib/src/adapters/date_time_adapter.dart#L28). | ||
class PreciseDateTimeAdapter extends TypeAdapter<DateTime> { | ||
@override | ||
final typeId = 18; | ||
|
||
@override | ||
DateTime read(BinaryReader reader) { | ||
var micros = reader.readInt(); | ||
var isUtc = reader.readBool(); | ||
return DateTime.fromMicrosecondsSinceEpoch(micros, isUtc: isUtc); | ||
} | ||
|
||
@override | ||
void write(BinaryWriter writer, DateTime obj) { | ||
writer.writeInt(obj.microsecondsSinceEpoch); | ||
writer.writeBool(obj.isUtc); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:concordium_wallet/providers/storage.dart'; | ||
import 'package:concordium_wallet/state/terms_and_conditions.dart'; | ||
import 'package:concordium_wallet/entities/accepted_terms_and_conditions.dart'; | ||
|
||
class TermsAndConditionsRepository { | ||
static const String key = "accepted_terms_and_condition"; | ||
|
||
final StorageProvider _storageProvider; | ||
|
||
const TermsAndConditionsRepository({required StorageProvider storageProvider}) : _storageProvider = storageProvider; | ||
|
||
/// Reads the currently accepted T&C version. | ||
Future<AcceptedTermsAndConditionsState?> getAcceptedTermsAndConditions() async { | ||
var model = await _storageProvider.acceptedTermsAndConditionBox.get(key); | ||
return _toState(model); | ||
} | ||
|
||
/// Writes the currently accepted T&C version. | ||
Future<void> writeAcceptedTermsAndConditions(AcceptedTermsAndConditionsState acceptedTermsAndConditions) { | ||
return _storageProvider.acceptedTermsAndConditionBox.put(key, _fromState(acceptedTermsAndConditions)); | ||
} | ||
|
||
/// Deletes the currently accepted T&C version. | ||
Future<void> deleteTermsAndConditionsAcceptedVersion() { | ||
return _storageProvider.acceptedTermsAndConditionBox.delete(key); | ||
} | ||
|
||
AcceptedTermsAndConditions _fromState(AcceptedTermsAndConditionsState state) { | ||
return AcceptedTermsAndConditions(version: state.version, acceptedAt: state.acceptedAt); | ||
} | ||
|
||
AcceptedTermsAndConditionsState? _toState(AcceptedTermsAndConditions? model) { | ||
if (model == null) { | ||
return null; | ||
} | ||
return AcceptedTermsAndConditionsState(version: model.version, acceptedAt: model.acceptedAt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.