// // XPSailingViewController.m // xplan-ios // // Created by 冯硕 on 2022/8/12. // #import "XPSailingViewController.h" ///Third #import #import ///Tool #import "ThemeColor.h" #import "XPMacro.h" #import "TTPopup.h" #import "XPHtmlUrl.h" ///Model #import "RoomSailingInfoModel.h" #import "RoomSailingPrizeListModel.h" #import "AttachmentModel.h" ///View #import "XPSailingRankView.h" #import "XPRoomHalfWebView.h" #import "XPSailingPrizeView.h" ///P #import "XPSailingPresenter.h" #import "XPSailingProtocol.h" @interface XPSailingViewController () ///顶部的View @property (nonatomic,strong) UIView * topView; ///背景图 @property (nonatomic,strong) UIImageView *backImageView; ///排行榜 @property (nonatomic,strong) UIButton *rankButton; ///h获取燃油 @property (nonatomic,strong) UIButton *getFuleButton; ///记录 @property (nonatomic,strong) UIButton *recordButton; ///内容 @property (nonatomic,strong) UIImageView *contentImageView; ///说明 @property (nonatomic,strong) UIButton *descriptionButton; ///燃油 @property (nonatomic,strong) UIButton *fuleButton; ///个数的容器 @property (nonatomic,strong) UIStackView * numberView; ///加 @property (nonatomic,strong) UIButton *addButton; ///减 @property (nonatomic,strong) UIButton *reduButton; ///输入个数 @property (nonatomic,strong) UITextField *textField; ///探索 @property (nonatomic,strong) UIButton *exploreButton; ///消耗燃油 @property (nonatomic,strong) UILabel *consumLabel; @property (nonatomic,assign) NSInteger exploreCount; ///航海的信息 @property (nonatomic,strong) RoomSailingInfoModel *sailingInfo; ///房主的uid @property (nonatomic,strong) NSString *roomUid; /// @property (nonatomic,strong) NSArray *levelArray; @end @implementation XPSailingViewController - (void)dealloc { [[NIMSDK sharedSDK].systemNotificationManager removeDelegate:self]; } - (instancetype)initWithRoomUid:(NSString *)roomUid { if (self = [super init]) { self.roomUid = roomUid; [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self]; } return self; } - (__kindof id)createPresenter { return [[XPSailingPresenter alloc] init]; } - (void)viewDidLoad { [super viewDidLoad]; [self.presenter getSailingInfo]; [self initSubViews]; [self initSubViewConstraints]; } #pragma mark - Private Method - (void)initSubViews { self.levelArray = @[@"1", @"10", @"50", @"100"]; self.exploreCount = 0; self.view.backgroundColor = [UIColor clearColor]; [self.view addSubview:self.topView]; [self.view addSubview:self.backImageView]; [self.backImageView addSubview:self.rankButton]; [self.backImageView addSubview:self.getFuleButton]; [self.backImageView addSubview:self.recordButton]; [self.backImageView addSubview:self.contentImageView]; [self.contentImageView addSubview:self.descriptionButton]; [self.contentImageView addSubview:self.fuleButton]; [self.contentImageView addSubview:self.numberView]; [self.contentImageView addSubview:self.exploreButton]; [self.contentImageView addSubview:self.consumLabel]; [self.numberView addArrangedSubview:self.reduButton]; [self.numberView addArrangedSubview:self.textField]; [self.numberView addArrangedSubview:self.addButton]; } - (void)initSubViewConstraints { [self.topView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(self.view); make.bottom.mas_equalTo(self.backImageView.mas_top); }]; [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.mas_equalTo(self.view); make.height.mas_equalTo(515); }]; [self.rankButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(41, 32)); make.top.mas_equalTo(self.backImageView).offset(129); make.right.mas_equalTo(self.backImageView).offset(-12); }]; [self.getFuleButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.right.mas_equalTo(self.rankButton); make.top.mas_equalTo(self.rankButton.mas_bottom).offset(8); }]; [self.recordButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.right.mas_equalTo(self.rankButton); make.top.mas_equalTo(self.getFuleButton.mas_bottom).offset(8); }]; CGFloat kscale = 199.0f / 356.0f; [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.backImageView).inset(9); make.bottom.mas_equalTo(self.backImageView).offset(-34); make.height.mas_equalTo((KScreenWidth - 18) * kscale); }]; [self.descriptionButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(65, 22)); make.right.mas_equalTo(self.contentImageView); make.top.mas_equalTo(self.contentImageView).offset(42); }]; [self.fuleButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.right.mas_equalTo(self.descriptionButton); make.top.mas_equalTo(self.descriptionButton.mas_bottom).offset(4); }]; [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.contentImageView); make.top.mas_equalTo(self.contentImageView).offset(77); make.height.mas_equalTo(30); }]; [self.reduButton mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(27); make.height.mas_equalTo(27); }]; [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(27); make.height.mas_equalTo(27); }]; [self.textField mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(75); make.height.mas_equalTo(30); }]; [self.exploreButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(150, 47)); make.centerX.mas_equalTo(self.contentImageView); make.top.mas_equalTo(self.numberView.mas_bottom).offset(12); }]; [self.consumLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.contentImageView); make.top.mas_equalTo(self.exploreButton.mas_bottom).offset(1); }]; } #pragma mark - XPSailingProtocol - (void)getSailingInfoSuccess:(RoomSailingInfoModel *)info { self.sailingInfo = info; NSString * fuleStr = [NSString stringWithFormat:@"%ld", info.keyNum]; [self.fuleButton setTitle:fuleStr forState:UIControlStateNormal]; } - (void)openSailingSuccess:(RoomSailingPrizeListModel *)model { self.sailingInfo.keyNum = model.remainKeyNum; NSString * fuleStr = [NSString stringWithFormat:@"%ld", self.sailingInfo.keyNum]; [self.fuleButton setTitle:fuleStr forState:UIControlStateNormal]; self.exploreButton.userInteractionEnabled = YES; if (model.prizeItemList.count > 0) { CGFloat height = 180; if (model.prizeItemList.count> 5) { height = height + 70 + 10; } XPSailingPrizeView * prizeView = [[XPSailingPrizeView alloc] initWithFrame:CGRectMake(0, 0, 283, height)]; prizeView.prizeItemList = model.prizeItemList; [TTPopup popupView:prizeView style:TTPopupStyleAlert]; } } - (void)openSailingFail:(NSString *)message { self.exploreButton.userInteractionEnabled = YES; } #pragma mark - Event Response - (void)dismissView { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)rankButtonAction:(UIButton *)sender { XPSailingRankView * rankView = [[XPSailingRankView alloc] init]; [TTPopup popupView:rankView style:TTPopupStyleActionSheet]; } - (void)getFuleButtonAction:(UIButton *)sender { XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init]; webView.url = URLWithType(kSailingActivityURL); webView.isSailing = YES; [TTPopup popupView:webView style:TTPopupStyleActionSheet]; } - (void)recordButtonAction:(UIButton *)sender { XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init]; webView.url = URLWithType(kSailingRecordURL); webView.isSailing = YES; [TTPopup popupView:webView style:TTPopupStyleActionSheet]; } - (void)descriptionButtonAction:(UIButton *)sender { XPRoomHalfWebView * webView = [[XPRoomHalfWebView alloc] init]; webView.isSailing = YES; webView.url = URLWithType(kSailingPlayDescdURL); [TTPopup popupView:webView style:TTPopupStyleActionSheet]; } - (void)addButtonAction:(UIButton *)sender { self.exploreCount ++; if (self.exploreCount >= self.levelArray.count) { self.exploreCount = 3; } } - (void)reduButtonAction:(UIButton *)sender { self.exploreCount --; if (self.exploreCount <= 0) { self.exploreCount = 0; } } - (void)exploreButtonAction:(UIButton *)sender { if (self.exploreCount > self.sailingInfo.keyNum) { TTAlertConfig * config = [[TTAlertConfig alloc] init]; config.title = @""; config.message = @"参与活动获得燃料"; config.confirmButtonConfig.title = @"去参与"; config.cancelButtonConfig.title = @"取消"; [TTPopup alertWithConfig:config confirmHandler:^{ [self getFuleButtonAction:self.getFuleButton]; } cancelHandler:^{ }]; } else { NSString * roomUid = [NSString stringWithFormat:@""]; [self.presenter openSailing:roomUid sendMessage:YES keyNum:self.textField.text.integerValue]; self.exploreButton.userInteractionEnabled = NO; } } #pragma mark - NIMSystemNotificationManagerDelegate - (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification { if (notification.receiverType == NIMSessionTypeP2P) { AttachmentModel *attachment = [AttachmentModel modelWithJSON:notification.content]; if (attachment.first == CustomMessageType_Account && attachment.second == Custom_Message_Sub_Account_Changed){ if (attachment.second == Custom_Message_Sub_Account_Changed) { [self.presenter getSailingInfo]; } } } } #pragma mark - Getters And Setters - (void)setExploreCount:(NSInteger)exploreCount { _exploreCount = exploreCount; if (_exploreCount == 0) { self.reduButton.enabled = NO; } else { self.reduButton.enabled = YES; } if (_exploreCount == 3) { self.addButton.enabled = NO; } else { self.addButton.enabled = YES; } self.textField.text = [self.levelArray objectAtIndex:_exploreCount]; } - (UIView *)topView { if (!_topView) { _topView = [[UIView alloc] init]; _topView.backgroundColor = [UIColor clearColor]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)]; [_topView addGestureRecognizer:tap]; return _topView; } return _topView; } - (UIImageView *)backImageView { if (!_backImageView) { _backImageView = [[UIImageView alloc] init]; _backImageView.userInteractionEnabled = YES; _backImageView.image = [UIImage imageNamed:@"room_sailing_bg"]; _backImageView.contentMode = UIViewContentModeScaleAspectFill; _backImageView.layer.masksToBounds = YES; } return _backImageView; } - (UIButton *)rankButton { if (!_rankButton) { _rankButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_rankButton setImage:[UIImage imageNamed:@"room_sailing_rank"] forState:UIControlStateNormal]; [_rankButton setImage:[UIImage imageNamed:@"room_sailing_rank"] forState:UIControlStateSelected]; [_rankButton addTarget:self action:@selector(rankButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _rankButton; } - (UIButton *)getFuleButton { if (!_getFuleButton) { _getFuleButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_getFuleButton setImage:[UIImage imageNamed:@"room_sailing_getfule"] forState:UIControlStateNormal]; [_getFuleButton setImage:[UIImage imageNamed:@"room_sailing_getfule"] forState:UIControlStateSelected]; [_getFuleButton addTarget:self action:@selector(getFuleButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _getFuleButton; } - (UIButton *)recordButton { if (!_recordButton) { _recordButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_recordButton setImage:[UIImage imageNamed:@"room_sailing_record"] forState:UIControlStateNormal]; [_recordButton setImage:[UIImage imageNamed:@"room_sailing_record"] forState:UIControlStateSelected]; [_recordButton addTarget:self action:@selector(recordButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _recordButton; } - (UIImageView *)contentImageView { if (!_contentImageView) { _contentImageView = [[UIImageView alloc] init]; _contentImageView.userInteractionEnabled = YES; _contentImageView.image = [UIImage imageNamed:@"room_sailing_explore_bg"]; } return _contentImageView; } - (UIButton *)descriptionButton { if (!_descriptionButton) { _descriptionButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_descriptionButton setImage:[UIImage imageNamed:@"room_sailing_description"] forState:UIControlStateNormal]; [_descriptionButton setImage:[UIImage imageNamed:@"room_sailing_description"] forState:UIControlStateSelected]; [_descriptionButton addTarget:self action:@selector(descriptionButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _descriptionButton; } - (UIButton *)fuleButton { if (!_fuleButton) { _fuleButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_fuleButton setTitle:@"0" forState:UIControlStateNormal]; [_fuleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _fuleButton.titleLabel.font = [UIFont systemFontOfSize:10]; [_fuleButton setTitleColor:[ThemeColor colorWithHexString:@"#643B29"] forState:UIControlStateNormal]; [_fuleButton setBackgroundImage:[UIImage imageNamed:@"room_sailing_rule_bg"] forState:UIControlStateNormal]; } return _fuleButton; } - (UIStackView *)numberView { if (!_numberView) { _numberView = [[UIStackView alloc] init]; _numberView.axis = UILayoutConstraintAxisHorizontal; _numberView.distribution = UIStackViewDistributionFill; _numberView.alignment = UIStackViewAlignmentCenter; _numberView.spacing = 6; } return _numberView; } - (UITextField *)textField { if (!_textField) { _textField = [[UITextField alloc] init]; _textField.textColor = [ThemeColor colorWithHexString:@"#64472E"]; _textField.backgroundColor = [ThemeColor colorWithHexString:@"#DACBB4"]; _textField.font = [UIFont systemFontOfSize:15]; _textField.layer.masksToBounds = YES; _textField.layer.cornerRadius = 11; _textField.text = @"1"; _textField.enabled = NO; _textField.textAlignment = NSTextAlignmentCenter; } return _textField; } - (UIButton *)addButton { if (!_addButton) { _addButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_addButton setImage:[UIImage imageNamed:@"room_sailing_add"] forState:UIControlStateNormal]; [_addButton setImage:[UIImage imageNamed:@"room_sailing_add"] forState:UIControlStateSelected]; [_addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _addButton; } - (UIButton *)reduButton { if (!_reduButton) { _reduButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_reduButton setImage:[UIImage imageNamed:@"room_sailing_redu"] forState:UIControlStateNormal]; [_reduButton setImage:[UIImage imageNamed:@"room_sailing_redu"] forState:UIControlStateSelected]; [_reduButton addTarget:self action:@selector(reduButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _reduButton; } - (UIButton *)exploreButton { if (!_exploreButton) { _exploreButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_exploreButton setImage:[UIImage imageNamed:@"room_sailing_explore"] forState:UIControlStateNormal]; [_exploreButton setImage:[UIImage imageNamed:@"room_sailing_explore"] forState:UIControlStateSelected]; [_exploreButton addTarget:self action:@selector(exploreButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _exploreButton; } - (UILabel *)consumLabel { if (!_consumLabel) { _consumLabel = [[UILabel alloc] init]; _consumLabel.font = [UIFont systemFontOfSize:10]; _consumLabel.textColor = [ThemeColor colorWithHexString:@"#A08161"]; _consumLabel.textAlignment = NSTextAlignmentCenter; _consumLabel.text = @"消耗1燃油连续探索"; } return _consumLabel; } @end