SNSViewController.m 4.66 KB
//
//  SNSViewController.m
//  LifeLog
//
//  Created by Nguyen Van Phong on 7/25/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "SNSViewController.h"
#import "SNSRecentTopicTableViewCell.h"
#import "ServerAPI.h"
#import "Utilities.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface SNSViewController ()

@end

@implementation SNSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"lifelog.tapbar.sns", nil);
    _firstTime = true;
    _curPage   = 1;
    _isEndOfResult = false;
    
    _curTweetList = [[NSMutableArray alloc] init];
    
    [self callRequestToUpdateData];

    //register nib for table view
    [self.tableRecent registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
}

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


#pragma mark UITableView Delegate
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    if((_curTweetList == nil || _curTweetList.count == 0) && !_firstTime) {
        UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
        noDataLabel.text             = @"No data available";
        noDataLabel.backgroundColor  = [UIColor clearColor];
        noDataLabel.textColor        = [UIColor whiteColor];
        noDataLabel.textAlignment    = NSTextAlignmentCenter;
        tableView.backgroundView = noDataLabel;
        tableView.backgroundView.layer.zPosition -= 1;
        return 0;
    }
    tableView.backgroundView = nil;
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _curTweetList.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
    TweetObject *object = [_curTweetList objectAtIndex:indexPath.row];
    if(object.avatarLink && ![object.avatarLink isKindOfClass:[NSNull class]]) {
        [cell.imgAvatar sd_setImageWithURL:[NSURL URLWithString:[Utilities getImageLink:object.avatarLink]]];
    }
    cell.lblDateTime.text   = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd    hh:mm"];
    cell.lblUsername.text   = object.userName;
    cell.lblDes.text        = object.content;
    cell.lblMode.text       = object.mode;
    cell.lblDistance.text   = [NSString stringWithFormat:@"%.0f m", object.distance];
    cell.lblDuration.text   = object.time;
    return cell;
}

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;
    if (indexPath.row == lastRowIndex) {
        // This is the last cell
        if(!_isLoading) {
            _curPage += 1;
            [self callRequestToUpdateData];
        }
    }
}
#pragma mark Private Function

-(void) callRequestToUpdateData {
    _isLoading = true;
    NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
    MBProgressHUD *hudView = nil;
    if(_curPage == 1) {
        hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    }
    [[ServerAPI server] requestRecentlyTweetsList:token withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
        SNSViewController __weak *weakSelf = self;
        _isLoading = false;
        dispatch_async(dispatch_get_main_queue(), ^{
            if(hudView != nil) {
                [hudView hideAnimated:true];
            }
        });
        if(error == nil) {
            if(_curPage == 1) {
                [_curTweetList removeAllObjects];
            }
            if(array.count != 0) {
                [_curTweetList addObjectsFromArray:array];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableRecent reloadData];
                });
            }
            else {
                _isEndOfResult = true;
                _curPage = MAX(1, _curPage - 1);
            }
        }
        else {
            _curPage = MAX(1, _curPage - 1);
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString *message = [error.userInfo objectForKey:@"message"];
                [Utilities showErrorMessage:message withViewController:weakSelf];
            });
        }

    }];
}

-(void) resetData {
    _isLoading = false;
    _isEndOfResult = false;
    _firstTime = true;
    _curPage = 1;
}

@end