Files
yinmeng-ios-store/yinmeng-ios/DingDangApp/CodeClass/Home/HomeViewController.m

300 lines
13 KiB
Objective-C

//
// HomeViewController.m
// DingDangApp
//
// Created by apple on 2023/5/27.
//
#import "HomeViewController.h"
#import "DDRoomPartyModel.h"
#import "DDRoomPartyViewCell.h"
#import "DDRoomPartyHeaderView.h"
#import "DDLunboModel.h"
#import "RoomUserInfoModel.h"
#import "DDSearchPageViewController.h"
#import "DDWebViewController.h"
#import "TZLocationManager.h"
#import "DDUpdateVersionView.h"
@interface HomeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *bgImgV;
@property (nonatomic, strong) UICollectionView *collectionView;
/*数据列表*/
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *bannerArr;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic,assign) CLAuthorizationStatus authStatus;
@end
@implementation HomeViewController
- (void)getLocation {
[[TZLocationManager manager] startLocation];
self.authStatus = [CLLocationManager authorizationStatus];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if ([CLLocationManager locationServicesEnabled] == NO) {
self.authStatus = -1;
return;
}
});
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
static int count = 0;
count ++;
if (count > 60) {
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self getLocation];
});
return;
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied ||
[CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
[AppDelegate getAppdelegate].isLocation = NO;
[ToolsObject ddUploadMyLocation];
return;
}
[[TZLocationManager manager] startLocationWithSuccessBlock:^(NSArray<CLLocation *> *locations) {
} failureBlock:^(NSError *error) {
} geocoderBlock:^(NSArray *geocoderArray) {
if (geocoderArray.count > 0) {
CLPlacemark * placemark = geocoderArray[0];
[AppDelegate getAppdelegate].longitude = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
[AppDelegate getAppdelegate].latitude = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
[AppDelegate getAppdelegate].locationCity = [NSString stringWithFormat:@"%@",placemark.locality];
[AppDelegate getAppdelegate].provinceStr = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
[AppDelegate getAppdelegate].countyStr = @"";
[AppDelegate getAppdelegate].addressStr = [NSString stringWithFormat:@"%@%@%@",[ToolsObject IsNullWithObject:[AppDelegate getAppdelegate].provinceStr]?@"":[AppDelegate getAppdelegate].provinceStr,[ToolsObject IsNullWithObject:[AppDelegate getAppdelegate].locationCity]?@"":[AppDelegate getAppdelegate].locationCity,placemark.name];
if (![AppDelegate getAppdelegate].isLocation) {
[AppDelegate getAppdelegate].isLocation = YES;
[ToolsObject ddUploadMyLocation];
}
}
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self requestBaseConfig];
[self getLocation];
self.dataArr = [[NSMutableArray alloc] init];
[self ddBaseUI];
self.pageNum = 1;
[self refreshBannerRequest];
[self refreshListData];
}
- (void)requestBaseConfig{
[NetworkRequest requestPOST:@"/common/base" parameters:@{} block:^(BaseResponse * _Nonnull response) {
if ([ToolsObject IsNullWithObject:response.data]) {
return;
}
RoomConfigModel * model = [RoomConfigModel mj_objectWithKeyValues:response.data];
if (model.app.version.is_need_update) {
[AppDelegate getAppdelegate].alertViewIsShow = YES;
if (model.app.version.enforce){
[DDUpdateVersionView showUpdateVersionView:YES contentStr:model.app.version.content versionStr:model.app.version.version];
}else{
[DDUpdateVersionView showUpdateVersionView:NO contentStr:model.app.version.content versionStr:model.app.version.version];
}
return;
}
}];
}
- (void)ddBaseUI {
[self.view addSubview:self.bgImgV];
[self.bgImgV mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.left.right.top.equalTo(self.view);
}];
[self.view addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view).inset(0);
make.top.mas_equalTo(0);
make.bottom.equalTo(self.view).inset(KTABBAR_H);
}];
}
- (void)refreshListData {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@(20) forKey:@"current_page"];
[dict setObject:@(self.pageNum) forKey:@"page_number"];
[dict setObject:@"hot" forKey:@"scene"];
WeakSelf(weakSelf)
/// /room/list
NSString *url = [DDEncryptManager dd_aesDecryptWithText:@"U7Vw0Xckn7CQFkKrvg3Lzw=="];
[NetworkRequest requestPOST:url parameters:dict block:^(BaseResponse * _Nonnull response) {
if (response.code == 200) {
if ([ToolsObject IsNullWithObject:response.data[@"record"]]) {
return;
}
NSMutableArray * dataArr = [NSArray yy_modelArrayWithClass:DDRoomPartyModel.class json:response.data[@"record"]].mutableCopy;
if (weakSelf.pageNum == 1) {
weakSelf.dataArr = dataArr ;
weakSelf.pageNum ++ ;
}else{
[weakSelf.dataArr addObjectsFromArray:dataArr] ;
weakSelf.pageNum ++ ;
}
dispatch_async(dispatch_get_main_queue(), ^{
[UIView performWithoutAnimation:^{
[weakSelf.collectionView.mj_header endRefreshing];
[weakSelf.collectionView.mj_footer endRefreshing];
[weakSelf.collectionView reloadData];
}];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[UIView performWithoutAnimation:^{
[weakSelf.collectionView.mj_header endRefreshing];
[weakSelf.collectionView.mj_footer endRefreshing];
}];
});
}
}];
}
-(void)refreshBannerRequest
{
NSMutableDictionary *requestDict = [NSMutableDictionary dictionary];
[requestDict setObject:@"1" forKey:@"position"];
WeakSelf(weakSelf)
/// /home/banner/list
NSString *url = [DDEncryptManager dd_aesDecryptWithText:@"taXStvDpHVvneGGdAV5tOOBccGTNu33SGtYAuLUhAKw="];
[NetworkRequest requestPOST:url parameters:requestDict block:^(BaseResponse * _Nonnull response) {
if (response.code == 200) {
if ([ToolsObject IsNullWithObject:response.data]){
return;
}
NSMutableArray * dataArr = [NSArray yy_modelArrayWithClass:DDLunboModel.class json:response.data].mutableCopy;
weakSelf.bannerArr = dataArr;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.collectionView reloadData];
});
}
}];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
DDRoomPartyViewCell *cell = (DDRoomPartyViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"DDRoomPartyViewCell" forIndexPath:indexPath];
if (indexPath.row < [self.dataArr count]) {
cell.model = self.dataArr[indexPath.row];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if ([ToolsObject IsNullWithObject:self.dataArr]){
return;
}
DDRoomPartyModel *model = self.dataArr[indexPath.row];
[[ToolsObject shareTools] jumpToRoomVC:model.room_id followUser:[RoomUserInfoModel new]];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake((kWidth - 32 - 10)/2, 186);
}
#pragma mark - header宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(kWidth, 500);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
DDRoomPartyHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DDRoomPartyHeaderView" forIndexPath:indexPath];
headerView.bannerArr = self.bannerArr;
WeakSelf(weakSelf)
headerView.searchBlock = ^{
DDSearchPageViewController * searchVC = [[DDSearchPageViewController alloc] init];
[weakSelf.navigationController pushViewController:searchVC animated:YES];
};
headerView.ranklistBlock = ^{
DDWebViewController * webVC = [DDWebViewController webControlerWithUrlStr:[NSString stringWithFormat:@"%@/#/listpage",KWebURL] isShowNavBar:NO navTitle:@""];
[weakSelf.navigationController pushViewController:webVC animated:NO];
};
headerView.roomlistBlock = ^{
NSMutableDictionary *requestDict = [NSMutableDictionary dictionary];
[requestDict setObject:@"2" forKey:@"type"];
/// /room/random/room
NSString *url = [DDEncryptManager dd_aesDecryptWithText:@"ZFpqoY77MXoepco/FBXmG6jMZNPM0O3VvHGrBX07elw="];
[NetworkRequest requestPOST:url parameters:requestDict block:^(BaseResponse * _Nonnull response) {
if (response.code == 200) {
if ([ToolsObject IsNullWithObject:response.data]){
return;
}
NSMutableArray *responseArr = response.data;
DDRoomPartyModel *model = [DDRoomPartyModel yy_modelWithJSON:responseArr.firstObject];
[[ToolsObject shareTools] jumpToRoomVC:model.room_id followUser:[RoomUserInfoModel new]];
}
}];
};
reusableview = headerView;
return reusableview;
}
reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableHeaderView" forIndexPath:indexPath];
return reusableview;
}
- (UIImageView *)bgImgV {
if (!_bgImgV) {
_bgImgV = [UIImageView new];
_bgImgV.image = [UIImage imageNamed:@"homepayty_1"];
_bgImgV.contentMode = UIViewContentModeScaleAspectFill;
} return _bgImgV;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.minimumLineSpacing = 12;
layout.minimumInteritemSpacing = 10;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionInset = UIEdgeInsetsMake(0, 16, 20, 16);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = UIColor.clearColor;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[DDRoomPartyViewCell class] forCellWithReuseIdentifier:@"DDRoomPartyViewCell"];
[_collectionView registerClass:[DDRoomPartyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DDRoomPartyHeaderView"];
if (@available(iOS 11.0, *)) {
_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
WeakSelf(weakSelf);
_collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
weakSelf.pageNum = 1;
[weakSelf refreshListData];
[weakSelf refreshBannerRequest];
}];
_collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
[weakSelf refreshListData];
}];
} return _collectionView;
}
@end