MGLiveWishView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // MGLiveWishView.m
  3. // BuguLive
  4. //
  5. // Created by 宋晨光 on 2019/12/5.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "MGLiveWishView.h"
  9. #import "MGAddWishViewController.h"
  10. @implementation MGLiveWishView
  11. - (instancetype)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. [self setUpView];
  16. }
  17. return self;
  18. }
  19. -(void)setUpView{
  20. UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kRealValue(50))];
  21. bgView.backgroundColor = kWhiteColor;
  22. bgView.layer.masksToBounds = YES;
  23. bgView.layer.cornerRadius = kRealValue(4);
  24. UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW / 2, kRealValue(44))];
  25. titleL.centerX = kScreenW / 2;
  26. titleL.text = ASLocalizedString(@"直播心愿单(0/3)");
  27. titleL.textAlignment = NSTextAlignmentCenter;
  28. titleL.font = [UIFont boldSystemFontOfSize:16];
  29. _titleL = titleL;
  30. UIButton *wishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  31. [wishBtn setTitle:ASLocalizedString(@"心愿单")forState:UIControlStateNormal];
  32. [wishBtn setTitleColor:[UIColor colorWithHexString:@"#CD49FF"] forState:UIControlStateNormal];
  33. wishBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  34. [wishBtn addTarget:self action:@selector(clickWish:) forControlEvents:UIControlEventTouchUpInside];
  35. wishBtn.frame = CGRectMake(kScreenW - kRealValue(60), 0, kRealValue(60), kRealValue(44));
  36. _wishBtn = wishBtn;
  37. [self addSubview:bgView];
  38. [self addSubview:titleL];
  39. [self addSubview:_wishBtn];
  40. [self addSubview:self.tableView];
  41. }
  42. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  43. return 1;
  44. }
  45. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  46. _titleL.text = [NSString stringWithFormat:ASLocalizedString(@"直播心愿单(%ld/3)"),self.listArr.count];
  47. return self.listArr.count > 0 ? self.listArr.count : 1;
  48. }
  49. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  50. if (self.listArr.count - 1 == indexPath.row || self.listArr.count == 0) {
  51. return kRealValue(300);
  52. }
  53. return kRealValue(140);
  54. }
  55. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  56. static NSString *identifier = @"MGLiveWishCell";
  57. MGLiveWishCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  58. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  59. cell.delegate = self;
  60. cell.bottomView.hidden = YES;
  61. if (self.listArr.count > 0) {
  62. [cell resetModelArr:self.listArr[indexPath.row] indexPath:indexPath.row];
  63. }
  64. if (indexPath.row == self.listArr.count - 1) {
  65. cell.bottomView.hidden = NO;
  66. }
  67. if (self.listArr.count == 0) {
  68. [cell resetModelArr:nil indexPath:indexPath.row];
  69. cell.bottomView.hidden = NO;
  70. }
  71. return cell;
  72. }
  73. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  74. }
  75. //手势代理
  76. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  77. if ([touch.view isKindOfClass:[UITableView class]]){
  78. return NO;
  79. }
  80. if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
  81. return NO;
  82. }
  83. return YES;
  84. }
  85. //点击事件
  86. -(void)protocolMGLiveWishClickType:(MGADD_WISH)wishType{
  87. //生成心愿单
  88. if (wishType == MGWISHTYPE_GENERATE) {
  89. //删除删掉的
  90. if (self.listDeleteArr.count > 0) {
  91. for (int i = 0; i < self.listDeleteArr.count; i ++) {
  92. MGLiveWishModel *model = self.listDeleteArr[i];
  93. [self deleteWishModel:model isShowAlert:i == self.listDeleteArr.count - 1];
  94. }
  95. }
  96. // //先全部删除
  97. // for (int i = 0; i < self.listArr.count; i ++) {
  98. // MGLiveWishModel *model = self.listArr[i];
  99. // [self deleteWishModel:model isShowAlert:NO];
  100. // }
  101. if (self.listArr.count < 1) {
  102. [FanweMessage alert:ASLocalizedString(@"请添加心愿")];
  103. return;
  104. }
  105. for (int i = 0; i < self.listArr.count; i ++) {
  106. MGLiveWishModel *model = self.listArr[i];
  107. if ([BGUtils isBlankString:model.g_id]) {
  108. [FanweMessage alert:ASLocalizedString(@"请选择礼物")];
  109. return;
  110. }
  111. if ([BGUtils isBlankString:model.g_num]) {
  112. [FanweMessage alert:ASLocalizedString(@"请填写礼物数量")];
  113. return;
  114. }
  115. if ([BGUtils isBlankString:model.txt]) {
  116. model.txt = @"";
  117. }
  118. [self generateWishModel:model isShowAlert:i == self.listArr.count - 1];
  119. }
  120. return;
  121. }
  122. if (wishType == MGWISHTYPE_ADD && self.listArr.count > 2) {
  123. [FanweMessage alertHUD:ASLocalizedString(@"心愿单不能超过三个!")];
  124. return;
  125. }
  126. if (self.clickLiveWishBlock) {
  127. self.clickLiveWishBlock(wishType);
  128. }
  129. }
  130. //生成心愿单
  131. -(void)generateWishModel:(MGLiveWishModel *)model isShowAlert:(BOOL)isShowAlert{
  132. if (StrValid(model.id)) {
  133. return;
  134. }
  135. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  136. [mDict setObject:@"user_wish" forKey:@"ctl"];
  137. [mDict setObject:@"pub_wish" forKey:@"act"];
  138. [mDict setObject:model.g_id forKey:@"g_id"];
  139. [mDict setObject:model.g_num forKey:@"g_num"];
  140. [mDict setObject:model.txt forKey:@"txt"];
  141. [[NetHttpsManager manager] POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  142. if ([responseJson toInt:@"status"] == 1)
  143. {
  144. if (isShowAlert)
  145. {
  146. [FanweMessage alert:ASLocalizedString(@"保存成功")];
  147. [self requestModel];
  148. }
  149. }
  150. } FailureBlock:^(NSError *error) {
  151. [FanweMessage alert:error.description];
  152. }];
  153. }
  154. //删除心愿单
  155. -(void)deleteWishModel:(MGLiveWishModel *)model isShowAlert:(BOOL)isShowAlert{
  156. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  157. [mDict setObject:@"user_wish" forKey:@"ctl"];
  158. [mDict setObject:@"del_wish" forKey:@"act"];
  159. [mDict setObject:model.id forKey:@"id"];
  160. [[NetHttpsManager manager] POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  161. if ([responseJson toInt:@"status"] == 1)
  162. {
  163. // if (isShowAlert) [FanweMessage alert:ASLocalizedString(@"生成成功")];
  164. }
  165. } FailureBlock:^(NSError *error) {
  166. [FanweMessage alert:error.description];
  167. }];
  168. }
  169. ////生成心愿
  170. //-(void)generateWish{
  171. // NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  172. // [mDict setObject:@"user_wish" forKey:@"ctl"];
  173. //// [mDict setObject:@"del_wish" forKey:@"act"];
  174. //
  175. // [[NetHttpsManager manager] POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  176. // if ([responseJson toInt:@"status"] == 1)
  177. // {
  178. //
  179. // }
  180. // } FailureBlock:^(NSError *error) {
  181. //
  182. // }];
  183. //}
  184. //删除
  185. -(void)protocolLiveWishClickDelteModel:(MGLiveWishModel *)model{
  186. if (![BGUtils isBlankString:model.id]) {
  187. [self.listDeleteArr addObject:model];
  188. }else{
  189. }
  190. [self.listArr removeObject:model];
  191. [self.tableView reloadData];
  192. // NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  193. // [mDict setObject:@"user_wish" forKey:@"ctl"];
  194. // [mDict setObject:@"del_wish" forKey:@"act"];
  195. // [mDict setObject:model.id forKey:@"id"];
  196. //
  197. // [[NetHttpsManager manager] POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  198. //
  199. // if ([responseJson toInt:@"status"] == 1)
  200. // {
  201. // [FanweMessage alertHUD:ASLocalizedString(@"删除心愿成功")];
  202. // [self.tableView reloadData];
  203. // }
  204. // } FailureBlock:^(NSError *error) {
  205. //
  206. // }];
  207. }
  208. //请求心愿单列表
  209. -(void)requestModel{
  210. self.listArr = [NSMutableArray array];
  211. self.listDeleteArr = [NSMutableArray array];
  212. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  213. [mDict setObject:@"user_wish" forKey:@"ctl"];
  214. [mDict setObject:@"wish_list" forKey:@"act"];
  215. //4-16 3.心愿单无效。
  216. [mDict setObject:_roomId forKey:@"room_id"];
  217. [[NetHttpsManager manager] POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  218. if ([responseJson toInt:@"status"] == 1)
  219. {
  220. NSArray *arr = [responseJson valueForKey:@"list"];
  221. if (arr.count > 0) {
  222. for (NSDictionary *dic in arr)
  223. {
  224. MGLiveWishModel *model = [MGLiveWishModel mj_objectWithKeyValues:dic];
  225. [self.listArr addObject:model];
  226. }
  227. }
  228. _titleL.text = [NSString stringWithFormat:ASLocalizedString(@"直播心愿单(%ld/3)"),self.listArr.count];
  229. [self.tableView reloadData];
  230. }
  231. } FailureBlock:^(NSError *error) {
  232. }];
  233. }
  234. #pragma mark - Show And Hide
  235. - (void)show:(UIView *)superView{
  236. [self requestModel];
  237. [superView addSubview:self.shadowView];
  238. [superView addSubview:self];
  239. [UIView animateWithDuration:0.25 animations:^{
  240. self.shadowView.alpha = 1;
  241. self.y = kScreenH / 2;
  242. }];
  243. }
  244. - (void)hide{
  245. if (self.clickHideLiveWishBlock) {
  246. self.clickHideLiveWishBlock();
  247. }
  248. [UIView animateWithDuration:0.25 animations:^{
  249. self.shadowView.alpha = 1;
  250. self.y = kScreenH;
  251. } completion:^(BOOL finished) {
  252. [self.shadowView removeFromSuperview];
  253. [self removeFromSuperview];
  254. }];
  255. }
  256. -(UITableView *)tableView{
  257. if (!_tableView) {
  258. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.titleL.bottom, kScreenW, kScreenH / 2 - self.titleL.bottom) style:UITableViewStylePlain];
  259. _tableView.delegate = self;
  260. _tableView.dataSource = self;
  261. [_tableView registerClass:[MGLiveWishCell class] forCellReuseIdentifier:@"MGLiveWishCell"];
  262. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  263. _tableView.tableFooterView = [UIView new];
  264. }
  265. return _tableView;
  266. }
  267. - (UIView *)shadowView{
  268. if (!_shadowView) {
  269. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  270. _shadowView.backgroundColor = kClearColor;
  271. _shadowView.userInteractionEnabled = YES;
  272. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  273. [_shadowView addGestureRecognizer:tap];
  274. }
  275. return _shadowView;
  276. }
  277. //心愿单列表
  278. -(void)clickWish:(UIButton *)sender{
  279. if (self.clickLiveWishBlock) {
  280. self.clickLiveWishBlock(MGWISHTYPE_LIST);
  281. }
  282. }
  283. -(void)clickAddWish:(UIButton *)sender{
  284. if (self.clickLiveWishBlock) {
  285. self.clickLiveWishBlock(MGWISHTYPE_ADD);
  286. }
  287. }
  288. @end