Skip to content

Commit

Permalink
Adds notification settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekspro committed Aug 18, 2024
1 parent 3cef34d commit 2e6cb2e
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Source/Pekspro.RadioStorm.MAUI/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public static MauiApp CreateMauiApp()
builder.Services.TryAddTransient<IVersionMigrator, Pekspro.RadioStorm.MAUI.Platforms.Windows.Services.VersionMigrator>();
builder.Services.TryAddSingleton<IAudioManager, WindowsAudioManager>();
builder.Services.TryAddSingleton<IReviewLauncher, Pekspro.RadioStorm.MAUI.Platforms.Windows.Services.ReviewLauncherWindows>();
builder.Services.TryAddSingleton<INotificationPermissionManager, NotificationPermissionManagerDefault>();
#elif ANDROID
builder.Services.TryAddSingleton<IAudioManager, AndroidAudioManager>();
builder.Services.TryAddSingleton<IReviewLauncher, Pekspro.RadioStorm.MAUI.Platforms.Android.Services.ReviewLauncherAndroid>();
builder.Services.TryAddSingleton<INotificationPermissionManager, Pekspro.RadioStorm.MAUI.Platforms.Android.Services.NotificationPermissionManagerAndroid>();
#endif
builder.Services.AddRadioStorm(builder.Configuration);
builder.Services.AddRadioStormUI(builder.Configuration);
Expand Down
39 changes: 39 additions & 0 deletions Source/Pekspro.RadioStorm.MAUI/Pages/Settings/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,45 @@
<Label Text="{x:Static r:Strings.Settings_Cache_Clearing}" IsVisible="{Binding IsCacheClearing}" />
<Label Text="{x:Static r:Strings.Settings_Cache_Cleared}" IsVisible="{Binding IsCacheCleared}" />

<VerticalStackLayout IsVisible="{Binding NotificationPermissionIsRequired}">

<Rectangle Style="{StaticResource Separator}" />

<Label Text="{x:Static r:Strings.Settings_Notifications_Title}"
Style="{StaticResource SubtitleTextBlockStyle}"
Margin="{OnPlatform WinUI='0,8,0,8', Android='0,8,0,8', Default=0}"
/>

<Label Text="{x:Static r:Strings.Settings_Notifications_Active_Description}"
IsVisible="{Binding HasNotificationPermission}"
Margin="{OnPlatform WinUI='0,0,0,8', Android='0,0,0,8', Default=0}"
/>

<Label Text="{x:Static r:Strings.Settings_Notifications_NotActive_Description}"
IsVisible="{Binding HasNotificationPermission, Converter={StaticResource InvertedBoolConverter}}"
Margin="{OnPlatform WinUI='0,0,0,8', Android='0,0,0,8', Default=0}"
/>

<Label Text="{x:Static r:Strings.Settings_Notifications_Denied_Description}"
IsVisible="{Binding HasNotificationPermissionBeenDenied}"
Margin="{OnPlatform WinUI='0,0,0,8', Android='0,0,0,8', Default=0}"
/>

<Button Command="{Binding RequestNotificationPermissionCommand}"
Text="{x:Static r:Strings.Settings_Notifications_Button_RequirePermissions}"
HorizontalOptions="Start"
IsVisible="{Binding NotificationPermissionsCanBeRequested}"
/>

<Button Command="{Binding RequestNotificationPermissionCommand}"
Text="{x:Static r:Strings.Settings_Notifications_Button_OpenSettings}"
HorizontalOptions="Start"
IsVisible="{Binding NotificationPermissionsCanBeRequested, Converter={StaticResource InvertedBoolConverter}}"
/>

</VerticalStackLayout>


<Rectangle Style="{StaticResource Separator}" />

<views:DownloadSettingsControl />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public SettingsPage(SettingsViewModel viewModel)

private SettingsViewModel ViewModel => (SettingsViewModel) BindingContext;

protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);

ViewModel.RefreshNotificationSettings();
}

protected override bool OnBackButtonPressed()
{
((AppShell)Shell.Current).GoToFavorites();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

</manifest>
45 changes: 40 additions & 5 deletions Source/Pekspro.RadioStorm.MAUI/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Android.App;
using Android;
using Android.App;
using Android.App.Job;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using AndroidX.Core.App;
using Microsoft.Identity.Client;
using Pekspro.RadioStorm.MAUI.Platforms.Android.Services;

Expand All @@ -15,6 +17,10 @@ namespace Pekspro.RadioStorm.MAUI;
LaunchMode = LaunchMode.SingleTop)]
public sealed class MainActivity : MauiAppCompatActivity
{
private const int RequestCodeNotificationPermission = 1001;

public bool HasNotificationPermissionBeenDenied { get; set; }

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Expand Down Expand Up @@ -65,21 +71,50 @@ protected override void OnCreate(Bundle savedInstanceState)
#endregion
}

/*

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
*/

if (requestCode == RequestCodeNotificationPermission)
{
HasNotificationPermissionBeenDenied = true;

if (grantResults.Length > 0)
{
HasNotificationPermissionBeenDenied = grantResults[0] != Permission.Granted;
}

IMessenger messenger = ServiceProviderHelper.GetService<IMessenger>();

messenger?.Send(new NotificationPermissionChangedMessage());
}
}

