Push vibrations update
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using Android.App;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using AndroidX.Core.App;
|
||||
using cfs_tracker.Data;
|
||||
@@ -6,20 +6,26 @@ using cfs_tracker.Helpers;
|
||||
using cfs_tracker.Models;
|
||||
using cfs_tracker.Services;
|
||||
using Microsoft.Maui.ApplicationModel;
|
||||
using Microsoft.Maui.Storage;
|
||||
|
||||
namespace cfs_tracker.Platforms.Android;
|
||||
|
||||
public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
{
|
||||
private const string ChannelId = "cfs_tracker_reminders";
|
||||
private const string ChannelName = "Erinnerungen";
|
||||
private const string ChannelIdSilent = "cfs_tracker_reminders_silent";
|
||||
private const string ChannelIdVibrate = "cfs_tracker_reminders_vibrate";
|
||||
private const string ChannelNameSilent = "Erinnerungen";
|
||||
private const string ChannelNameVibrate = "Erinnerungen mit Vibration";
|
||||
private const string ChannelDescription = "Wie geht es dir? Erinnerungen";
|
||||
private const string PrefScheduledIds = "notification_schedule_ids";
|
||||
private const string PrefVibrationEnabled = "notification_vibration_enabled";
|
||||
|
||||
private const string PrefYellowLabel = "notification_label_yellow";
|
||||
private const string PrefRedLabel = "notification_label_red";
|
||||
private const string PrefGrayLabel = "notification_label_gray";
|
||||
|
||||
private static readonly long[] NotificationVibrationPattern = new long[] { 0, 250, 150, 250 };
|
||||
|
||||
public const string ActionShowNotification = "cfs_tracker.action.SHOW_NOTIFICATION";
|
||||
public const string ActionSaveStatus = "cfs_tracker.action.SAVE_STATUS";
|
||||
public const string ActionOpenQuickEntry = "cfs_tracker.action.OPEN_QUICK_ENTRY";
|
||||
@@ -56,10 +62,19 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
return status == PermissionStatus.Granted;
|
||||
}
|
||||
|
||||
public Task<bool> GetVibrationEnabledAsync() =>
|
||||
Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false));
|
||||
|
||||
public Task SetVibrationEnabledAsync(bool isEnabled)
|
||||
{
|
||||
Preferences.Default.Set(PrefVibrationEnabled, isEnabled);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task SyncAsync()
|
||||
{
|
||||
var context = Platform.AppContext;
|
||||
EnsureChannel(context);
|
||||
EnsureChannels(context);
|
||||
|
||||
CancelAllScheduled(context);
|
||||
|
||||
@@ -141,7 +156,10 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
string title,
|
||||
string message)
|
||||
{
|
||||
EnsureChannel(context);
|
||||
EnsureChannels(context);
|
||||
|
||||
var vibrationEnabled = Preferences.Default.Get(PrefVibrationEnabled, false);
|
||||
var channelId = GetChannelId(vibrationEnabled);
|
||||
|
||||
var today = DateTime.Today;
|
||||
var todayText = today.ToString("yyyy-MM-dd");
|
||||
@@ -158,7 +176,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
|
||||
var smallIcon = context.ApplicationInfo?.Icon ?? global::Android.Resource.Drawable.IcDialogInfo;
|
||||
|
||||
var builder = new NotificationCompat.Builder(context, ChannelId)
|
||||
var builder = new NotificationCompat.Builder(context, channelId)
|
||||
.SetSmallIcon(smallIcon)
|
||||
.SetContentTitle(title)
|
||||
.SetContentText(message)
|
||||
@@ -170,6 +188,9 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
.AddAction(0, redLabel, redPendingIntent)
|
||||
.AddAction(0, grayLabel, grayPendingIntent);
|
||||
|
||||
if (vibrationEnabled && !OperatingSystem.IsAndroidVersionAtLeast(26))
|
||||
builder.SetVibrate(NotificationVibrationPattern);
|
||||
|
||||
NotificationManagerCompat.From(context).Notify(notificationId, builder.Build());
|
||||
}
|
||||
|
||||
@@ -206,7 +227,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
BuildPendingIntentFlags());
|
||||
}
|
||||
|
||||
internal static void EnsureChannel(Context context)
|
||||
internal static void EnsureChannels(Context context)
|
||||
{
|
||||
if (!OperatingSystem.IsAndroidVersionAtLeast(26))
|
||||
return;
|
||||
@@ -215,12 +236,20 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
if (manager is null)
|
||||
return;
|
||||
|
||||
var channel = new NotificationChannel(ChannelId, ChannelName, NotificationImportance.Default)
|
||||
var silentChannel = new NotificationChannel(ChannelIdSilent, ChannelNameSilent, NotificationImportance.Default)
|
||||
{
|
||||
Description = ChannelDescription
|
||||
};
|
||||
silentChannel.EnableVibration(false);
|
||||
manager.CreateNotificationChannel(silentChannel);
|
||||
|
||||
manager.CreateNotificationChannel(channel);
|
||||
var vibrationChannel = new NotificationChannel(ChannelIdVibrate, ChannelNameVibrate, NotificationImportance.Default)
|
||||
{
|
||||
Description = ChannelDescription
|
||||
};
|
||||
vibrationChannel.EnableVibration(true);
|
||||
vibrationChannel.SetVibrationPattern(NotificationVibrationPattern);
|
||||
manager.CreateNotificationChannel(vibrationChannel);
|
||||
}
|
||||
|
||||
private void CancelAllScheduled(Context context)
|
||||
@@ -287,6 +316,9 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
|
||||
return target;
|
||||
}
|
||||
|
||||
private static string GetChannelId(bool vibrationEnabled) =>
|
||||
vibrationEnabled ? ChannelIdVibrate : ChannelIdSilent;
|
||||
|
||||
private static int BuildAlarmRequestCode(int scheduleId, DayOfWeek day)
|
||||
{
|
||||
return scheduleId * 10 + (int)day;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using cfs_tracker.Data;
|
||||
using cfs_tracker.Models;
|
||||
using cfs_tracker.Services;
|
||||
using Microsoft.Maui.Storage;
|
||||
using Microsoft.Toolkit.Uwp.Notifications;
|
||||
using Windows.UI.Notifications;
|
||||
|
||||
@@ -9,6 +10,8 @@ namespace cfs_tracker.Platforms.Windows;
|
||||
public sealed class WindowsNotificationScheduler : INotificationScheduler
|
||||
{
|
||||
private const string GroupName = "cfs_tracker_weekly";
|
||||
private const string PrefVibrationEnabled = "notification_vibration_enabled";
|
||||
|
||||
private readonly NotificationScheduleRepository _notificationScheduleRepository;
|
||||
|
||||
public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository)
|
||||
@@ -21,6 +24,17 @@ public sealed class WindowsNotificationScheduler : INotificationScheduler
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
public Task<bool> GetVibrationEnabledAsync()
|
||||
{
|
||||
return Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false));
|
||||
}
|
||||
|
||||
public Task SetVibrationEnabledAsync(bool isEnabled)
|
||||
{
|
||||
Preferences.Default.Set(PrefVibrationEnabled, isEnabled);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task SyncAsync()
|
||||
{
|
||||
try
|
||||
|
||||
@@ -3,5 +3,7 @@ namespace cfs_tracker.Services;
|
||||
public interface INotificationScheduler
|
||||
{
|
||||
Task<bool> EnsurePermissionAsync();
|
||||
Task<bool> GetVibrationEnabledAsync();
|
||||
Task SetVibrationEnabledAsync(bool isEnabled);
|
||||
Task SyncAsync();
|
||||
}
|
||||
@@ -1,7 +1,21 @@
|
||||
using Microsoft.Maui.Storage;
|
||||
|
||||
namespace cfs_tracker.Services;
|
||||
|
||||
public sealed class NoOpNotificationScheduler : INotificationScheduler
|
||||
{
|
||||
private const string PrefVibrationEnabled = "notification_vibration_enabled";
|
||||
|
||||
public Task<bool> EnsurePermissionAsync() => Task.FromResult(true);
|
||||
|
||||
public Task<bool> GetVibrationEnabledAsync() =>
|
||||
Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false));
|
||||
|
||||
public Task SetVibrationEnabledAsync(bool isEnabled)
|
||||
{
|
||||
Preferences.Default.Set(PrefVibrationEnabled, isEnabled);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SyncAsync() => Task.CompletedTask;
|
||||
}
|
||||
@@ -42,6 +42,9 @@ public partial class OptionsViewModel : ObservableObject
|
||||
[ObservableProperty]
|
||||
private string currentDatabasePath = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool notificationVibrationEnabled;
|
||||
|
||||
[ObservableProperty]
|
||||
private OptionsTab selectedTab = OptionsTab.Notifications;
|
||||
|
||||
@@ -139,6 +142,7 @@ public partial class OptionsViewModel : ObservableObject
|
||||
await LoadSymptomsAsync();
|
||||
await LoadSchedulesAsync();
|
||||
|
||||
NotificationVibrationEnabled = await _notificationScheduler.GetVibrationEnabledAsync();
|
||||
UseExternalDatabase = _databaseLocationService.UseExternalDatabase;
|
||||
ExternalDatabasePath = _databaseLocationService.ExternalDatabasePath;
|
||||
CurrentDatabasePath = _appDatabase.CurrentPath;
|
||||
@@ -438,6 +442,8 @@ public partial class OptionsViewModel : ObservableObject
|
||||
|
||||
private async Task SaveSchedulesAsync()
|
||||
{
|
||||
await _notificationScheduler.SetVibrationEnabledAsync(NotificationVibrationEnabled);
|
||||
|
||||
var granted = await _notificationScheduler.EnsurePermissionAsync();
|
||||
if (!granted)
|
||||
{
|
||||
@@ -464,7 +470,9 @@ public partial class OptionsViewModel : ObservableObject
|
||||
await _notificationScheduler.SyncAsync();
|
||||
await LoadSchedulesAsync();
|
||||
|
||||
StatusText = "Benachrichtigungszeiten gespeichert.";
|
||||
StatusText = NotificationVibrationEnabled
|
||||
? "Benachrichtigungszeiten gespeichert. Vibration ist aktiv."
|
||||
: "Benachrichtigungszeiten gespeichert. Vibration ist aus.";
|
||||
}
|
||||
|
||||
private Task DeleteScheduleAsync(NotificationScheduleItemViewModel? item)
|
||||
|
||||
@@ -153,6 +153,20 @@
|
||||
<Label Text="Ein Zeitblock kann auf mehreren Wochentagen aktiv sein."
|
||||
TextColor="{DynamicResource TextMuted}" />
|
||||
|
||||
<Grid ColumnDefinitions="Auto,*"
|
||||
ColumnSpacing="8">
|
||||
<CheckBox IsChecked="{Binding NotificationVibrationEnabled}" />
|
||||
<VerticalStackLayout Grid.Column="1"
|
||||
Spacing="2">
|
||||
<Label Text="Vibration für Push-Nachrichten"
|
||||
VerticalOptions="Center"
|
||||
TextColor="{DynamicResource TextPrimary}" />
|
||||
<Label Text="Auf Android wird dafür automatisch der passende Benachrichtigungskanal verwendet."
|
||||
FontSize="12"
|
||||
TextColor="{DynamicResource TextMuted}" />
|
||||
</VerticalStackLayout>
|
||||
</Grid>
|
||||
|
||||
<Button Text="Zeit hinzufügen"
|
||||
Style="{StaticResource PrimaryButtonStyle}"
|
||||
Command="{Binding AddScheduleCommand}" />
|
||||
|
||||
Reference in New Issue
Block a user