mirror of
https://github.com/XFox111/GUTSchedule.git
synced 2026-04-22 06:58:01 +03:00
103 lines
4.3 KiB
C#
103 lines
4.3 KiB
C#
using Android;
|
|
using Android.App;
|
|
using Android.Content;
|
|
using Android.Content.PM;
|
|
using Android.OS;
|
|
using Android.Runtime;
|
|
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;
|
|
|
|
namespace GUT.Schedule
|
|
{
|
|
/// <summary>
|
|
/// Splash screen activity. Loads init data
|
|
/// </summary>
|
|
[Activity(MainLauncher = true)]
|
|
public class StartActivity : AppCompatActivity
|
|
{
|
|
TextView status;
|
|
|
|
protected override void OnCreate(Bundle savedInstanceState)
|
|
{
|
|
SetContentView(Resource.Layout.splash_screen);
|
|
base.OnCreate(savedInstanceState);
|
|
|
|
status = FindViewById<TextView>(Resource.Id.status);
|
|
|
|
status.Text = "Проверка наличия разрешений";
|
|
|
|
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteCalendar) != Permission.Granted)
|
|
{
|
|
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.WriteCalendar))
|
|
ShowDialog("Доступ к календарю", "Разрешите приложению получать доступ к календарю. Без этого разрешения приложение не сможет добавлять расписание в ваш календарь", RequestPermissions);
|
|
else
|
|
RequestPermissions();
|
|
}
|
|
else
|
|
Proceed();
|
|
}
|
|
|
|
private async void Proceed()
|
|
{
|
|
try
|
|
{
|
|
status.Text = "Загрузка списка календарей";
|
|
Calendar.LoadCalendars();
|
|
if (Calendar.Calendars.Count == 0)
|
|
{
|
|
ShowDialog("Создайте новый календарь", "На вашем устройстве нет календарей пригодных для записи расписания");
|
|
return;
|
|
}
|
|
|
|
status.Text = "Загрузка списка факультетов";
|
|
await Parser.LoadFaculties();
|
|
|
|
status.Text = "Загрузка дат смещения";
|
|
using HttpClient client = new HttpClient();
|
|
Data.FirstWeekDay = int.Parse(await client.GetStringAsync("https://xfox111.net/schedule_offset.txt"));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ShowDialog(e.GetType().ToString(), e.Message, FinishAndRemoveTask);
|
|
return;
|
|
}
|
|
StartActivity(new Intent(this, typeof(MainActivity)));
|
|
}
|
|
|
|
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
|
|
{
|
|
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
if (grantResults.All(i => i == Permission.Granted))
|
|
Proceed();
|
|
else
|
|
ShowDialog("Доступ к календарю", "Разрешите приложению получать доступ к календарю. Без этого разрешения приложение не сможет добавлять расписание в ваш календарь", RequestPermissions);
|
|
}
|
|
|
|
private void RequestPermissions() =>
|
|
ActivityCompat.RequestPermissions(this, new[]
|
|
{
|
|
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)
|
|
|
|
private void ShowDialog(string title, string content, Action action = 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());
|
|
|
|
Android.Support.V7.App.AlertDialog dialog = builder.Create();
|
|
dialog.Show();
|
|
}
|
|
|
|
public override void OnBackPressed() { } // Disables back button
|
|
}
|
|
} |