-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUicModule.cs
222 lines (175 loc) · 7.79 KB
/
UicModule.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
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
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using ArcGIS.Core.Events;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Mapping.Events;
using ProEvergreen;
using Reactive.Bindings;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Email;
using uic_addin.Models;
using uic_addin.Views;
using Module = ArcGIS.Desktop.Framework.Contracts.Module;
namespace uic_addin {
internal class UicModule : Module {
private static UicModule _this;
private readonly IEnumerable<string> _addinKeys = new[] { "UICAddin.Evergreen.BetaChannel" };
public readonly Dictionary<string, DockPane> DockPanes = new Dictionary<string, DockPane>(2);
public readonly string HasUpdateState = "has_update";
public readonly string HasImplementationState = "has_implemented";
public readonly Dictionary<string, Layer> Layers = new Dictionary<string, Layer>(1);
private SubscriptionToken _token;
public ReactiveProperty<Evergreen> Evergreen { get; } = new ReactiveProperty<Evergreen> {
Value = new Evergreen("uic-utah", "uic-addin")
};
public ReactiveProperty<bool> IsCurrent { get; } = new ReactiveProperty<bool>(true);
public EvergreenSettings EvergreenSettings { get; set; } = new EvergreenSettings {
BetaChannel = true
};
public static UicModule Current => _this ?? (_this = (UicModule)FrameworkApplication.FindModule("UICModule"));
public Dictionary<string, string> Settings { get; set; } = new Dictionary<string, string>();
protected override bool Initialize() {
SetupLogging();
FrameworkApplication.State.Deactivate(HasImplementationState);
QueuedTask.Run(async () => {
await CheckForLastest();
Log.Debug("Initializing UIC Workflow Addin {version}", EvergreenSettings.CurrentVersion.AddInVersion);
});
if (MapView.Active == null) {
_token = MapViewInitializedEvent.Subscribe(args => CacheLayers(args.MapView));
} else {
CacheLayers(MapView.Active);
if (Layers.Count < 1 || Layers.Any(x => x.Value == null)) {
return false;
}
}
return true;
}
private void SetupLogging() {
var addinFolder = GetAddinFolder();
var logLocation = Path.Combine(addinFolder, "{Date}-log.txt");
var email = new EmailConnectionInfo {
EmailSubject = "UIC Addin",
FromEmail = "noreply@utah.gov",
ToEmail = "sgourley@utah.gov",
MailServer = "send.state.ut.us",
Port = 25
};
Log.Logger = new LoggerConfiguration()
.WriteTo.Email(email, restrictedToMinimumLevel: LogEventLevel.Error)
.WriteTo.RollingFile(logLocation, retainedFileCountLimit: 7)
.MinimumLevel.Verbose()
.CreateLogger();
}
public string GetAddinFolder() {
var myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var arcGisProLocation = Path.Combine(myDocs, "ArcGIS", "AddIns", "ArcGISPro");
var attribute = (GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute),
true)[0];
var proAddinFolder = $"{{{attribute.Value}}}";
var addinFolder = Path.Combine(arcGisProLocation, proAddinFolder);
return addinFolder;
}
protected override async Task OnReadSettingsAsync(ModuleSettingsReader settings) {
Settings.Clear();
if (settings == null) {
try {
await CheckForLastest();
} catch {
// ignored
}
await base.OnReadSettingsAsync(null);
return;
}
foreach (var key in _addinKeys) {
var value = settings.Get(key);
if (value != null) {
Settings[key] = value.ToString();
}
}
EvergreenSettings.BetaChannel = Convert.ToBoolean(Settings["UICAddin.Evergreen.BetaChannel"]);
try {
await CheckForLastest();
} catch {
// ignored
}
}
protected override async Task OnWriteSettingsAsync(ModuleSettingsWriter settings) {
foreach (var key in Settings.Keys) {
settings.Add(key, Settings[key]);
}
try {
await CheckForLastest();
} catch {
// ignored
}
}
protected override bool CanUnload() {
foreach (var pane in DockPanes.Values) {
if (pane.IsVisible) {
pane.Hide();
}
}
return true;
}
public void CacheLayers(MapView view = null) {
Log.Debug("Caching project layers");
if (_token != null) {
MapViewInitializedEvent.Unsubscribe(_token);
}
var activeView = MapView.Active ?? view;
if (activeView == null) {
Log.Debug("MapView is empty");
return;
}
// Layers[FacilityModel.TableName] = LayerService.GetTable(FacilityModel.TableName, activeView.Map)
// as FeatureLayer;
}
private void FindPaneFromId(string id) {
if (DockPanes.ContainsKey(id)) {
return;
}
DockPanes[id] = FrameworkApplication.DockPaneManager.Find(id);
}
public async Task CheckForLastest() {
var useBetaChannel = true;
if (Current.Settings.TryGetValue("UICAddin.Evergreen.BetaChannel", out var value)) {
bool.TryParse(value, out useBetaChannel);
}
EvergreenSettings.LatestRelease = await Evergreen.Value.GetLatestReleaseFromGithub(useBetaChannel);
EvergreenSettings.CurrentVersion = Evergreen.Value.GetCurrentAddInVersion();
try {
IsCurrent.Value = Evergreen.Value.IsCurrent(EvergreenSettings.CurrentVersion.AddInVersion,
EvergreenSettings.LatestRelease);
} catch (ArgumentNullException) {
if (EvergreenSettings.CurrentVersion == null) {
// pro addin version couldnt be found
throw;
}
// github doesn't have a version. most likely only prereleases and no stable
IsCurrent.Value = true;
}
var wrapper = FrameworkApplication.GetPlugInWrapper("UpdateAvailableButton");
if (IsCurrent.Value) {
FrameworkApplication.State.Deactivate(HasUpdateState);
wrapper.Caption = "Up to date! 💙";
wrapper.DisabledTooltip = $"You are using the most recent version: {Current.EvergreenSettings?.LatestRelease?.TagName}!";
wrapper.TooltipHeading = "UIC Add-in";
} else {
FrameworkApplication.State.Activate(HasUpdateState);
wrapper.Caption = "Update Available";
wrapper.TooltipHeading = "Stay Current!";
wrapper.Tooltip = $"Update to {Current.EvergreenSettings?.LatestRelease?.TagName}";
}
}
}
}