// // ScanCartItemCell.m // HMLG Scan Order // // Created by Rui Zhang on 3/21/22. // Copyright © 2022 United Software Applications, Inc. All rights reserved. // #import "ScanCartItemCell.h" #import "AppDelegate.h" #import "RANetwork.h" #import "CartViewController.h" #define NUMBERS @"0123456789\n" @implementation ScanCartItemCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } -(void) init_Stepper:(int) step max:(int) max min:(int)min value:(int)value { // [self.stepper becomeFirstResponder]; if(self.setpperQTY!=nil) { if(min<=0) min=1; if(step<=0) step=1; self.setpperQTY.minimumValue= min; self.setpperQTY.stepValue= step; self.setpperQTY.value= value; self.pre_val = value; [self.setpperQTY addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; // self.totalchange=0; } } - (void)valueChanged:(UIStepper *)sender { self.editQTY.text = [NSString stringWithFormat:@"%d",(int)sender.value ]; [self set_Count:(int)sender.value ]; // // [celldelegate stepClicked:(int)sender.value]; // -(void) stepClicked:(int) value; } - (IBAction)setpperChanged:(UIStepper *)sender { DebugLog(@"setpperChanged"); [self cancel_setQTY]; [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8]; } -(void) cancel_setQTY { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil]; } -(void) delayRun { // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil]; // // 启动 // [thread start]; [RANetwork request_update_cartqty:self.cart_id value:self.setpperQTY.value completionHandler:^(NSMutableDictionary *result) { NSDictionary* cart_json =result; if([[cart_json valueForKey:@"result"] intValue]==2) { if(self.onReturnQTY) self.onReturnQTY(self.setpperQTY.value,cart_json); // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) { // [self.delegate returnQTY:sender.value ]; // } } else { UIViewController* vc=[RAUtils getViewController :self]; [((CartViewController *)vc).itemListTable reloadData]; // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ; [RAUtils message_box:@"Change Model Count" message:cart_json[@"err_msg"] completion:nil]; } }]; } -(void) set_Count:(int) count { self.setpperQTY.value=count; // double dprice=0; // self.labelUnitPrice.text =[NSString stringWithFormat:@"%.2f",self.unit_price]; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // NSString* price = [NSString stringWithFormat:@"%.2f",self.unit_price*count*(1-self.discount/100.0) ]; // if(appDelegate.can_see_price&&appDelegate.price_hidden==false) // { // // } // else // { // price=nil; // } if(self.noprice&&self.unit_price==0.0) self.labelSubtotal.text =@"No Price"; else self.labelSubtotal.text =[RAConvertor currencyNumber:self.unit_price*count*(1-self.discount/100.0)];//price; self.editQTY.text =[NSString stringWithFormat:@"%d",count]; } #pragma mark textField delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ DebugLog(@"text:%@",textField.text); NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:filtered]; return canChange; } - (void)textFieldDidEndEditing:(UITextField *)textField { // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; int qty = [textField.text intValue]; if(qty==0) qty=self.setpperQTY.minimumValue; // NSMutableDictionary* item= self.modellist[indexPath.row]; // int qty = [textField.text intValue]; // int c=qty; int mpack= self.setpperQTY.stepValue; int m=c%mpack; if(m!=0) { [RAUtils message_box:@"Warrning" message:[NSString stringWithFormat:@"QTY must be a multiple of %d",mpack] completion:nil]; textField.text = [NSString stringWithFormat:@"%d",self.pre_val]; return; } // int c=qty; // int m=c%(int)(self.setpperQTY.stepValue); // if(m!=0&&appDelegate.alert_sold_in_quantities) // else { [self set_Count:qty]; [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) { NSDictionary* cart_json =result; if([[cart_json valueForKey:@"result"] intValue]==2) { // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) { // [self.delegate returnQTY:qty]; // } if(self.onReturnQTY) self.onReturnQTY(qty,cart_json); } else { UIViewController* vc=[RAUtils getViewController :self]; [((CartViewController *)vc).itemListTable reloadData]; // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ; [RAUtils message_box:@"Change Model Count" message:cart_json[@"err_msg"] completion:nil]; } }]; } } - (void)textFieldDidBeginEditing:(UITextField *)textField { NSString* text = textField.text; if(text.length==0) text=@"0"; self.pre_val = [textField.text intValue]; // self.last_edit = textField.text; } @end