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

🐛 Check type on hive local data #919

Merged
merged 3 commits into from
Mar 15, 2024
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
230 changes: 0 additions & 230 deletions .github/workflows/manual-build.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

part of 'migration_manager.dart';

final migration_492 = LocalDataMigration(
minAppVersion: 492,
final migration_512 = LocalDataMigration(
minAppVersion: 512,
run: (ref) async {
// We need to get the genesis address for contact's list
// https://github.com/archethic-foundation/archethic-wallet/issues/887
if (ref.read(SessionProviders.session).isLoggedOut) {
log(
'Skipping migration 492 process : user logged out.',
name: logName,
);
return;
}

final contacts = await ref.read(
ContactProviders.fetchContacts().future,
);
Expand Down
5 changes: 3 additions & 2 deletions lib/application/migrations/migration_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part '437.dart';
part '492.dart';
part '512.dart';
part 'migration_manager.freezed.dart';
part 'migration_manager.g.dart';

Expand Down Expand Up @@ -48,7 +48,7 @@ class LocalDataMigrationNotifier

Future<void> migrateLocalData() async {
final preferences = await HivePreferencesDatasource.getInstance();
final currentDataVersion = preferences.getCurrentDataVersion();
final currentDataVersion = await preferences.getCurrentDataVersion();
log('Current data version: $currentDataVersion', name: logName);

final migrations = ref.read(_migrationsProvider)
Expand Down Expand Up @@ -119,6 +119,7 @@ class LocalDataMigration {
@riverpod
List<LocalDataMigration> _migrations(_MigrationsRef ref) => [
migration_437,
migration_512,
];

class CurrentVersionRepository {
Expand Down
2 changes: 1 addition & 1 deletion lib/application/migrations/migration_manager.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion lib/infrastructure/datasources/hive_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// SPDX-License-Identifier: AGPL-3.0-or-later

import 'dart:developer';
import 'dart:ui';

import 'package:aewallet/domain/models/market_price_history.dart';
Expand Down Expand Up @@ -62,6 +63,17 @@ class HivePreferencesDatasource {
T _getValue<T>(dynamic key, {T? defaultValue}) =>
_box.get(key, defaultValue: defaultValue) as T;

Future<void> migrateStringToIntData(String key) async {
log('Migrate local data for $key', name: 'migrateStringToIntData');
final value = _box.get(key);
if (value is String) {
final intValue = int.tryParse(value);
if (intValue != null) {
await _box.put(key, intValue);
}
}
}

Future<void> _setValue<T>(dynamic key, T value) => _box.put(key, value);

Future<void> _removeValue<T>(dynamic key) => _box.delete(key);
Expand All @@ -75,7 +87,20 @@ class HivePreferencesDatasource {
Future<void> setCurrentDataVersion(int version) =>
_setValue(currentVersion, version);

int getCurrentDataVersion() => _getValue(currentVersion, defaultValue: 0);
Future<int> getCurrentDataVersion() async {
try {
return _getValue(currentVersion, defaultValue: 0);
} catch (e) {
log('Error type: $e', name: 'getCurrentDataVersion');
final value = _box.get(currentVersion);
if (value is String) {
await migrateStringToIntData(currentVersion);
return int.tryParse(value) ?? 0;
} else {
return value;
}
}
}

Future<void> setAuthMethod(AuthenticationMethod method) =>
_setValue(authMethod, method.getIndex());
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Fully decentralized and cryptocurrency non-custodial hot wallet tha

publish_to: "none" # Remove this line if you wish to publish to pub.dev

version: 2.3.4+508
version: 2.3.5+511

environment:
sdk: ">=3.3.0 <4.0.0"
Expand Down