/
ProgressPleaseWaitService.cs
132 lines (105 loc) · 4.38 KB
/
ProgressPleaseWaitService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProgressPleaseWaitService.cs" company="WildGums">
// Copyright (c) 2008 - 2015 WildGums. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Orchestra.Services
{
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using Catel;
using Catel.IoC;
using Catel.Logging;
using Catel.Services;
internal class ProgressPleaseWaitService : PleaseWaitService
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
private readonly IDependencyResolver _dependencyResolver;
private ProgressBar _progressBar;
private ResourceDictionary _resourceDictionary;
private readonly DispatcherTimer _hidingTimer;
public ProgressPleaseWaitService(IDispatcherService dispatcherService, IDependencyResolver dependencyResolver)
: base(dispatcherService)
{
Argument.IsNotNull(() => dependencyResolver);
_dependencyResolver = dependencyResolver;
_hidingTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(10)
};
_hidingTimer.Tick += OnHideTimerTick;
}
public override void Hide()
{
base.Hide();
_hidingTimer.Start();
}
public override void UpdateStatus(int currentItem, int totalItems, string statusFormat = "")
{
base.UpdateStatus(currentItem, totalItems, statusFormat);
var progressBar = InitializeProgressBar();
if (progressBar != null)
{
_dispatcherService.BeginInvoke(() =>
{
progressBar.SetCurrentValue(System.Windows.Controls.Primitives.RangeBase.MinimumProperty, (double)0);
progressBar.SetCurrentValue(System.Windows.Controls.Primitives.RangeBase.MaximumProperty, (double)totalItems);
progressBar.SetCurrentValue(System.Windows.Controls.Primitives.RangeBase.ValueProperty, (double)currentItem);
if (currentItem < 0 || currentItem >= totalItems)
{
Hide();
}
else if (progressBar.Visibility != Visibility.Visible)
{
Log.Debug("Showing progress bar");
_hidingTimer.Stop();
progressBar.SetCurrentValue(UIElement.VisibilityProperty, Visibility.Visible);
}
}, true);
}
}
private void OnHideTimerTick(object sender, EventArgs eventArgs)
{
Log.Debug("Hiding progress bar");
_hidingTimer.Stop();
var progressBar = InitializeProgressBar();
if (progressBar == null)
{
return;
}
var storyboard = GetHideProgressBarStoryboard();
storyboard.Completed += (s, e) =>
{
progressBar.SetCurrentValue(UIElement.VisibilityProperty, Visibility.Hidden);
};
storyboard.Begin(progressBar);
}
private ProgressBar InitializeProgressBar()
{
if (_progressBar == null)
{
_progressBar = _dependencyResolver.TryResolve<ProgressBar>("pleaseWaitService");
if (_progressBar != null)
{
Log.Debug("Found progress bar that will represent progress inside the ProgressPleaseWaitService");
}
}
return _progressBar;
}
private Storyboard GetHideProgressBarStoryboard()
{
if (_resourceDictionary == null)
{
_resourceDictionary = new ResourceDictionary
{
Source = new Uri("/Orchestra.Core;Component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute)
};
}
var storyBoard = (Storyboard)_resourceDictionary["FadeOutStoryboard"];
return storyBoard;
}
}
}