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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Tango.Core.DI;
using Tango.Core.ExtensionMethods;
using Tango.FSE.BL.Web;
using Tango.FSE.Common;
using Tango.FSE.Common.Authentication;
using Tango.FSE.Common.Build;
using Tango.FSE.Common.DemoMode;
using Tango.FSE.Common.FSEApplication;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.Threading;
using Tango.FSE.Common.Updates;
using Tango.FSE.UI.Dialogs;
using Tango.FSE.Web.Messages;
namespace Tango.FSE.UI.Updates
{
/// <summary>
/// Represents the default <see cref="IUpdatesManager"/> implementation.
/// </summary>
/// <seealso cref="Tango.FSE.Common.Updates.IUpdatesManager" />
[TangoCreateWhenRegistered]
public class DefaultUpdatesManager : FSEExtendedObject, IUpdatesManager
{
private Timer _autoUpdateCheckTimer;
private const double AUTO_UPDATE_CHECK_INTERVAL_MINUTES = 30;
private bool _performedFirstCheck;
[TangoInject]
private FSEWebClient WebClient { get; set; }
[TangoInject]
private IFSEApplicationManager ApplicationManager { get; set; }
[TangoInject]
private INotificationProvider NotificationProvider { get; set; }
[TangoInject]
private IDispatcherProvider DispatcherProvider { get; set; }
[TangoInject]
private IAuthenticationProvider AuthenticationProvider { get; set; }
[TangoInject(TangoInjectMode.WhenAvailable)]
private IDemoModeManager DemoModeManager { get; set; }
[TangoInject]
private IBuildProvider BuildProvider { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to perform an automatic update checks.
/// </summary>
public bool AutoCheckForUpdates { get; set; }
/// <summary>
/// Gets or sets the automatic update check interval.
/// </summary>
public TimeSpan AutoUpdateCheckInterval { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="DefaultUpdatesManager"/> class.
/// </summary>
public DefaultUpdatesManager()
{
_autoUpdateCheckTimer = new Timer(TimeSpan.FromMinutes(AUTO_UPDATE_CHECK_INTERVAL_MINUTES).TotalMilliseconds);
_autoUpdateCheckTimer.Elapsed += _autoUpdateCheckTimer_Elapsed;
_autoUpdateCheckTimer.Stop();
}
/// <summary>
/// Checks for updates and returns a response containing the relevant blob/cdn addresses.
/// </summary>
/// <returns></returns>
public async Task<CheckForUpdatesResponse> CheckForUpdates()
{
try
{
LogManager.Log("Checking for updates...");
var appVersion = ApplicationManager.Version;
var response = await WebClient.CheckForUpdates(new CheckForUpdatesRequest()
{
Version = appVersion.ToString(),
Build = Tango.FSE.BL.BuildProvider.Build
});
LogManager.Log($"Update check response received:\n{response.ToJsonString()}");
return response;
}
catch (Exception ex)
{
throw LogManager.Log(ex, "Error checking for updates.");
}
}
/// <summary>
/// Called when is ready and user is logged-in. (happens every time a user logs-in)
/// </summary>
public async void OnApplicationReady(IFSEApplicationManager applicationManager)
{
if (!_performedFirstCheck)
{
_performedFirstCheck = true;
if (!applicationManager.DemoMode)
{
try
{
await Task.Delay(3000);
LogManager.Log("Performing first application update check...");
var response = await CheckForUpdates();
if (response.IsUpdateAvailable)
{
LogManager.Log("Update is available. Invoking application update dialog...");
DisplayApplicationUpdateDialog(response);
}
}
catch
{
LogManager.Log("First application run update check failed.");
}
}
}
_autoUpdateCheckTimer.Start();
if (applicationManager.DemoMode)
{
DemoModeManager.InsertCommand(async () =>
{
await CheckForUpdatesWithDialog();
}, "Emulate Application Update", "Emulates an application update snackbar notification.");
}
}
private async void _autoUpdateCheckTimer_Elapsed(object sender, ElapsedEventArgs e)
{
_autoUpdateCheckTimer.Stop();
if (Settings.AutoCheckForUpdates)
{
await CheckForUpdatesWithDialog();
}
_autoUpdateCheckTimer.Start();
}
private async Task CheckForUpdatesWithDialog()
{
try
{
LogManager.Log("Performing automatic update check...");
var response = await CheckForUpdates();
if (response.IsUpdateAvailable)
{
LogManager.Log("Application update is available. Pushing snackbar item...");
DispatcherProvider.Invoke(() =>
{
NotificationProvider.PushSnackbarItem(
MessageType.ApplicationUpdate,
"Application Update",
true,
$"New version of {BuildProvider.BuildName} is available\nTap to see more details.",
TimeSpan.FromMinutes(5),
null,
() =>
{
LogManager.Log("Application update snackbar item pressed. Invoking application update dialog...");
DisplayApplicationUpdateDialog(response);
});
});
}
}
catch
{
LogManager.Log("Automatic update check failed.");
}
}
private void DisplayApplicationUpdateDialog(CheckForUpdatesResponse response)
{
DispatcherProvider.Invoke(async () =>
{
var vm = await NotificationProvider.ShowDialog<ApplicationUpdateViewVM>(new ApplicationUpdateViewVM()
{
Version = Version.Parse(response.Version).ToString(3),
Comments = response.Comments
});
if (vm.DialogResult)
{
Process.Start(AuthenticationProvider.CurrentEnvironment.MachineServiceAddress + $"/fse?buildVariant={(int)BuildProvider.CurrentBuild}");
}
});
}
}
}
|