Show a subtle donation snackbar

This commit is contained in:
Ming Ming 2022-05-17 19:29:01 +08:00
parent c6a21ee606
commit 41e9b6214a
4 changed files with 79 additions and 0 deletions

View file

@ -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";
}
}
}

View file

@ -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<bool> setLastDonationDialogTime(int value) => _set<int>(
PrefKey.lastDonationDialogTime,
value,
(key, value) => provider.setInt(key, 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));
}
extension AccountPrefExtension on AccountPref {

View file

@ -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<void> showIfNeeded(BuildContext context) async {
final prefController = context.read<PrefController>();
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<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(clock.now().toUtc().millisecondsSinceEpoch);
}
}

View file

@ -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