session-ios/Signal/src/view controllers/UITests/SignalsViewController.m

264 lines
8.7 KiB
Mathematica
Raw Normal View History

2014-10-29 21:58:58 +01:00
//
// SignalsViewController.m
// Signal
//
// Created by Dylan Bourgeois on 27/10/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
//
#import "DemoDataFactory.h"
#import "InboxTableViewCell.h"
2014-10-29 21:58:58 +01:00
#import "MessagesViewController.h"
#import "SignalsViewController.h"
#import "TSStorageManager.h"
#import "TSDatabaseView.h"
#import "TSSocketManager.h"
2014-10-29 21:58:58 +01:00
#import <YapDatabase/YapDatabaseViewChange.h>
#import "YapDatabaseViewTransaction.h"
#import "YapDatabaseViewMappings.h"
#import "YapDatabaseViewConnection.h"
#import "YapDatabaseFullTextSearch.h"
#import "YapDatabase.h"
#define CELL_HEIGHT 71.0f
2014-10-29 21:58:58 +01:00
#define HEADER_HEIGHT 44.0f
static NSString *const inboxTableViewCell = @"inBoxTableViewCell";
static NSString *const kSegueIndentifier = @"showSegue";
2014-10-29 21:58:58 +01:00
@interface SignalsViewController () {
NSArray * _dataArray;
NSUInteger numberOfCells;
}
@property (strong, nonatomic) DemoDataModel *demoData;
@property (nonatomic, strong) YapDatabaseConnection *uiDatabaseConnection;
@property (nonatomic, strong) YapDatabaseViewMappings *threadMappings;
2014-10-29 21:58:58 +01:00
@end
@implementation SignalsViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.uiDatabaseConnection beginLongLivedReadTransaction];
self.threadMappings = [[YapDatabaseViewMappings alloc] initWithGroups:@[TSThreadGroup]
view:TSThreadDatabaseViewExtensionName];
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){
[self.threadMappings updateWithTransaction:transaction];
}];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yapDatabaseModified:)
name:TSUIDatabaseConnectionDidUpdateNotification
object:nil];
[TSSocketManager becomeActive];
2014-10-29 21:58:58 +01:00
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)tableViewSetUp
{
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
2014-10-29 21:58:58 +01:00
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return (NSInteger)[self.threadMappings numberOfSections];
2014-10-29 21:58:58 +01:00
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (NSInteger)[self.threadMappings numberOfItemsInSection:(NSUInteger)section];
2014-10-29 21:58:58 +01:00
}
- (InboxTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
2014-10-29 21:58:58 +01:00
InboxTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:inboxTableViewCell];
TSThread *thread = [self threadForIndexPath:indexPath];
2014-10-29 21:58:58 +01:00
if (!cell) {
cell = [[InboxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:inboxTableViewCell];
2014-10-29 21:58:58 +01:00
cell.delegate = self;
}
[cell configureWithThread:thread];
2014-10-29 21:58:58 +01:00
[cell configureForState:_segmentedControl.selectedSegmentIndex == 0 ? kInboxState : kArchiveState];
2014-10-29 21:58:58 +01:00
return cell;
}
2014-10-29 21:58:58 +01:00
- (TSThread*)threadForIndexPath:(NSIndexPath*)indexPath {
__block TSThread *thread = nil;
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
thread = [[transaction extension:TSThreadDatabaseViewExtensionName] objectAtIndexPath:indexPath withMappings:self.threadMappings];
}];
return thread;
2014-10-29 21:58:58 +01:00
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return CELL_HEIGHT;
}
2014-10-29 21:58:58 +01:00
#pragma mark - HomeFeedTableViewCellDelegate
- (void)tableViewCellTappedDelete:(InboxTableViewCell*)cell {
2014-10-29 21:58:58 +01:00
NSLog(@"Delete");
}
- (void)tableViewCellTappedArchive:(InboxTableViewCell*)cell {
2014-10-29 21:58:58 +01:00
NSLog(@"Archive");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:kSegueIndentifier sender:self];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:kSegueIndentifier])
{
MessagesViewController * vc = [segue destinationViewController];
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
2014-10-29 21:58:58 +01:00
if (selectedIndexPath) {
vc.senderTitleString = ((DemoDataModel*)_dataArray[(NSUInteger)selectedIndexPath.row])._sender;
2014-10-29 21:58:58 +01:00
} else if (_contactFromCompose) {
vc.senderTitleString = _contactFromCompose.fullName;
2014-10-29 21:58:58 +01:00
} else if (_groupFromCompose) {
vc.senderTitleString = _groupFromCompose.groupName;
2014-10-29 21:58:58 +01:00
}
2014-10-29 21:58:58 +01:00
}
}
#pragma mark - IBAction
-(IBAction)segmentDidChange:(id)sender
{
switch (_segmentedControl.selectedSegmentIndex) {
case 0:
numberOfCells=5;
[self.tableView reloadData];
2014-10-29 21:58:58 +01:00
break;
case 1:
numberOfCells=3;
[self.tableView reloadData];
2014-10-29 21:58:58 +01:00
break;
}
}
#pragma mark Database delegates
- (YapDatabaseConnection *)uiDatabaseConnection {
NSAssert([NSThread isMainThread], @"Must access uiDatabaseConnection on main thread!");
if (!_uiDatabaseConnection) {
YapDatabase *database = TSStorageManager.sharedManager.database;
_uiDatabaseConnection = [database newConnection];
[_uiDatabaseConnection beginLongLivedReadTransaction];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yapDatabaseModified:)
name:YapDatabaseModifiedNotification
object:database];
}
return _uiDatabaseConnection;
}
- (void)yapDatabaseModified:(NSNotification *)notification {
NSArray *notifications = notification.userInfo[@"notifications"];
NSArray *sectionChanges = nil;
NSArray *rowChanges = nil;
[[self.uiDatabaseConnection ext:TSThreadDatabaseViewExtensionName] getSectionChanges:&sectionChanges
rowChanges:&rowChanges
forNotifications:notifications
withMappings:self.threadMappings];
if ([sectionChanges count] == 0 && [rowChanges count] == 0){
return;
}
[self.tableView beginUpdates];
for (YapDatabaseViewSectionChange *sectionChange in sectionChanges)
{
switch (sectionChange.type)
{
case YapDatabaseViewChangeDelete :
{
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionChange.index]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case YapDatabaseViewChangeInsert :
{
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionChange.index]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case YapDatabaseViewChangeUpdate:
case YapDatabaseViewChangeMove:
break;
}
}
for (YapDatabaseViewRowChange *rowChange in rowChanges)
{
switch (rowChange.type)
{
case YapDatabaseViewChangeDelete :
{
[self.tableView deleteRowsAtIndexPaths:@[ rowChange.indexPath ]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case YapDatabaseViewChangeInsert :
{
[self.tableView insertRowsAtIndexPaths:@[ rowChange.newIndexPath ]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case YapDatabaseViewChangeMove :
{
[self.tableView deleteRowsAtIndexPaths:@[ rowChange.indexPath ]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:@[ rowChange.newIndexPath ]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
case YapDatabaseViewChangeUpdate :
{
[self.tableView reloadRowsAtIndexPaths:@[ rowChange.indexPath ]
withRowAnimation:UITableViewRowAnimationNone];
break;
}
}
}
[self.tableView endUpdates];
}
2014-10-29 21:58:58 +01:00
@end