Skip to content

Commit

Permalink
Only show ad info dialog for free users (#1773)
Browse files Browse the repository at this point in the history
This PR fixes that we showed the dialog also to plus users. To fix this,
we listen to the `shouldShowInfoDialog`. It's better to transform this
to a listenable value because the `SubscriptionService` will need a
short moment to load the user document.
  • Loading branch information
nilsreichardt authored Oct 16, 2024
1 parent c9318ee commit 48336be
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
14 changes: 8 additions & 6 deletions app/lib/ads/ads_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AdsController extends ChangeNotifier {

/// Defines if ads are visible for the current user.
bool areAdsVisible = false;
bool shouldShowInfoDialog = false;
StreamSubscription<bool>? _subscription;

AdsController({
Expand Down Expand Up @@ -56,19 +57,19 @@ class AdsController extends ChangeNotifier {
/// Determines if the user should be shown an info dialog about ads.
///
/// We show an info dialog about our ads experiment after the first app open.
bool shouldShowInfoDialog() {
bool _shouldShowInfoDialog() {
if (!isQualifiedForAds()) {
return false;
}

final hasShownDialog =
keyValueStore.getBool('ads-info-dialog-shown') ?? false;
if (!hasShownDialog) {
keyValueStore.setBool('ads-info-dialog-shown', true);
return true;
keyValueStore.getBool('ads-info-dialog-shown22') ?? false;
if (hasShownDialog) {
return false;
}

return false;
keyValueStore.setBool('ads-info-dialog-shown', true);
return true;
}

// Returns the test ad unit ID for the given [format].
Expand Down Expand Up @@ -112,6 +113,7 @@ class AdsController extends ChangeNotifier {
if (hasUnlocked) {
areAdsVisible = false;
} else {
shouldShowInfoDialog = _shouldShowInfoDialog();
areAdsVisible = isQualifiedForAds();
}
notifyListeners();
Expand Down
70 changes: 45 additions & 25 deletions app/lib/dashboard/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,60 @@ class _DashboardPageState extends State<DashboardPage> {
void initState() {
super.initState();
showTipCardIfIsAvailable(context);
maybeShowAdInfoDialog();
}

void maybeShowAdInfoDialog() {
final controller = context.read<AdsController>();
WidgetsBinding.instance.addPostFrameCallback((_) {
final shouldShowDialog = controller.shouldShowInfoDialog();
if (shouldShowDialog) {
showAdInfoDialog(context);
}
});
}

@override
Widget build(BuildContext context) {
return SharezoneCustomScaffold(
appBarConfiguration: SliverAppBarConfiguration(
title: const _AppBarTitle(),
backgroundColor:
Theme.of(context).isDarkTheme ? ElevationColors.dp8 : blueColor,
expandedHeight: 210,
elevation: 1,
pinned: true,
actions: const <Widget>[_ProfileAvatar()],
flexibleSpace: _AppBarBottom(),
drawerIconColor: Colors.white,
return _AdsInfoDialogListener(
child: SharezoneCustomScaffold(
appBarConfiguration: SliverAppBarConfiguration(
title: const _AppBarTitle(),
backgroundColor:
Theme.of(context).isDarkTheme ? ElevationColors.dp8 : blueColor,
expandedHeight: 210,
elevation: 1,
pinned: true,
actions: const <Widget>[_ProfileAvatar()],
flexibleSpace: _AppBarBottom(),
drawerIconColor: Colors.white,
),
navigationItem: NavigationItem.overview,
body: const DashboardPageBody(),
floatingActionButton: const _DashboardPageFAB(),
),
navigationItem: NavigationItem.overview,
body: const DashboardPageBody(),
floatingActionButton: const _DashboardPageFAB(),
);
}
}

class _AdsInfoDialogListener extends StatefulWidget {
const _AdsInfoDialogListener({required this.child});

final Widget child;

@override
State<_AdsInfoDialogListener> createState() => _AdsInfoDialogListenerState();
}

class _AdsInfoDialogListenerState extends State<_AdsInfoDialogListener> {
// The AdsController already has a flag for this, but just to be safe for race
// conditions, we also keep track of it here.
bool hasShownDialog = false;

@override
Widget build(BuildContext context) {
final showDialog = context.watch<AdsController>().shouldShowInfoDialog;
if (showDialog && !hasShownDialog) {
hasShownDialog = true;
context.read<AdsController>().shouldShowInfoDialog = false;

WidgetsBinding.instance.addPostFrameCallback((_) {
showAdInfoDialog(context);
});
}
return widget.child;
}
}

// Wird nur fürs Testing benutzt, weil die Page oben durch unser Scaffold auch
// den Navigation-Stuff per Provider bräuchte, der momentan noch sehr schwer
// testbar zu machen ist, da überall in dem Navigation Flutter-Code 100
Expand Down

0 comments on commit 48336be

Please sign in to comment.