Files
BeWoPlaner/BeWoLauncher/View/Frames/ProgressFrame.xaml.cs
Rene Evertz 6497a7ded5 Working now
- more logs
- download controller + manager überarbeitet
- Retry wird besser angezeigt
- Formatierungen
2024-06-07 13:42:25 +02:00

263 lines
9.3 KiB
C#

using BeWoLauncher.Logic.Controller;
using BS.SharedLauncher.Enums;
using BS.SharedLauncher.Exceptions;
using BS.SharedLauncher.Update;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace BeWoLauncher.View.Frames
{
/// <summary>
/// Interaktionslogik für UpdateView.xaml
/// </summary>
public partial class ProgressFrame : FrameBase
{
private static TimeSpan duration = TimeSpan.FromSeconds(1);
private int lastStep = -1;
private string[][] keywords = new string[][]
{
new string[]{"Installation", "Ungebrauchte Datei(en) gefunden.", "Ungebrauchte Datei(en) entfernt!" },
new string[]{"Update", "Ungebrauchte Datei(en) gefunden.", "Ungebrauchte Datei(en) entfernt!"},
new string[]{"Deinstallation", "Deinstallation startet...", "Deinstallation erfolgreich!"},
};
private int keyword_mode;
public readonly BackgroundWorker Worker = new BackgroundWorker();
public event EventHandler Successful;
public event EventHandler Exception;
public LauncherMode LauncherMode { get; set; }
public ProgressFrame(LauncherMode mode)
{
InitializeComponent();
LauncherMode = mode;
if (mode == LauncherMode.Install || mode == LauncherMode.Update)
{
proBar1.IsIndeterminate = false;
proBar1.Minimum = 0;
proBar1.Maximum = 100;
proBar2.IsIndeterminate = false;
proBar2.Minimum = 0;
proBar2.Maximum = DownloadController.Manager.FilesToProcessTotal;
}
else
{
proBar1.Visibility = Visibility.Collapsed;
proBar2.IsIndeterminate = true;
proBar2.Minimum = 0;
proBar2.Maximum = DownloadController.Manager.FilesToDeleteInApplyCount;
}
keyword_mode = (int)mode;
Loaded += (s, e) => Start();
}
private string getKeyword(int pos) => keywords[keyword_mode][pos];
public string Message
{
get { return lblMessage.Content as string; }
set { lblMessage.Content = value; }
}
public string SubMessage
{
get { return lblSubmessage.Content as string; }
set { lblSubmessage.Content = value; }
}
public void Start()
{
var skipLock = bool.TryParse(ConfigurationManager.AppSettings.Get("SkipLock"), out bool c) && c;
Worker.DoWork += worker_DoWork;
Worker.ProgressChanged += worker_ProgressChanged;
Worker.RunWorkerCompleted += worker_completed;
Worker.WorkerReportsProgress = true;
Worker.RunWorkerAsync(skipLock);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
var skipLock = (bool)e.Argument;
var worker = sender as BackgroundWorker;
DownloadController.Manager.SkipLock = skipLock;
DownloadController.Manager.Feedback = worker.ReportProgress;
DownloadController.Manager.StartProcess(worker);
}
private void worker_completed(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
if(e.Error is HideClientUpdateException)
{
ViewController.ErrorString = e.Error.Message;
}
else
{
ViewController.Error = e.Error;
}
Exception.Invoke(this, new EventArgs());
}
else
{
Successful.Invoke(this, new EventArgs());
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var progress = (LauncherProgress)e.ProgressPercentage;
var obj = e.UserState;
switch (progress)
{
case LauncherProgress.ProgressStart:
Message = $"{getKeyword(0)} wird vorbereitet...";
SubMessage = "";
Bar2Activate();
break;
case LauncherProgress.DownloadStart:
Message = "Download startet...";
SubMessage = "";
Bar1Show();
Bar1Activate();
Bar1Reset();
Bar1SetLimit(DownloadController.Manager.FilesToDownloadCount);
break;
case LauncherProgress.DownloadUpdate:
var args = obj as DownloadProgressEventArgs;
Message = "Download wird ausgeführt...";
SubMessage = $"{args.File.RelativePath} ({args.Size}) wurde heruntergeladen...";
Bar1MakeStep();
Bar2MakeStep();
break;
case LauncherProgress.DownloadRetry:
var args7 = obj as DownloadRetryEventArgs;
Message = "Download wird wiederholt...";
SubMessage = $"{args7.File.RelativePath} wird erneut heruntergeladen... (Versuch: {args7.Try_Num}/{args7.Max_Tries})";
break;
case LauncherProgress.DownloadFinish:
Message = "Download abgeschlossen!";
SubMessage = "";
Bar1Hide();
break;
case LauncherProgress.ApplyStart:
Message = "Füge Pakete hinzu...";
SubMessage = "";
Bar1Show();
Bar1Activate();
Bar1Reset();
Bar1SetLimit(DownloadController.Manager.FilesToApplyCount);
break;
case LauncherProgress.ApplyUpdate:
var args2 = obj as ApplyFileEventArgs;
SubMessage = $"{args2.RelativePath}\n wurde installiert...";
Bar1MakeStep();
Bar2MakeStep();
break;
case LauncherProgress.ApplyFinished:
Message = "Hinzufügen abgeschlossen!";
SubMessage = "";
Bar1Hide();
break;
case LauncherProgress.DeleteStart:
Message = getKeyword(1);
SubMessage = "";
Bar1Show();
Bar1Activate();
Bar1Reset();
Bar1SetLimit(DownloadController.Manager.FilesToDeleteInApplyCount);
break;
case LauncherProgress.DeleteUpdate:
var args3 = obj as string;
SubMessage = $"{args3}\n wurde entfernt!";
Bar1MakeStep();
Bar2MakeStep();
break;
case LauncherProgress.DeleteFinished:
Message = getKeyword(2);
SubMessage = "";
Bar1Hide();
break;
case LauncherProgress.CleanupStart:
Message = "Räume Ordner auf...";
SubMessage = "";
Bar1Show();
Bar1Deactivate();
break;
case LauncherProgress.CleanupUpdate:
break;
case LauncherProgress.CleanupFinished:
Message = "Ordner aufgeräumt!";
SubMessage = "";
break;
case LauncherProgress.ProgressEnd:
Message = $"{getKeyword(0)} abgeschlossen!";
SubMessage = "";
Bar2Fill();
break;
default:
break;
}
}
#region a
public void Bar1Activate() => proBar1.IsIndeterminate = false;
public void Bar1Deactivate() => proBar1.IsIndeterminate = true;
public void Bar1SetLimit(int max) => proBar1.Maximum = max;
public void Bar1Reset() => proBar1.Value = 0;
public void Bar1MakeStep() => proBar1.Value++;
public void Bar1Hide() => proBar1.Visibility = Visibility.Hidden;
public void Bar1Show() => proBar1.Visibility = Visibility.Visible;
public void Bar2Activate() => proBar2.IsIndeterminate = false;
public void Bar2Deactivate() => proBar2.IsIndeterminate = true;
public void Bar2SetLimit(int max) => proBar2.Maximum = max;
public void Bar2MakeStep() => proBar2.Value++;
public void Bar2MakeStep(int val) => proBar2.Value += val;
public void Bar2Fill() => proBar2.Value = proBar2.Maximum;
public void Bar2Hide() => proBar2.Visibility = Visibility.Hidden;
public void Bar2Show() => proBar2.Visibility = Visibility.Visible;
public void Bar2SetPercent(double percentage)
{
DoubleAnimation animation = new DoubleAnimation(percentage, duration);
proBar2.BeginAnimation(ProgressBar.ValueProperty, animation);
}
#endregion
}
}