No Description

FKPriceWarnEditController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // FKPriceWarnController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/5/25.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKPriceWarnEditController.h"
  9. #import "FKPriceWarnInfoCell.h"
  10. #import "FKMessageWarnCell.h"
  11. #import "FKPhoneNumberCell.h"
  12. #import "FKPriceWarnConfirmCell.h"
  13. #import "ZYKeyboardUtil.h"
  14. #import "FKPriceWarnRequest.h"
  15. static NSString *FK_INFO_CELL_IDENTIFY = @"FK_INFO_CELL_IDENTIFY";
  16. static NSString *FK_MESSAGE_WARN_CELL_IDENTIFY = @"FK_MESSAGE_WARN_CELL_IDENTIFY";
  17. static NSString *FK_PHONE_NUMBER_CELL_IDENTIFY = @"FK_PHONE_NUMBER_CELL_IDENTIFY";
  18. static NSString *FK_CONFIRM_CELL_IDENTIFY = @"FK_CONFIRM_CELL_IDENTIFY";
  19. typedef NS_ENUM(NSUInteger, FKPriceInfoType) {
  20. FKPriceInfoTypeSpec = 0,
  21. FKPriceInfoTypePrice,
  22. FKPriceInfoTypeSetPrice,
  23. };
  24. #define K_CREAT_PRICE_WARN_REQ 2002
  25. @interface FKPriceWarnEditController () <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate, UITextFieldDelegate, FLNetworkDelegate>
  26. @property (nonatomic, strong) FKPriceWarnItem *priceWarnItem;
  27. @property (nonatomic, strong) UITableView *tableView;
  28. @property (nonatomic, strong) ZYKeyboardUtil *keyboardUtil;
  29. @property (nonatomic, strong) UITextField *textField;
  30. @property (nonatomic, assign) BOOL didShowKeyBoard;
  31. @end
  32. @implementation FKPriceWarnEditController
  33. - (instancetype)initWithPriceWarnItem:(FKPriceWarnItem *)priceWarnItem{
  34. if (self = [super init]){
  35. self.priceWarnItem = priceWarnItem;
  36. }
  37. return self;
  38. }
  39. - (void)viewDidLoad{
  40. [super viewDidLoad];
  41. [self.view addSubview:self.tableView];
  42. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.edges.insets(UIEdgeInsetsZero);
  44. }];
  45. [self configKeyBoardUtil];
  46. [self addTapGesture];
  47. }
  48. - (void)viewWillAppear:(BOOL)animated{
  49. [super viewWillAppear:animated];
  50. [self.navigationController setNavigationBarHidden:NO animated:YES];
  51. self.navigationItem.title = @"降价提醒";
  52. }
  53. - (void)viewDidAppear:(BOOL)animated{
  54. [super viewDidAppear:animated];
  55. if (!self.didShowKeyBoard){
  56. // 防止重复
  57. [self.textField becomeFirstResponder];
  58. self.didShowKeyBoard = YES;
  59. }
  60. }
  61. #pragma mark - response
  62. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
  63. [self.hudView hide:NO];
  64. if (header.code.intValue == RESPONSE_MSG_NORMAL){
  65. if (identify == K_CREAT_PRICE_WARN_REQ){
  66. [FLProgressHUDHelper showText:@"设置降价提醒成功" inView:self.view];
  67. WeakSelf(weakSelf);
  68. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  69. [weakSelf.navigationController popViewControllerAnimated:YES];
  70. });
  71. }
  72. }else{
  73. [FLProgressHUDHelper showText:header.msg inView:self.view];
  74. }
  75. }
  76. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{
  77. [self.hudView hide:NO];
  78. [FLProgressHUDHelper showText:header.msg inView:self.view];
  79. }
  80. #pragma mark - tableView dataSource
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  82. return 5;
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  85. if (section == 3) {
  86. if ([self.priceWarnItem didOpenMessageWarn]) return 2;
  87. }
  88. return 1;
  89. }
  90. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  91. if (indexPath.section <= 2){
  92. FKPriceWarnInfoCell *infoCell = [tableView dequeueReusableCellWithIdentifier:FK_INFO_CELL_IDENTIFY];
  93. [self configInfoCell:infoCell indexPath:indexPath];
  94. return infoCell;
  95. } else if (indexPath.section == 3){
  96. if (indexPath.row == 0){
  97. FKMessageWarnCell *messageCell = [tableView dequeueReusableCellWithIdentifier:FK_MESSAGE_WARN_CELL_IDENTIFY];
  98. [messageCell.actSwitch setOn:[self.priceWarnItem didOpenMessageWarn]];
  99. [messageCell.actSwitch addTarget:self
  100. action:@selector(switchChange:)
  101. forControlEvents:UIControlEventValueChanged];
  102. return messageCell;
  103. }else if (indexPath.row == 1){
  104. FKPhoneNumberCell *phoneCell = [tableView dequeueReusableCellWithIdentifier:FK_PHONE_NUMBER_CELL_IDENTIFY];
  105. phoneCell.titleLabel.text = [self getUserPhoneNum];
  106. return phoneCell;
  107. }
  108. } else if (indexPath.section == 4){
  109. FKPriceWarnConfirmCell *confirmCell = [tableView dequeueReusableCellWithIdentifier:FK_CONFIRM_CELL_IDENTIFY];
  110. [confirmCell.confirmBtn addTarget:self action:@selector(clickConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
  111. return confirmCell;
  112. }
  113. return nil;
  114. }
  115. - (void)configInfoCell:(FKPriceWarnInfoCell *)infoCell indexPath:(NSIndexPath *)indexPath{
  116. if (indexPath.section == 0){
  117. infoCell.titleLabel.text = @"已选规格:";
  118. infoCell.textField.text = self.priceWarnItem.specName;
  119. infoCell.textField.userInteractionEnabled = NO;
  120. infoCell.textField.textColor = UIColorFromRGB(0xff624a);
  121. }else if (indexPath.section == 1){
  122. infoCell.titleLabel.text = @"当前价格:";
  123. infoCell.textField.text = [FLStringHelper convertFenToRMBmoneyString:self.priceWarnItem.currentPrice];
  124. infoCell.textField.userInteractionEnabled = NO;
  125. infoCell.textField.textColor = UIColorFromRGB(0xff624a);
  126. }else if (indexPath.section == 2){
  127. infoCell.titleLabel.text = @"期望价格:¥ ";
  128. infoCell.textField.userInteractionEnabled = YES;
  129. infoCell.textField.text = [FLStringHelper convertFenToYuan:self.priceWarnItem.expectPrice];
  130. infoCell.textField.delegate = self;
  131. infoCell.textField.placeholder = @"低于此价格会通知您";
  132. infoCell.textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  133. self.textField = infoCell.textField;
  134. }
  135. }
  136. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  137. if (indexPath.section == 4) return 80.0f;
  138. return 45.0f;
  139. }
  140. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  141. if (section == 0){
  142. if (UISCREENWIDTH <= 320) return 70;
  143. return 40;
  144. }
  145. if (section == 4) return 30;
  146. return 0;
  147. }
  148. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  149. if (section == 3) return CGFLOAT_MIN;
  150. return 10.0f;
  151. }
  152. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  153. if (section == 0){
  154. return [self createHeaderForUp:YES];
  155. }else if (section == 4){
  156. return [self createHeaderForUp:NO];
  157. }
  158. return nil;
  159. }
  160. #pragma mark - textField delegate
  161. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  162. [self.view endEditing:YES];
  163. return YES;
  164. }
  165. - (void)textFieldDidBeginEditing:(UITextField *)textField{
  166. [self.textField performSelector:@selector(selectAll:) withObject:textField];
  167. }
  168. #pragma mark - alertView delegate
  169. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  170. if (buttonIndex == 1){
  171. // 开启
  172. NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  173. [[UIApplication sharedApplication] openURL:url];
  174. }
  175. }
  176. #pragma mark - method
  177. - (UIView *)createHeaderForUp:(BOOL)up{
  178. UILabel *titleLabel = [[UILabel alloc]init];
  179. titleLabel.textColor = UIColorFromRGB(0x666666);
  180. titleLabel.font = [UIFont systemFontOfSize:12];
  181. UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, 0)];
  182. [contentView addSubview:titleLabel];
  183. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  184. make.left.equalTo(contentView).offset(15);
  185. make.right.equalTo(contentView).offset(- 15);
  186. make.centerY.equalTo(contentView);
  187. }];
  188. if (up){
  189. titleLabel.text = @"一旦此商品发生降价,您将收到APP推送(需开启)及短信通知";
  190. if (UISCREENWIDTH <= 320) titleLabel.text = @"一旦此商品发生降价,您将收到APP推送消息(需开启)及短信通知";
  191. titleLabel.numberOfLines = 0;
  192. }else{
  193. titleLabel.text = @"通知栏也会通知您哦";
  194. }
  195. return contentView;
  196. }
  197. - (NSString *)getUserPhoneNum{
  198. NSString *phoneNum = [FKUserManager sharedManager].user.mobile;
  199. if (phoneNum.length < 7){
  200. return phoneNum;
  201. }
  202. return [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
  203. }
  204. - (void)configKeyBoardUtil{
  205. WeakSelf(weakSelf);
  206. [self.keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) {
  207. [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.textField, nil];
  208. }];
  209. [self.keyboardUtil setAnimateWhenKeyboardDisappearBlock:^(CGFloat keyboardHeight) {
  210. [weakSelf checkInputRes];
  211. }];
  212. }
  213. - (void)addTapGesture{
  214. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTapGesture:)];
  215. [self.view addGestureRecognizer:tap];
  216. }
  217. - (CGFloat)checkInputRes{
  218. CGFloat value = [self.textField.text floatValue];
  219. if (value <= 0){
  220. self.textField.text = @"";
  221. }
  222. return self.textField.text.floatValue;
  223. }
  224. - (NSInteger)getCurrentNotifyType{
  225. for (FKMessageWarnCell *cell in self.tableView.visibleCells) {
  226. if ([cell isKindOfClass:[FKMessageWarnCell class]]){
  227. return cell.actSwitch.isOn ? kNotifyTypeBoth : kNotifyTypePush;
  228. }
  229. }
  230. return kNotifyTypePush;
  231. }
  232. #pragma mark - action
  233. - (void)switchChange:(UISwitch *)sender{
  234. if (sender.isOn){
  235. self.priceWarnItem.notifyType = kNotifyTypeBoth;
  236. }else{
  237. self.priceWarnItem.notifyType = kNotifyTypePush;
  238. }
  239. [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationFade];
  240. }
  241. - (void)clickConfirmBtn{
  242. CGFloat currentPrice = [FLStringHelper convertFenStringToYuanValue:self.priceWarnItem.currentPrice];
  243. if ([self checkInputRes] <= 0) {
  244. [FLProgressHUDHelper showText:@"请输入期望价格" inView:self.view];
  245. return;
  246. }else if ([self checkInputRes] > currentPrice){
  247. [FLProgressHUDHelper showText:@"期望价格不得高于当前价格" inView:self.view];
  248. return;
  249. }
  250. // 判断用户是否开启push通知
  251. UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
  252. if (!setting.types){
  253. // 未开启
  254. if (![[NSUserDefaults standardUserDefaults] boolForKey:HAS_SHOW_NOTIFY_OPEN_WARN]){
  255. UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
  256. message:@"您还没有开启“通知”可能无法及时收到降价通知哦!"
  257. delegate:self
  258. cancelButtonTitle:@"不开启"
  259. otherButtonTitles:@"马上开启", nil];
  260. [alertView show];
  261. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:HAS_SHOW_NOTIFY_OPEN_WARN];
  262. [[NSUserDefaults standardUserDefaults] synchronize];
  263. return;
  264. }
  265. }
  266. NSInteger warnPrice = [self checkInputRes] * 100;
  267. [FKPriceWarnRequest reqPriceWarnWithGoodsId:self.priceWarnItem.goodsID
  268. expectPrice:warnPrice
  269. notifyType:[self getCurrentNotifyType]
  270. identify:K_CREAT_PRICE_WARN_REQ
  271. delegate:self];
  272. }
  273. - (void)clickTapGesture:(UIGestureRecognizer *)gesture{
  274. [self.view endEditing:YES];
  275. }
  276. #pragma mark - property
  277. - (UITableView *)tableView {
  278. if (_tableView == nil) {
  279. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  280. _tableView.dataSource = self;
  281. _tableView.delegate = self;
  282. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  283. _tableView.sectionHeaderHeight = 0;
  284. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  285. [_tableView registerClass:[FKPriceWarnInfoCell class] forCellReuseIdentifier:FK_INFO_CELL_IDENTIFY];
  286. [_tableView registerClass:[FKMessageWarnCell class] forCellReuseIdentifier:FK_MESSAGE_WARN_CELL_IDENTIFY];
  287. [_tableView registerClass:[FKPhoneNumberCell class] forCellReuseIdentifier:FK_PHONE_NUMBER_CELL_IDENTIFY];
  288. [_tableView registerClass:[FKPriceWarnConfirmCell class] forCellReuseIdentifier:FK_CONFIRM_CELL_IDENTIFY];
  289. }
  290. return _tableView;
  291. }
  292. - (ZYKeyboardUtil *)keyboardUtil{
  293. if (_keyboardUtil == nil) {
  294. _keyboardUtil = [[ZYKeyboardUtil alloc]init];
  295. }
  296. return _keyboardUtil;
  297. }
  298. @end