WardPopView.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. //
  2. // WardPopView.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/1/28.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "WardPopView.h"
  9. #import "WardViewTopCell.h"
  10. #import "WardViewCell.h"
  11. #import "WardPopViewModel.h"
  12. @interface WardPopView()<UITableViewDelegate,UITableViewDataSource>
  13. @property (nonatomic, strong) UITableView *tableView;
  14. @property (nonatomic, strong) NSMutableArray *dataArray;
  15. @property (nonatomic, strong) UIView *shadowView;
  16. @property (nonatomic, strong) UILabel *titleLabel;
  17. @property (nonatomic, copy) clickOpenBtnBlock clickOpenBtnBlock;
  18. @property (nonatomic, copy) NSString *user_id;
  19. @property (nonatomic, strong) UIView *noWardView;
  20. @property (nonatomic, strong) UILabel *noWardViewLabel;
  21. @property (nonatomic, strong) NSDictionary *json;
  22. @property (nonatomic, copy) clickWardPopViewCellBlock clickWardPopViewCellBlock;
  23. @property (nonatomic, strong) UILabel *tipLabel;
  24. @end
  25. @implementation WardPopView
  26. - (instancetype)initWithFrame:(CGRect)frame UserId:(nonnull NSString *)userID ResponseJson:(nonnull NSDictionary *)json{
  27. if (self = [super initWithFrame:frame]) {
  28. // self.backgroundColor = KMGMainBGColor;
  29. self.layer.cornerRadius = 10;
  30. self.clipsToBounds = YES;
  31. self.user_id = userID;
  32. self.json = json;
  33. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectZero];
  34. [self addSubview:bottomView];
  35. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.left.equalTo(self).offset(10);
  37. make.right.equalTo(self).offset(-10);
  38. make.bottom.equalTo(self).offset(-10);
  39. make.height.equalTo(@50);
  40. }];
  41. bottomView.layer.cornerRadius = 5;
  42. bottomView.backgroundColor = [UIColor colorWithHexString:@"#F2F0FF"];
  43. [self initSubview];
  44. }
  45. return self;
  46. }
  47. -(void)setGradientLayer
  48. {
  49. CAGradientLayer *gl = [CAGradientLayer layer];
  50. gl.frame = self.bounds;
  51. gl.startPoint = CGPointMake(0, 0);
  52. gl.endPoint = CGPointMake(1, 1);
  53. gl.colors = @[(__bridge id)[UIColor colorWithRed:174/255.0 green:44/255.0 blue:241/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:137/255.0 green:106/255.0 blue:255/255.0 alpha:1.0].CGColor];
  54. gl.locations = @[@(0.0),@(1.0f)];
  55. [self.layer insertSublayer:gl atIndex:0];
  56. self.layer.cornerRadius = 20;
  57. }
  58. - (void)initSubview{
  59. [self setGradientLayer];
  60. [self addSubview:self.titleLabel];
  61. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.titleLabel.bottom - 0.5, self.width, 0.5)];
  62. lineView.backgroundColor = kClearColor;
  63. [self addSubview:lineView];
  64. [self addSubview:self.tableView];
  65. [self setData];
  66. UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, kRealValue(25), kRealValue(25))];
  67. closeBtn.right = self.right - kRealValue(25);
  68. [closeBtn setImage:[UIImage imageNamed:@"pl_publishlive_close"] forState:UIControlStateNormal];
  69. [closeBtn addTarget:self action:@selector(closeBtn:) forControlEvents:UIControlEventTouchUpInside];
  70. [self addSubview:closeBtn];
  71. [self bringSubviewToFront:closeBtn];
  72. }
  73. -(void)closeBtn:(UIButton *)sender{
  74. [self hide];
  75. }
  76. - (void)requestWardData{
  77. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  78. [dict setValue:@"guardians" forKey:@"ctl"];
  79. [dict setValue:@"index" forKey:@"act"];
  80. [dict setValue:self.user_id forKey:@"host_id"];
  81. FWWeakify(self)
  82. [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  83. FWStrongify(self)
  84. NSLog(@"104responseJson%@",responseJson);
  85. if ([responseJson toInt:@"status"] == 1) {
  86. //守护数量
  87. [self.dataArray removeAllObjects];
  88. //是否已经开通守护
  89. NSString *is_guartian = responseJson[@"is_guartian"];
  90. //守护数量
  91. NSString *guardian_sum = responseJson[@"guardian_sum"];
  92. NSString *guardian_time = responseJson[@"guartian_time"];
  93. if (guardian_time.length > 10) {
  94. guardian_time = [guardian_time substringToIndex:10];
  95. }
  96. [self.titleLabel setText:[NSString stringWithFormat:ASLocalizedString(@"守护(%@)"),guardian_sum]];
  97. if (![self.user_id isEqualToString:[IMAPlatform sharedInstance].host.imUserId]) {
  98. if (!self.tipLabel) {
  99. UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.tableView.bottom + 10, self.width - 120, 40)];
  100. tipLabel.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
  101. tipLabel.font = [UIFont systemFontOfSize:15];
  102. tipLabel.numberOfLines = 0;
  103. tipLabel.backgroundColor = kClearColor;
  104. [self addSubview:tipLabel];
  105. self.tipLabel = tipLabel;
  106. }
  107. if (is_guartian.integerValue == 2) {
  108. self.tipLabel.text = ASLocalizedString(@"快去为你喜爱的主播开通守护吧!");
  109. }else{
  110. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",ASLocalizedString(@"到期时间:"),guardian_time]];
  111. [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#666666"] range:NSMakeRange(ASLocalizedString(@"到期时间:").length, guardian_time.length)];
  112. self.tipLabel.attributedText = attr;
  113. }
  114. UIButton *openBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.tipLabel.right, self.tableView.bottom + 5, 100, 40)];
  115. if (is_guartian.integerValue == 1) {
  116. [openBtn setTitle:ASLocalizedString(@"续费守护")forState:UIControlStateNormal];
  117. }else{
  118. [openBtn setTitle:ASLocalizedString(@"开通守护")forState:UIControlStateNormal];
  119. }
  120. [openBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  121. [openBtn setBackgroundImage:[UIImage imageNamed:@"lr_btn_ward_bg"] forState:UIControlStateNormal];
  122. [openBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  123. [openBtn addTarget:self action:@selector(openBtnAction) forControlEvents:UIControlEventTouchUpInside];
  124. openBtn.layer.cornerRadius = 20;
  125. openBtn.clipsToBounds = YES;
  126. [self addSubview:openBtn];
  127. }else{
  128. self.tableView.frame = CGRectMake(0, self.titleLabel.bottom, self.bounds.size.width, self.bounds.size.height - self.titleLabel.height);
  129. }
  130. NSArray *list = responseJson[@"list"];
  131. if (list && list.count) {
  132. for (NSDictionary *dict in list) {
  133. WardPopViewModel *model = [WardPopViewModel mj_objectWithKeyValues:dict];
  134. [self.dataArray addObject:model];
  135. }
  136. if (self.dataArray.count > 0) {
  137. WardPopViewModel *model = self.dataArray.firstObject;
  138. // if (model.total_diamonds.integerValue < 1) {
  139. //
  140. // }
  141. WardPopViewModel *emptyModel = [WardPopViewModel new];
  142. if (model.total_diamonds.intValue == 0) {
  143. emptyModel.uid = @"0";
  144. [self.dataArray insertObject:emptyModel atIndex:0];
  145. }
  146. }
  147. // WardPopViewModel *firstModel = self.dataArray.firstObject;
  148. // if (!firstModel.total_diamonds.integerValue) {
  149. // WardPopViewModel *model = [[WardPopViewModel alloc]init];
  150. // model.total_diamonds = @"0";
  151. // [self.dataArray insertObject:model atIndex:0];
  152. // }
  153. // if (!self.dataArray.count) {
  154. // [self addSubview:self.noWardView];
  155. // }else{
  156. [self.noWardView removeFromSuperview];
  157. // }
  158. [self.tableView reloadData];
  159. }else{
  160. // [self addSubview:self.noWardView];
  161. if ([self.user_id isEqualToString:[IMAPlatform sharedInstance].host.imUserId]) {
  162. self.noWardViewLabel.text = ASLocalizedString(@"我的守护正星夜兼程的赶来");
  163. }else{
  164. self.noWardViewLabel.text = ASLocalizedString(@"成为TA的第一个守护");
  165. }
  166. }
  167. }else{
  168. //接口请求失败
  169. NSLog(ASLocalizedString(@"守护列表请求数据失败responseJson:%@"),responseJson);
  170. }
  171. } FailureBlock:^(NSError *error) {
  172. NSLog(ASLocalizedString(@"守护列表请求数据失败error:%@"),error);
  173. }];
  174. }
  175. - (void)setData{
  176. [self.dataArray removeAllObjects];
  177. //是否已经开通守护
  178. NSString *is_guartian = [NSString stringWithFormat:@"%@", self.json[@"is_guartian"]];
  179. //守护数量
  180. NSString *guardian_sum = [NSString stringWithFormat:@"%@", self.json[@"guardian_sum"]];
  181. NSString *guartian_time = [NSString stringWithFormat:@"%@", self.json[@"guartian_time"]];
  182. guardian_sum = [BGUtils isBlankString:guardian_sum] ? @"0" : guardian_sum;
  183. guartian_time = [BGUtils isBlankString:guartian_time] ? @"" : guartian_time;
  184. if (guartian_time.length > 10) {
  185. guartian_time = [guartian_time substringToIndex:10];
  186. }
  187. [self.titleLabel setText:[NSString stringWithFormat:ASLocalizedString(@"守护(%@)"),guardian_sum]];
  188. if (![self.user_id isEqualToString:[IMAPlatform sharedInstance].host.imUserId]) {
  189. if (!self.tipLabel) {
  190. UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.tableView.bottom + 10, self.width - 120, 40)];
  191. tipLabel.textColor = [UIColor colorWithHexString:@"#666666"];
  192. tipLabel.font = [UIFont systemFontOfSize:15];
  193. tipLabel.numberOfLines = 0;
  194. tipLabel.backgroundColor = kClearColor;
  195. [self addSubview:tipLabel];
  196. self.tipLabel = tipLabel;
  197. }
  198. if (is_guartian.integerValue == 2) {
  199. self.tipLabel.text = ASLocalizedString(@"快去为你喜爱的主播开通守护吧!");
  200. }else{
  201. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",ASLocalizedString(@"到期: "),guartian_time]];
  202. [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#399ADF"] range:NSMakeRange(ASLocalizedString(@"到期: ").length, guartian_time.length)];
  203. self.tipLabel.attributedText = attr;
  204. }
  205. UIButton *openBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.tipLabel.right, self.tableView.bottom + 5, 100, 40)];
  206. if (is_guartian.integerValue == 1) {
  207. [openBtn setTitle:ASLocalizedString(@"续费守护")forState:UIControlStateNormal];
  208. }else{
  209. [openBtn setTitle:ASLocalizedString(@"开通守护")forState:UIControlStateNormal];
  210. }
  211. [openBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  212. [openBtn setBackgroundImage:[UIImage imageNamed:@"lr_btn_ward_bg"] forState:UIControlStateNormal];
  213. [openBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  214. [openBtn addTarget:self action:@selector(openBtnAction) forControlEvents:UIControlEventTouchUpInside];
  215. openBtn.layer.cornerRadius = 20;
  216. openBtn.clipsToBounds = YES;
  217. [self addSubview:openBtn];
  218. }else{
  219. self.tableView.frame = CGRectMake(0, self.titleLabel.bottom, self.bounds.size.width, self.bounds.size.height - self.titleLabel.height);
  220. }
  221. NSArray *list = self.json[@"list"];
  222. if (list && list.count) {
  223. for (NSDictionary *dict in list) {
  224. WardPopViewModel *model = [WardPopViewModel mj_objectWithKeyValues:dict];
  225. [self.dataArray addObject:model];
  226. }
  227. WardPopViewModel *firstModel = self.dataArray.firstObject;
  228. if (!firstModel.total_diamonds.integerValue) {
  229. WardPopViewModel *model = [[WardPopViewModel alloc]init];
  230. model.total_diamonds = @"0";
  231. [self.dataArray insertObject:model atIndex:0];
  232. }
  233. if (!self.dataArray.count) {
  234. // [self addSubview:self.noWardView];
  235. if ([self.user_id isEqualToString:[IMAPlatform sharedInstance].host.imUserId]) {
  236. self.noWardViewLabel.text = ASLocalizedString(@"我的守护正星夜兼程的赶来");
  237. }else{
  238. self.noWardViewLabel.text = ASLocalizedString(@"成为TA的第一个守护");
  239. }
  240. }else{
  241. [self.noWardView removeFromSuperview];
  242. }
  243. [self.tableView reloadData];
  244. }else{
  245. // [self addSubview:self.noWardView];
  246. if ([self.user_id isEqualToString:[IMAPlatform sharedInstance].host.imUserId]) {
  247. self.noWardViewLabel.text = ASLocalizedString(@"我的守护正星夜兼程的赶来");
  248. }else{
  249. self.noWardViewLabel.text = ASLocalizedString(@"成为TA的第一个守护");
  250. }
  251. }
  252. }
  253. - (void)show:(UIView *)superView{
  254. [self requestWardData];
  255. [superView addSubview:self.shadowView];
  256. [superView addSubview:self];
  257. [UIView animateWithDuration:0.25 animations:^{
  258. // self.center = CGPointMake(kScreenW / 2, kScreenH / 2);
  259. self.bottom = kScreenH;
  260. self.shadowView.alpha = 1;
  261. }];
  262. }
  263. - (void)hide{
  264. [UIView animateWithDuration:0.25 animations:^{
  265. self.frame = CGRectMake(0, kScreenH, self.width, self.height);
  266. self.shadowView.alpha = 0;
  267. } completion:^(BOOL finished) {
  268. [self removeFromSuperview];
  269. [self.shadowView removeFromSuperview];
  270. }];
  271. }
  272. - (void)openBtnAction{
  273. [self hide];
  274. if (self.clickOpenBtnBlock) {
  275. self.clickOpenBtnBlock();
  276. }
  277. }
  278. - (void)setClickOpenBtnBlock:(clickOpenBtnBlock)clickOpenBtnBlock{
  279. _clickOpenBtnBlock = clickOpenBtnBlock;
  280. }
  281. #pragma mark - UITableViewDelegate UITableViewDataSource
  282. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  283. return self.dataArray.count > 0 ? self.dataArray.count:1;
  284. }
  285. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  286. if (self.dataArray.count < 1) {
  287. WardViewTopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WardViewTopCell class])];
  288. cell.layer.cornerRadius = 0;
  289. cell.clipsToBounds = YES;
  290. [cell setModel:nil];
  291. return cell;
  292. }
  293. if (indexPath.row >= self.dataArray.count) {
  294. WardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WardViewCell class])];
  295. return cell;
  296. }
  297. WardPopViewModel *model = self.dataArray[indexPath.row];
  298. if (indexPath.row == 0) {
  299. WardViewTopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WardViewTopCell class])];
  300. cell.layer.cornerRadius = 0;
  301. cell.clipsToBounds = YES;
  302. [cell setModel:model];
  303. return cell;
  304. }
  305. WardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WardViewCell class])];
  306. cell.backgroundColor = [UIColor colorWithHexString:@"#A060F0"];
  307. [cell setModel:model];
  308. return cell;
  309. }
  310. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  311. if (indexPath.row == 0) {
  312. return 176;
  313. }
  314. return 79;
  315. }
  316. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  317. NSLog(ASLocalizedString(@"点击了守护列表中的第%ld项"),indexPath.row);
  318. if (self.dataArray.count > 0) {
  319. WardPopViewModel *model = self.dataArray[indexPath.row];
  320. if (model.uid) {
  321. [self hide];
  322. if (self.clickWardPopViewCellBlock) {
  323. self.clickWardPopViewCellBlock(model);
  324. }
  325. }
  326. }
  327. }
  328. #pragma mark - Lazy Load
  329. - (UIView *)shadowView{
  330. if (!_shadowView) {
  331. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScaleH)];
  332. _shadowView.backgroundColor = [kBlackColor colorWithAlphaComponent:0.3];
  333. _shadowView.alpha = 0;
  334. _shadowView.userInteractionEnabled = YES;
  335. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  336. [_shadowView addGestureRecognizer:tap];
  337. }
  338. return _shadowView;
  339. }
  340. - (UITableView *)tableView{
  341. if (!_tableView) {
  342. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.titleLabel.bottom, self.bounds.size.width, self.bounds.size.height - self.titleLabel.height - 60) style:UITableViewStylePlain];
  343. _tableView.delegate = self;
  344. _tableView.dataSource = self;
  345. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  346. [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WardViewTopCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([WardViewTopCell class])];
  347. [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WardViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([WardViewCell class])];
  348. _tableView.backgroundColor = kClearColor;
  349. }
  350. return _tableView;
  351. }
  352. - (NSMutableArray *)dataArray{
  353. if (!_dataArray) {
  354. _dataArray = [[NSMutableArray alloc]init];
  355. }
  356. return _dataArray;
  357. }
  358. - (UILabel *)titleLabel{
  359. if (!_titleLabel) {
  360. _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, 50)];
  361. _titleLabel.text = ASLocalizedString(@"守护");
  362. _titleLabel.textColor = kWhiteColor;
  363. _titleLabel.backgroundColor = kClearColor;
  364. _titleLabel.font = [UIFont systemFontOfSize:15];
  365. _titleLabel.textAlignment = NSTextAlignmentCenter;
  366. }
  367. return _titleLabel;
  368. }
  369. - (UIView *)noWardView{
  370. if (!_noWardView) {
  371. _noWardView = [[UIView alloc]initWithFrame:self.tableView.bounds];
  372. _noWardView.top = kRealValue(44);
  373. _noWardView.height = self.tableView.height - kRealValue(44);
  374. UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 103, 103)];
  375. imageView.centerX = self.tableView.width / 2;
  376. imageView.top = self.titleLabel.bottom + 10;
  377. imageView.image = [UIImage imageNamed:@"lr_img_back_ward_no"];
  378. [_noWardView addSubview:imageView];
  379. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, imageView.bottom + 20, self.tableView.width - 20, 20)];
  380. label.textColor = RGB(230, 230, 230);
  381. label.textAlignment = NSTextAlignmentCenter;
  382. label.font = [UIFont systemFontOfSize:15];
  383. [_noWardView addSubview:label];
  384. self.noWardViewLabel = label;
  385. }
  386. return _noWardView;
  387. }
  388. /*
  389. // Only override drawRect: if you perform custom drawing.
  390. // An empty implementation adversely affects performance during animation.
  391. - (void)drawRect:(CGRect)rect {
  392. // Drawing code
  393. }
  394. */
  395. @end