d11f0e46c0
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
174 lines
4.5 KiB
C#
174 lines
4.5 KiB
C#
using cfs_tracker.Models;
|
|
|
|
namespace cfs_tracker.Data;
|
|
|
|
public class NotificationScheduleRepository
|
|
{
|
|
private readonly AppDatabase _database;
|
|
|
|
public NotificationScheduleRepository(AppDatabase database)
|
|
{
|
|
_database = database;
|
|
}
|
|
|
|
public async Task<List<NotificationSchedule>> GetAllAsync()
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
var list = await _database.Connection.Table<NotificationSchedule>()
|
|
.ToListAsync();
|
|
|
|
return list
|
|
.OrderBy(x => x.Hour)
|
|
.ThenBy(x => x.Minute)
|
|
.ThenBy(x => x.Id)
|
|
.ToList();
|
|
}
|
|
|
|
public async Task<NotificationSchedule?> GetByIdAsync(int id)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
return await _database.Connection.Table<NotificationSchedule>()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
}
|
|
|
|
public async Task<int> AddAsync(
|
|
int weekdayMask,
|
|
int hour,
|
|
int minute,
|
|
bool isEnabled = true)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
var item = new NotificationSchedule
|
|
{
|
|
WeekdayMask = NormalizeWeekdayMask(weekdayMask),
|
|
Hour = NormalizeHour(hour),
|
|
Minute = NormalizeMinute(minute),
|
|
IsEnabled = isEnabled
|
|
};
|
|
|
|
await _database.Connection.InsertAsync(item);
|
|
return item.Id;
|
|
}
|
|
|
|
public async Task SaveAsync(NotificationSchedule item)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
if (item is null)
|
|
return;
|
|
|
|
item.WeekdayMask = NormalizeWeekdayMask(item.WeekdayMask);
|
|
item.Hour = NormalizeHour(item.Hour);
|
|
item.Minute = NormalizeMinute(item.Minute);
|
|
|
|
if (item.Id <= 0)
|
|
{
|
|
await _database.Connection.InsertAsync(item);
|
|
return;
|
|
}
|
|
|
|
var existing = await _database.Connection.Table<NotificationSchedule>()
|
|
.FirstOrDefaultAsync(x => x.Id == item.Id);
|
|
|
|
if (existing is null)
|
|
{
|
|
await _database.Connection.InsertAsync(item);
|
|
return;
|
|
}
|
|
|
|
existing.WeekdayMask = item.WeekdayMask;
|
|
existing.Hour = item.Hour;
|
|
existing.Minute = item.Minute;
|
|
existing.IsEnabled = item.IsEnabled;
|
|
|
|
await _database.Connection.UpdateAsync(existing);
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
var existing = await _database.Connection.Table<NotificationSchedule>()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (existing is not null)
|
|
await _database.Connection.DeleteAsync(existing);
|
|
}
|
|
|
|
public async Task DeleteAllAsync()
|
|
{
|
|
await _database.InitAsync();
|
|
await _database.Connection.DeleteAllAsync<NotificationSchedule>();
|
|
}
|
|
|
|
public async Task ReplaceAllAsync(IEnumerable<NotificationSchedule>? items)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
var normalized = (items ?? Enumerable.Empty<NotificationSchedule>())
|
|
.Select(x => new NotificationSchedule
|
|
{
|
|
Id = x.Id,
|
|
WeekdayMask = NormalizeWeekdayMask(x.WeekdayMask),
|
|
Hour = NormalizeHour(x.Hour),
|
|
Minute = NormalizeMinute(x.Minute),
|
|
IsEnabled = x.IsEnabled
|
|
})
|
|
.Where(x => x.WeekdayMask != 0)
|
|
.OrderBy(x => x.Hour)
|
|
.ThenBy(x => x.Minute)
|
|
.ThenBy(x => x.Id)
|
|
.ToList();
|
|
|
|
await _database.Connection.RunInTransactionAsync(connection =>
|
|
{
|
|
connection.DeleteAll<NotificationSchedule>();
|
|
|
|
foreach (var item in normalized)
|
|
{
|
|
item.Id = 0;
|
|
connection.Insert(item);
|
|
}
|
|
});
|
|
}
|
|
|
|
private static int NormalizeWeekdayMask(int mask)
|
|
{
|
|
const int allDaysMask =
|
|
(int)(
|
|
NotificationWeekdays.Monday |
|
|
NotificationWeekdays.Tuesday |
|
|
NotificationWeekdays.Wednesday |
|
|
NotificationWeekdays.Thursday |
|
|
NotificationWeekdays.Friday |
|
|
NotificationWeekdays.Saturday |
|
|
NotificationWeekdays.Sunday);
|
|
|
|
return mask & allDaysMask;
|
|
}
|
|
|
|
private static int NormalizeHour(int hour)
|
|
{
|
|
if (hour < 0)
|
|
return 0;
|
|
|
|
if (hour > 23)
|
|
return 23;
|
|
|
|
return hour;
|
|
}
|
|
|
|
private static int NormalizeMinute(int minute)
|
|
{
|
|
if (minute < 0)
|
|
return 0;
|
|
|
|
if (minute > 59)
|
|
return 59;
|
|
|
|
return minute;
|
|
}
|
|
} |