-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLifetimeService.cs
228 lines (188 loc) · 6.73 KB
/
LifetimeService.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
223
224
225
226
227
228
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Telegram.Collections;
using Telegram.Controls;
using Telegram.Navigation;
using Telegram.Views;
using Telegram.Views.Host;
using Windows.Storage;
using Windows.UI.Xaml.Media;
namespace Telegram.Services
{
public interface ILifetimeService
{
void Update();
ISessionService Create(bool update = true, bool test = false);
ISessionService Remove(ISessionService item);
ISessionService Remove(ISessionService item, ISessionService active);
void Destroy(ISessionService item);
bool ShouldClose(ISessionService item);
bool Contains(string phoneNumber);
void Register(ISessionService item);
void Unregister(ISessionService item);
MvxObservableCollection<ISessionService> Items { get; }
ISessionService ActiveItem { get; set; }
ISessionService PreviousItem { get; set; }
}
public partial class LifetimeService : BindableBase, ILifetimeService
{
private readonly Dictionary<int, ISessionService> _sessions = new Dictionary<int, ISessionService>();
public LifetimeService()
{
Items = new MvxObservableCollection<ISessionService>();
}
public void Register(ISessionService item)
{
_sessions[item.Id] = item;
}
public void Unregister(ISessionService item)
{
_sessions[item.Id] = null;
}
public void Update()
{
Items.ReplaceWith(TypeResolver.Current.GetSessions());
ActiveItem = Items.FirstOrDefault(x => x.IsActive) ?? Items.FirstOrDefault();
}
private void Update(ISessionService session)
{
Items.ReplaceWith(TypeResolver.Current.GetSessions());
ActiveItem = session;
}
public MvxObservableCollection<ISessionService> Items { get; }
private ISessionService _previousItem;
public ISessionService PreviousItem
{
get => _previousItem;
set => _previousItem = value;
}
private ISessionService _activeItem;
public ISessionService ActiveItem
{
get => _activeItem;
set
{
if (_activeItem == value)
{
return;
}
if (_activeItem != null)
{
_activeItem.IsActive = false;
_previousItem = _activeItem;
SettingsService.Current.PreviousSession = _activeItem.Id;
}
if (value != null)
{
value.IsActive = true;
SettingsService.Current.ActiveSession = value.Id;
}
//Set(ref _activeItem, value);
_activeItem = value;
}
}
public ISessionService Create(bool update = true, bool test = false)
{
var app = BootStrapper.Current as App;
var sessions = TypeResolver.Current.GetSessions().ToList();
var id = sessions.Count > 0 ? sessions.Max(x => x.Id) + 1 : 0;
var settings = ApplicationData.Current.LocalSettings.CreateContainer($"{id}", ApplicationDataCreateDisposition.Always);
settings.Values["UseTestDC"] = test;
var container = TypeResolver.Current.Build(id);
var session = container.Resolve<ISessionService>();
if (update)
{
Update(session);
}
return session;
}
public ISessionService Remove(ISessionService item)
{
return Remove(item, _previousItem ?? Create());
}
public ISessionService Remove(ISessionService item, ISessionService active)
{
TypeResolver.Current.Destroy(item.Id);
active ??= _previousItem ?? Create();
Update(active);
item.Aggregator.Unsubscribe(item);
//WindowContext.Unsubscribe(item);
WindowContext.Current.NavigationServices.RemoveByFrameId($"{item.Id}");
WindowContext.Current.NavigationServices.RemoveByFrameId($"Main{item.Id}");
return active;
}
public async void Destroy(ISessionService item)
{
ISessionService replace = null;
if (item.IsActive)
{
ActiveItem = replace = _previousItem ?? Items.FirstOrDefault(x => x.Id != item.Id) ?? Create(false);
}
TypeResolver.Current.Destroy(item.Id);
Update();
item.Aggregator.Unsubscribe(item);
//WindowContext.Unsubscribe(item);
await WindowContext.ForEachAsync(window =>
{
if (window.Content is RootPage root && replace != null)
{
root.Switch(replace);
}
if (window.IsInMainView)
{
window.NavigationServices.RemoveByFrameId($"{item.Id}");
window.NavigationServices.RemoveByFrameId($"Main{item.Id}");
foreach (var popup in VisualTreeHelper.GetOpenPopups(window))
{
if (popup.Child is ContentPopup toast)
{
toast.Tag = null;
toast.Hide();
}
}
return Task.CompletedTask;
}
else
{
return WindowContext.Current.ConsolidateAsync();
}
});
await Task.Factory.StartNew(() =>
{
try
{
Directory.Delete(Path.Combine(ApplicationData.Current.LocalFolder.Path, $"{item.Id}"), true);
}
catch { }
});
}
public bool ShouldClose(ISessionService item)
{
return true;
}
public bool Contains(string phoneNumber)
{
foreach (var session in Items)
{
var user = session.ClientService.GetUser(session.UserId);
if (user == null)
{
continue;
}
if (user.PhoneNumber.Contains(phoneNumber) || phoneNumber.Contains(user.PhoneNumber))
{
return true;
}
}
return false;
}
}
}