Adding final project to dev branch
This commit is contained in:
137
WguApp/Views/AssessmentPage.xaml.cs
Normal file
137
WguApp/Views/AssessmentPage.xaml.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WguApp.Models;
|
||||
using WguApp.Services;
|
||||
|
||||
namespace WguApp.Views;
|
||||
|
||||
public partial class AssessmentPage : ContentPage
|
||||
{
|
||||
|
||||
private Assessment _assessment;
|
||||
|
||||
public AssessmentPage(Assessment assessment)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_assessment = assessment;
|
||||
|
||||
Title = assessment.Name;
|
||||
|
||||
TitleEntry.Text = _assessment.Name;
|
||||
StartDatePicker.Date = _assessment.StartDate;
|
||||
EndDatePicker.Date = _assessment.EndDate;
|
||||
StartNotifCheck.IsChecked = _assessment.StartNotifCheck;
|
||||
EndNotifCheck.IsChecked = _assessment.EndNotifCheck;
|
||||
|
||||
StartNotifCheck.CheckedChanged += async (sender, e) => { await ToggleStartNotification(); };
|
||||
EndNotifCheck.CheckedChanged += async (sender, e) => { await ToggleEndNotification(); };
|
||||
}
|
||||
|
||||
private async Task ToggleStartNotification()
|
||||
{
|
||||
if (StartNotifCheck.IsChecked)
|
||||
{
|
||||
var id = await LocalNotificationService.ScheduleNotification($"{_assessment.Name} Start Reminder", $"Your assessment '{_assessment.Id} starts in 12 hours'", _assessment.StartDate.Subtract(TimeSpan.FromHours(12)));
|
||||
_assessment.StartNotifId = id ?? 0;
|
||||
LoggerService.LogToFile($"{_assessment.Id}:{_assessment.Name} set start notification {_assessment.StartNotifId}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var res = await LocalNotificationService.CancelNotification(_assessment.StartNotifId);
|
||||
LoggerService.LogToFile($"{_assessment.Id}:{_assessment.Name} canceled start notification {_assessment.StartNotifId}");
|
||||
_assessment.StartNotifId = 0;
|
||||
}
|
||||
|
||||
_assessment.StartNotifCheck = StartNotifCheck.IsChecked;
|
||||
}
|
||||
|
||||
private async Task ToggleEndNotification()
|
||||
{
|
||||
if (EndNotifCheck.IsChecked)
|
||||
{
|
||||
var id = await LocalNotificationService.ScheduleNotification($"{_assessment.Name} End Reminder", $"Your assessment '{_assessment.Id} ends in 12 hours'", _assessment.EndDate.Subtract(TimeSpan.FromHours(12)));
|
||||
_assessment.EndNotifId = id ?? 0;
|
||||
LoggerService.LogToFile($"{_assessment.Id}:{_assessment.Name} set end notification {_assessment.EndNotifId}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var res = await LocalNotificationService.CancelNotification(_assessment.EndNotifId);
|
||||
LoggerService.LogToFile($"{_assessment.Id}:{_assessment.Name} canceled end notification {_assessment.EndNotifId}");
|
||||
_assessment.EndNotifId = 0;
|
||||
}
|
||||
|
||||
_assessment.EndNotifCheck = EndNotifCheck.IsChecked;
|
||||
}
|
||||
|
||||
private (string msg, bool result) ValidateFields()
|
||||
{
|
||||
var message = string.Empty;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(TitleEntry.Text)) message += "Title cannot be blank\n";
|
||||
if (StartDatePicker.Date > EndDatePicker.Date) message += "Start Date cannot be after the End Date\n";
|
||||
|
||||
return string.IsNullOrWhiteSpace(message) ? (message, true) : (message, false);
|
||||
}
|
||||
|
||||
private async Task<bool> Save()
|
||||
{
|
||||
var validationResult = ValidateFields();
|
||||
|
||||
if (!validationResult.result)
|
||||
{
|
||||
await DisplayAlert("Validation Error", validationResult.msg, "Ok");
|
||||
return false;
|
||||
}
|
||||
|
||||
_assessment.Name = TitleEntry.Text;
|
||||
_assessment.StartDate = StartDatePicker.Date;
|
||||
_assessment.EndDate = EndDatePicker.Date;
|
||||
_assessment.StartNotifCheck = StartNotifCheck.IsChecked;
|
||||
_assessment.EndNotifCheck = EndNotifCheck.IsChecked;
|
||||
|
||||
var updateResult = await DatabaseService.UpdateAssessment(_assessment);
|
||||
|
||||
if (updateResult)
|
||||
{
|
||||
await DisplayAlert("Info", $"{_assessment.Name} Saved!", "Ok");
|
||||
}
|
||||
else
|
||||
{
|
||||
await DisplayAlert("Error", "Could not save term", "Ok");
|
||||
}
|
||||
|
||||
Title = _assessment.Name;
|
||||
|
||||
if (_assessment.StartNotifId != 0)
|
||||
{
|
||||
await LocalNotificationService.UpdateNotification(_assessment.StartNotifId, $"{_assessment.Name} Start Reminder",
|
||||
$"Your course '{_assessment.Id} starts in 12 hours'", _assessment.StartDate.Subtract(TimeSpan.FromHours(12)));
|
||||
}
|
||||
|
||||
if (_assessment.EndNotifId != 0)
|
||||
{
|
||||
await LocalNotificationService.UpdateNotification(_assessment.EndNotifId, $"{_assessment.Name} End Reminder",
|
||||
$"Your course '{_assessment.Id} ends in 12 hours'", _assessment.EndDate.Subtract(TimeSpan.FromHours(12)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async void SaveButton_OnClicked(object? sender, EventArgs e)
|
||||
{
|
||||
await Save();
|
||||
}
|
||||
|
||||
private async void DeleteButton_OnClicked(object? sender, EventArgs e)
|
||||
{
|
||||
var result = await DisplayAlert("Delete Course", $"Do you really want to delete '{_assessment.Name}'? This action cannot be undone.", "Delete", "Cancel");
|
||||
if (!result) return;
|
||||
|
||||
await DatabaseService.DeleteAssessment(_assessment);
|
||||
await Navigation.PopAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user