cdts/xdts-ios 3/TreeHole/Code/Features/Bottle/ViewController/BottleDetialController.m
2023-07-27 18:25:18 +08:00

506 lines
21 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// BottleViewController.m
// TreeHole
//
// Created by mambaxie on 2022/4/27.
//
#import "BottleDetialController.h"
#import "ProfileViewController.h"
#import "BottleCollectionCell.h"
#import "BottleTypePickerViewController.h"
#import "BottleService.h"
#import "BottlePublishViewController.h"
#import "BottleReadView.h"
#import "MessageNotificationListViewController.h"
#import "WZLBadgeImport.h"
#import <UserNotifications/UNUserNotificationCenter.h>
#import <UserNotifications/UNNotificationSettings.h>
#import <GTSDK/GeTuiSdk.h> // GetuiSdk头文件
#define OpenAppDayFlag @"notificationAlertDateFlag"
#define NotificationAlertFlag @"notificationAlertFlag"
@interface BottleDetialController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) HomeBottleInfo *info;
@property (nonatomic, strong) UIButton *hurlBottleButton;
@property (nonatomic, strong) UIButton *getBottleButton;
@property (nonatomic, assign) NSTimeInterval disappearTime;
@property (nonatomic, strong) UIButton *navBottleBtn;
@end
@implementation BottleDetialController
- (BOOL)isSupportContentScrollView {
return YES;
}
- (BOOL)mt_nagationBarTransparent{
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self startLoading];
self.view.backgroundColor = HEX_COLOR(0x0B0B0B);
// [BottleService getHomeBottleInfoWithCompletion:^(HomeBottleInfo * _Nullable info) {
NSLog(@"self.info 1111111:%@",self.info);
// self.info = info;
// [self endLoading:nil];
[self setupUI];
// }];
self.title = @"酒馆派对";
if (self.qmui_previousViewController) {
///在栈底就不需要设置返回按钮
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(0, 0, FIX_SIZE(24), FIX_SIZE(24));
[leftBtn setImage:ImageNamed(@"TH_bottle_alert_back_icon") forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}
UIButton *navBottleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
navBottleBtn.frame = CGRectMake(0, 0, FIX_SIZE(24), FIX_SIZE(24));
[navBottleBtn setImage:ImageNamed(@"TH_bottle_refresh_icon") forState:UIControlStateNormal];
[navBottleBtn addTarget:self action:@selector(refreshHomeBottleData) forControlEvents:UIControlEventTouchUpInside];
self.navBottleBtn = navBottleBtn;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navBottleBtn];
#pragma mark -- 为啥这里也要请求viewwillapper已经有了
// [self refreshHomeBottleData];
// [self creatTopUI];
[self initNotificationAlert];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void)back
{
[self.navigationController popViewControllerAnimated:YES];
}
//
//-(void)creatTopUI
//{
// UIButton *topView = [[UIButton alloc]init];
// [self.view addSubview:topView];
// topView.frame = CGRectMake(5, 120, SCREEN_WIDTH-20, 30);
// topView.backgroundColor = [UIColor blackColor];
// [topView addTarget:self action:@selector(topClick) forControlEvents:UIControlEventTouchUpInside];
//
// UIImageView *icon = [[UIImageView alloc]init];
// [topView addSubview:icon];
// icon.frame = CGRectMake(12, 10, 30, 20);
// icon.backgroundColor = [UIColor redColor];
//
// UILabel *title = [[UILabel alloc]init];
// [topView addSubview:title];
// title.text = @"酒馆派对火热进行中...";
// title.textColor = [UIColor whiteColor];
//
// UILabel *countLab = [[UILabel alloc]init];
// [topView addSubview:countLab];
// countLab.text = @"153.8w+次参与 >";
// countLab.textColor = [UIColor whiteColor];
// countLab.font = [UIFont systemFontOfSize:14];
//
//
//}
-(void)topClick
{
BottleDetialController *deDetial = [[BottleDetialController alloc]init];
[self.navigationController pushViewController:deDetial animated:YES];
}
- (void)onAppDidBecomeActive{
if ([[PYAppService currentViewController] isKindOfClass:[BottleDetialController class]]) {
[self refreshHomeBottleData]; // 刷新数据
// [self reloadMsgBadge];
}
}
- (void)initNotificationAlert {
NSDate *nowDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *strDate = [dateFormatter stringFromDate:nowDate];
NSLog(@"%@", strDate);
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
dispatch_async(dispatch_get_main_queue(), ^{
// 获取状态
UNAuthorizationStatus authorizationStatus = settings.authorizationStatus;
//一天一次
NSString *dateFlag = [[NSUserDefaults standardUserDefaults] objectForKey:OpenAppDayFlag];
[[NSUserDefaults standardUserDefaults] setObject:strDate forKey:OpenAppDayFlag];
if (authorizationStatus == UNAuthorizationStatusNotDetermined) {
if ([dateFlag isEqualToString:strDate]) {
return;
}
NSNumber *flag = [[NSUserDefaults standardUserDefaults] objectForKey:NotificationAlertFlag];
if (flag.intValue == 0){
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.title = @"开通消息推送?";
config.message = @"开通后第一时间收到Ta的消息";
config.cancelTitle = @"下次";
config.cancelHandler =^(MTAlertButton *button) {
//第3天再提示
[[NSUserDefaults standardUserDefaults] setObject:@(-2) forKey:NotificationAlertFlag];
};
config.otherTitle = @"允许";
config.otherHandler = ^(MTAlertButton *button) {
[self registNotification];
[[NSUserDefaults standardUserDefaults] setObject:@(1) forKey:NotificationAlertFlag];
};
}];
}
if (flag.intValue < 0){
[[NSUserDefaults standardUserDefaults] setObject:@(flag.intValue + 1) forKey:NotificationAlertFlag];
}
}else if (authorizationStatus == UNAuthorizationStatusDenied){
if ([dateFlag isEqualToString:strDate]) {
return;
}
NSNumber *flag = [[NSUserDefaults standardUserDefaults] objectForKey:NotificationAlertFlag];
if (flag.intValue < 0){
[[NSUserDefaults standardUserDefaults] setObject:@(flag.intValue + 1) forKey:NotificationAlertFlag];
}else if (flag.intValue > 0){
//第5天再提示
[[NSUserDefaults standardUserDefaults] setObject:@(-4) forKey:NotificationAlertFlag];
}else{
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.title = @"不错过Ta的消息";
config.message = @"打开推送,畅快聊";
config.cancelTitle = @"下次";
config.cancelHandler =^(MTAlertButton *button) {
//第3天再提示
[[NSUserDefaults standardUserDefaults] setObject:@(-4) forKey:NotificationAlertFlag];
};
config.otherTitle = @"OK";
config.otherHandler = ^(MTAlertButton *button) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
};
}];
}
}else{
[self registNotification];
}
});
}];
}
// 向苹果后台请求 DeviceToken
- (void)registNotification
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
// 注册远程通知
[GeTuiSdk registerRemoteNotification: (UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge)];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self refreshHomeBottleData];
// [self reloadMsgBadge];
[self reloadHurlBottoleButtonBadge];
if (self.disappearTime > 0) {
// 从其它tab切换回瓶子tab如果间隔时间超过5分钟自动刷新瓶子tab有loading
if ([[NSDate date] timeIntervalSince1970] - self.disappearTime > 60 * 5) { // 5分钟
[self refreshHomeBottleData]; // 刷新数据
}
}
self.disappearTime = 0;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if ([PYAppService selectedTabItemIndex] != 1) {
self.disappearTime = [[NSDate date] timeIntervalSince1970];
} else {
self.disappearTime = 0;
}
}
- (void)reloadMsgBadge {
[LoginService launchAppToHomeWithCompletion:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
int unreadCount = [rsp[@"unread_notice"] intValue];
self.getBottleButton.badgeCenterOffset = CGPointMake(-FIX_SIZE(5), FIX_SIZE(5));
if (unreadCount > 0) {
[self.getBottleButton showBadgeWithStyle:WBadgeStyleNumber value:unreadCount animationType:WBadgeAnimTypeNone];
} else {
[self.getBottleButton clearBadge];
}
[PYAppService showBadgeOnTabBarItemAtIndex:1 number:unreadCount];
[PYAppService appConfig].is_wallet_notice = [rsp[@"is_wallet_notice"] intValue];
[PYAppService appConfig].wallet_des = rsp[@"wallet_des"];
if ([ProfileViewController shouldShowBadge]) {
[PYAppService showBadgeOnTabBarItemAtIndex:0 number:-1];
}
}
}];
}
- (void)setupUI {
self.contentScrollView.alwaysBounceVertical = NO;
self.contentScrollView.alwaysBounceHorizontal = NO;
self.contentScrollView.showsVerticalScrollIndicator = NO;
self.contentScrollView.showsHorizontalScrollIndicator = NO;
self.contentScrollView.backgroundColor = [UIColor clearColor];
self.contentScrollView.contentInset = UIEdgeInsetsZero;
self.contentScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//末班修改的地方
[self.contentScrollView addSubview:self.collectionView];
[self.view addSubview:self.hurlBottleButton];
// _collectionView.sd_layout.leftSpaceToView(self.view, 0).heightIs(SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT).rightSpaceToView(self.view, 0).topSpaceToView(self.view, 0);
// [self.view addSubview:self.hurlBottleButton];
// _hurlBottleButton.sd_layout.widthIs(FIX_SIZE(64)).heightIs(FIX_SIZE(64)).rightSpaceToView(self.view, 17).bottomSpaceToView(self.view, -FIX_SIZE(60));
}
-(UICollectionView *)collectionView{
if (!_collectionView) {
NSLog(@"self.info : %@",self.info);
int collectionMaxColumn = self.info.cabinet_list_num;
NSLog(@"self.info.collectionMaxColumn : %d",collectionMaxColumn);
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 3;
layout.minimumInteritemSpacing = 0;
layout.itemSize = CGSizeMake(FIX_SIZE(BottleCollectionCellWidth), FIX_SIZE(BottleCollectionCellHeight));
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT );
_collectionView.contentInset = UIEdgeInsetsMake(0, FIX_SIZE(8), FIX_SIZE(25), FIX_SIZE(8));
_collectionView.width = FIX_SIZE(BottleCollectionCellWidth) * collectionMaxColumn + _collectionView.contentInset.left + _collectionView.contentInset.right;
_collectionView.backgroundColor = HEX_COLOR(0x0B0B0B);
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
[_collectionView registerClass:BottleCollectionCell.class forCellWithReuseIdentifier:BottleCollectionCellReuseID];
}
return _collectionView;
}
-(UIButton *)hurlBottleButton{
if (!_hurlBottleButton) {
_hurlBottleButton = [UIButton buttonWithType:UIButtonTypeCustom];
_hurlBottleButton.size = CGSizeMake(FIX_SIZE(74), FIX_SIZE(71));
[_hurlBottleButton setImage:ImageNamed(@"TH_home_bottle_btn") forState:UIControlStateNormal];
_hurlBottleButton.backgroundColor = [UIColor clearColor];
_hurlBottleButton.bottom = self.collectionView.bottom - FIX_SIZE(60) - FIX_SIZE(64);
_hurlBottleButton.right = SCREEN_WIDTH - 17;
[_hurlBottleButton addTarget:self action:@selector(hurlBottleBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _hurlBottleButton;
}
/// 捞瓶子
- (void)getBottle {
[BottleReadView showDetailWithAttach];
}
-(void)hurlBottleBtnClick{
WeakSelf(self);
if ([PYAppService showUploadAvatarAlertIfNeed]) {
return;
}
BottleTypePickerViewController *picker = [BottleTypePickerViewController pickerViewControllerWithCompletion:^(BottleTypePickerViewController * _Nonnull picker, BottleTypeInfo * _Nonnull info) {
[picker.navigationController dismissViewControllerAnimated:NO completion:^{
BottlePublishViewController *publishVC = [[BottlePublishViewController alloc] init];
publishVC.info = info;
publishVC.publishSucceedAction = ^{
if (weakself.info.left_bottle_num > 0) {
weakself.info.left_bottle_num --;
[weakself reloadHurlBottoleButtonBadge];
}
};
[weakself.navigationController presentViewController:[[PYNavigationViewController alloc] initWithRootViewController:publishVC] animated:YES completion:nil];
}];
}];
PYNavigationViewController *nav = [[PYNavigationViewController alloc] initWithRootViewController:picker];
[[PYAppService currentNavigationController] presentViewController:nav animated:YES completion:nil];
}
- (void)reloadHurlBottoleButtonBadge {
int number = self.info.left_bottle_num;
[self.hurlBottleButton setBadgeFont:MT_FONT_MEDIUM_SIZE(13)];
[self.hurlBottleButton setBadgeCenterOffset:CGPointMake(-FIX_SIZE(15), FIX_SIZE(15))];
[self.hurlBottleButton setBadgeBgColor:THEME_COLOR];
[self.hurlBottleButton setBadgeTextColor:HEX_COLOR(0xf74c31)];
if (number < 0) {
[self.hurlBottleButton showCustomBadgeWithContent:@""];
} else if (number > 0) {
[self.hurlBottleButton showNumberBadgeWithValue:number];
} else if (number == 0) {
[self.hurlBottleButton showCustomBadgeWithContent:@"0"];
}
}
- (void)refreshHomeBottleData {
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
rotationAnimation.duration = 1.0;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;
[self.navBottleBtn.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[SVProgressHUD showWithStatus:nil];
[BottleService getHomeBottleInfoWithCompletion:^(HomeBottleInfo * _Nullable info) {
[self endLoading:nil];
if (info.bottleInfos.count <= 0) {
[SVProgressHUD showErrorWithStatus:@"暂时没有新的瓶子啦"];
[MTAlertView showWithSetupBlcok:^(MTAlertViewConfig *config) {
config.message = @" 更多推荐瓶子正在来的路上\n酒馆派对正在火热进行中🔥\n快去参与派对查看更多的瓶子吧~";
config.otherTitle = @"查看更多瓶子";
config.otherHandler = ^(MTAlertButton *button) {
[self topClick];
};
}];
return;
}
if (info.toast_msg.length > 0) {
[ToastUtil showToast:info.toast_msg];
// [SVProgressHUD showInfoWithStatus:info.toast_msg];
}
[SVProgressHUD dismiss];
self.info = info;
// [self craetGoodBottal];
[self setUpColltionView:info];
}];
}
//-(void)craetGoodBottal
//{
// BottleInfo *info = self.info.bottleInfos[0];
// if (info.cover.length > 0) { // 有瓶子
// BottleReadView *readView = [BottleReadView showDetailWithBottleInfo:info shouldHideBottleAction:^(bool shouldHide) {
//
//// [self.view addSubview:readView];
//
// if (shouldHide) {
// info.cover = nil;
//// [collectionView reloadItemsAtIndexPaths:@[indexPath]];
// }
// }];
// readView.dismissAction = ^(BOOL didClickNext) {
// if (didClickNext) {
// [self refreshHomeBottleData];
// }
// };
// }
//}
-(void)setUpColltionView:(HomeBottleInfo *)info
{
UICollectionView *collectionView = self.collectionView;
// collectionView.contentInset = UIEdgeInsetsMake(0, FIX_SIZE(8), FIX_SIZE(15), FIX_SIZE(15));
collectionView.width = FIX_SIZE(BottleCollectionCellWidth) * info.cabinet_list_num + collectionView.contentInset.left + collectionView.contentInset.right;
collectionView.contentSize = CGSizeMake(collectionView.width, 0);
[collectionView setContentOffset:CGPointMake(-collectionView.contentInset.left, 0) animated:NO];
[collectionView reloadData];
self.contentScrollView.contentSize = CGSizeMake(collectionView.width, 0);
[self.contentScrollView setContentOffset:CGPointZero animated:NO];
[self reloadHurlBottoleButtonBadge];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.info.cabinet_list_num == 0) {
return 0;
}
// NSUInteger rowCount = self.info.bottleInfos.count / self.info.cabinet_list_num;
// self.rightView.height = rowCount * FIX_SIZE(BottleCollectionCellHeight);
[self reloadHurlBottoleButtonBadge];
// NSLog(@"self.info.bottleInfos : %@",self.info.bottleInfos);
return self.info.bottleInfos.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BottleCollectionCell *cell = (BottleCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:BottleCollectionCellReuseID forIndexPath:indexPath];
cell.info = self.info.bottleInfos[indexPath.item];
return cell;
}
#pragma mark - UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
NSLog(@"indexPath.item==%zd",indexPath.item);
BottleInfo *info = self.info.bottleInfos[indexPath.item];
NSLog(@"info mj_keyValues==%@",[info mj_keyValues]);
if (info.cover.length > 0) { // 有瓶子
BottleReadView *readView = [BottleReadView showDetailWithBottleInfo:info Views:self.view isHome:NO shouldHideBottleAction:^(bool shouldHide) {
if (shouldHide) {
info.cover = nil;
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
}
}];
readView.ishome = NO;
readView.dismissAction = ^(BOOL didClickNext) {
if (didClickNext) {
[self refreshHomeBottleData];
}
};
}
}
@end