Пример #1
0
 public static SessionController getInstance()
 {
     if (instance == null)
     {
         instance = new SessionController();
     }
     return(instance);
 }
        public static async Task CheckForUpdates()
        {
            string path = "api/versions.json";

            try
            {
                using (HttpResponseMessage response = await ApiHelper.DDApiClient.GetAsync(path))
                {
                    var localVersion = RealmController.getMetaData().localVersion;
                    if (response.IsSuccessStatusCode)
                    {
                        //var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
                        //string[] versions = await response.Content.ReadAsAsync<string[]>(formatters);

                        var asString = await response.Content.ReadAsStringAsync();

                        string[] versions = JsonConvert.DeserializeObject <string[]>(asString);

                        var newestVersion = versions[0];

                        if (versionShouldUpdate(localVersion, newestVersion))
                        {
                            // save newest verison local and update
                            Console.WriteLine("save newest verison local and update");
                            MetaData md = new MetaData(newestVersion.ToString());
                            SessionController.getInstance().metaData = md;
                            RealmController.createOrUpdateMetaData(md); // TODO: remember to use this
                            // update champions
                            await FetchAndSaveChampions();
                        }
                        else
                        {
                            SessionController.getInstance().metaData = RealmController.getMetaData();

                            if (RealmController.getChampions() == null)
                            {
                                await FetchAndSaveChampions();
                            }
                            // everything up to date
                            Console.WriteLine("everything up to date");
                        }
                    }
                    else
                    {
                        Console.WriteLine("error getting version");
                        //throw new Exception(response.ReasonPhrase);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }