Skip to content

Commit

Permalink
feat: handle update modal hiding after app update
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-hector committed Dec 24, 2024
1 parent 519632a commit 3858775
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 2 additions & 0 deletions lib/app/components/app_update_handler/app_update_handler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: ice License 1.0

import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:ion/app/components/app_update_handler/hooks/use_app_update.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// SPDX-License-Identifier: ice License 1.0

import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:ion/app/features/config/providers/force_update_provider.c.dart';

bool useAppUpdate(WidgetRef ref) {
final isModalShown = useState(false);

final forceUpdateState = ref.watch(forceUpdateProvider);

useEffect(
() {
if (forceUpdateState.showUpdateModal && !isModalShown.value) {
isModalShown.value = true;
} else if (!forceUpdateState.showUpdateModal && isModalShown.value) {
isModalShown.value = false;
}

return null;
},
[forceUpdateState],
Expand Down
21 changes: 9 additions & 12 deletions lib/app/features/config/providers/force_update_provider.c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:ion/app/features/config/providers/config_provider.c.dart';
import 'package:ion/app/features/config/providers/force_update_last_sync_date_provider.c.dart';
import 'package:ion/app/features/core/providers/app_lifecycle_provider.c.dart';
import 'package:ion/app/services/logger/logger.dart';
import 'package:ion/app/utils/force_update.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
Expand All @@ -30,6 +29,8 @@ class ForceUpdate extends _$ForceUpdate {

@override
ForceUpdateState build() {
_checkAndUpdateConfig();

ref.listen<AppLifecycleState>(
appLifecycleProvider,
(previous, next) {
Expand All @@ -45,8 +46,6 @@ class ForceUpdate extends _$ForceUpdate {
Future<void> _checkAndUpdateConfig() async {
final lastSyncDate = ref.read(forceUpdateLastSyncDateNotifierProvider);

Logger.log('lastSyncDate: $lastSyncDate');

if (lastSyncDate == null ||
DateTime.now().difference(lastSyncDate).inMilliseconds >= eightHoursInMilliseconds) {
final remoteVersion =
Expand All @@ -55,17 +54,15 @@ class ForceUpdate extends _$ForceUpdate {
final packageInfo = await PackageInfo.fromPlatform();
final localVersion = packageInfo.version;

if (ForceUpdateUtil.isVersionOutdated(localVersion, remoteVersion)) {
state = state.copyWith(showUpdateModal: true);
} else {
if (localVersion == remoteVersion) {
state = state.copyWith(showUpdateModal: false);
}

ref.read(forceUpdateLastSyncDateNotifierProvider.notifier).updateLastSyncDate(DateTime.now());
ref
.read(forceUpdateLastSyncDateNotifierProvider.notifier)
.updateLastSyncDate(DateTime.now());
} else if (ForceUpdateUtil.isVersionOutdated(localVersion, remoteVersion)) {
state = state.copyWith(showUpdateModal: true);
}
}
}

void resetUpdateModal() {
state = state.copyWith(showUpdateModal: false);
}
}

0 comments on commit 3858775

Please sign in to comment.