Skip to content

Commit

Permalink
Show in-app rating prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Feb 1, 2024
1 parent d645128 commit e4d9d65
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion JournalApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public App()

public static (DateTimeOffset LeftAt, DateOnly LastDate)? IndexDateState { get; set; }

public int LaunchCount
public static int LaunchCount
{
get => Preferences.Get("launches", 0);
set => Preferences.Set("launches", value);
Expand Down
4 changes: 4 additions & 0 deletions JournalApp/Components/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@inject KeyEventService KeyEventService
@inject NavigationManager NavigationManager
@inject IScrollManager ScrollManager
@inject AppRatingService AppRatingService

<div class="page-title">
<div class="page-header">
Expand Down Expand Up @@ -291,6 +292,9 @@
logger.LogInformation("Saving new note");
note.Category.Points.Add(note);
await db.SaveChangesAsync();

// Better to ask for a rating after a pleasant event like finishing a note than obstructively like on launch.
await AppRatingService.TryShowInAppRatingDialog();
}

ValueTask OnLocationChanging(LocationChangingContext e)
Expand Down
46 changes: 46 additions & 0 deletions JournalApp/Data/AppRatingService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Plugin.Maui.AppRating;

namespace JournalApp;

public class AppRatingService(ILogger<AppRatingService> logger, IAppRating appRating)
{
public async Task TryShowInAppRatingDialog()
{
if (!OperatingSystem.IsAndroid())
{
logger.LogDebug("Not on Android");
return;
}

if (App.LaunchCount < 20 || LastRatingDate + TimeSpan.FromDays(90) > DateTimeOffset.Now)
{
logger.LogDebug("Too soon");
return;
}

logger.LogInformation("Showing in-app rating prompt");
LastRatingDate = DateTimeOffset.Now;

// Doesn't do anything unless installed from the Play Store.
await appRating.PerformInAppRateAsync();
}

private DateTimeOffset LastRatingDate
{
get
{
var lastExportString = Preferences.Get("last_rating", null);

if (DateTimeOffset.TryParse(lastExportString, out var parsed))
{
return parsed;
}
else
{
LastRatingDate = default;
return default;
}
}
set => Preferences.Set("last_rating", value.ToString("O"));
}
}
1 change: 1 addition & 0 deletions JournalApp/JournalApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="MudBlazor" Version="6.15.0" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.133" PrivateAssets="All" />
<PackageReference Include="Plugin.Maui.AppRating" Version="1.1.0" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions JournalApp/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Maui;
using MudBlazor;
using MudBlazor.Services;
using Plugin.Maui.AppRating;

namespace JournalApp;

Expand Down Expand Up @@ -47,6 +48,8 @@ public static MauiApp CreateMauiApp()
builder.Services.AddSingleton<KeyEventService>();
builder.Services.AddSingleton(Share.Default);
builder.Services.AddSingleton<AppThemeService>();
builder.Services.AddSingleton(AppRating.Default);
builder.Services.AddSingleton<AppRatingService>();

// Seed the database.
using var provider = builder.Services.BuildServiceProvider();
Expand Down
25 changes: 25 additions & 0 deletions JournalApp/Resources/Raw/Credits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,31 @@ SOFTWARE.



https://github.com/FabriBertani/Plugin.Maui.AppRating/blob/main/LICENSE
MIT License

Copyright (c) 2023 Fabricio Bertani

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



Inspired by

eMoods: https://play.google.com/store/apps/details?id=com.emoodtracker.wellness
Expand Down

0 comments on commit e4d9d65

Please sign in to comment.