ASGiftCardListViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // ASGiftCardListViewController.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/11/2.
  6. //
  7. #import "ASGiftCardListViewController.h"
  8. #import "ASGiftCardTableView.h"
  9. #import "ASGiftCardListViewModel.h"
  10. @interface ASGiftCardListViewController ()<UIScrollViewDelegate>
  11. @property (nonatomic, strong) ASGiftCardListViewModel *vm;
  12. @property (nonatomic, assign) NSInteger unUsePage;
  13. @property (nonatomic, assign) NSInteger cantUsePage;
  14. @property (nonatomic, strong) UIView *topBgV;
  15. @property (nonatomic, strong) UIView *bottomBgV;
  16. @property (nonatomic, strong) UIButton *availabelBt;
  17. @property (nonatomic, strong) UIButton *usedBt;
  18. @property (nonatomic, strong) UIScrollView *scrollV;
  19. @property (nonatomic, strong) ASGiftCardTableView *allTableV;
  20. @property (nonatomic, strong) ASGiftCardTableView *usedTableV;
  21. @property (nonatomic, strong) UIView *unUseTableHeadV;
  22. @property (nonatomic, strong) UILabel *unUseTipLb;
  23. @end
  24. @implementation ASGiftCardListViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.vm = [ASGiftCardListViewModel new];
  28. self.unUsePage = 1;
  29. self.cantUsePage = 1;
  30. [self loadSubVs];
  31. [self configSubVs];
  32. }
  33. - (void)viewWillAppear:(BOOL)animated {
  34. [super viewWillAppear:animated];
  35. [self checkEmpty];
  36. }
  37. - (void)checkEmpty {
  38. if (_allTableV.dataArr.count == 0 && self.availabelBt.isSelected) {
  39. [self showEmptyV: self.allTableV];
  40. } else if (_usedTableV.dataArr.count == 0 && self.usedBt.isSelected) {
  41. [self showEmptyV: self.usedTableV];
  42. } else {
  43. [self hiddenEmpty];
  44. }
  45. }
  46. // MARK: - nets
  47. - (void)getUnuseListData {
  48. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  49. __weak typeof(self) weakSelf = self;
  50. [self.vm getUnuseCardList:self.unUsePage com:^(BOOL hasNext, NSString * _Nonnull msg) {
  51. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  52. [weakSelf.allTableV.mj_header endRefreshing];
  53. if (hasNext) {
  54. [weakSelf.allTableV.mj_footer endRefreshing];
  55. } else {
  56. [weakSelf.allTableV.mj_footer endRefreshingWithNoMoreData];
  57. }
  58. weakSelf.allTableV.dataArr = weakSelf.vm.unUseCardList;
  59. [weakSelf checkEmpty];
  60. [weakSelf.allTableV reloadData];
  61. }];
  62. }
  63. - (void)getCantUseListData {
  64. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  65. __weak typeof(self) weakSelf = self;
  66. [self.vm getCantUseCardList:self.cantUsePage com:^(BOOL hasNext, NSString * _Nonnull msg) {
  67. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  68. [weakSelf.usedTableV.mj_header endRefreshing];
  69. if (hasNext) {
  70. [weakSelf.usedTableV.mj_footer endRefreshing];
  71. } else {
  72. [weakSelf.usedTableV.mj_footer endRefreshingWithNoMoreData];
  73. }
  74. weakSelf.usedTableV.dataArr = weakSelf.vm.cantUseCardList;
  75. [weakSelf checkEmpty];
  76. [weakSelf.usedTableV reloadData];
  77. }];
  78. }
  79. // MARK: - loadSubVs
  80. - (void)configSubVs {
  81. self.view.backgroundColor = _E0FFF5;
  82. self.customNavBar.backgroundColor = _F0FFFA;
  83. self.titleStr = @"My Giftcard";
  84. __weak typeof(self) weakSelf = self;
  85. [self setNavRightSearch:^{
  86. }];
  87. self.allTableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  88. weakSelf.unUsePage = 1;
  89. [weakSelf getUnuseListData];
  90. }];
  91. self.allTableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  92. weakSelf.unUsePage += 1;
  93. [weakSelf getUnuseListData];
  94. }];
  95. self.usedTableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  96. weakSelf.cantUsePage = 1;
  97. [weakSelf getCantUseListData];
  98. }];
  99. self.usedTableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  100. weakSelf.cantUsePage += 1;
  101. [weakSelf getCantUseListData];
  102. }];
  103. [self.allTableV.mj_header beginRefreshing];
  104. [self.usedTableV.mj_header beginRefreshing];
  105. }
  106. - (void)loadSubVs {
  107. [self.view addSubview:self.topBgV];
  108. [self.view addSubview:self.bottomBgV];
  109. [self.topBgV addSubview:self.availabelBt];
  110. [self.topBgV addSubview:self.usedBt];
  111. [self.bottomBgV addSubview:self.scrollV];
  112. [self.scrollV addSubview:self.allTableV];
  113. [self.scrollV addSubview:self.usedTableV];
  114. [self.unUseTableHeadV addSubview:self.unUseTipLb];
  115. NSString *tipStr = @"Gift Card Will Be Applied Automatically,Go to Cart/CheckOut Page to Check.";
  116. CGRect rect = [tipStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-40, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:nil context:nil];
  117. self.unUseTableHeadV.frame = CGRectMake(0, 0, SCREEN_WIDTH, rect.size.height + 28);
  118. [self.unUseTipLb mas_makeConstraints:^(MASConstraintMaker *make) {
  119. make.top.equalTo(self.unUseTableHeadV).offset(8);
  120. make.leading.equalTo(self.unUseTableHeadV).offset(20);
  121. make.trailing.equalTo(self.unUseTableHeadV).offset(-20);
  122. make.height.equalTo(@(rect.size.height));
  123. }];
  124. self.allTableV.tableHeaderView = self.unUseTableHeadV;
  125. [self.topBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  126. make.top.equalTo(self.customNavBar.mas_bottom);
  127. make.leading.trailing.equalTo(self.view);
  128. make.height.equalTo(@60);
  129. }];
  130. [self.availabelBt mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.centerY.equalTo(self.topBgV);
  132. make.height.equalTo(@40);
  133. make.leading.equalTo(self.topBgV).offset(10);
  134. }];
  135. [self.usedBt mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.centerY.equalTo(self.availabelBt);
  137. make.height.equalTo(@40);
  138. make.leading.equalTo(self.availabelBt.mas_trailing).offset(10);
  139. make.trailing.equalTo(self.topBgV).offset(-10);
  140. make.width.equalTo(self.availabelBt);
  141. }];
  142. [self.bottomBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.top.equalTo(self.topBgV.mas_bottom);
  144. make.leading.trailing.bottom.equalTo(self.view);
  145. }];
  146. [self.scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
  147. make.top.equalTo(self.bottomBgV).offset(10);
  148. make.leading.trailing.bottom.equalTo(self.bottomBgV);
  149. }];
  150. [self.allTableV mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.top.leading.bottom.equalTo(self.scrollV);
  152. make.width.equalTo(self.bottomBgV);
  153. make.height.equalTo(self.bottomBgV).offset(-10);
  154. }];
  155. [self.usedTableV mas_makeConstraints:^(MASConstraintMaker *make) {
  156. make.leading.equalTo(self.allTableV.mas_trailing);
  157. make.top.trailing.bottom.equalTo(self.scrollV);
  158. make.width.equalTo(self.bottomBgV);
  159. make.height.equalTo(self.bottomBgV).offset(-10);
  160. }];
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. [UIView viewAddHorColorBg:self.bottomBgV colorArr:@[
  163. (id)_E0FFF5.CGColor,
  164. (id)Col_FFF.CGColor
  165. ] startP:CGPointMake(0.5, 0) endP:CGPointMake(0.5, 1)];
  166. [self.bottomBgV bringSubviewToFront:self.scrollV];
  167. });
  168. }
  169. // MARK: - bt Actions
  170. - (void)topBtAction:(UIButton *)bt {
  171. if (bt == self.usedBt) {
  172. self.availabelBt.selected = false;
  173. self.usedBt.selected = true;
  174. [self.scrollV setContentOffset:CGPointMake(KScreenWidth, 0) animated:true];
  175. } else {
  176. self.availabelBt.selected = true;
  177. self.usedBt.selected = false;
  178. [self.scrollV setContentOffset:CGPointMake(0, 0) animated:true];
  179. }
  180. [self setBtStatus:self.availabelBt];
  181. [self setBtStatus:self.usedBt];
  182. [self checkEmpty];
  183. }
  184. - (void)setBtStatus:(UIButton *)bt {
  185. bt.backgroundColor = bt.isSelected ? _113632 : Col_FFF;
  186. bt.titleLabel.font = [UIFont fontWithName:bt.isSelected ? Rob_Bold : Rob_Regular size:16];
  187. }
  188. // MARK: - subvs
  189. - (UIView *)topBgV {
  190. if (!_topBgV) {
  191. UIView *v = [UIView baseV];
  192. v.backgroundColor = _E0FFF5;
  193. _topBgV = v;
  194. }
  195. return _topBgV;
  196. }
  197. - (UIView *)bottomBgV{
  198. if (!_bottomBgV) {
  199. UIView *v = [UIView baseV];
  200. v.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
  201. v.backgroundColor = UIColor.clearColor;
  202. _bottomBgV = v;
  203. }
  204. return _bottomBgV;
  205. }
  206. - (UIButton *)availabelBt {
  207. if (!_availabelBt) {
  208. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  209. [bt addTarget:self action:@selector(topBtAction:) forControlEvents:UIControlEventTouchUpInside];
  210. bt.selected = true;
  211. bt.backgroundColor = _113632;
  212. bt.titleLabel.font = [UIFont fontWithName:Rob_Bold size:16];
  213. [bt setTitleColor:Col_000 forState:UIControlStateNormal];
  214. bt.layer.cornerRadius = 4;
  215. bt.layer.masksToBounds = true;
  216. [bt setTitleColor:Col_FFF forState:UIControlStateSelected];
  217. [bt setTitle:@"AVAILABLE" forState:UIControlStateNormal];
  218. _availabelBt = bt;
  219. }
  220. return _availabelBt;
  221. }
  222. - (UIButton *)usedBt {
  223. if (!_usedBt) {
  224. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  225. [bt addTarget:self action:@selector(topBtAction:) forControlEvents:UIControlEventTouchUpInside];
  226. bt.backgroundColor = Col_FFF;
  227. bt.titleLabel.font = [UIFont fontWithName:Rob_Regular size:16];
  228. [bt setTitleColor:Col_000 forState:UIControlStateNormal];
  229. bt.layer.cornerRadius = 4;
  230. bt.layer.masksToBounds = true;
  231. [bt setTitleColor:Col_FFF forState:UIControlStateSelected];
  232. [bt setTitle:@"EXPIRED/USED" forState:UIControlStateNormal];
  233. _usedBt = bt;
  234. }
  235. return _usedBt;
  236. }
  237. - (UIScrollView *)scrollV {
  238. if (!_scrollV) {
  239. UIScrollView *v = [[UIScrollView alloc] init];
  240. v.showsVerticalScrollIndicator = false;
  241. v.showsHorizontalScrollIndicator = false;
  242. v.delegate = self;
  243. v.pagingEnabled = true;
  244. v.backgroundColor = UIColor.clearColor;
  245. v.alwaysBounceHorizontal = true;
  246. _scrollV = v;
  247. }
  248. return _scrollV;
  249. }
  250. - (ASGiftCardTableView *)allTableV {
  251. if (!_allTableV) {
  252. ASGiftCardTableView *v = [[ASGiftCardTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  253. _allTableV = v;
  254. }
  255. return _allTableV;
  256. }
  257. - (ASGiftCardTableView *)usedTableV {
  258. if (!_usedTableV) {
  259. ASGiftCardTableView *v = [[ASGiftCardTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  260. _usedTableV = v;
  261. }
  262. return _usedTableV;
  263. }
  264. - (UIView *)unUseTableHeadV {
  265. if (!_unUseTableHeadV) {
  266. UIView *v = [UIView baseV];
  267. v.backgroundColor = UIColor.clearColor;
  268. _unUseTableHeadV = v;
  269. }
  270. return _unUseTableHeadV;
  271. }
  272. - (UILabel *)unUseTipLb {
  273. if (!_unUseTipLb) {
  274. UILabel *lb = [UILabel baseLb];
  275. lb.numberOfLines = 0;
  276. lb.font = [UIFont fontWithName:Rob_Regular size:12];
  277. lb.textColor = Col_000;
  278. _unUseTipLb = lb;
  279. }
  280. return _unUseTipLb;
  281. }
  282. // MARK: - scrollview delegate
  283. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  284. NSInteger index = (NSInteger)(scrollView.contentOffset.x/KScreenWidth);
  285. self.availabelBt.selected = index == 0;
  286. self.usedBt.selected = index == 1;
  287. [self checkEmpty];
  288. [self setBtStatus:self.availabelBt];
  289. [self setBtStatus:self.usedBt];
  290. }
  291. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  292. NSInteger index = (NSInteger)(scrollView.contentOffset.x/KScreenWidth);
  293. self.availabelBt.selected = index == 0;
  294. self.usedBt.selected = index == 1;
  295. [self checkEmpty];
  296. [self setBtStatus:self.availabelBt];
  297. [self setBtStatus:self.usedBt];
  298. }
  299. @end