// <OnActivityResultSnippet>
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
AuthenticationContinuationHelper
.SetAuthenticationContinuationEventArgs(requestCode, resultCode, data);
}
// </OnActivityResultSnippet>

public void RequestNotificationPermission()
{
if (OperatingSystem.IsAndroidVersionAtLeast(33))
{
ActivityCompat.RequestPermissions(this, [Manifest.Permission.PostNotifications], RequestCodeNotificationPermission);
}
}

public void OpenAppSettings()
{
// Open settings view instead.
var intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);
var uri = Android.Net.Uri.FromParts("package", PackageName, null);
intent.SetData(uri);
StartActivity(intent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Android;
using Android.Content;
using Android.Content.PM;
using AndroidX.Core.App;
using AndroidX.Core.Content;

#nullable disable

namespace Pekspro.RadioStorm.MAUI.Platforms.Android.Services;

public class NotificationPermissionManagerAndroid : INotificationPermissionManager
{
private Context Context => Platform.AppContext;

private global::Android.App.Activity Activity => Platform.CurrentActivity;

public bool PermissionIsRequired => OperatingSystem.IsAndroidVersionAtLeast(33);

public bool HasPermission
{
get
{
if (OperatingSystem.IsAndroidVersionAtLeast(33))
{
Permission permission = ContextCompat.CheckSelfPermission(Context, Manifest.Permission.PostNotifications);

return permission == Permission.Granted;
}
else
{
return true;
}
}
}

public bool HasPermissionBeenDenied => ((MainActivity)Activity).HasNotificationPermissionBeenDenied;

public bool ShouldExplainWhyPermissionsIsNeeded
{
get
{
if (OperatingSystem.IsAndroidVersionAtLeast(33))
{
return ActivityCompat.ShouldShowRequestPermissionRationale(Activity, Manifest.Permission.PostNotifications);
}
else
{
return true;
}
}
}

public void RequestPermission()
{
if (HasPermissionBeenDenied || HasPermission)
{
((MainActivity)Activity).OpenAppSettings();
}
else
{
((MainActivity)Activity).RequestNotificationPermission();
}
}
}
2 changes: 2 additions & 0 deletions Source/Pekspro.RadioStorm.Sandbox.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Pekspro.RadioStorm.Sandbox.WPF.LogFile;
using Pekspro.RadioStorm.Sandbox.WPF.Player;
using Pekspro.RadioStorm.Settings.SynchronizedSettings.FileProvider;
using Pekspro.RadioStorm.UI.Utilities;

namespace Pekspro.RadioStorm.Sandbox.WPF;

Expand Down Expand Up @@ -34,6 +35,7 @@ private void ConfigureServices(IConfiguration configuration, IServiceCollection
services.TryAddSingleton<IAudioManager, WpfAudioManager>();
services.TryAddSingleton<IMainThreadRunner, MainThreadRunner>();
services.TryAddSingleton<IVersionProvider, VersionProvider>();
services.TryAddSingleton<INotificationPermissionManager, NotificationPermissionManagerDefault>();
services.AddRadioStorm(configuration);
services.AddRadioStormSandboxTools(configuration);
services.AddRadioStormFileProviders(configuration, true);
Expand Down
54 changes: 54 additions & 0 deletions Source/Pekspro.RadioStorm.UI/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Source/Pekspro.RadioStorm.UI/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,24 @@
<data name="Settings_LiveTile_Title" xml:space="preserve">
<value>Levande panel</value>
</data>
<data name="Settings_Notifications_Active_Description" xml:space="preserve">
<value>Du kommer få en avisering när en nedladdning sker, sovtimer används med mera. Du kan ändra detta i inställningarna.</value>
</data>
<data name="Settings_Notifications_Button_OpenSettings" xml:space="preserve">
<value>Öppna appinställningar.</value>
</data>
<data name="Settings_Notifications_Button_RequirePermissions" xml:space="preserve">
<value>Aktivera aviseringar</value>
</data>
<data name="Settings_Notifications_Denied_Description" xml:space="preserve">
<value>Du har nekat tillgång till aviseringar och behöver därför aktivera detta i inställningarna.</value>
</data>
<data name="Settings_Notifications_NotActive_Description" xml:space="preserve">
<value>Aktivera om du vill se en avisering när en nedladdning sker, sovtimer används mer mera.</value>
</data>
<data name="Settings_Notifications_Title" xml:space="preserve">
<value>Aviseringar</value>
</data>
<data name="Settings_Open_About" xml:space="preserve">
<value>Visa mer information om RadioStorm</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Pekspro.RadioStorm.UI.Utilities;

public interface INotificationPermissionManager
{
bool PermissionIsRequired { get; }

bool HasPermission { get; }

bool HasPermissionBeenDenied { get; }

bool ShouldExplainWhyPermissionsIsNeeded { get; }

void RequestPermission();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Pekspro.RadioStorm.UI.Utilities;

public sealed record NotificationPermissionChangedMessage();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Pekspro.RadioStorm.UI.Utilities;

public class NotificationPermissionManagerDefault : INotificationPermissionManager
{
public bool HasPermission => true;

public bool HasPermissionBeenDenied => false;

public bool PermissionIsRequired => false;

public bool ShouldExplainWhyPermissionsIsNeeded => false;

public void RequestPermission() { }
}
Loading

0 comments on commit 2e6cb2e

Please sign in to comment.