-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
214 lines (213 loc) · 7.44 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using Newtonsoft.Json;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Collections.ObjectModel;
using EasyService.Models;
using System.Diagnostics;
using EasyService.Views;
namespace EasyService {
public partial class MainWindow : MetroWindow {
public readonly string mac = HelperMethods.GetBeautiMacAddress();
public ObservableCollection<RequestInfo> Requests;
public ProgressDialogController controller;
private readonly MainWindowViewModel viewModel;
private bool allowSelection;
public bool launch = false;
public MainWindow() {
InitializeComponent();
viewModel = (MainWindowViewModel)DataContext;
viewModel.mainWindow = this;
LaunchApp();
}
public async void LaunchApp() {
controller = await this.ShowProgressAsync("Загрузка приложения", "Пожалуйста подождите...");
controller.SetIndeterminate();
ipv4.Text = "IP: " + HelperMethods.GetIPv4();
hostName.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
await RefreshWelcomePageAndRequestsList();
mainContentControl.Content = new Views.Welcome();
allowSelection = true;
}
public async Task RefreshWelcomePageAndRequestsList() {
allowSelection = false;
Refresh_Button.IsEnabled = false;
if (!launch) {
GetWelcomeText();
}
try {
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Checker", viewModel.cheker);
var responseBody = await client.GetStringAsync($"{viewModel.addres}/api/v1/{mac}/requests");
var requestsFromJson = JsonConvert.DeserializeObject<List<RequestInfo>>(responseBody);
Requests = new ObservableCollection<RequestInfo>();
foreach (var item in requestsFromJson) {
switch (item.Status) {
case "В обработке":
Requests.Add(new RequestInfo {
Id = item.Id,
Status = item.Status,
IdName = "Заявка №" + item.Id,
CreatedAt = item.Topic,
Icon = "ClipboardTextClockOutline",
IconColor = "#00cfcf"
});
break;
case "В работе":
Requests.Add(new RequestInfo {
Id = item.Id,
Status = item.Status,
IdName = "Заявка №" + item.Id,
CreatedAt = item.Topic,
Icon = "ProgressWrench",
IconColor = "#e06900"
});
break;
case "Завершено":
Requests.Add(new RequestInfo {
Id = item.Id,
Status = item.Status,
IdName = "Заявка №" + item.Id,
CreatedAt = item.Topic,
Icon = "CheckCircleOutline",
IconColor = "#85de00"
});
break;
case "Отменено":
Requests.Add(new RequestInfo {
Id = item.Id,
Status = item.Status,
IdName = "Заявка №" + item.Id,
CreatedAt = item.Topic,
Icon = "CloseCircleOutline",
IconColor = "#b30000"
});
break;
default:
Requests.Add(new RequestInfo {
Id = item.Id,
Status = item.Status,
IdName = "Заявка №" + item.Id,
CreatedAt = item.Topic,
Icon = "HelpCircleOutline",
IconColor = "#f5f5f5"
});
break;
}
}
RequestsList.ItemsSource = Requests;
if (RequestsList.Items.Count > 0) {
RequestsList.ScrollIntoView(RequestsList.Items[0]);
}
launch = true;
if (controller.IsOpen) {
await controller.CloseAsync();
}
}
catch {
if (controller.IsOpen) {
await controller.CloseAsync();
}
_ = await this.ShowMessageAsync("Ошибка", "Отсутствует соединение с сервером");
if (Requests != null) {
Requests.Clear();
}
ShowNetworkProblem();
launch = false;
}
Refresh_Button.IsEnabled = true;
allowSelection = true;
}
public async void GetWelcomeText() {
try {
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Checker", viewModel.cheker);
var responseBody = await client.GetStringAsync($"{viewModel.addres}/api/v1/info");
var requestsFromJson = JsonConvert.DeserializeObject<Options>(responseBody);
viewModel.WelcomeText = requestsFromJson.WelcomeTextApp;
viewModel.WelcomeIconType = "CommentAlertOutline";
viewModel.WelcomeIconColor = "#ffffff";
Title = requestsFromJson.CompanyName;
}
catch { }
}
public void ShowNetworkProblem() {
viewModel.WelcomeText = "Отсутствует соединение с сервером";
viewModel.WelcomeIconType = "AlertOutline";
viewModel.WelcomeIconColor = "#e8b600";
}
public void RequestsListSelectionChanged(object sender, SelectionChangedEventArgs e) {
LoadRequestPage();
}
public async void LoadRequestPage() {
if (allowSelection) {
ScrollViewerForUserControl.ScrollToTop();
var requestInfo = (RequestInfo)RequestsList.SelectedItem;
controller = await this.ShowProgressAsync("Загрузка заявки", "Пожалуйста подождите...");
controller.SetIndeterminate();
mainContentControl.Content = new RequestShow(viewModel, requestInfo.Id, requestInfo.Status);
Button_Create.Visibility = Visibility.Hidden;
Button_Close.Visibility = Visibility.Visible;
}
}
private async void Button_Refresh_Click(object sender, RoutedEventArgs e) {
await Refresh();
}
public async Task Refresh(bool needAgainLoadRequestpage = true) {
var selectedItemIndex = RequestsList.SelectedIndex;
if (mainContentControl.Content is RequestShow && RequestsList.SelectedItem != null && needAgainLoadRequestpage) {
LoadRequestPage();
}
await RefreshWelcomePageAndRequestsList();
if (selectedItemIndex != -1) {
allowSelection = false;
RequestsList.SelectedItem = RequestsList.Items[selectedItemIndex];
allowSelection = true;
}
}
private void Button_Phone_Click(object sender, RoutedEventArgs e) {
_ = Process.Start("explorer.exe", viewModel.addres + "/contacts");
}
private void Button_Doc_Click(object sender, RoutedEventArgs e) {
_ = Process.Start("explorer.exe", viewModel.addres + "/docs");
}
private void Button_Web_Click(object sender, RoutedEventArgs e) {
_ = Process.Start("explorer.exe", viewModel.addres);
}
private void Button_Create_Click(object sender, RoutedEventArgs e) {
ScrollViewerForUserControl.ScrollToTop();
mainContentControl.Content = new RequestForm(viewModel);
Button_Create.Visibility = Visibility.Hidden;
Button_Close.Visibility = Visibility.Visible;
}
public void CloseAnyForm() {
allowSelection = false;
RequestsList.SelectedItem = null;
allowSelection = true;
mainContentControl.Content = new Welcome();
Button_Create.Visibility = Visibility.Visible;
Button_Close.Visibility = Visibility.Hidden;
}
private void Button_Close_Click(object sender, RoutedEventArgs e) {
CloseAnyForm();
}
}
}