-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathProfileBotsTabViewModel.cs
111 lines (92 loc) · 3.18 KB
/
ProfileBotsTabViewModel.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
//
// 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.Threading.Tasks;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels.Profile
{
public partial class ProfileBotsTabViewModel : ViewModelBase, IHandle, IIncrementalCollectionOwner
{
private long _chatId;
private long _botUserId;
public ProfileBotsTabViewModel(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
Items = new IncrementalCollection<User>(this);
}
protected override Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
if (parameter is long chatId && ClientService.TryGetChat(chatId, out Chat chat) && ClientService.TryGetUser(chat, out User user))
{
_chatId = chatId;
_botUserId = user.Id;
}
return Task.CompletedTask;
}
public override void Subscribe()
{
Aggregator.Subscribe<UpdatePremiumState>(this, Handle);
}
private void Handle(UpdatePremiumState update)
{
if (update.IsPremium && _canUnlockMore)
{
Dispatcher.Dispatch(Reload);
}
}
private void Reload()
{
CanUnlockMore = false;
HasMoreItems = true;
Items.Clear();
}
public IncrementalCollection<User> Items { get; private set; }
private bool _canUnlockMore;
public bool CanUnlockMore
{
get => _canUnlockMore && !IsPremium && IsPremiumAvailable;
set => Set(ref _canUnlockMore, value);
}
private int _totalCount;
public int TotalCount
{
get => _totalCount;
set => Set(ref _totalCount, value);
}
public async Task<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
var total = 0u;
var response = await ClientService.SendAsync(new GetBotSimilarBots(_botUserId));
if (response is Telegram.Td.Api.Users users)
{
CanUnlockMore = users.TotalCount > users.UserIds.Count;
TotalCount = users.TotalCount;
foreach (var chat in ClientService.GetUsers(users.UserIds))
{
Items.Add(chat);
total++;
}
}
HasMoreItems = false;
return new LoadMoreItemsResult
{
Count = total
};
}
public bool HasMoreItems { get; private set; } = true;
public void UnlockMore()
{
NavigationService.ShowPromo(new PremiumSourceLimitExceeded(new PremiumLimitTypeSimilarChatCount()));
}
}
}