CommunityController.m 6.3 KB
//
//  CommunityController.m
//  DreamSleep
//
//  Created by peter on 2022/9/2.
//

#import "CommunityController.h"
#import "CommunityView.h"
#import "DynamicController.h"
#import "MessageController.h"
#import "ReportController.h"
#import "DynamicDetailController.h"

@interface CommunityController () <CommunityViewDelegate, DynamicControllerDelegate, DynamicDetailControllerDelegate>
@property (nonatomic, strong) CommunityView *communityView;
@property (nonatomic, assign) int offset;
@property (nonatomic, strong) NSURLSessionDataTask *likeDataTask;
/// 存储动态id
@property (nonatomic, assign) int talkID;
@property (nonatomic, strong) ComListViewModel *comListViewModel;
@end

@implementation CommunityController

- (void)loadView {
    self.comListViewModel = [ComListViewModel new];
    self.view = self.communityView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark - private
- (void)setupUI {
    self.offset = 1;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UILabel *leftLab = [UILabel dkLabWithText:@"小梦社区" font:BoldFont(24.0)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftLab];
    
    UIButton *messageBtn = [UIButton new];
    [messageBtn addTarget:self action:@selector(messageAction) forControlEvents:UIControlEventTouchUpInside];
    [messageBtn dk_setImage:DKImagePickerWithNames(@"ic_message_shequ", @"dk_ic_message_shequ", @"ic_message_shequ") forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:messageBtn];
}

- (void)messageAction {
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    [self.navigationController pushViewController:[MessageController new] animated:YES];
}

#pragma mark - CommunityViewDelegate
- (void)tapCommunityHeaderModule:(NSInteger)index {
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    NSString *clsName = index == 1 ? @"ArticleController" : @"EvaluateController";
    [self.navigationController pushViewController:[NSClassFromString(clsName) new] animated:YES];
}

- (void)getDynamicListRequest:(BOOL)loadMore {
    if (loadMore == NO) { self.offset = 1; }
    
    [self.comListViewModel querySleepDynamicListWithLoadMore:loadMore offset:self.offset completion:^(ComListViewModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            if (requestModel.latestDataList.count == 0) {
                if (loadMore) {
                    [self.communityView endRefreshingWithNoMoreData];
                } else {
                    [self.communityView endRefreshing:NO];
                }
            } else {
                self.offset++;
                [self.communityView updateCommunityMoments:loadMore];
            }
        } else {
            [DSProgressHUD showToast:requestModel.errMessage];
            [self.communityView endRefreshing:loadMore];
        }
    }];
}

- (void)publishLogicDeal {
    // 1、判断用户有没有登录
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    // 2、登录了,判断是否绑定了手机号
    if (![LoginUtils getUserMobile]) {
        UIViewController *bindVC = [[UIStoryboard storyboardWithName:@"BindMobileController" bundle:nil] instantiateViewControllerWithIdentifier:@"BindMobileController"];
        [self.navigationController pushViewController:bindVC animated:YES];
        return;
    }
    // 3、进入发布动态页面
    DynamicController *dyController = [DynamicController new];
    dyController.delegate = self;
    [self.navigationController pushViewController:dyController animated:YES];
}

- (void)didTapInformItem:(int)talkID {
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    // 弹出举报框
    self.talkID = talkID;
    [self.communityView showInformCancelView];
}

- (void)jumpToReportPage {
    ReportController *reportVC = [ReportController new];
    reportVC.talkID = self.talkID;
    [self.navigationController pushViewController:reportVC animated:YES];
}

- (void)didSelectItemWithModel:(ComDynModel *)comDynModel {
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    DynamicDetailController *detailVC = [DynamicDetailController new];
    detailVC.comDynModel = comDynModel;
    detailVC.delegate = self;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didTapLikeItem:(ComDynModel *)comDynModel cell:(ComDynamicCell *)cell {
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    // 1、用户点赞或取消点赞直接响应UI变化
    [ComListViewModel updateLikeData:comDynModel];
    [cell fireLikeAnimate];
    [cell updateLikeUI];
    
    // 2、更新点赞或取消点赞请求
    if (self.likeDataTask) { [self.likeDataTask cancel]; }
    self.likeDataTask = [ComListViewModel userDynamicPraiseWithComDynModel:comDynModel completion:^(ComListViewModel * _Nonnull requestModel) {
        // 3、如果请求失败,延迟执行回退
        if (requestModel.resCode != DSResCodeSuccess) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [cell updateLikeUI];
                [DSProgressHUD showToast:requestModel.errMessage];
            });
        }
    }];
}

#pragma mark - DynamicControllerDelegate
- (void)publishSuccessWithData:(id)model {
    [self.comListViewModel insertUserDyModel:model];
    [self.communityView updateListView];
}

#pragma mark - DynamicDetailControllerDelegate
- (void)updateLikeOrRemark {
    [self.communityView updateListView];
}

#pragma mark - lazy
- (CommunityView *)communityView {
    if (!_communityView) {
        _communityView = [[CommunityView alloc] initWithDelegate:self comListViewModel:self.comListViewModel];
    }
    return _communityView;
}

#pragma mark - 导航栏日间、黑夜模式
- (NaviStyle)navigationBarStyle {
    return [self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ? NaviStyleLight : NaviStyleDark;
}

@end