Exemplo n.º 1
0
        public static async Task <bool> GetUpdate()
        {
            TaskCompletionSource <bool> r = new TaskCompletionSource <bool>();
            string currentVersion         = VersionTracking.CurrentVersion;
            string result = CloudMusic.ApiHelper.HttpClient.HttpGet(url);

            if (!result.Equals("err"))
            {
                GitHubReleasesModel releasesModel = JsonConvert.DeserializeObject <GitHubReleasesModel>(result);
                string lastReleasesVersion        = releasesModel.tag_name;
                string IgnoreVersion = Preferences.Get("IgnoreVersion", "");
                if (lastReleasesVersion.Equals(IgnoreVersion))
                {
                    r.SetResult(false);
                    return(await r.Task);
                }
                string downloadurl = releasesModel.assets[0].browser_download_url;
                size = releasesModel.assets[0].size;
                bool hasupdate = ContainsVersion(currentVersion, lastReleasesVersion);
                if (hasupdate && Device.RuntimePlatform.Equals(Device.Android))
                {
                    Device.BeginInvokeOnMainThread(async() =>
                    {
                        try
                        {
                            string content = string.Format("{0}\r\n\r\n大小({1}MB)", releasesModel.body, ((double)size / 1024 / 1024).ToString("f2"));
                            if (await App.Current.MainPage.DisplayAlert("发现新版本" + lastReleasesVersion, content, "更新", "忽略此版本"))
                            {
                                DependencyService.Get <IToast>().ShortAlert("更新已在后台下载,请稍后");
                                System.Net.WebClient webClient     = new System.Net.WebClient();
                                webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
                                webClient.DownloadDataCompleted   += WebClient_DownloadDataCompleted;
                                byte[] data     = await webClient.DownloadDataTaskAsync(downloadurl);
                                string fileName = releasesModel.assets[0].name;
                                DependencyService.Get <IFileOpener>().OpenFile(data, fileName);
                            }
                            else
                            {
                                Preferences.Set("IgnoreVersion", lastReleasesVersion);
                            }
                        }
                        catch (Exception ex)
                        {
                            DependencyService.Get <IToast>().ShortAlert("更新下载失败:" + ex.Message);
                            DependencyService.Get <INotification>().NotificationCancel(1);
                        }
                    });
                }
                else
                {
                    r.SetResult(false);
                    return(await r.Task);
                }
            }
            r.SetResult(true);
            return(await r.Task);
        }
Exemplo n.º 2
0
        public static async Task <bool> GetUpdate()
        {
            if (Connectivity.NetworkAccess == NetworkAccess.Internet &&
                Barrel.Current.IsExpired(key: url))
            {
                TaskCompletionSource <bool> r = new TaskCompletionSource <bool>();
                string currentVersion         = VersionTracking.CurrentVersion;
                var    service = Prism.PrismApplicationBase.Current.Container.Resolve <IRequestProvider>();
                GitHubReleasesModel releasesModel = await service.GetAsync <GitHubReleasesModel>(url);

                if (releasesModel != null && !string.IsNullOrEmpty(releasesModel.tag_name))
                {
                    string lastReleasesVersion = releasesModel.tag_name;
                    string IgnoreVersion       = Preferences.Get("IgnoreVersion", "");
                    if (lastReleasesVersion.Equals(IgnoreVersion))
                    {
                        r.SetResult(false);
                        return(await r.Task);
                    }
                    string downloadurl = releasesModel.assets[0].browser_download_url;
                    size = releasesModel.assets[0].size;
                    bool hasupdate = ContainsVersion(currentVersion, lastReleasesVersion);
                    if (hasupdate && Device.RuntimePlatform.Equals(Device.Android))
                    {
                        Device.BeginInvokeOnMainThread(async() =>
                        {
                            try
                            {
                                string content = string.Format("{0}\r\n\r\nSize({1}MB)", releasesModel.body, ((double)size / 1024 / 1024).ToString("f2"));
                                if (await App.Current.MainPage.DisplayAlert("New Version" + lastReleasesVersion, content, "Update", "Cancel"))
                                {
                                    DependencyService.Get <IDisplayMessage>().ShowMessageInfo("Update has been downloaded in the background, please wait");
                                    System.Net.WebClient webClient     = new System.Net.WebClient();
                                    webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
                                    webClient.DownloadDataCompleted   += WebClient_DownloadDataCompleted;
                                    byte[] data     = await webClient.DownloadDataTaskAsync(downloadurl);
                                    string fileName = releasesModel.assets[0].name;
                                    DependencyService.Get <IFileOpener>().OpenFile(data, fileName);
                                }
                                else
                                {
                                    Preferences.Set("IgnoreVersion", lastReleasesVersion);
                                }
                            }
                            catch (Exception ex)
                            {
                                DependencyService.Get <IDisplayMessage>().ShowMessageInfo("Update download failed:" + ex.Message);
                                DependencyService.Get <IProgressUpdate>().ProgressCancel(1);
                            }
                        });
                    }
                    else
                    {
                        r.SetResult(false);
                        return(await r.Task);
                    }
                }
                r.SetResult(true);
                //Saves the cache and pass it a timespan for expiration
                Barrel.Current.Add(key: url, data: r.Task, expireIn: TimeSpan.FromDays(1));
                return(await r.Task);
            }
            else
            {
                return(Barrel.Current.Get <bool>(key: url));
            }
        }