forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGAvatarViewModel.m
107 lines (90 loc) · 3.58 KB
/
TGAvatarViewModel.m
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
#import "TGAvatarViewModel.h"
#import <WatchCommonWatch/WatchCommonWatch.h>
#import "TGStringUtils.h"
#import "TGWatchColor.h"
#import "WKInterfaceGroup+Signals.h"
#import "TGBridgeMediaSignals.h"
@interface TGAvatarViewModel ()
{
TGBridgeUser *_currentUser;
TGBridgeChat *_currentChat;
}
@end
@implementation TGAvatarViewModel
- (void)updateWithUser:(TGBridgeUser *)user context:(TGBridgeContext *)context isVisible:(bool (^)(void))isVisible
{
TGBridgeUser *oldUser = _currentUser;
_currentUser = user;
if (_currentUser.identifier == context.userId)
{
self.label.hidden = true;
self.group.backgroundColor = [UIColor hexColor:0x222223];
[self.group setBackgroundImageSignal:[SSignal single:@"SavedMessagesAvatar"] isVisible:isVisible];
}
else if (_currentUser.photoSmall.length > 0)
{
if (![_currentUser.photoSmall isEqualToString:oldUser.photoSmall])
{
self.label.hidden = true;
self.group.backgroundColor = [UIColor hexColor:0x222223];
__block bool completed = false;
__weak TGAvatarViewModel *weakSelf = self;
[self.group setBackgroundImageSignal:[[[TGBridgeMediaSignals avatarWithPeerId:_currentUser.identifier url:_currentUser.photoSmall type:TGBridgeMediaAvatarTypeSmall] onNext:^(id next)
{
completed = true;
}] onDispose:^
{
__strong TGAvatarViewModel *strongSelf = weakSelf;
if (strongSelf != nil && !completed)
strongSelf->_currentUser = nil;
}] isVisible:isVisible];
}
}
else
{
if (oldUser.photoSmall.length > 0 || ![[oldUser displayName] isEqualToString:[_currentUser displayName]])
{
self.label.hidden = false;
self.label.text = [TGStringUtils initialsForFirstName:_currentUser.firstName lastName:_currentUser.lastName single:true];
self.group.backgroundColor = [TGColor colorForUserId:(int32_t)user.identifier myUserId:context.userId];
}
}
}
- (void)updateWithChat:(TGBridgeChat *)chat isVisible:(bool (^)(void))isVisible
{
TGBridgeChat *oldChat = _currentChat;
_currentChat = chat;
if (_currentChat.groupPhotoSmall.length > 0)
{
if (![_currentChat.groupPhotoSmall isEqualToString:oldChat.groupPhotoSmall])
{
self.label.hidden = true;
self.group.backgroundColor = [UIColor hexColor:0x222223];
__block bool completed = false;
__weak TGAvatarViewModel *weakSelf = self;
[self.group setBackgroundImageSignal:[[[TGBridgeMediaSignals avatarWithPeerId:_currentChat.identifier url:_currentChat.groupPhotoSmall type:TGBridgeMediaAvatarTypeSmall] onNext:^(id next)
{
completed = true;
}] onDispose:^
{
__strong TGAvatarViewModel *strongSelf = weakSelf;
if (strongSelf != nil && !completed)
strongSelf->_currentChat = nil;
}] isVisible:isVisible];
}
}
else
{
if (oldChat.groupPhotoSmall.length > 0 || ![[oldChat groupTitle] isEqualToString:[_currentChat groupTitle]])
{
self.label.hidden = false;
self.label.text = [TGStringUtils initialForGroupName:_currentChat.groupTitle];
self.group.backgroundColor = [TGColor colorForGroupId:_currentChat.identifier];
}
}
}
- (void)updateIfNeeded
{
[self.group updateIfNeeded];
}
@end