MyGroupViewController.m 4.64 KB
//
//  MyGroupViewController.m
//  LifeLog
//
//  Created by nvtu on 8/20/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "MyGroupViewController.h"

#import "Utilities.h"
#import "ServerAPI.h"
#import "SNSRecentTopicTableViewCell.h"

@interface MyGroupViewController ()

@end

@implementation MyGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isMemberList = false;
    _curListGrp = [[NSMutableArray alloc] init];
    [self.tableBase registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
    
    [self requestGroupList];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark IBAction

- (IBAction)clickBack:(id)sender {
    [self.navigationController popViewControllerAnimated:true];
}

- (IBAction)clickShowGrp:(id)sender {
    self.tableGrp.hidden = !self.tableGrp.isHidden;
}

- (IBAction)clickSwitch:(id)sender {
    isMemberList = !isMemberList;
    if(isMemberList) {
        [sender setTitle:NSLocalizedString(@"lifelog.grDetail.bt.viewTweet", nil) forState:UIControlStateNormal];
    }
    else {
        [sender setTitle:NSLocalizedString(@"lifelog.grDetail.bt.viewMem", nil) forState:UIControlStateNormal];
    }
    [sender setUserInteractionEnabled:false];
    [self resetData];
}

#pragma mark UITableView Delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    if(tableView == self.tableGrp)
        return 1;
    else
        return [super numberOfSectionsInTableView:tableView];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableView == self.tableGrp)
        return _curListGrp.count;
    else
        return [super tableView:tableView numberOfRowsInSection:section];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == self.tableGrp) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
        if(cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GroupCell"];
        }
        GroupObject *object = [_curListGrp objectAtIndex:indexPath.row];
        cell.textLabel.text = object.name;
        return cell;
    }
    else {
        SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
        if(isMemberList) {
            MemberObject *object = [_curDataList objectAtIndex:indexPath.row];
            [cell setMemberData:object];
        }
        else {
            TweetObject *object = [_curDataList objectAtIndex:indexPath.row];
            [cell setTweetsData:object];
        }
        return cell;
    }
}

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == self.tableGrp) {
        return;
    }
    [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == self.tableGrp) {
        [tableView setHidden:true];
        [self resetGroupData:indexPath.row];
    }
}

#pragma mark Private Function

- (void) resetGroupData : (int) index {
    _curGroup = [_curListGrp objectAtIndex:index];
    self.lblGrpName.text = _curGroup.name;
    [_curDataList removeAllObjects];
    [self.tableBase reloadData];
    [self requestGroupDetail];
}

- (void) requestGroupList {
    NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
    MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    [[ServerAPI server] requestGroupList:token CompletionHandler:^(NSArray *array, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [hudView hideAnimated:true];
        });
        if(error == nil) {
            [_curListGrp removeAllObjects];
            [_curListGrp addObjectsFromArray:array];
            MyGroupViewController __weak *weakSelf = self;
            dispatch_async(dispatch_get_main_queue(), ^{
                [hudView hideAnimated:true];
                [self.tableGrp reloadData];
                if(_curListGrp.count > 0) {
                    [weakSelf resetGroupData:0];
                }
                else {
                    weakSelf.lblGrpName.text = @"No Group";
                }
                [weakSelf.btShowGrp setEnabled:(_curListGrp.count > 0)];
            });
        }
    }];
}

@end