-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathATLMConversationListViewController.m
More file actions
executable file
·302 lines (250 loc) · 13.5 KB
/
Copy pathATLMConversationListViewController.m
File metadata and controls
executable file
·302 lines (250 loc) · 13.5 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// ATLMConversationListViewController.m
// Atlas Messenger
//
// Created by Kevin Coleman on 8/29/14.
// Copyright (c) 2014 Layer, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "ATLMConversationListViewController.h"
#import "ATLMUser.h"
#import "ATLMConversationViewController.h"
#import "ATLMSettingsViewController.h"
#import "ATLMConversationDetailViewController.h"
#import "ATLMNavigationController.h"
#import "ATLMSplitViewController.h"
#import "LYRIdentity+ATLParticipant.h"
#import "MEConversationListCell.h"
@interface ATLMConversationListViewController () <ATLConversationListViewControllerDelegate, ATLConversationListViewControllerDataSource, ATLMSettingsViewControllerDelegate, UIActionSheetDelegate>
@end
@implementation ATLMConversationListViewController
NSString *const ATLMConversationListTableViewAccessibilityLabel = @"Conversation List Table View";
NSString *const ATLMSettingsButtonAccessibilityLabel = @"Settings Button";
NSString *const ATLMComposeButtonAccessibilityLabel = @"Compose Button";
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.accessibilityLabel = ATLMConversationListTableViewAccessibilityLabel;
self.tableView.isAccessibilityElement = YES;
self.delegate = self;
self.dataSource = self;
self.allowsEditing = YES;
self.cellClass = [MEConversationListCell class];
// Left navigation item
UIButton* infoButton= [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[infoButton addTarget:self action:@selector(settingsButtonTapped) forControlEvents:UIControlEventTouchUpInside];
infoButton.accessibilityLabel = ATLMSettingsButtonAccessibilityLabel;
[self.navigationItem setLeftBarButtonItem:infoItem];
// Right navigation item
UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(composeButtonTapped)];
composeButton.accessibilityLabel = ATLMComposeButtonAccessibilityLabel;
[self.navigationItem setRightBarButtonItem:composeButton];
[self registerNotificationObservers];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - ATLConversationListViewControllerDelegate
/**
Atlas - Informs the delegate of a conversation selection. Atlas Messenger pushses a subclass of the `ATLConversationViewController`.
*/
- (void)conversationListViewController:(ATLConversationListViewController *)conversationListViewController didSelectConversation:(LYRConversation *)conversation
{
[self presentControllerWithConversation:conversation];
}
/**
Atlas - Informs the delegate a conversation was deleted. Atlas Messenger does not need to react as the superclass will handle removing the conversation in response to a deletion.
*/
- (void)conversationListViewController:(ATLConversationListViewController *)conversationListViewController didDeleteConversation:(LYRConversation *)conversation deletionMode:(LYRDeletionMode)deletionMode
{
NSLog(@"Conversation Successfully Deleted");
}
/**
Atlas - Informs the delegate that a conversation deletion attempt failed. Atlas Messenger does not do anything in response.
*/
- (void)conversationListViewController:(ATLConversationListViewController *)conversationListViewController didFailDeletingConversation:(LYRConversation *)conversation deletionMode:(LYRDeletionMode)deletionMode error:(NSError *)error
{
NSLog(@"Conversation Deletion Failed with Error: %@", error);
}
/**
Atlas - Informs the delegate that a search has been performed. Atlas messenger queries for, and returns objects conforming to the `ATLParticipant` protocol whose `fullName` property contains the search text.
*/
- (void)conversationListViewController:(ATLConversationListViewController *)conversationListViewController didSearchForText:(nonnull NSString *)searchText completion:(nonnull void (^)(NSSet<id<ATLParticipant>> * _Nonnull))completion
{
LYRQuery *query = [LYRQuery queryWithQueryableClass:[LYRIdentity class]];
query.predicate = [LYRPredicate predicateWithProperty:@"displayName" predicateOperator:LYRPredicateOperatorLike value:[searchText stringByAppendingString:@"%"]];
[self.layerClient executeQuery:query completion:^(NSOrderedSet<id<ATLParticipant>> * _Nullable resultSet, NSError * _Nullable error) {
if (resultSet) {
completion(resultSet.set);
} else {
completion([NSSet set]);
}
}];
}
- (id<ATLAvatarItem>)conversationListViewController:(ATLConversationListViewController *)conversationListViewController avatarItemForConversation:(LYRConversation *)conversation
{
NSMutableSet *participants = conversation.participants.mutableCopy;
[participants removeObject:self.layerClient.authenticatedUser];
return participants.anyObject;
}
#pragma mark - ATLConversationListViewControllerDataSource
/**
Atlas - Returns a label that is used to represent the conversation. Atlas Messenger puts the name representing the `lastMessage.sentByUserID` property first in the string.
*/
- (NSString *)conversationListViewController:(ATLConversationListViewController *)conversationListViewController titleForConversation:(LYRConversation *)conversation
{
// If we have a Conversation name in metadata, return it.
NSString *conversationTitle = conversation.metadata[ATLMConversationMetadataNameKey];
if (conversationTitle.length) {
return conversationTitle;
}
NSMutableSet *participants = [conversation.participants mutableCopy];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID != %@", self.layerClient.authenticatedUser.userID];
[participants filterUsingPredicate:predicate];
if (participants.count == 0) return @"Personal Conversation";
if (participants.count == 1) return [[participants allObjects][0] displayName];
NSMutableArray *firstNames = [NSMutableArray new];
[participants enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
id<ATLParticipant> participant = obj;
if (participant.firstName) {
// Put the last message sender's name first
if ([conversation.lastMessage.sender.userID isEqualToString:participant.userID]) {
[firstNames insertObject:participant.firstName atIndex:0];
} else {
[firstNames addObject:participant.firstName];
}
}
}];
NSString *firstNamesString = [firstNames componentsJoinedByString:@", "];
return firstNamesString;
}
#pragma mark - Conversation Selection
// The following method handles presenting the correct `ATLMConversationViewController`, regardeless of the current state of the navigation stack.
- (void)presentControllerWithConversation:(LYRConversation *)conversation
{
ATLMConversationViewController *existingConversationViewController = [self existingConversationViewController];
if (existingConversationViewController && existingConversationViewController.conversation == conversation) {
if (self.navigationController.topViewController == existingConversationViewController) return;
[self.navigationController popToViewController:existingConversationViewController animated:YES];
return;
}
BOOL shouldShowAddressBar = (conversation.participants.count > 2 || !conversation.participants.count);
ATLMConversationViewController *conversationViewController = [ATLMConversationViewController conversationViewControllerWithLayerClient:self.applicationController.layerClient];
conversationViewController.applicationController = self.applicationController;
conversationViewController.displaysAddressBar = shouldShowAddressBar;
conversationViewController.conversation = conversation;
[self.applicationController.splitViewController setDetailViewController:conversationViewController];
}
#pragma mark - Actions
- (void)settingsButtonTapped
{
ATLMSettingsViewController *settingsViewController = [[ATLMSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
settingsViewController.applicationController = self.applicationController;
settingsViewController.settingsDelegate = self;
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
[self.navigationController presentViewController:controller animated:YES completion:nil];
}
- (void)composeButtonTapped
{
[self presentControllerWithConversation:nil];
}
#pragma mark - Conversation Selection From Push Notification
- (void)selectConversation:(LYRConversation *)conversation
{
if (conversation) {
[self presentControllerWithConversation:conversation];
}
}
#pragma mark - ATLMSettingsViewControllerDelegate
- (void)logoutTappedInSettingsViewController:(ATLMSettingsViewController *)settingsViewController
{
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
[SVProgressHUD show];
if (self.applicationController.layerClient.isConnected) {
[self.applicationController.layerClient deauthenticateWithCompletion:^(BOOL success, NSError *error) {
[SVProgressHUD dismiss];
}];
} else {
[SVProgressHUD showErrorWithStatus:@"Unable to logout. Layer is not connected"];
}
}
- (void)settingsViewControllerDidFinish:(ATLMSettingsViewController *)settingsViewController
{
[settingsViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Notification Handlers
- (void)conversationDeleted:(NSNotification *)notification
{
if (self.ATLM_navigationController.isAnimating) {
[self.ATLM_navigationController notifyWhenCompletionEndsUsingBlock:^{
[self conversationDeleted:notification];
}];
return;
}
ATLMConversationViewController *conversationViewController = [self existingConversationViewController];
if (!conversationViewController) return;
LYRConversation *deletedConversation = notification.object;
if (![conversationViewController.conversation isEqual:deletedConversation]) return;
conversationViewController = nil;
[self.navigationController popToViewController:self animated:YES];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Conversation Deleted"
message:@"The conversation has been deleted."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
- (void)conversationParticipantsDidChange:(NSNotification *)notification
{
if (self.ATLM_navigationController.isAnimating) {
[self.ATLM_navigationController notifyWhenCompletionEndsUsingBlock:^{
[self conversationParticipantsDidChange:notification];
}];
return;
}
NSString *authenticatedUserID = self.applicationController.layerClient.authenticatedUser.userID;
if (!authenticatedUserID) return;
LYRConversation *conversation = notification.object;
if ([[conversation.participants valueForKeyPath:@"userID"] containsObject:authenticatedUserID]) return;
ATLMConversationViewController *conversationViewController = [self existingConversationViewController];
if (!conversationViewController) return;
if (![conversationViewController.conversation isEqual:conversation]) return;
[self.navigationController popToViewController:self animated:YES];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Removed From Conversation"
message:@"You have been removed from the conversation."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
#pragma mark - Helpers
- (ATLMConversationViewController *)existingConversationViewController
{
if (!self.navigationController) return nil;
NSUInteger listViewControllerIndex = [self.navigationController.viewControllers indexOfObject:self];
if (listViewControllerIndex == NSNotFound) return nil;
NSUInteger nextViewControllerIndex = listViewControllerIndex + 1;
if (nextViewControllerIndex >= self.navigationController.viewControllers.count) return nil;
id nextViewController = [self.navigationController.viewControllers objectAtIndex:nextViewControllerIndex];
if (![nextViewController isKindOfClass:[ATLMConversationViewController class]]) return nil;
return nextViewController;
}
- (void)registerNotificationObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(conversationDeleted:) name:ATLMConversationDeletedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(conversationParticipantsDidChange:) name:ATLMConversationParticipantsDidChangeNotification object:nil];
}
@end