123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- //
- // FKPriceWarnController.m
- // FirstLink
- //
- // Created by jack on 16/5/25.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKPriceWarnEditController.h"
- #import "FKPriceWarnInfoCell.h"
- #import "FKMessageWarnCell.h"
- #import "FKPhoneNumberCell.h"
- #import "FKPriceWarnConfirmCell.h"
- #import "ZYKeyboardUtil.h"
- #import "FKPriceWarnRequest.h"
- static NSString *FK_INFO_CELL_IDENTIFY = @"FK_INFO_CELL_IDENTIFY";
- static NSString *FK_MESSAGE_WARN_CELL_IDENTIFY = @"FK_MESSAGE_WARN_CELL_IDENTIFY";
- static NSString *FK_PHONE_NUMBER_CELL_IDENTIFY = @"FK_PHONE_NUMBER_CELL_IDENTIFY";
- static NSString *FK_CONFIRM_CELL_IDENTIFY = @"FK_CONFIRM_CELL_IDENTIFY";
- typedef NS_ENUM(NSUInteger, FKPriceInfoType) {
- FKPriceInfoTypeSpec = 0,
- FKPriceInfoTypePrice,
- FKPriceInfoTypeSetPrice,
- };
- #define K_CREAT_PRICE_WARN_REQ 2002
- @interface FKPriceWarnEditController () <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate, UITextFieldDelegate, FLNetworkDelegate>
- @property (nonatomic, strong) FKPriceWarnItem *priceWarnItem;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) ZYKeyboardUtil *keyboardUtil;
- @property (nonatomic, strong) UITextField *textField;
- @property (nonatomic, assign) BOOL didShowKeyBoard;
- @end
- @implementation FKPriceWarnEditController
- - (instancetype)initWithPriceWarnItem:(FKPriceWarnItem *)priceWarnItem{
- if (self = [super init]){
- self.priceWarnItem = priceWarnItem;
- }
- return self;
- }
- - (void)viewDidLoad{
- [super viewDidLoad];
-
- [self.view addSubview:self.tableView];
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
-
- [self configKeyBoardUtil];
- [self addTapGesture];
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- self.navigationItem.title = @"降价提醒";
- }
- - (void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- if (!self.didShowKeyBoard){
- // 防止重复
- [self.textField becomeFirstResponder];
- self.didShowKeyBoard = YES;
- }
- }
- #pragma mark - response
- - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{
-
- [self.hudView hide:NO];
-
- if (header.code.intValue == RESPONSE_MSG_NORMAL){
- if (identify == K_CREAT_PRICE_WARN_REQ){
- [FLProgressHUDHelper showText:@"设置降价提醒成功" inView:self.view];
-
- WeakSelf(weakSelf);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [weakSelf.navigationController popViewControllerAnimated:YES];
- });
- }
- }else{
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- }
- - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{
- [self.hudView hide:NO];
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- #pragma mark - tableView dataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 5;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- if (section == 3) {
- if ([self.priceWarnItem didOpenMessageWarn]) return 2;
- }
- return 1;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- if (indexPath.section <= 2){
- FKPriceWarnInfoCell *infoCell = [tableView dequeueReusableCellWithIdentifier:FK_INFO_CELL_IDENTIFY];
- [self configInfoCell:infoCell indexPath:indexPath];
- return infoCell;
- } else if (indexPath.section == 3){
- if (indexPath.row == 0){
- FKMessageWarnCell *messageCell = [tableView dequeueReusableCellWithIdentifier:FK_MESSAGE_WARN_CELL_IDENTIFY];
- [messageCell.actSwitch setOn:[self.priceWarnItem didOpenMessageWarn]];
- [messageCell.actSwitch addTarget:self
- action:@selector(switchChange:)
- forControlEvents:UIControlEventValueChanged];
-
- return messageCell;
- }else if (indexPath.row == 1){
- FKPhoneNumberCell *phoneCell = [tableView dequeueReusableCellWithIdentifier:FK_PHONE_NUMBER_CELL_IDENTIFY];
- phoneCell.titleLabel.text = [self getUserPhoneNum];
- return phoneCell;
- }
- } else if (indexPath.section == 4){
- FKPriceWarnConfirmCell *confirmCell = [tableView dequeueReusableCellWithIdentifier:FK_CONFIRM_CELL_IDENTIFY];
- [confirmCell.confirmBtn addTarget:self action:@selector(clickConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
- return confirmCell;
- }
- return nil;
- }
- - (void)configInfoCell:(FKPriceWarnInfoCell *)infoCell indexPath:(NSIndexPath *)indexPath{
- if (indexPath.section == 0){
- infoCell.titleLabel.text = @"已选规格:";
- infoCell.textField.text = self.priceWarnItem.specName;
- infoCell.textField.userInteractionEnabled = NO;
- infoCell.textField.textColor = UIColorFromRGB(0xff624a);
- }else if (indexPath.section == 1){
- infoCell.titleLabel.text = @"当前价格:";
- infoCell.textField.text = [FLStringHelper convertFenToRMBmoneyString:self.priceWarnItem.currentPrice];
- infoCell.textField.userInteractionEnabled = NO;
- infoCell.textField.textColor = UIColorFromRGB(0xff624a);
- }else if (indexPath.section == 2){
- infoCell.titleLabel.text = @"期望价格:¥ ";
- infoCell.textField.userInteractionEnabled = YES;
- infoCell.textField.text = [FLStringHelper convertFenToYuan:self.priceWarnItem.expectPrice];
- infoCell.textField.delegate = self;
- infoCell.textField.placeholder = @"低于此价格会通知您";
- infoCell.textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
- self.textField = infoCell.textField;
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- if (indexPath.section == 4) return 80.0f;
- return 45.0f;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
-
- if (section == 0){
- if (UISCREENWIDTH <= 320) return 70;
- return 40;
- }
- if (section == 4) return 30;
- return 0;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
- if (section == 3) return CGFLOAT_MIN;
- return 10.0f;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
-
- if (section == 0){
- return [self createHeaderForUp:YES];
- }else if (section == 4){
- return [self createHeaderForUp:NO];
- }
- return nil;
- }
- #pragma mark - textField delegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField{
- [self.view endEditing:YES];
- return YES;
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField{
- [self.textField performSelector:@selector(selectAll:) withObject:textField];
- }
- #pragma mark - alertView delegate
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- if (buttonIndex == 1){
- // 开启
- NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- [[UIApplication sharedApplication] openURL:url];
- }
- }
- #pragma mark - method
- - (UIView *)createHeaderForUp:(BOOL)up{
-
- UILabel *titleLabel = [[UILabel alloc]init];
- titleLabel.textColor = UIColorFromRGB(0x666666);
- titleLabel.font = [UIFont systemFontOfSize:12];
-
- UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, 0)];
- [contentView addSubview:titleLabel];
- [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(contentView).offset(15);
- make.right.equalTo(contentView).offset(- 15);
- make.centerY.equalTo(contentView);
- }];
-
- if (up){
- titleLabel.text = @"一旦此商品发生降价,您将收到APP推送(需开启)及短信通知";
- if (UISCREENWIDTH <= 320) titleLabel.text = @"一旦此商品发生降价,您将收到APP推送消息(需开启)及短信通知";
- titleLabel.numberOfLines = 0;
- }else{
- titleLabel.text = @"通知栏也会通知您哦";
- }
-
-
- return contentView;
- }
- - (NSString *)getUserPhoneNum{
- NSString *phoneNum = [FKUserManager sharedManager].user.mobile;
- if (phoneNum.length < 7){
- return phoneNum;
- }
- return [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
- }
- - (void)configKeyBoardUtil{
- WeakSelf(weakSelf);
- [self.keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) {
- [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.textField, nil];
- }];
-
- [self.keyboardUtil setAnimateWhenKeyboardDisappearBlock:^(CGFloat keyboardHeight) {
- [weakSelf checkInputRes];
- }];
- }
- - (void)addTapGesture{
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTapGesture:)];
- [self.view addGestureRecognizer:tap];
- }
- - (CGFloat)checkInputRes{
- CGFloat value = [self.textField.text floatValue];
- if (value <= 0){
- self.textField.text = @"";
- }
- return self.textField.text.floatValue;
- }
- - (NSInteger)getCurrentNotifyType{
- for (FKMessageWarnCell *cell in self.tableView.visibleCells) {
- if ([cell isKindOfClass:[FKMessageWarnCell class]]){
- return cell.actSwitch.isOn ? kNotifyTypeBoth : kNotifyTypePush;
- }
- }
- return kNotifyTypePush;
- }
- #pragma mark - action
- - (void)switchChange:(UISwitch *)sender{
-
- if (sender.isOn){
- self.priceWarnItem.notifyType = kNotifyTypeBoth;
- }else{
- self.priceWarnItem.notifyType = kNotifyTypePush;
- }
-
- [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationFade];
- }
- - (void)clickConfirmBtn{
-
- CGFloat currentPrice = [FLStringHelper convertFenStringToYuanValue:self.priceWarnItem.currentPrice];
- if ([self checkInputRes] <= 0) {
- [FLProgressHUDHelper showText:@"请输入期望价格" inView:self.view];
- return;
- }else if ([self checkInputRes] > currentPrice){
- [FLProgressHUDHelper showText:@"期望价格不得高于当前价格" inView:self.view];
- return;
- }
-
- // 判断用户是否开启push通知
- UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
- if (!setting.types){
- // 未开启
- if (![[NSUserDefaults standardUserDefaults] boolForKey:HAS_SHOW_NOTIFY_OPEN_WARN]){
- UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil
- message:@"您还没有开启“通知”可能无法及时收到降价通知哦!"
- delegate:self
- cancelButtonTitle:@"不开启"
- otherButtonTitles:@"马上开启", nil];
- [alertView show];
-
- [[NSUserDefaults standardUserDefaults] setBool:YES forKey:HAS_SHOW_NOTIFY_OPEN_WARN];
- [[NSUserDefaults standardUserDefaults] synchronize];
- return;
- }
- }
-
- NSInteger warnPrice = [self checkInputRes] * 100;
-
- [FKPriceWarnRequest reqPriceWarnWithGoodsId:self.priceWarnItem.goodsID
- expectPrice:warnPrice
- notifyType:[self getCurrentNotifyType]
- identify:K_CREAT_PRICE_WARN_REQ
- delegate:self];
- }
- - (void)clickTapGesture:(UIGestureRecognizer *)gesture{
- [self.view endEditing:YES];
- }
- #pragma mark - property
- - (UITableView *)tableView {
- if (_tableView == nil) {
- _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.sectionHeaderHeight = 0;
- _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
-
- [_tableView registerClass:[FKPriceWarnInfoCell class] forCellReuseIdentifier:FK_INFO_CELL_IDENTIFY];
- [_tableView registerClass:[FKMessageWarnCell class] forCellReuseIdentifier:FK_MESSAGE_WARN_CELL_IDENTIFY];
- [_tableView registerClass:[FKPhoneNumberCell class] forCellReuseIdentifier:FK_PHONE_NUMBER_CELL_IDENTIFY];
- [_tableView registerClass:[FKPriceWarnConfirmCell class] forCellReuseIdentifier:FK_CONFIRM_CELL_IDENTIFY];
- }
- return _tableView;
- }
- - (ZYKeyboardUtil *)keyboardUtil{
- if (_keyboardUtil == nil) {
- _keyboardUtil = [[ZYKeyboardUtil alloc]init];
- }
- return _keyboardUtil;
- }
- @end
|