Пример #1
0
 public static Timetable Load()
 {
     try
     {
         // File system has not been initialized yet.
         if (PlatformHelper.RuntimePlatform == Platform.WASM)
         {
             var timetable = new Timetable();
             timetable.ReloadAsync();
             return(timetable);
         }
         using (var stream = File.OpenText(DataFilePath))
         {
             var timetable = (Timetable)JsonConvert.DeserializeObject(stream.ReadToEnd().Replace("$ASSEMBLY_NAME", Assembly.GetExecutingAssembly().GetName().Name), typeof(Timetable),
                                                                      new JsonSerializerSettings()
             {
                 TypeNameHandling = TypeNameHandling.Auto
             });
             if (timetable == null)
             {
                 timetable = new Timetable();
                 // Yes, no await doesn't do any harm. The UI can continue work normally.
                 timetable.SaveAsync();
             }
             return(timetable);
         }
     }
     catch
     {
         var timetable = new Timetable();
         timetable.SaveAsync();
         return(timetable);
     }
 }
Пример #2
0
        public static Timetable Load()
        {
            try
            {
                // File system has not been initialized yet.
                if (PlatformHelper.RuntimePlatform == Platform.WASM)
                {
                    var timetable = new Timetable();
                    _ = timetable.ReloadAsync();
                    return(timetable);
                }
                using (var stream = File.OpenText(DataFilePath))
                {
                    var timetable = (Timetable)JsonConvert.DeserializeObject(stream.ReadToEnd().Replace("$ASSEMBLY_NAME", Assembly.GetExecutingAssembly().GetName().Name), typeof(Timetable),
                                                                             new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.Auto
                    });
                    if (timetable == null)
                    {
                        timetable = new Timetable();
                        // Yes, no await doesn't do any harm. The UI can continue work normally.
                        _ = timetable.SaveAsync();
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(timetable.UpdateURL))
                        {
                            System.Diagnostics.Debug.WriteLine($"Automatically updating from {timetable.UpdateURL}");

                            _ = timetable.UpdateAsync().ContinueWith((task) =>
                            {
                                System.Diagnostics.Debug.WriteLine($"Done updating from {timetable.UpdateURL}");
                                timetable.Loaded?.Invoke(timetable, EventArgs.Empty);
                            });
                        }
                        else
                        {
                            timetable.Loaded?.Invoke(timetable, EventArgs.Empty);
                        }
                    }
                    return(timetable);
                }
            }
            catch
            {
                var timetable = new Timetable();
                _ = timetable.SaveAsync();
                return(timetable);
            }
        }