Options fix

clean up and fixes

- Heute gefixt
- Code Cleanup

NotificationScheduleItemViewModel

-fixed NotificationScheduleItemViewModel for Windows compat

push implementation

Push fixes

Push benachrichtungen eingebaut
- Status bei push
- Push auch in background
- Toggle für symtome angepasst

push system change

exporter update

Optionen fix

- Optionen aufgeteilt auf 3 tabs
- Anpassungen haben jetzt Farbslider

Optimierung fix
This commit is contained in:
2026-03-24 23:35:01 +01:00
parent d85f2e0fb3
commit d11f0e46c0
46 changed files with 2362 additions and 517 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
</manifest>
@@ -0,0 +1,297 @@
using Android.App;
using Android.Content;
using AndroidX.Core.App;
using cfs_tracker.Data;
using cfs_tracker.Helpers;
using cfs_tracker.Models;
using cfs_tracker.Services;
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 ChannelDescription = "Wie geht es dir? Erinnerungen";
private const string PrefScheduledIds = "notification_schedule_ids";
private const string PrefYellowLabel = "notification_label_yellow";
private const string PrefRedLabel = "notification_label_red";
private const string PrefGrayLabel = "notification_label_gray";
public const string ExtraScheduleId = "schedule_id";
public const string ExtraDayOfWeek = "day_of_week";
public const string ExtraHour = "hour";
public const string ExtraMinute = "minute";
public const string ExtraRoute = "route";
public const string ExtraDate = "date";
public const string ExtraStatus = "status";
public const string ExtraCommit = "commit";
private readonly NotificationScheduleRepository _notificationScheduleRepository;
private readonly AppearanceService _appearanceService;
public AndroidNotificationScheduler(
NotificationScheduleRepository notificationScheduleRepository,
AppearanceService appearanceService)
{
_notificationScheduleRepository = notificationScheduleRepository;
_appearanceService = appearanceService;
}
public async Task<bool> EnsurePermissionAsync()
{
var permission = await Permissions.RequestAsync<NotificationPermission>();
return permission == PermissionStatus.Granted;
}
public async Task SyncAsync()
{
var context = Platform.AppContext;
EnsureChannel(context);
var granted = await EnsurePermissionAsync();
if (!granted)
return;
SaveActionLabelsToPreferences();
CancelAllScheduled(context);
var schedules = await _notificationScheduleRepository.GetAllAsync();
var requestKeys = new List<string>();
foreach (var item in schedules.Where(x => x.IsEnabled && x.WeekdayMask != 0))
{
foreach (var day in GetScheduledDays(item.WeekdayMask))
{
ScheduleNextAlarm(context, item.Id, day, item.Hour, item.Minute);
requestKeys.Add($"{item.Id}:{(int)day}");
}
}
Preferences.Default.Set(PrefScheduledIds, string.Join(",", requestKeys));
}
private void SaveActionLabelsToPreferences()
{
Preferences.Default.Set(
PrefYellowLabel,
_appearanceService.GetText(AppearanceKeys.StatusYellowText, "Gelb"));
Preferences.Default.Set(
PrefRedLabel,
_appearanceService.GetText(AppearanceKeys.StatusRedText, "Rot"));
Preferences.Default.Set(
PrefGrayLabel,
_appearanceService.GetText(AppearanceKeys.StatusGrayText, "Schwarz"));
}
internal static void ScheduleNextAlarm(
Context context,
int scheduleId,
DayOfWeek dayOfWeek,
int hour,
int minute)
{
var triggerAt = ComputeNextOccurrence(dayOfWeek, hour, minute);
var alarmManager = (AlarmManager?)context.GetSystemService(Context.AlarmService);
if (alarmManager is null)
return;
var intent = new Intent(context, typeof(CfsNotificationReceiver));
intent.PutExtra(ExtraScheduleId, scheduleId);
intent.PutExtra(ExtraDayOfWeek, (int)dayOfWeek);
intent.PutExtra(ExtraHour, hour);
intent.PutExtra(ExtraMinute, minute);
var requestCode = BuildAlarmRequestCode(scheduleId, dayOfWeek);
var pendingIntent = PendingIntent.GetBroadcast(
context,
requestCode,
intent,
BuildPendingIntentFlags());
var when = triggerAt.ToUnixTimeMilliseconds();
if (OperatingSystem.IsAndroidVersionAtLeast(23))
alarmManager.SetAndAllowWhileIdle(AlarmType.RtcWakeup, when, pendingIntent);
else
alarmManager.Set(AlarmType.RtcWakeup, when, pendingIntent);
}
internal static void ShowNotification(
Context context,
int scheduleId,
string title,
string message)
{
EnsureChannel(context);
var today = DateTime.Today.ToString("yyyy-MM-dd");
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 yellowLabel = Preferences.Default.Get(PrefYellowLabel, "Gelb");
var redLabel = Preferences.Default.Get(PrefRedLabel, "Rot");
var grayLabel = Preferences.Default.Get(PrefGrayLabel, "Schwarz");
var smallIcon = context.ApplicationInfo?.Icon ?? global::Android.Resource.Drawable.IcDialogInfo;
var builder = new NotificationCompat.Builder(context, ChannelId)
.SetSmallIcon(smallIcon)
.SetContentTitle(title)
.SetContentText(message)
.SetPriority((int)NotificationPriority.High)
.SetAutoCancel(true)
.SetContentIntent(openPendingIntent)
.AddAction(0, yellowLabel, yellowPendingIntent)
.AddAction(0, redLabel, redPendingIntent)
.AddAction(0, grayLabel, grayPendingIntent);
NotificationManagerCompat.From(context).Notify(scheduleId, builder.Build());
}
private static Intent BuildLaunchIntent(Context context, string date, string? status, bool commit)
{
var launchIntent = context.PackageManager?.GetLaunchIntentForPackage(context.PackageName)
?? new Intent(context, typeof(MainActivity));
launchIntent.AddFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop | ActivityFlags.NewTask);
launchIntent.PutExtra(ExtraRoute, "quickentry");
launchIntent.PutExtra(ExtraDate, date);
if (!string.IsNullOrWhiteSpace(status))
launchIntent.PutExtra(ExtraStatus, status);
launchIntent.PutExtra(ExtraCommit, commit);
return launchIntent;
}
internal static void EnsureChannel(Context context)
{
if (!OperatingSystem.IsAndroidVersionAtLeast(26))
return;
var manager = (NotificationManager?)context.GetSystemService(Context.NotificationService);
if (manager is null)
return;
var channel = new NotificationChannel(ChannelId, ChannelName, NotificationImportance.Default)
{
Description = ChannelDescription
};
manager.CreateNotificationChannel(channel);
}
private void CancelAllScheduled(Context context)
{
var stored = Preferences.Default.Get(PrefScheduledIds, string.Empty);
if (string.IsNullOrWhiteSpace(stored))
return;
var alarmManager = (AlarmManager?)context.GetSystemService(Context.AlarmService);
if (alarmManager is null)
return;
foreach (var raw in stored.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
var parts = raw.Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (parts.Length != 2)
continue;
if (!int.TryParse(parts[0], out var scheduleId))
continue;
if (!int.TryParse(parts[1], out var dayValue))
continue;
var day = (DayOfWeek)dayValue;
var requestCode = BuildAlarmRequestCode(scheduleId, day);
var intent = new Intent(context, typeof(CfsNotificationReceiver));
var pendingIntent = PendingIntent.GetBroadcast(
context,
requestCode,
intent,
BuildPendingIntentFlags());
alarmManager.Cancel(pendingIntent);
NotificationManagerCompat.From(context).Cancel(scheduleId);
}
Preferences.Default.Remove(PrefScheduledIds);
}
private static PendingIntentFlags BuildPendingIntentFlags()
{
var flags = PendingIntentFlags.UpdateCurrent;
if (OperatingSystem.IsAndroidVersionAtLeast(23))
flags |= PendingIntentFlags.Immutable;
return flags;
}
private static DateTimeOffset ComputeNextOccurrence(DayOfWeek targetDay, int hour, int minute)
{
var now = DateTimeOffset.Now;
var todayAtTime = new DateTimeOffset(now.Year, now.Month, now.Day, hour, minute, 0, now.Offset);
var days = ((int)targetDay - (int)now.DayOfWeek + 7) % 7;
var target = todayAtTime.AddDays(days);
if (target <= now)
target = target.AddDays(7);
return target;
}
private static int BuildAlarmRequestCode(int scheduleId, DayOfWeek day)
{
return scheduleId * 10 + (int)day;
}
private static IEnumerable<DayOfWeek> GetScheduledDays(int mask)
{
var flags = (NotificationWeekdays)mask;
if (flags.HasFlag(NotificationWeekdays.Monday)) yield return DayOfWeek.Monday;
if (flags.HasFlag(NotificationWeekdays.Tuesday)) yield return DayOfWeek.Tuesday;
if (flags.HasFlag(NotificationWeekdays.Wednesday)) yield return DayOfWeek.Wednesday;
if (flags.HasFlag(NotificationWeekdays.Thursday)) yield return DayOfWeek.Thursday;
if (flags.HasFlag(NotificationWeekdays.Friday)) yield return DayOfWeek.Friday;
if (flags.HasFlag(NotificationWeekdays.Saturday)) yield return DayOfWeek.Saturday;
if (flags.HasFlag(NotificationWeekdays.Sunday)) yield return DayOfWeek.Sunday;
}
}
@@ -0,0 +1,31 @@
using Android.Content;
namespace cfs_tracker.Platforms.Android;
[BroadcastReceiver(Enabled = true, Exported = false)]
public sealed class CfsNotificationReceiver : BroadcastReceiver
{
public override void OnReceive(Context? context, Intent? intent)
{
if (context is null || intent is null)
return;
var scheduleId = intent.GetIntExtra(AndroidNotificationScheduler.ExtraScheduleId, 0);
var dayOfWeek = (DayOfWeek)intent.GetIntExtra(AndroidNotificationScheduler.ExtraDayOfWeek, (int)DayOfWeek.Monday);
var hour = intent.GetIntExtra(AndroidNotificationScheduler.ExtraHour, 9);
var minute = intent.GetIntExtra(AndroidNotificationScheduler.ExtraMinute, 0);
AndroidNotificationScheduler.ShowNotification(
context,
scheduleId,
"Wie geht es dir?",
"Tippe, um deinen Status einzutragen.");
AndroidNotificationScheduler.ScheduleNextAlarm(
context,
scheduleId,
dayOfWeek,
hour,
minute);
}
}
+51 -3
View File
@@ -1,9 +1,57 @@
using Android.App;
using Android.Content.PM;
using Android.OS;
using cfs_tracker.Services;
namespace cfs_tracker;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[Activity(
Theme = "@style/Maui.SplashTheme",
MainLauncher = true,
LaunchMode = LaunchMode.SingleTop,
ConfigurationChanges = ConfigChanges.ScreenSize
| ConfigChanges.Orientation
| ConfigChanges.UiMode
| ConfigChanges.ScreenLayout
| ConfigChanges.SmallestScreenSize
| ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle? savedInstanceState){ base.OnCreate(savedInstanceState); }
}
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
HandleNotificationIntent(Intent);
}
protected override void OnNewIntent(Android.Content.Intent? intent)
{
base.OnNewIntent(intent);
if (intent is not null)
HandleNotificationIntent(intent);
}
private static void HandleNotificationIntent(Android.Content.Intent? intent)
{
if (intent is null)
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;
var services = IPlatformApplication.Current?.Services;
var navigator = services?.GetService(typeof(NotificationNavigationService)) as NotificationNavigationService;
if (navigator is null)
return;
navigator.QueueQuickEntry(date, status, commit);
_ = navigator.FlushAsync();
}
}
@@ -0,0 +1,19 @@
using Android;
namespace cfs_tracker.Platforms.Android;
public class NotificationPermission : Permissions.BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions
{
get
{
var result = new List<(string androidPermission, bool isRuntime)>();
if (OperatingSystem.IsAndroidVersionAtLeast(33))
result.Add((Manifest.Permission.PostNotifications, true));
return result.ToArray();
}
}
}
+1 -2
View File
@@ -1,5 +1,4 @@
using Microsoft.UI.Xaml;
using System.Diagnostics;
using System.Diagnostics;
namespace cfs_tracker.WinUI;
@@ -0,0 +1,112 @@
using cfs_tracker.Data;
using cfs_tracker.Models;
using cfs_tracker.Services;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
namespace cfs_tracker.Platforms.Windows;
public sealed class WindowsNotificationScheduler : INotificationScheduler
{
private const string GroupName = "cfs_tracker_weekly";
private readonly NotificationScheduleRepository _notificationScheduleRepository;
public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository)
{
_notificationScheduleRepository = notificationScheduleRepository;
}
public Task<bool> EnsurePermissionAsync()
{
return Task.FromResult(true);
}
public async Task SyncAsync()
{
try
{
CancelExisting();
var schedules = await _notificationScheduleRepository.GetAllAsync();
var notifier = ToastNotificationManagerCompat.CreateToastNotifier();
foreach (var item in schedules.Where(x => x.IsEnabled && x.WeekdayMask != 0))
{
foreach (var day in GetScheduledDays(item.WeekdayMask))
{
var when = ComputeNextOccurrence(day, item.Hour, item.Minute);
var content = new ToastContentBuilder()
.AddArgument("route", "day")
.AddText("Wie geht es dir?")
.AddText("Tippe, um deinen Status einzutragen.")
.GetToastContent();
var scheduled = new ScheduledToastNotification(content.GetXml(), when)
{
Tag = BuildTag(item.Id, day),
Group = GroupName
};
notifier.AddToSchedule(scheduled);
}
}
}
catch
{
// Windows darf hier still ins Leere laufen.
}
}
private static string BuildTag(int id, DayOfWeek day) => $"schedule-{id}-{(int)day}";
private static DateTimeOffset ComputeNextOccurrence(DayOfWeek targetDay, int hour, int minute)
{
var now = DateTimeOffset.Now;
var todayAtTime = new DateTimeOffset(
now.Year,
now.Month,
now.Day,
hour,
minute,
0,
now.Offset);
var days = ((int)targetDay - (int)now.DayOfWeek + 7) % 7;
var target = todayAtTime.AddDays(days);
if (target <= now)
target = target.AddDays(7);
return target;
}
private static void CancelExisting()
{
try
{
var notifier = ToastNotificationManagerCompat.CreateToastNotifier();
var scheduled = notifier.GetScheduledToastNotifications();
foreach (var toast in scheduled.Where(x => x.Group == GroupName).ToList())
notifier.RemoveFromSchedule(toast);
}
catch
{
// bewusst no-op
}
}
private static IEnumerable<DayOfWeek> GetScheduledDays(int mask)
{
var flags = (NotificationWeekdays)mask;
if (flags.HasFlag(NotificationWeekdays.Monday)) yield return DayOfWeek.Monday;
if (flags.HasFlag(NotificationWeekdays.Tuesday)) yield return DayOfWeek.Tuesday;
if (flags.HasFlag(NotificationWeekdays.Wednesday)) yield return DayOfWeek.Wednesday;
if (flags.HasFlag(NotificationWeekdays.Thursday)) yield return DayOfWeek.Thursday;
if (flags.HasFlag(NotificationWeekdays.Friday)) yield return DayOfWeek.Friday;
if (flags.HasFlag(NotificationWeekdays.Saturday)) yield return DayOfWeek.Saturday;
if (flags.HasFlag(NotificationWeekdays.Sunday)) yield return DayOfWeek.Sunday;
}
}