From 45557da7bfbce7a5047a4d3689d0063c5df2be6c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 24 Mar 2017 23:26:16 +0100 Subject: [PATCH] =?UTF-8?q?=C3=81thelyez=C3=A9s=20folytatva?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Orarend/API.cs | 39 +++++++++++++++---------- Orarend/Orarend.csproj | 1 + Orarend/Timer.cs | 47 +++++++++++++++++++++++++++++++ OrarendAndroidApp/MainActivity.cs | 12 ++++---- 4 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 Orarend/Timer.cs diff --git a/Orarend/API.cs b/Orarend/API.cs index e8ed7b0..037d65f 100644 --- a/Orarend/API.cs +++ b/Orarend/API.cs @@ -1,5 +1,6 @@ using HtmlAgilityPack; using Orarend.Events; +using SimpleTimerPortable; using System; using System.Collections.Generic; using System.Globalization; @@ -210,12 +211,13 @@ namespace Orarend /// /// A stream, ahonnan betöltse az adatokat /// Megadja, mi történjen egy hiba esetén - public static void Betöltés(Stream s, Action hibánál) + /// Elvégezte-e a betöltést + public static bool Betöltés(Stream s, Action hibánál) { using (s) { - if (!!(Órarendek.Count > 0 || Osztályok?.Length > 0)) - return; + if (!!!betöltés()) + return false; using (var ms = new MemoryStream()) { s.CopyTo(ms); @@ -226,31 +228,38 @@ namespace Orarend ms.Seek(0, SeekOrigin.Begin); var serializer = new DataContractJsonSerializer(typeof(API)); serializer.ReadObject(ms); //A példányt beállítja, mikor elkezdi, nem várja meg, hogy végezzen (betöltés()) - betöltés(); + return true; } catch (Exception e) { hibánál(e); - Betöltés(); + return Betöltés(); } } + else return false; } } } //TODO: Tényleges órarendből állapítsa meg azt is, hogyha egyáltalán nincs ott egy óra, és máshol sincs, és ezt írja ki /// /// Betölti az alapértelemzett értékeket + /// Elvégezte-e a betöltést /// - public static void Betöltés() + public static bool Betöltés() { + if (!betöltés()) + return false; példány = new API(); - betöltés(); + return true; } private static Timer timer; - private static void betöltés() + private static bool betöltés() { - timer = new Timer(CsengőTimer, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 5)); + if (!!(Órarendek.Count > 0 || Osztályok?.Length > 0 || timer != null)) + return false; + timer = new Timer(CsengőTimer, null, new TimeSpan(0, 0, 0, 0, 100), new TimeSpan(0, 0, 5)); + return true; } public static void Mentés(Stream s) @@ -296,12 +305,12 @@ namespace Orarend frissítésHa1ÓraEltelt(); } else - timer.Change(Timeout.Infinite, Timeout.Infinite); + timer.Cancel(); } } private static DateTime utolsófrissítésplusz1óra = DateTime.MinValue; - public event EventHandler Frissítéskor; + public static event EventHandler Frissítéskor; private static void frissítésHa1ÓraEltelt() { if (utolsófrissítésplusz1óra > DateTime.Now) @@ -310,7 +319,7 @@ namespace Orarend //HelyettesítésFrissítés(false); } - public DayOfWeek MaiNap + public static DayOfWeek MaiNap { get { @@ -320,8 +329,8 @@ namespace Orarend } } - private static Helyettesítés[] helyettesítésInnenIde(int i, int j) - { //TODO: API-ba + public static Helyettesítés[] HelyettesítésInnenIde(Órarend órarend, int i, int j) + { return new Helyettesítés[] { órarend.Helyettesítések.FirstOrDefault(h => (int)h.EredetiNap == i + 1 && h.EredetiSorszám == j + 1), @@ -353,7 +362,7 @@ namespace Orarend bool becsengetés; int x = (int)DateTime.Today.DayOfWeek - 1; Óra óra; - var innenide = helyettesítésInnenIde(x, i); + var innenide = API.HelyettesítésInnenIde(órarend, x, i); Func óraperc = ts => ts.Hours > 0 ? ts.ToString("h\\ómm\\p") : ts.ToString("mm") + " perc"; if (x != -1 && x < 6 && (óra = innenide[1] != null ? innenide[1].ÚjÓra : innenide[0] != null ? innenide[0].EredetiNap != innenide[0].ÚjNap || innenide[0].EredetiSorszám != innenide[0].ÚjSorszám ? null : innenide[0].ÚjÓra : órarend.Órák[x][i]) != null) { //-1: Vasárnap diff --git a/Orarend/Orarend.csproj b/Orarend/Orarend.csproj index 86d85c8..78dd8d4 100644 --- a/Orarend/Orarend.csproj +++ b/Orarend/Orarend.csproj @@ -41,6 +41,7 @@ + diff --git a/Orarend/Timer.cs b/Orarend/Timer.cs new file mode 100644 index 0000000..b0128ea --- /dev/null +++ b/Orarend/Timer.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace SimpleTimerPortable +{ + internal delegate void TimerCallback(object state); + + internal sealed class Timer : CancellationTokenSource, IDisposable + { + public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) + { + this.callback = callback; + this.state = state; + start(dueTime, period); + } + + private TimerCallback callback; + private object state; + private void start(TimeSpan dueTime, TimeSpan period) + { + Task.Delay(dueTime, Token).ContinueWith(async (t, s) => + { + var tuple = (Tuple)s; + + while (true) + { + if (IsCancellationRequested) + break; + await Task.Run(() => tuple.Item1(tuple.Item2)); + await Task.Delay(period); + } + + }, Tuple.Create(callback, state), CancellationToken.None, + TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion, + TaskScheduler.Default); + } + + public new void Dispose() { base.Cancel(); } + + public void Change(TimeSpan dueTime, TimeSpan period) + { + Cancel(); + start(dueTime, period); + } + } +} diff --git a/OrarendAndroidApp/MainActivity.cs b/OrarendAndroidApp/MainActivity.cs index 383bef5..d1a13c2 100644 --- a/OrarendAndroidApp/MainActivity.cs +++ b/OrarendAndroidApp/MainActivity.cs @@ -14,6 +14,7 @@ using System.Threading; using System.IO; using System.Net; using Android.Preferences; +using Orarend.Events; namespace OrarendAndroidApp { @@ -40,11 +41,12 @@ namespace OrarendAndroidApp //ActionBar.SetCustomView(FindViewById(Resource.Id.spinner), new ActionBar.LayoutParams(GravityFlags.Left)); handler = new Handler(); string[] list = FileList(); + bool betöltötte; if (list.Contains(DATA_FILENAME)) - API.Betöltés(OpenFileInput(DATA_FILENAME), e => Hiba("Hiba az adatok betöltése során!\n" + e)); + betöltötte = API.Betöltés(OpenFileInput(DATA_FILENAME), e => Hiba("Hiba az adatok betöltése során!\n" + e)); else - API.Betöltés(); - if (API.CsengőTimerEvent == null) + betöltötte = API.Betöltés(); + if (betöltötte) API.CsengőTimerEvent += CsengőTimer; } @@ -188,7 +190,7 @@ namespace OrarendAndroidApp addCell((j + 1).ToString(), DarkTheme ? Color.White : Color.Black, tr); for (int i = 0; i < 6; i++) { - var innenide = helyettesítésInnenIde(i, j); + var innenide = API.HelyettesítésInnenIde(órarend, i, j); var helyettesítés = innenide[0]; var helyettesítésIde = innenide[1]; addCell(helyettesítésIde != null ? helyettesítésIde.ÚjÓra.EgyediNév : helyettesítés != null ? helyettesítés.EredetiNap != helyettesítés.ÚjNap || helyettesítés.EredetiSorszám != helyettesítés.ÚjSorszám ? "Áthelyezve" : helyettesítés.ÚjÓra?.EgyediNév ?? "elmarad" : órarend.Órák[i][j]?.EgyediNév ?? "", helyettesítés == null ? (DarkTheme ? Color.WhiteSmoke : Color.Black) : Color.Red, tr, new int[2] { i, j }); @@ -224,7 +226,7 @@ namespace OrarendAndroidApp Helyettesítés helyettesítésIde = null; if (ij != null) { - var innenide = helyettesítésInnenIde(ij[0], ij[1]); + var innenide = API.HelyettesítésInnenIde(órarend, ij[0], ij[1]); helyettesítésInnen = innenide[0]; helyettesítésIde = innenide[1]; }