refactor and reuse code for both system notification and dialog notification for app updates

This commit is contained in:
ashilkn 2024-02-12 18:11:43 +05:30
parent 6845a209e2
commit f465e616c4
2 changed files with 33 additions and 41 deletions

View file

@ -73,30 +73,39 @@ class UpdateService {
return _latestVersion;
}
Future<void> showUpdateNotification() async {
if (!isIndependent()) {
return;
}
Future<bool> canShowUpdateNoification() async {
final shouldUpdate = await this.shouldUpdate();
final lastNotificationShownTime =
_prefs.getInt(kUpdateAvailableShownTimeKey) ?? 0;
final now = DateTime.now().microsecondsSinceEpoch;
final hasBeen3DaysSinceLastNotification =
(now - lastNotificationShownTime) > (3 * microSecondsInDay);
if (shouldUpdate &&
hasBeen3DaysSinceLastNotification &&
_latestVersion!.shouldNotify) {
final hasBeenThresholdDaysSinceLastNotification =
(now - lastNotificationShownTime) >
((_latestVersion!.shouldNotify ? 1 : 3) * microSecondsInDay);
return shouldUpdate && hasBeenThresholdDaysSinceLastNotification;
}
Future<void> showUpdateNotification() async {
if (await canShowUpdateNoification()) {
// ignore: unawaited_futures
NotificationService.instance.showNotification(
"Update available",
"Click to install our best version yet",
);
await _prefs.setInt(kUpdateAvailableShownTimeKey, now);
await resetUpdateAvailableShownTime();
} else {
_logger.info("Debouncing notification");
}
}
Future<void> resetUpdateAvailableShownTime() {
return _prefs.setInt(
kUpdateAvailableShownTimeKey,
DateTime.now().microsecondsSinceEpoch,
);
}
Future<LatestVersionInfo> _getLatestVersionInfo() async {
final response = await NetworkClient.instance
.getDio()

View file

@ -11,7 +11,6 @@ import 'package:media_extension/media_extension_action_types.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:move_to_background/move_to_background.dart';
import 'package:photos/core/configuration.dart';
import "package:photos/core/constants.dart";
import 'package:photos/core/event_bus.dart';
import 'package:photos/ente_theme_data.dart';
import 'package:photos/events/account_configured_event.dart';
@ -104,8 +103,6 @@ class _HomeWidgetState extends State<HomeWidget> {
late StreamSubscription<BackupFoldersUpdatedEvent> _backupFoldersUpdatedEvent;
late StreamSubscription<AccountConfiguredEvent> _accountConfiguredEvent;
late StreamSubscription<CollectionUpdatedEvent> _collectionUpdatedEvent;
static const kUpdateAvailableDialogShownTimeKey =
"update_available_dialog_shown_time_key";
@override
void initState() {
@ -197,37 +194,23 @@ class _HomeWidgetState extends State<HomeWidget> {
},
);
_initDeepLinks();
UpdateService.instance.shouldUpdate().then((shouldUpdate) {
if (shouldUpdate) {
Future.delayed(Duration.zero, () {
SharedPreferences.getInstance().then(
(value) {
final lastDialogShownTime =
value.getInt(kUpdateAvailableDialogShownTimeKey) ?? 0;
final now = DateTime.now().microsecondsSinceEpoch;
final hasBeen3DaysSinceLastShown =
(now - lastDialogShownTime) > (3 * microSecondsInDay);
if (hasBeen3DaysSinceLastShown) {
value.setInt(
kUpdateAvailableDialogShownTimeKey,
DateTime.now().microsecondsSinceEpoch,
);
showDialog(
context: context,
builder: (BuildContext context) {
return AppUpdateDialog(
UpdateService.instance.getLatestVersionInfo(),
);
},
barrierColor: Colors.black.withOpacity(0.85),
);
}
UpdateService.instance.canShowUpdateNoification().then((value) {
Future.delayed(Duration.zero, () {
if (value) {
showDialog(
context: context,
builder: (BuildContext context) {
return AppUpdateDialog(
UpdateService.instance.getLatestVersionInfo(),
);
},
barrierColor: Colors.black.withOpacity(0.85),
);
});
}
UpdateService.instance.resetUpdateAvailableShownTime();
}
});
});
// For sharing images coming from outside the app
_initMediaShareSubscription();
WidgetsBinding.instance.addPostFrameCallback(