1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-02 17:38:07 +01:00
Telegram/Telegraph/TGGroupInfoUserCollectionItem.m

300 lines
9.1 KiB
Mathematica
Raw Normal View History

2014-07-10 16:11:09 +02:00
/*
* This is the source code of Telegram for iOS v. 1.1
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Peter Iakovlev, 2013.
*/
#import "TGGroupInfoUserCollectionItem.h"
#import "TGGroupInfoUserCollectionItemView.h"
#import "TGDateUtils.h"
#import "TGUser.h"
2015-10-01 18:19:52 +02:00
#import "TGConversation.h"
2014-07-10 16:11:09 +02:00
#import "ASHandle.h"
2015-10-01 18:19:52 +02:00
#import "TGStringUtils.h"
2014-07-10 16:11:09 +02:00
@interface TGGroupInfoUserCollectionItem () <TGGroupInfoUserCollectionItemViewDelegate>
{
bool _canEdit;
bool _disabled;
}
@end
@implementation TGGroupInfoUserCollectionItem
- (instancetype)init
{
self = [super init];
if (self != nil)
{
_canEdit = true;
}
return self;
}
- (Class)itemViewClass
{
return [TGGroupInfoUserCollectionItemView class];
}
- (CGSize)itemSizeForContainerSize:(CGSize)containerSize
{
return CGSizeMake(containerSize.width, 48);
}
- (void)bindView:(TGGroupInfoUserCollectionItemView *)view
{
[super bindView:view];
view.delegate = self;
[view setFirstName:_user.firstName lastName:_user.lastName uidForPlaceholderCalculation:_user.uid];
2015-10-01 18:19:52 +02:00
if (_customStatus != nil) {
[view setStatus:_customStatus active:false];
} else {
if (_user.kind == TGUserKindBot || _user.kind == TGUserKindSmartBot)
{
NSString *botStatus = nil;
if (_user.kind == TGUserKindBot)
botStatus = TGLocalized(@"Bot.GroupStatusDoesNotReadHistory");
else
botStatus = TGLocalized(@"Bot.GroupStatusReadsHistory");
[view setStatus:botStatus active:false];
}
else
{
bool active = false;
NSString *status = [self _statusStringFromUserPresence:_user.presence active:&active];
[view setStatus:status active:active];
}
}
2014-07-10 16:11:09 +02:00
[view setAvatarUri:_user.photoUrlSmall];
[view setEnableEditing:_canEdit animated:false];
2015-10-01 18:19:52 +02:00
if (_conversation == nil)
{
[view setIsSecretChat:false];
}
else
{
[view setIsSecretChat:_conversation.isEncrypted];
if (_user == nil)
{
[view setFirstName:_conversation.chatTitle lastName:@"" uidForPlaceholderCalculation:(int32_t)_conversation.conversationId];
[view setStatus:[self stringForMemberCount:_conversation.chatParticipantCount] active:false];
[view setAvatarUri:_conversation.chatPhotoSmall];
}
}
if (_optionTitle != nil) {
view.optionText = _optionTitle;
}
2016-02-25 01:03:51 +01:00
[view setDisplaySwitch:_displaySwitch];
[view setEnableSwitch:_enableSwitch animated:false];
[view setSwitchIsOn:_switchIsOn animated:false];
2014-07-10 16:11:09 +02:00
[(TGGroupInfoUserCollectionItemView *)[self boundView] setDisabled:_disabled animated:false];
}
- (void)unbindView
{
((TGGroupInfoUserCollectionItemView *)[self boundView]).delegate = nil;
[super unbindView];
}
2016-02-25 01:03:51 +01:00
- (void)setDisplaySwitch:(bool)displaySwitch {
_displaySwitch = displaySwitch;
[(TGGroupInfoUserCollectionItemView *)[self boundView] setDisplaySwitch:_displaySwitch];
}
- (void)setEnableSwitch:(bool)enableSwitch {
_enableSwitch = enableSwitch;
[(TGGroupInfoUserCollectionItemView *)[self boundView] setEnableSwitch:_enableSwitch animated:true];
}
- (void)setSwitchIsOn:(bool)switchIsOn {
_switchIsOn = switchIsOn;
[(TGGroupInfoUserCollectionItemView *)[self boundView] setSwitchIsOn:_switchIsOn animated:true];
}
- (void)setCustomStatus:(NSString *)customStatus {
_customStatus = customStatus;
TGGroupInfoUserCollectionItemView *view = (TGGroupInfoUserCollectionItemView *)[self boundView];
if (_customStatus != nil) {
[view setStatus:_customStatus active:false];
} else {
if (_user.kind == TGUserKindBot || _user.kind == TGUserKindSmartBot)
{
NSString *botStatus = nil;
if (_user.kind == TGUserKindBot)
botStatus = TGLocalized(@"Bot.GroupStatusDoesNotReadHistory");
else
botStatus = TGLocalized(@"Bot.GroupStatusReadsHistory");
[view setStatus:botStatus active:false];
}
else
{
bool active = false;
NSString *status = [self _statusStringFromUserPresence:_user.presence active:&active];
[view setStatus:status active:active];
}
}
}
2014-07-10 16:11:09 +02:00
- (NSString *)_statusStringFromUserPresence:(TGUserPresence)presence active:(out bool *)active
{
if (presence.online)
{
if (active != NULL)
*active = true;
return TGLocalizedStatic(@"Presence.online");
}
else if (presence.lastSeen != 0)
return [TGDateUtils stringForRelativeLastSeen:presence.lastSeen];
return TGLocalized(@"Presence.offline");
}
- (void)setUser:(TGUser *)user
{
_user = user;
if ([self boundView] != nil)
{
TGGroupInfoUserCollectionItemView *view = (TGGroupInfoUserCollectionItemView *)[self boundView];
[view setFirstName:user.firstName lastName:user.lastName uidForPlaceholderCalculation:_user.uid];
2015-10-01 18:19:52 +02:00
if (_customStatus != nil) {
[view setStatus:_customStatus active:false];
} else {
if (_user.kind == TGUserKindBot || _user.kind == TGUserKindSmartBot)
{
NSString *botStatus = nil;
if (_user.kind == TGUserKindBot)
botStatus = TGLocalized(@"Bot.GroupStatusDoesNotReadHistory");
else
botStatus = TGLocalized(@"Bot.GroupStatusReadsHistory");
[view setStatus:botStatus active:false];
}
else
{
bool active = false;
NSString *status = [self _statusStringFromUserPresence:_user.presence active:&active];
[view setStatus:status active:active];
}
}
2014-07-10 16:11:09 +02:00
[view setAvatarUri:_user.photoUrlSmall];
}
}
2015-10-01 18:19:52 +02:00
- (NSString *)stringForMemberCount:(int)memberCount
{
if (memberCount == 1)
return TGLocalizedStatic(@"Conversation.StatusMembers_1");
else if (memberCount == 2)
return TGLocalizedStatic(@"Conversation.StatusMembers_2");
else if (memberCount >= 3 && memberCount <= 10)
return [[NSString alloc] initWithFormat:TGLocalizedStatic(@"Conversation.StatusMembers_3_10"), [TGStringUtils stringWithLocalizedNumber:memberCount]];
else
return [[NSString alloc] initWithFormat:TGLocalizedStatic(@"Conversation.StatusMembers_any"), [TGStringUtils stringWithLocalizedNumber:memberCount]];
}
- (void)setConversation:(TGConversation *)conversation
{
_conversation = conversation;
if ([self boundView] != nil)
{
TGGroupInfoUserCollectionItemView *view = (TGGroupInfoUserCollectionItemView *)[self boundView];
if (_conversation == nil)
{
[view setIsSecretChat:false];
}
else
{
[view setIsSecretChat:_conversation.isEncrypted];
}
if (_user == nil)
{
[view setFirstName:_conversation.chatTitle lastName:@"" uidForPlaceholderCalculation:(int32_t)_conversation.conversationId];
[view setStatus:[self stringForMemberCount:_conversation.chatParticipantCount] active:false];
[view setAvatarUri:_conversation.chatPhotoSmall];
}
}
}
2014-07-10 16:11:09 +02:00
- (void)setCanEdit:(bool)canEdit
{
[self setCanEdit:canEdit animated:false];
}
- (void)setCanEdit:(bool)canEdit animated:(bool)animated
{
if (_canEdit != canEdit)
{
_canEdit = canEdit;
[(TGGroupInfoUserCollectionItemView *)[self boundView] setEnableEditing:_canEdit animated:animated];
}
}
- (void)setDisabled:(bool)disabled
{
if (_disabled != disabled)
{
_disabled = disabled;
[(TGGroupInfoUserCollectionItemView *)[self boundView] setDisabled:_disabled animated:true];
}
}
- (void)updateTimestamp
{
2015-10-01 18:19:52 +02:00
if (_user != nil && _user.kind == TGUserKindGeneric && _customStatus == nil)
{
bool active = false;
NSString *status = [self _statusStringFromUserPresence:_user.presence active:&active];
[(TGGroupInfoUserCollectionItemView *)[self boundView] setStatus:status active:active];
}
2014-07-10 16:11:09 +02:00
}
- (void)itemSelected:(id)__unused actionTarget
{
2015-10-01 18:19:52 +02:00
if (_conversation == nil)
[_interfaceHandle requestAction:@"openUser" options:@{@"uid": @(_user.uid)}];
else
[_interfaceHandle requestAction:@"openConversation" options:@{@"conversationId": @(_conversation.conversationId)}];
2014-07-10 16:11:09 +02:00
}
- (void)groupInfoUserItemViewRequestedDeleteAction:(TGGroupInfoUserCollectionItemView *)groupInfoUserItemView
{
2015-10-01 18:19:52 +02:00
if (groupInfoUserItemView == [self boundView] && (_user.uid != 0 || _conversation.conversationId != 0))
{
if (_conversation == nil)
[_interfaceHandle requestAction:@"deleteUser" options:@{@"uid": @(_user.uid)}];
else
[_interfaceHandle requestAction:@"deleteConversation" options:@{@"conversationId": @(_conversation.conversationId)}];
}
2014-07-10 16:11:09 +02:00
}
2016-02-25 01:03:51 +01:00
- (void)switchValueChanged:(bool)switchValue {
if (_toggled) {
_toggled(switchValue);
}
}
2014-07-10 16:11:09 +02:00
@end