Ejemplo n.º 1
0
        private async Task UpdateEvent()
        {
            View v = LayoutInflater.Inflate(Resource.Layout.ProgressDialogLayout, null);

            ProgressBar totalProgressBar = v.FindViewById <ProgressBar>(Resource.Id.TotalProgressBar);
            TextView    totalProgress    = v.FindViewById <TextView>(Resource.Id.TotalProgressPercentage);
            ProgressBar nowProgressBar   = v.FindViewById <ProgressBar>(Resource.Id.NowProgressBar);
            TextView    nowProgress      = v.FindViewById <TextView>(Resource.Id.NowProgressPercentage);

            var pd = new AndroidX.AppCompat.App.AlertDialog.Builder(this, ETC.dialogBGDownload);

            pd.SetTitle(Resource.String.UpdateEventDialog_Title);
            pd.SetMessage(Resources.GetString(Resource.String.UpdateEventDialog_Message));
            pd.SetView(v);
            pd.SetCancelable(false);

            Dialog dialog = pd.Create();

            dialog.Show();

            await Task.Delay(100);

            try
            {
                nowProgressBar.Indeterminate   = true;
                totalProgressBar.Indeterminate = true;

                if (!Directory.Exists(ETC.tempPath))
                {
                    Directory.CreateDirectory(ETC.tempPath);
                }
                if (!Directory.Exists(Path.Combine(ETC.cachePath, "Event", "Images")))
                {
                    Directory.CreateDirectory(Path.Combine(ETC.cachePath, "Event", "Images"));
                }

                using (WebClient wc = new WebClient())
                {
                    string url    = Path.Combine(ETC.server, "EventVer.txt");
                    string target = Path.Combine(ETC.tempPath, "EventVer.txt");

                    await wc.DownloadFileTaskAsync(url, target);

                    await Task.Delay(500);

                    nowProgressBar.Indeterminate   = false;
                    totalProgressBar.Indeterminate = false;
                    totalProgressBar.Progress      = 0;

                    wc.DownloadProgressChanged += (sender, e) =>
                    {
                        nowProgressBar.Progress = e.ProgressPercentage;
                        nowProgress.Text        = e.BytesReceived > 2048 ? $"{e.BytesReceived / 1024}KB" : $"{e.BytesReceived}B";
                    };
                    wc.DownloadFileCompleted += (sender, e) =>
                    {
                        totalProgressBar.Progress += 1;
                        totalProgress.Text         = $"{totalProgressBar.Progress} / {totalProgressBar.Max}";
                    };

                    int totalCount = 0;

                    using (StreamReader sr = new StreamReader(new FileStream(Path.Combine(ETC.tempPath, "EventVer.txt"), FileMode.Open, FileAccess.Read)))
                    {
                        totalCount += int.Parse(sr.ReadToEnd().Split(';')[2]);
                    }

                    totalProgressBar.Max = totalCount;

                    for (int i = 1; i <= totalCount; ++i)
                    {
                        string url2    = Path.Combine(ETC.server, "Data", "Images", "Events", "Event_" + i + ".png");
                        string target2 = Path.Combine(ETC.cachePath, "Event", "Images", "Event_" + i + ".png");

                        await wc.DownloadFileTaskAsync(url2, target2);

                        await Task.Delay(100);
                    }

                    await Task.Delay(500);

                    RunOnUiThread(() => { pd.SetMessage(Resources.GetString(Resource.String.UpdateEventDialog_RefreshVersionMessage)); });

                    string oldVersion = Path.Combine(ETC.cachePath, "Event", "EventVer.txt");
                    string newVersion = Path.Combine(ETC.tempPath, "EventVer.txt");

                    ETC.CopyFile(newVersion, oldVersion);

                    await Task.Delay(1000);
                }
            }
            catch (Exception ex)
            {
                ETC.LogError(ex, this);
            }
            finally
            {
                dialog.Dismiss();
            }
        }