1
0
mirror of https://github.com/XFox111/GUTSchedule.git synced 2026-04-22 06:58:01 +03:00

Updated UWP app to work with local in-app calendars

This commit is contained in:
Michael Gordeev
2020-03-13 14:21:30 +03:00
parent a71d31dd25
commit 5ae9efdc08
10 changed files with 100 additions and 71 deletions
@@ -37,7 +37,7 @@
<string name="forSemesterButton">For semester</string>
<string name="reminderSpinner">Set reminders for</string>
<string name="reminderNote">(i) Attention, for cloud-based Google calendars Google automatically sets reminders for 30 minutes if there\'s no reminder set by user</string>
<string name="reminderNote">(i) Attention, if you choose \"None\" for cloud-based Google calendars Google sets default reminder for events automatically sets reminders for 30 minutes if there\'s no reminder set by user</string>
<string name="noReminderOption">None</string>
<string name="inTimeReminderOption">At the start of event</string>
<string name="fiveMinuteReminderOption">5 minutes</string>
+15 -15
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.Appointments;
using Windows.ApplicationModel.Resources;
namespace GUTSchedule.UWP
{
@@ -11,21 +12,24 @@ namespace GUTSchedule.UWP
{
public static async Task<IReadOnlyList<AppointmentCalendar>> GetCalendars()
{
AppointmentStore calendarStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AllCalendarsReadWrite);
AppointmentStore calendarStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AppCalendarsReadWrite);
return await calendarStore.FindAppointmentCalendarsAsync();
}
public static async Task Export(List<Occupation> schedule, bool addGroupToTitle, int reminder, string calendar)
public static async Task Export(List<Occupation> schedule, bool addGroupToTitle, int reminder)
{
AppointmentStore calendarStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AllCalendarsReadWrite);
AppointmentCalendar cal = await calendarStore.GetAppointmentCalendarAsync(calendar);
string calendarName = schedule.Any(i => string.IsNullOrWhiteSpace(i.Group)) ? ResourceLoader.GetForCurrentView().GetString("mySchedule") : schedule.FirstOrDefault().Group;
AppointmentCalendar cal =
(await calendarStore.FindAppointmentCalendarsAsync()).FirstOrDefault(i => i.DisplayName == calendarName) ??
await calendarStore.CreateAppointmentCalendarAsync(calendarName);
foreach (Occupation item in schedule)
{
Appointment appointment = new Appointment
{
BusyStatus = AppointmentBusyStatus.Busy,
Details = item.Opponent + "\xFEFF",
Details = item.Opponent,
DetailsKind = AppointmentDetailsKind.PlainText,
Location = item.Cabinet,
Reminder = reminder < 0 ? (TimeSpan?)null : TimeSpan.FromMinutes(reminder),
@@ -42,18 +46,14 @@ namespace GUTSchedule.UWP
}
}
public static async Task Clear(bool keepPrevious = true)
public static async Task Clear(IEnumerable<object> targets, bool keepPrevious)
{
AppointmentStore appointmentStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AppCalendarsReadWrite);
List<Appointment> appointments = (await appointmentStore.FindAppointmentsAsync(keepPrevious ? DateTime.Today : DateTime.Today.Subtract(TimeSpan.FromDays(3000)), TimeSpan.FromDays(10000))).ToList();
foreach(Appointment i in appointments)
if (i.Details.Contains('\xFEFF'))
{
AppointmentCalendar cal = await appointmentStore.GetAppointmentCalendarAsync(i.CalendarId);
await cal.DeleteAppointmentAsync(i.LocalId);
}
foreach (AppointmentCalendar calendar in targets)
if(keepPrevious)
foreach (Appointment appointment in await calendar.FindAppointmentsAsync(DateTime.Now, TimeSpan.FromDays(365)))
await calendar.DeleteAppointmentAsync(appointment.LocalId);
else
await calendar.DeleteAsync();
}
}
}
@@ -0,0 +1,26 @@
<ContentDialog
x:Class="GUTSchedule.UWP.ClearCalendarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:appointments="using:Windows.ApplicationModel.Appointments"
mc:Ignorable="d"
x:Uid="clearScheduleTitle"
Loaded="ContentDialog_Loaded"
Title="Clear schedule calendar"
SecondaryButtonText="Cancel"
PrimaryButtonText="Clear">
<StackPanel>
<ListView SelectionMode="Multiple" x:Name="targetsList">
<ListView.ItemTemplate>
<DataTemplate x:DataType="appointments:AppointmentCalendar">
<TextBlock Text="{Binding DisplayName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<CheckBox x:Uid="clearUpcoming" Content="Remove only upcoming events" x:Name="clearUpcoming"/>
</StackPanel>
</ContentDialog>
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
namespace GUTSchedule.UWP
{
public sealed partial class ClearCalendarControl : ContentDialog
{
public IEnumerable<object> SelectedCalendars => targetsList.SelectedItems;
public bool ClearUpcomingOnly => clearUpcoming.IsChecked.Value;
public ClearCalendarControl() =>
InitializeComponent();
private async void ContentDialog_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) =>
targetsList.ItemsSource = await Calendar.GetCalendars();
}
}
@@ -131,6 +131,9 @@
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Calendar.cs" />
<Compile Include="ClearCalendarControl.xaml.cs">
<DependentUpon>ClearCalendarControl.xaml</DependentUpon>
</Compile>
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
@@ -200,6 +203,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="ClearCalendarControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -114,8 +114,6 @@
<CheckBox x:Uid="addGroupToTitle" Content="Add group number to event title" x:Name="addGroupToTitle" Checked="AddGroupToTitle_Checked" Unchecked="AddGroupToTitle_Checked"/>
<TextBlock x:Uid="titleNote" Style="{StaticResource CaptionTextBlockStyle}" Text="note"/>
<ComboBox x:Uid="destinationCalendar" Header="Destination calendar" x:Name="calendar" SelectionChanged="Calendar_SelectionChanged"/>
<TextBlock Foreground="Red" Visibility="Collapsed" Text="Error" x:Name="errorPlaceholder"/>
<Button x:Uid="addScheduleButton" Content="Add schedule" Background="{StaticResource SystemAccentColor}" Foreground="White" FontWeight="Bold" Margin="0,10" Click="Export"/>
+8 -50
View File
@@ -58,23 +58,6 @@ namespace GUTSchedule.UWP
reminder.SelectedIndex = (int?)settings.Values["Reminder"] ?? 2;
addGroupToTitle.IsChecked = (bool?)settings.Values["AddGroupToTitle"] ?? false;
calendar.ItemsSource = (await Calendar.GetCalendars()).Select(i => new ComboBoxItem
{
Content = i.DisplayName,
Tag = i.LocalId,
IsSelected = (string)settings.Values["Calendar"] == i.LocalId
}).ToList();
calendar.SelectedIndex = (calendar.ItemsSource as List<ComboBoxItem>).FindIndex(i => i.IsSelected);
if((calendar.ItemsSource as List<ComboBoxItem>).Count < 1)
{
MessageDialog dialog = new MessageDialog(resources.GetString("createCalendarMessage"), resources.GetString("createCalendarTitle"));
dialog.Commands.Add(new UICommand("OK", (command) => CoreApplication.Exit()));
await dialog.ShowAsync();
}
if (calendar.SelectedIndex < 0)
calendar.SelectedIndex = 0;
}
catch (HttpRequestException e)
{
@@ -95,15 +78,13 @@ namespace GUTSchedule.UWP
switch (((FrameworkElement)sender).Tag)
{
case "clear":
MessageDialog dialog = new MessageDialog(resources.GetString("clearScheduleMessage"), resources.GetString("clearScheduleTitle"));
dialog.Commands.Add(new UICommand(resources.GetString("clearUpcomingOption"), (command) => Clear()));
dialog.Commands.Add(new UICommand(resources.GetString("clearAllOption"), (command) => Clear(true)));
dialog.Commands.Add(new UICommand(resources.GetString("cancelOption")));
dialog.CancelCommandIndex = 2;
dialog.DefaultCommandIndex = 0;
await dialog.ShowAsync();
ClearCalendarControl clearControl = new ClearCalendarControl();
ContentDialogResult result = await clearControl.ShowAsync();
if(result == ContentDialogResult.Primary)
{
await Calendar.Clear(clearControl.SelectedCalendars, clearControl.ClearUpcomingOnly);
await new MessageDialog(resources.GetString("clearScheduleDone"), resources.GetString("clearScheduleTitle/Title")).ShowAsync();
}
break;
case "about":
Frame.Navigate(typeof(AboutPage));
@@ -220,7 +201,7 @@ namespace GUTSchedule.UWP
List<Occupation> schedule = await Parser.GetSchedule(exportParameters);
status.Text = resources.GetString("calendarExportStatus");
await Calendar.Export(schedule, addGroupToTitle.IsChecked.Value, (reminder.SelectedIndex - 1) * 5, ((ComboBoxItem)calendar.SelectedItem).Tag as string);
await Calendar.Export(schedule, addGroupToTitle.IsChecked.Value, (reminder.SelectedIndex - 1) * 5);
status.Text = resources.GetString("doneStatus");
await Task.Delay(1000);
@@ -242,26 +223,6 @@ namespace GUTSchedule.UWP
TopAppBar.Visibility = Visibility.Visible;
}
private async void Clear(bool keepPrevious = true)
{
try
{
await Calendar.Clear(keepPrevious);
MessageDialog dialog = new MessageDialog(resources.GetString("clearScheduleDone"), resources.GetString("clearScheduleTitle"));
dialog.Commands.Add(new UICommand("OK", (command) => loading.Visibility = Visibility.Collapsed));
await dialog.ShowAsync();
}
catch (Exception e)
{
MessageDialog dialog = new MessageDialog(e.Message, e.GetType().ToString());
dialog.Commands.Add(new UICommand("OK", (command) => loading.Visibility = Visibility.Collapsed));
await dialog.ShowAsync();
}
}
private void Faculty_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateGroupsList();
@@ -277,9 +238,6 @@ namespace GUTSchedule.UWP
private void Reminder_SelectionChanged(object sender, SelectionChangedEventArgs e) =>
settings.Values["Reminder"] = reminder.SelectedIndex;
private void Calendar_SelectionChanged(object sender, SelectionChangedEventArgs e) =>
settings.Values["Calendar"] = (calendar.SelectedItem as ComboBoxItem).Tag;
private void Group_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(group.SelectedItem != null)
@@ -47,6 +47,5 @@
<Capabilities>
<Capability Name="internetClient" />
<uap:Capability Name="appointments"/>
<rescap:Capability Name="appointmentSystem"/>
</Capabilities>
</Package>
@@ -162,9 +162,18 @@
<data name="clearScheduleMessage" xml:space="preserve">
<value>This action will purge exported schedule from all available calendars.
It will affect only events created by the app.
'All' - will purge all timetable events including the past ones
'All' - will purge all timetable events including the past ones
'Upcoming' - will affect only upcoming timetable events</value>
</data>
<data name="clearScheduleTitle.PrimaryButtonText" xml:space="preserve">
<value>Clear schedule</value>
</data>
<data name="clearScheduleTitle.SecondaryButtonText" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="clearScheduleTitle.Title" xml:space="preserve">
<value>Clear schedule</value>
</data>
<data name="clearUpcoming.Content" xml:space="preserve">
<value>Remove only upcoming events</value>
</data>
@@ -258,6 +267,9 @@
<data name="linksTitle.Text" xml:space="preserve">
<value>Useful links</value>
</data>
<data name="loadingStatus" xml:space="preserve">
<value>Loading</value>
</data>
<data name="mySchedule" xml:space="preserve">
<value>My schedule</value>
</data>
@@ -162,9 +162,18 @@
<data name="clearScheduleMessage" xml:space="preserve">
<value>Это действие удалит экспортированное расписание из всех доступных календарей.
Данное действие затронет только расписание, экспортированное этим приложением
'Все' - удалит все события расписания, включая прошедшие
'Все' - удалит все события расписания, включая прошедшие
'Только новые' - удалит будущие события расписания</value>
</data>
<data name="clearScheduleTitle.PrimaryButtonText" xml:space="preserve">
<value>Очистить расписание</value>
</data>
<data name="clearScheduleTitle.SecondaryButtonText" xml:space="preserve">
<value>Отмена</value>
</data>
<data name="clearScheduleTitle.Title" xml:space="preserve">
<value>Очистка расписания</value>
</data>
<data name="clearUpcoming.Content" xml:space="preserve">
<value>Удалить только будущие события</value>
</data>
@@ -258,6 +267,9 @@
<data name="linksTitle.Text" xml:space="preserve">
<value>Полезные ссылки</value>
</data>
<data name="loadingStatus" xml:space="preserve">
<value>Загрузка</value>
</data>
<data name="mySchedule" xml:space="preserve">
<value>Мое расписание</value>
</data>