Show a subtle donation snackbar

This commit is contained in:
Ming Ming 2022-05-17 19:29:01 +08:00
parent a69d07d974
commit e3b1c5ed30
3 changed files with 77 additions and 0 deletions

View file

@ -267,6 +267,22 @@ class Pref {
value,
(key, value) => provider.setBool(key, value));
int? getLastDonationDialogTime() =>
provider.getInt(PrefKey.lastDonationDialogTime);
int getLastDonationDialogTimeOr(int def) =>
getLastDonationDialogTime() ?? def;
Future<bool> setLastDonationDialogTime(int value) =>
provider.setInt(PrefKey.lastDonationDialogTime, value);
bool? shouldRemindDonationLater() =>
provider.getBool(PrefKey.shouldRemindDonationLater);
bool shouldRemindDonationLaterOr([bool def = false]) =>
shouldRemindDonationLater() ?? def;
Future<bool> setShouldRemindDonationLater(bool value) => _set<bool>(
PrefKey.shouldRemindDonationLater,
value,
(key, value) => provider.setBool(key, value));
Future<bool> _set<T>(PrefKey key, T value,
Future<bool> Function(PrefKey key, T value) setFn) async {
if (await setFn(key, value)) {
@ -581,6 +597,8 @@ enum PrefKey {
memoriesRange,
saveEditResultToServer,
hasShownSaveEditResultDialog,
lastDonationDialogTime,
shouldRemindDonationLater,
// account pref
isEnableFaceRecognitionApp,
@ -656,6 +674,10 @@ extension on PrefKey {
return "saveEditResultToServer";
case PrefKey.hasShownSaveEditResultDialog:
return "hasShownSaveEditResultDialog";
case PrefKey.lastDonationDialogTime:
return "lastDonationDialogTime";
case PrefKey.shouldRemindDonationLater:
return "shouldRemindDonationLater";
// account pref
case PrefKey.isEnableFaceRecognitionApp:

View file

@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/object_extension.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
class DonationDialogHandler {
Future<void> showIfNeeded(BuildContext context) async {
final lastMs = Pref().getLastDonationDialogTime();
if (lastMs == null) {
// first time
final firstRun =
(Pref().getFirstRunTime()?.run(DateTime.fromMillisecondsSinceEpoch) ??
DateTime.now())
.toUtc();
final now = DateTime.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 = DateTime.now().toUtc();
if (now.isAfter(last) &&
now.difference(last) < const Duration(days: 365)) {
return;
} else {
// 1 year later...
return _show(context);
}
}
}
Future<void> _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(
DateTime.now().toUtc().millisecondsSinceEpoch);
}
}

View file

@ -39,6 +39,7 @@ import 'package:nc_photos/throttler.dart';
import 'package:nc_photos/use_case/startup_sync.dart';
import 'package:nc_photos/widget/album_browser_util.dart' as album_browser_util;
import 'package:nc_photos/widget/builder/photo_list_item_builder.dart';
import 'package:nc_photos/widget/donation_dialog.dart';
import 'package:nc_photos/widget/handler/add_selection_to_album_handler.dart';
import 'package:nc_photos/widget/handler/archive_selection_handler.dart';
import 'package:nc_photos/widget/handler/double_tap_exit_handler.dart';
@ -80,6 +81,10 @@ class _HomePhotosState extends State<HomePhotos>
_web?.onInitState();
_prefUpdatedListener.begin();
_imageProcessorUploadSuccessListener?.begin();
WidgetsBinding.instance.addPostFrameCallback((_) {
DonationDialogHandler().showIfNeeded(context);
});
}
@override