Layout fixes and Push fixes

- Layout und Kalender angepasst
- Push status meldung angepasst für Android
This commit is contained in:
2026-03-27 11:58:53 +01:00
parent d11f0e46c0
commit 7b824b6f0c
14 changed files with 780 additions and 609 deletions
@@ -5,6 +5,7 @@ using cfs_tracker.Data;
using cfs_tracker.Helpers;
using cfs_tracker.Models;
using cfs_tracker.Services;
using Microsoft.Maui.ApplicationModel;
namespace cfs_tracker.Platforms.Android;
@@ -19,6 +20,10 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
private const string PrefRedLabel = "notification_label_red";
private const string PrefGrayLabel = "notification_label_gray";
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";
public const string ExtraScheduleId = "schedule_id";
public const string ExtraDayOfWeek = "day_of_week";
public const string ExtraHour = "hour";
@@ -28,6 +33,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
public const string ExtraDate = "date";
public const string ExtraStatus = "status";
public const string ExtraCommit = "commit";
public const string ExtraNotificationId = "notification_id";
private readonly NotificationScheduleRepository _notificationScheduleRepository;
private readonly AppearanceService _appearanceService;
@@ -42,8 +48,12 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
public async Task<bool> EnsurePermissionAsync()
{
var permission = await Permissions.RequestAsync<NotificationPermission>();
return permission == PermissionStatus.Granted;
var status = await Permissions.CheckStatusAsync<NotificationPermission>();
if (status == PermissionStatus.Granted)
return true;
status = await Permissions.RequestAsync<NotificationPermission>();
return status == PermissionStatus.Granted;
}
public async Task SyncAsync()
@@ -51,12 +61,13 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
var context = Platform.AppContext;
EnsureChannel(context);
CancelAllScheduled(context);
var granted = await EnsurePermissionAsync();
if (!granted)
return;
SaveActionLabelsToPreferences();
CancelAllScheduled(context);
var schedules = await _notificationScheduleRepository.GetAllAsync();
var requestKeys = new List<string>();
@@ -101,6 +112,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
return;
var intent = new Intent(context, typeof(CfsNotificationReceiver));
intent.SetAction(ActionShowNotification);
intent.PutExtra(ExtraScheduleId, scheduleId);
intent.PutExtra(ExtraDayOfWeek, (int)dayOfWeek);
intent.PutExtra(ExtraHour, hour);
@@ -125,41 +137,20 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
internal static void ShowNotification(
Context context,
int scheduleId,
DayOfWeek dayOfWeek,
string title,
string message)
{
EnsureChannel(context);
var today = DateTime.Today.ToString("yyyy-MM-dd");
var today = DateTime.Today;
var todayText = today.ToString("yyyy-MM-dd");
var notificationId = BuildNotificationId(scheduleId, dayOfWeek);
var openQuickEntryIntent = BuildLaunchIntent(context, today, null, false);
var yellowIntent = BuildLaunchIntent(context, today, "yellow", true);
var redIntent = BuildLaunchIntent(context, today, "red", true);
var grayIntent = BuildLaunchIntent(context, today, "gray", true);
var openPendingIntent = PendingIntent.GetActivity(
context,
scheduleId * 10 + 1,
openQuickEntryIntent,
BuildPendingIntentFlags());
var yellowPendingIntent = PendingIntent.GetActivity(
context,
scheduleId * 10 + 2,
yellowIntent,
BuildPendingIntentFlags());
var redPendingIntent = PendingIntent.GetActivity(
context,
scheduleId * 10 + 3,
redIntent,
BuildPendingIntentFlags());
var grayPendingIntent = PendingIntent.GetActivity(
context,
scheduleId * 10 + 4,
grayIntent,
BuildPendingIntentFlags());
var openPendingIntent = CreateOpenQuickEntryPendingIntent(context, notificationId, todayText);
var yellowPendingIntent = CreateSaveStatusPendingIntent(context, notificationId, "yellow", todayText);
var redPendingIntent = CreateSaveStatusPendingIntent(context, notificationId, "red", todayText);
var grayPendingIntent = CreateSaveStatusPendingIntent(context, notificationId, "gray", todayText);
var yellowLabel = Preferences.Default.Get(PrefYellowLabel, "Gelb");
var redLabel = Preferences.Default.Get(PrefRedLabel, "Rot");
@@ -174,28 +165,45 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
.SetPriority((int)NotificationPriority.High)
.SetAutoCancel(true)
.SetContentIntent(openPendingIntent)
.SetOnlyAlertOnce(true)
.AddAction(0, yellowLabel, yellowPendingIntent)
.AddAction(0, redLabel, redPendingIntent)
.AddAction(0, grayLabel, grayPendingIntent);
NotificationManagerCompat.From(context).Notify(scheduleId, builder.Build());
NotificationManagerCompat.From(context).Notify(notificationId, builder.Build());
}
private static Intent BuildLaunchIntent(Context context, string date, string? status, bool commit)
private static PendingIntent CreateOpenQuickEntryPendingIntent(Context context, int notificationId, string date)
{
var launchIntent = context.PackageManager?.GetLaunchIntentForPackage(context.PackageName)
?? new Intent(context, typeof(MainActivity));
launchIntent.SetAction(ActionOpenQuickEntry);
launchIntent.AddFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop | ActivityFlags.NewTask);
launchIntent.PutExtra(ExtraRoute, "quickentry");
launchIntent.PutExtra(ExtraDate, date);
launchIntent.PutExtra(ExtraNotificationId, notificationId);
if (!string.IsNullOrWhiteSpace(status))
launchIntent.PutExtra(ExtraStatus, status);
return PendingIntent.GetActivity(
context,
100000 + notificationId,
launchIntent,
BuildPendingIntentFlags());
}
launchIntent.PutExtra(ExtraCommit, commit);
private static PendingIntent CreateSaveStatusPendingIntent(Context context, int notificationId, string status, string date)
{
var intent = new Intent(context, typeof(CfsNotificationReceiver));
intent.SetAction(ActionSaveStatus);
intent.PutExtra(ExtraStatus, status);
intent.PutExtra(ExtraDate, date);
intent.PutExtra(ExtraNotificationId, notificationId);
return launchIntent;
return PendingIntent.GetBroadcast(
context,
BuildActionRequestCode(notificationId, status),
intent,
BuildPendingIntentFlags());
}
internal static void EnsureChannel(Context context)
@@ -241,6 +249,8 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
var requestCode = BuildAlarmRequestCode(scheduleId, day);
var intent = new Intent(context, typeof(CfsNotificationReceiver));
intent.SetAction(ActionShowNotification);
var pendingIntent = PendingIntent.GetBroadcast(
context,
requestCode,
@@ -248,7 +258,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
BuildPendingIntentFlags());
alarmManager.Cancel(pendingIntent);
NotificationManagerCompat.From(context).Cancel(scheduleId);
NotificationManagerCompat.From(context).Cancel(BuildNotificationId(scheduleId, day));
}
Preferences.Default.Remove(PrefScheduledIds);
@@ -282,6 +292,22 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
return scheduleId * 10 + (int)day;
}
private static int BuildNotificationId(int scheduleId, DayOfWeek day)
{
return 500000 + scheduleId * 10 + (int)day;
}
private static int BuildActionRequestCode(int notificationId, string status)
{
return status switch
{
"yellow" => 600000 + notificationId * 10 + 1,
"red" => 600000 + notificationId * 10 + 2,
"gray" => 600000 + notificationId * 10 + 3,
_ => 600000 + notificationId * 10
};
}
private static IEnumerable<DayOfWeek> GetScheduledDays(int mask)
{
var flags = (NotificationWeekdays)mask;
@@ -1,4 +1,8 @@
using Android.Content;
using AndroidX.Core.App;
using cfs_tracker.Data;
using cfs_tracker.Models;
using Microsoft.Maui;
namespace cfs_tracker.Platforms.Android;
@@ -10,6 +14,20 @@ public sealed class CfsNotificationReceiver : BroadcastReceiver
if (context is null || intent is null)
return;
switch (intent.Action)
{
case AndroidNotificationScheduler.ActionShowNotification:
HandleAlarmTriggered(context, intent);
break;
case AndroidNotificationScheduler.ActionSaveStatus:
HandleSaveStatus(context, intent);
break;
}
}
private static void HandleAlarmTriggered(Context context, Intent intent)
{
var scheduleId = intent.GetIntExtra(AndroidNotificationScheduler.ExtraScheduleId, 0);
var dayOfWeek = (DayOfWeek)intent.GetIntExtra(AndroidNotificationScheduler.ExtraDayOfWeek, (int)DayOfWeek.Monday);
var hour = intent.GetIntExtra(AndroidNotificationScheduler.ExtraHour, 9);
@@ -18,6 +36,7 @@ public sealed class CfsNotificationReceiver : BroadcastReceiver
AndroidNotificationScheduler.ShowNotification(
context,
scheduleId,
dayOfWeek,
"Wie geht es dir?",
"Tippe, um deinen Status einzutragen.");
@@ -28,4 +47,74 @@ public sealed class CfsNotificationReceiver : BroadcastReceiver
hour,
minute);
}
private void HandleSaveStatus(Context context, Intent intent)
{
var pendingResult = GoAsync();
_ = Task.Run(async () =>
{
try
{
var statusText = intent.GetStringExtra(AndroidNotificationScheduler.ExtraStatus) ?? string.Empty;
var notificationId = intent.GetIntExtra(AndroidNotificationScheduler.ExtraNotificationId, 0);
if (!TryParseState(statusText, out var state))
return;
var services = IPlatformApplication.Current?.Services;
var repository = services?.GetService(typeof(HealthEntryRepository)) as HealthEntryRepository;
if (repository is null)
return;
await repository.AddQuickEntryAsync(
state: state,
activityId: null,
activityDurationMinutes: null,
symptomIds: null,
timestamp: DateTime.Now,
source: EntrySource.App);
NotificationManagerCompat.From(context).Cancel(notificationId);
}
finally
{
pendingResult.Finish();
}
});
}
private static bool TryParseState(string? value, out HealthState state)
{
switch ((value ?? string.Empty).Trim().ToLowerInvariant())
{
case "green":
case "gruen":
case "grün":
state = HealthState.Green;
return true;
case "yellow":
case "gelb":
state = HealthState.Yellow;
return true;
case "red":
case "rot":
state = HealthState.Red;
return true;
case "gray":
case "grey":
case "grau":
case "schwarz":
state = HealthState.Gray;
return true;
default:
state = default;
return false;
}
}
}
+13 -5
View File
@@ -1,4 +1,5 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using cfs_tracker.Services;
@@ -23,25 +24,26 @@ public class MainActivity : MauiAppCompatActivity
HandleNotificationIntent(Intent);
}
protected override void OnNewIntent(Android.Content.Intent? intent)
protected override void OnNewIntent(Intent? intent)
{
base.OnNewIntent(intent);
if (intent is not null)
HandleNotificationIntent(intent);
}
private static void HandleNotificationIntent(Android.Content.Intent? intent)
private static void HandleNotificationIntent(Intent? intent)
{
if (intent is null)
return;
if (!string.Equals(intent.Action, Platforms.Android.AndroidNotificationScheduler.ActionOpenQuickEntry, StringComparison.OrdinalIgnoreCase))
return;
var route = intent.GetStringExtra(Platforms.Android.AndroidNotificationScheduler.ExtraRoute);
if (!string.Equals(route, "quickentry", StringComparison.OrdinalIgnoreCase))
return;
var dateText = intent.GetStringExtra(Platforms.Android.AndroidNotificationScheduler.ExtraDate);
var status = intent.GetStringExtra(Platforms.Android.AndroidNotificationScheduler.ExtraStatus);
var commit = intent.GetBooleanExtra(Platforms.Android.AndroidNotificationScheduler.ExtraCommit, false);
if (!DateTime.TryParse(dateText, out var date))
date = DateTime.Today;
@@ -51,7 +53,13 @@ public class MainActivity : MauiAppCompatActivity
if (navigator is null)
return;
navigator.QueueQuickEntry(date, status, commit);
navigator.QueueQuickEntry(date);
_ = navigator.FlushAsync();
intent.SetAction(string.Empty);
intent.RemoveExtra(Platforms.Android.AndroidNotificationScheduler.ExtraRoute);
intent.RemoveExtra(Platforms.Android.AndroidNotificationScheduler.ExtraDate);
intent.RemoveExtra(Platforms.Android.AndroidNotificationScheduler.ExtraStatus);
intent.RemoveExtra(Platforms.Android.AndroidNotificationScheduler.ExtraCommit);
}
}
+2 -1
View File
@@ -1,8 +1,9 @@
using Android;
using Microsoft.Maui.ApplicationModel;
namespace cfs_tracker.Platforms.Android;
public class NotificationPermission : Permissions.BasePlatformPermission
public sealed class NotificationPermission : Permissions.BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions
{