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

#import "CommunityController.h"
#import "CommunityView.h"
#import "ComListViewModel.h"
#import "DynamicController.h"
#import "TestFlutterController.h"
#import "DynamicDetailController.h"

@interface CommunityController () <CommunityViewDelegate, DynamicControllerDelegate, DynamicDetailControllerDelegate>
@property (nonatomic, strong) CommunityView *communityView;
@property (nonatomic, strong) TestFlutterController *flutterEngine;
@property (nonatomic, assign) int offset;
@property (nonatomic, strong) NSURLSessionDataTask *likeDataTask;
@end

@implementation CommunityController

- (void)loadView {
    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.flutterEngine = [TestFlutterController new];
    [self.navigationController pushViewController:self.flutterEngine 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; }
    
    [ComListViewModel querySleepDynamicListWithOffset:self.offset completion:^(ComListViewModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            if (loadMore) {
                if (self.offset > requestModel.totalCount) {
                    [DSProgressHUD showToast:@"无更多动态"];
                    [self.communityView updateCommunityMoments:loadMore listArr:@[]];
                    return;
                }
            }
            self.offset++;
            [self.communityView updateCommunityMoments:loadMore listArr:requestModel.listArr];
        } 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)userID {
    if (![LoginUtils getUserLoginData]) {
        [LoginUtils jumpToLoginControllerWithTarget:self];
        return;
    }
    // 弹出举报框
    [self.communityView showInformCancelView];
}

- (void)didSubmitInformContent {
    // 1、提交举报请求
    
}

- (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变化
    if (comDynModel.isLike) {
        comDynModel.likeCount--;
    } else {
        comDynModel.likeCount++;
    }
    comDynModel.isLike = !comDynModel.isLike;
    [cell fireLikeAnimate];
    [cell updateLikeUI];
    
    // 2、更新点赞或取消点赞请求
    if (self.likeDataTask) { [self.likeDataTask cancel]; }
    self.likeDataTask = [ComListViewModel userDynamicPraiseWithTalkID:comDynModel.dynamicID 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(), ^{
                // 修改数据model
                if (comDynModel.isLike) {
                    comDynModel.likeCount--;
                } else {
                    comDynModel.likeCount++;
                }
                comDynModel.isLike = !comDynModel.isLike;
                [cell updateLikeUI];
                [DSProgressHUD showToast:requestModel.errMessage];
            });
        }
    }];
}

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

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

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

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

@end