diff --git a/app/lib/entity/pref.dart b/app/lib/entity/pref.dart index 4cac6ea6..11a35d9a 100644 --- a/app/lib/entity/pref.dart +++ b/app/lib/entity/pref.dart @@ -118,6 +118,8 @@ enum PrefKey implements PrefKeyInterface { viewerAppBarButtons, viewerBottomAppBarButtons, homeCollectionsNavBarButtons, + lastDonationDialogTime, + shouldRemindDonationLater, ; @override @@ -218,6 +220,10 @@ enum PrefKey implements PrefKeyInterface { return "viewerBottomAppBarButtons"; case PrefKey.homeCollectionsNavBarButtons: return "homeCollectionsNavBarButtons"; + case PrefKey.lastDonationDialogTime: + return "lastDonationDialogTime"; + case PrefKey.shouldRemindDonationLater: + return "shouldRemindDonationLater"; } } } diff --git a/app/lib/entity/pref/extension.dart b/app/lib/entity/pref/extension.dart index 365eb8b8..5b46ec75 100644 --- a/app/lib/entity/pref/extension.dart +++ b/app/lib/entity/pref/extension.dart @@ -165,6 +165,24 @@ extension PrefExtension on Pref { (key, value) => provider.setBool(key, value)); bool? isNewHttpEngine() => provider.getBool(PrefKey.isNewHttpEngine); + + int? getLastDonationDialogTime() => + provider.getInt(PrefKey.lastDonationDialogTime); + int getLastDonationDialogTimeOr(int def) => + getLastDonationDialogTime() ?? def; + Future setLastDonationDialogTime(int value) => _set( + PrefKey.lastDonationDialogTime, + value, + (key, value) => provider.setInt(key, value)); + + bool? shouldRemindDonationLater() => + provider.getBool(PrefKey.shouldRemindDonationLater); + bool shouldRemindDonationLaterOr([bool def = false]) => + shouldRemindDonationLater() ?? def; + Future setShouldRemindDonationLater(bool value) => _set( + PrefKey.shouldRemindDonationLater, + value, + (key, value) => provider.setBool(key, value)); } extension AccountPrefExtension on AccountPref { diff --git a/app/lib/widget/donation_dialog.dart b/app/lib/widget/donation_dialog.dart new file mode 100644 index 00000000..7762465d --- /dev/null +++ b/app/lib/widget/donation_dialog.dart @@ -0,0 +1,50 @@ +import 'package:clock/clock.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:nc_photos/app_localizations.dart'; +import 'package:nc_photos/controller/pref_controller.dart'; +import 'package:nc_photos/entity/pref.dart'; +import 'package:nc_photos/k.dart' as k; +import 'package:nc_photos/snack_bar_manager.dart'; + +class DonationDialogHandler { + Future showIfNeeded(BuildContext context) async { + final prefController = context.read(); + final lastMs = Pref().getLastDonationDialogTime(); + if (lastMs == null) { + // first time + final firstRun = prefController.firstRunTimeValue ?? clock.now().toUtc(); + final now = clock.now().toUtc(); + if (now.isAfter(firstRun) && + now.difference(firstRun) < const Duration(days: 7)) { + // unnecessary + return; + } else { + return _show(context); + } + } else { + final last = DateTime.fromMillisecondsSinceEpoch(lastMs, isUtc: true); + final now = clock.now().toUtc(); + if (now.isAfter(last) && + now.difference(last) < const Duration(days: 365)) { + return; + } else { + // 1 year later... + return _show(context); + } + } + } + + Future _show(BuildContext context) async { + SnackBarManager().showSnackBar(SnackBar( + content: Text(L10n.global().donationShortMessage), + action: SnackBarAction( + label: L10n.global().donationButtonLabel, + onPressed: () {}, + ), + duration: k.snackBarDurationNormal, + )); + await Pref() + .setLastDonationDialogTime(clock.now().toUtc().millisecondsSinceEpoch); + } +} diff --git a/app/lib/widget/home_photos2.dart b/app/lib/widget/home_photos2.dart index 72b01889..ef5b8748 100644 --- a/app/lib/widget/home_photos2.dart +++ b/app/lib/widget/home_photos2.dart @@ -46,6 +46,7 @@ import 'package:nc_photos/theme/dimension.dart'; import 'package:nc_photos/url_launcher_util.dart'; import 'package:nc_photos/widget/collection_browser.dart'; import 'package:nc_photos/widget/collection_picker.dart'; +import 'package:nc_photos/widget/donation_dialog.dart'; import 'package:nc_photos/widget/double_tap_exit_container/double_tap_exit_container.dart'; import 'package:nc_photos/widget/file_sharer_dialog.dart'; import 'package:nc_photos/widget/finger_listener.dart'; @@ -288,6 +289,10 @@ class _BodyState extends State<_Body> { void initState() { super.initState(); _onBackToTopListener.begin(); + + WidgetsBinding.instance.addPostFrameCallback((_) { + DonationDialogHandler().showIfNeeded(context); + }); } @override