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

Added retry action for timed out connections, fixed error message popup on export thread

This commit is contained in:
Michael Gordeev
2020-01-18 11:21:34 +03:00
parent 1778c5e99e
commit b11aebdcf5
3 changed files with 33 additions and 8 deletions
@@ -1,10 +1,10 @@
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using GUT.Schedule.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace GUT.Schedule
@@ -17,13 +17,18 @@ namespace GUT.Schedule
{
TextView status;
protected override async void OnCreate(Bundle savedInstanceState)
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.export_progress);
status = FindViewById<TextView>(Resource.Id.status);
Export();
}
private async void Export()
{
try
{
status.Text = "Загрузка расписания";
@@ -38,15 +43,28 @@ namespace GUT.Schedule
status.Text = "Готово";
await Task.Delay(1000);
}
catch (HttpRequestException e)
{
Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this);
builder.SetMessage("Невозможно загрузить расписание. Проверьте интернет-соединение или попробуйте позже")
.SetTitle(e.Message)
.SetPositiveButton("ОК", (s, e) => base.OnBackPressed())
.SetNegativeButton("Повторить", (s, e) => Export());
Android.Support.V7.App.AlertDialog dialog = builder.Create();
dialog.Show();
return;
}
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<DialogClickEventArgs>)null);
.SetPositiveButton("ОК", (s, e) => base.OnBackPressed());
Android.Support.V7.App.AlertDialog dialog = builder.Create();
dialog.Show();
return;
}
base.OnBackPressed(); // Navigates back to main activity (always because I don't allow backward navigation)
}
@@ -61,6 +61,11 @@ namespace GUT.Schedule
using HttpClient client = new HttpClient();
Data.FirstWeekDay = int.Parse(await client.GetStringAsync("https://xfox111.net/schedule_offset.txt"));
}
catch(HttpRequestException e)
{
ShowDialog(e.Message, "Невозможно загрузить расписание. Проверьте интернет-соединение или попробуйте позже", Proceed, FinishAndRemoveTask, "Повторить", "Выйти");
return;
}
catch (Exception e)
{
ShowDialog(e.GetType().ToString(), e.Message, FinishAndRemoveTask);
@@ -85,14 +90,16 @@ namespace GUT.Schedule
Manifest.Permission.ReadCalendar,
Manifest.Permission.WriteCalendar,
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)
}, 0);
private void ShowDialog(string title, string content, Action action = null)
private void ShowDialog(string title, string content, Action posAction = null, Action negAction = null, string posActionLabel = null, string negActionLabel = null)
{
Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this);
builder.SetMessage(content)
.SetTitle(title)
.SetPositiveButton("ОК", (s, e) => action?.Invoke());
.SetTitle(title).SetPositiveButton(posActionLabel ?? "OK", (s, e) => posAction?.Invoke());
if (negAction != null)
builder.SetNegativeButton(negActionLabel ?? "Close", (s, e) => negAction.Invoke());
Android.Support.V7.App.AlertDialog dialog = builder.Create();
dialog.Show();
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="104" android:versionName="1.0.4" package="com.xfox111.gut.schedule" android:installLocation="auto">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="105" android:versionName="1.0.5" package="com.xfox111.gut.schedule" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="28" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="ГУТ.Расписание" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"></application>
<uses-permission android:name="android.permission.READ_CALENDAR" />