Skip to content
Snippets Groups Projects
BLRootViewController.m 5.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • EthanRDoesMC's avatar
    EthanRDoesMC committed
    #import "BLRootViewController.h"
    #import "ChatViewController.h"
    @interface BLRootViewController ()
    
    @property (nonatomic, strong) NSMutableArray * chats;
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    @end
    
    @implementation BLRootViewController
    
    
    
    
    
    
    -(void)reloadChats {
        
        self.navigationItem.prompt = @"Reloading chats";
        if ([BrooklynBridge riseAndShineIMDaemon]) {
            _chats = [[NSMutableArray alloc] initWithArray:[BrooklynBridge conversationArray]];
            //_numberOfChats = [[IMChatRegistry sharedInstance] numberOfExistingChats];
            self.navigationItem.prompt = nil;
            
        }
        else {
            self.navigationItem.prompt = @"Failed to load chats in time!";
        }
        
    }
    
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    - (void)loadView {
    
        [[BrooklynBridge sharedBridge] playLoadingChime];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    //    [self setDefinesPresentationContext:YES];
    //    [self setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    	[super loadView];
    	self.title = @"Brooklyn";
    	self.navigationItem.leftBarButtonItem = self.editButtonItem;
    	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped:)];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        [[UIApplication sharedApplication] _setBackgroundStyle:1];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        [self.tableView reloadData];
    
        //self.navigationItem.prompt = nil;
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    //    [self.tableView setBackgroundColor:[UIColor clearColor]];
    //    []
    //    [self.tableView setSeparatorEffect:<#(API_AVAILABLE(ios(8.0)) UIVisualEffect *)#>]
    }
    -(void)viewDidLoad {
        [super viewDidLoad];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        self.tableView.backgroundColor = [UIColor clearColor];
        UIBlurEffect * be = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView * vev = [[UIVisualEffectView alloc] initWithEffect:be];
        self.tableView.backgroundView = vev;
        self.tableView.separatorEffect = [UIVibrancyEffect effectForBlurEffect:be];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
    
        [self reloadChats];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        [self.tableView reloadData];
    
        [[BrooklynBridge sharedBridge] stopLoadingChime];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        
    }
    -(void)viewDidAppear:(BOOL)animated {
    
        [self reloadChats];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        [super viewDidAppear:animated];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    - (void)addButtonTapped:(id)sender {
        BLNewChatViewController * newc = [[BLNewChatViewController alloc] initWithNibName: @"BLNewChatViewController" bundle:nil];
        [self.navigationController pushViewController:newc animated:YES];
    }
    
    #pragma mark - Table View Data Source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        return _chats.count;
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	static NSString *CellIdentifier = @"ChatCell";
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    	if (!cell) {
    		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    	}
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    	return cell;
    }
    
    
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
            CKConversation * chat = _chats[indexPath.row];
        if (chat.isGroupConversation) {
        cell.accessoryView = [[UIImageView alloc] initWithImage:chat.thumbnail];
            cell.accessoryView.contentMode = UIViewContentModeScaleAspectFit;
        }
        else {
            cell.accessoryView = [[UIImageView alloc] initWithImage:chat.recipient.transcriptContactImage];
        }
        if (chat.hasDisplayName) {
            cell.textLabel.text = chat.displayName;
        } else {
            cell.textLabel.text = chat.name;
        }
            cell.detailTextLabel.text = chat.previewText;
            if (chat.outgoingBubbleColor) {
                cell.detailTextLabel.textColor = [UIColor colorWithRed: 0.10 green: 0.51 blue: 0.99 alpha: 1.00];
            } else {
                cell.detailTextLabel.textColor = [UIColor colorWithRed: 0.26 green: 0.80 blue: 0.28 alpha: 1.00];
            }
    }
    
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        [[BrooklynBridge sharedBridge] playLoadingChime];
        self.navigationItem.prompt = @"Deleting chat";
        //[tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
        CKConversation *chat3 = _chats[indexPath.row];
        [chat3 deleteAllMessagesAndRemoveGroup];
        [_chats removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
        self.navigationItem.prompt = nil;
        [[BrooklynBridge sharedBridge] stopLoadingChime];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    }
    
    
    
    #pragma mark - Table View Delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        [[BrooklynBridge sharedBridge] playLoadingChime];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
    	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        CKConversation * chat2 = _chats[indexPath.row];
        [chat2 loadAllMessages];
    
    EthanRDoesMC's avatar
    EthanRDoesMC committed
        ChatViewController * cvc = [[ChatViewController alloc] initWithConversation:chat2];
        [self.navigationController pushViewController:cvc animated:YES];
    
        [[BrooklynBridge sharedBridge] stopLoadingChime];