-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OTRXMPPBuddyManager.m
77 lines (62 loc) · 2.67 KB
/
OTRXMPPBuddyManager.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
//
// OTRXMPPBuddyManager.m
// ChatSecure
//
// Created by David Chiles on 1/6/16.
// Copyright © 2016 Chris Ballinger. All rights reserved.
//
#import "OTRXMPPBuddyManager.h"
#import "ChatSecureCoreCompat-Swift.h"
@import XMPPFramework;
#import "OTRXMPPBuddy.h"
@import YapDatabase;
@interface OTRXMPPBuddyManager () <OTRYapViewHandlerDelegateProtocol>
@property (nonatomic, strong) OTRYapViewHandler *viewHandler;
@end
@implementation OTRXMPPBuddyManager
- (void)setDatabaseConnection:(YapDatabaseConnection *)databaseConnection
{
_databaseConnection = databaseConnection;
}
- (BOOL)activate:(XMPPStream *)aXmppStream
{
NSString *accountKey = aXmppStream.tag;
self.viewHandler = [[OTRYapViewHandler alloc] initWithDatabaseConnection:self.databaseConnection databaseChangeNotificationName:[DatabaseNotificationName LongLivedTransactionChanges]];
self.viewHandler.delegate = self;
NSArray *groups = @[accountKey];
NSString *viewName = [YapDatabaseConstants extensionName:DatabaseExtensionNameBuddyDeleteActionViewName];
[self.viewHandler setup:viewName groups:groups];
return YES;
}
- (void)deactivate {
self.viewHandler = nil;
}
- (void)handleDatabaseViewChanges:(OTRYapViewHandler *)viewHandler {
__block NSMutableArray *buddiesToDelete = [[NSMutableArray alloc] init];
[self.databaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
YapDatabaseViewTransaction *databaseViewTransaction = [transaction ext:viewHandler.mappings.view];
NSUInteger sectionCount = [viewHandler.mappings numberOfSections];
for(NSUInteger section = 0; section < sectionCount; section++) {
NSUInteger rows = [viewHandler.mappings numberOfItemsInSection:section];
for (NSUInteger row = 0; row < rows; row++) {
OTRXMPPBuddy *buddy = [databaseViewTransaction objectAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] withMappings:viewHandler.mappings];
if (buddy) {
[buddiesToDelete addObject:buddy];
}
}
}
} completionQueue:self.moduleQueue completionBlock:^{
if([buddiesToDelete count] > 0) {
[self.protocol removeBuddies:buddiesToDelete];
}
}];
}
#pragma MARK OTRYapViewHandlerDelegateProtocol Methods
- (void)didSetupMappings:(OTRYapViewHandler *)handler
{
[self handleDatabaseViewChanges:handler];
}
- (void)didReceiveChanges:(OTRYapViewHandler *)handler sectionChanges:(NSArray<YapDatabaseViewSectionChange *> *)sectionChanges rowChanges:(NSArray<YapDatabaseViewRowChange *> *)rowChanges {
[self handleDatabaseViewChanges:handler];
}
@end