From cdfb5c3f2db8eb071a983ae13d27694cf6905e04 Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Sat, 28 Dec 2019 12:56:10 +0300 Subject: [PATCH] Added catches for some errors, Allowed to write schedule to any calendar --- .../GUT.Schedule/Activities/ExportActivity.cs | 29 +++++++++--- .../GUT.Schedule/Activities/StartActivity.cs | 46 +++++++++++++++---- GUT.Schedule/GUT.Schedule/Calendar.cs | 7 +-- .../Properties/AndroidManifest.xml | 2 +- 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/GUT.Schedule/GUT.Schedule/Activities/ExportActivity.cs b/GUT.Schedule/GUT.Schedule/Activities/ExportActivity.cs index a1afafc..1071e28 100644 --- a/GUT.Schedule/GUT.Schedule/Activities/ExportActivity.cs +++ b/GUT.Schedule/GUT.Schedule/Activities/ExportActivity.cs @@ -1,7 +1,9 @@ using Android.App; +using Android.Content; using Android.OS; using Android.Widget; using GUT.Schedule.Models; +using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -22,17 +24,30 @@ namespace GUT.Schedule status = FindViewById(Resource.Id.status); - status.Text = "Загрузка расписания"; - List schedule = await Parser.LoadSchedule(); + try + { + status.Text = "Загрузка расписания"; + List schedule = await Parser.LoadSchedule(); - schedule = schedule.FindAll(i => i.StartTime.Date >= Data.StartDate && i.StartTime.Date <= Data.EndDate); // Filtering schedule according to export range + schedule = schedule.FindAll(i => i.StartTime.Date >= Data.StartDate && i.StartTime.Date <= Data.EndDate); // Filtering schedule according to export range - status.Text = "Экспортирование в календарь"; - Calendar.Export(schedule); + status.Text = "Экспортирование в календарь"; + Calendar.Export(schedule); - status.Text = "Готово"; - await Task.Delay(1000); + status.Text = "Готово"; + await Task.Delay(1000); + } + catch (Exception e) + { + Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this); + builder.SetMessage(e.Message) + .SetTitle(e.GetType().ToString()) + .SetPositiveButton("ОК", (EventHandler)null); + + Android.Support.V7.App.AlertDialog dialog = builder.Create(); + dialog.Show(); + } base.OnBackPressed(); // Navigates back to main activity (always because I don't allow backward navigation) } diff --git a/GUT.Schedule/GUT.Schedule/Activities/StartActivity.cs b/GUT.Schedule/GUT.Schedule/Activities/StartActivity.cs index 4d7fa75..6af57ea 100644 --- a/GUT.Schedule/GUT.Schedule/Activities/StartActivity.cs +++ b/GUT.Schedule/GUT.Schedule/Activities/StartActivity.cs @@ -8,6 +8,7 @@ using Android.Support.V4.App; using Android.Support.V4.Content; using Android.Support.V7.App; using Android.Widget; +using System; using System.Linq; using System.Net.Http; @@ -33,7 +34,7 @@ namespace GUT.Schedule if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteCalendar) != Permission.Granted) { if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.WriteCalendar)) - ShowDialog(); + ShowRationale(); else RequestPermissions(); } @@ -43,15 +44,31 @@ namespace GUT.Schedule private async void Proceed() { - status.Text = "Загрузка списка календарей"; - Calendar.LoadCalendars(); + try + { + status.Text = "Загрузка списка календарей"; + Calendar.LoadCalendars(); + if (Calendar.Calendars.Count == 0) + ShowDialog("Создайте новый календарь", "На вашем устройстве нет календарей пригодных для записи расписания"); + } + catch (Exception e) + { + ShowDialog(e.GetType().ToString(), e.Message); + } - status.Text = "Загрузка списка факультетов"; - await Parser.LoadFaculties(); + try + { + status.Text = "Загрузка списка факультетов"; + await Parser.LoadFaculties(); - status.Text = "Загрузка дат смещения"; - using (HttpClient client = new HttpClient()) + status.Text = "Загрузка дат смещения"; + using HttpClient client = new HttpClient(); Data.FirstWeekDay = int.Parse(await client.GetStringAsync("https://xfox111.net/schedule_offset.txt")); + } + catch + { + ShowDialog("Не удалось загрузить данные", "Проверьте интернет-соединение или попробуйте позже"); + } StartActivity(new Intent(this, typeof(MainActivity))); } @@ -63,7 +80,7 @@ namespace GUT.Schedule if (grantResults.All(i => i == Permission.Granted)) Proceed(); else - ShowDialog(); + ShowRationale(); } private void RequestPermissions() => @@ -74,7 +91,7 @@ namespace GUT.Schedule Manifest.Permission.Internet }, 76); // IDK why I need requestCode value to be set (instead of 76 there can be any other number. Anyway it doesn't affect anything) - private void ShowDialog() + private void ShowRationale() { Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this); builder.SetMessage("Разрешите приложению получать доступ к календарю. Без этого разрешения приложение не сможет добавлять расписание в ваш календарь") @@ -85,6 +102,17 @@ namespace GUT.Schedule dialog.Show(); } + private void ShowDialog(string title, string content) + { + Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this); + builder.SetMessage(content) + .SetTitle(title) + .SetPositiveButton("ОК", (EventHandler)null); + + Android.Support.V7.App.AlertDialog dialog = builder.Create(); + dialog.Show(); + } + public override void OnBackPressed() { } // Disables back button } } \ No newline at end of file diff --git a/GUT.Schedule/GUT.Schedule/Calendar.cs b/GUT.Schedule/GUT.Schedule/Calendar.cs index b79ebe7..8148c69 100644 --- a/GUT.Schedule/GUT.Schedule/Calendar.cs +++ b/GUT.Schedule/GUT.Schedule/Calendar.cs @@ -27,9 +27,7 @@ namespace GUT.Schedule string[] calendarsProjection = { CalendarContract.Calendars.InterfaceConsts.Id, CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName, - CalendarContract.Calendars.InterfaceConsts.AccountName, - CalendarContract.Calendars.InterfaceConsts.OwnerAccount, - CalendarContract.Calendars.InterfaceConsts.AccountType, + CalendarContract.Calendars.InterfaceConsts.AccountName }; // Retrieving calendars data @@ -38,8 +36,7 @@ namespace GUT.Schedule cursor.MoveToNext(); for (int i = 0; i < cursor.Count; i++) { - if (cursor.GetString(4) == "com.google" && !cursor.GetString(3).Contains("google")) // Loading only users' main calendars - Calendars.Add((cursor.GetString(0), $"{cursor.GetString(1)} ({cursor.GetString(2)})")); + Calendars.Add((cursor.GetString(0), $"{cursor.GetString(1)} ({cursor.GetString(2)})")); cursor.MoveToNext(); } } diff --git a/GUT.Schedule/GUT.Schedule/Properties/AndroidManifest.xml b/GUT.Schedule/GUT.Schedule/Properties/AndroidManifest.xml index 1fdf14e..44174fc 100644 --- a/GUT.Schedule/GUT.Schedule/Properties/AndroidManifest.xml +++ b/GUT.Schedule/GUT.Schedule/Properties/AndroidManifest.xml @@ -1,5 +1,5 @@  - +