// // HomeViewController.m // iShop // // Created by Rui Zhang on 12/14/23. // #import "HomeViewController.h" #import "RANetwork.h" #import "HomeTableViewCellSlide.h" #import "HomeTableViewCellButtonBanner.h" #import "HomeTableViewCellBanner.h" #import "SliderPage.h" #import "FileCache.h" #import "ImageUtils.h" #import "ItemSearchViewController.h" #import "CatalogListViewController.h" #import "WebViewController.h" @interface HomeViewController () @end @implementation HomeViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispatch_async(dispatch_get_main_queue(), ^{ [RANetwork request_category:^(NSMutableDictionary *result) { NSDictionary* category=result; if([[category valueForKey:@"result"] intValue]==2) { RASingleton.sharedInstance.categoryMenu =category; } }]; }); return; } -(void) LoadData { dispatch_async(dispatch_get_main_queue(), ^{ if(self.isrefreshing) { return; } self.isrefreshing=true; DebugLog(@"HOMEVIEW BEGIN LOAD"); self.mum.hidden = false; [self.mum startAnimating]; self.mum.center = self.view.center; self.label_net_err.hidden=true; self.tableView.hidden=true; [RANetwork request_home:0 customid:0 completionHandler:^(NSMutableDictionary *result) { [self.mum stopAnimating]; NSMutableDictionary* data=result; self.content = data; [self.tableView reloadData]; self.tableView.hidden=false; if([[data valueForKey:@"result"] intValue]==2) { DebugLog(@"HOMEVIEW JSON LOADED!"); } else if([[data valueForKey:@"result"] intValue]==RESULT_NET_ERROR) { self.label_net_err.hidden=false; } else { [RAUtils message_box:@"Loading Home" message:[data valueForKey:@"err_msg"] completion:nil]; } self.isrefreshing=false; ; }]; return; }); } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ -(void) dealloc { DebugLog(@"HOME VIEWCONTROLLER DEALLOC"); } -(void) refresh_on_login { // do not refresh on login; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // [[self navigationController] setNavigationBarHidden:YES animated:NO]; // if(!CGSizeEqualToSize(self.ori_size, [[UIScreen mainScreen] bounds].size)) // { // [self.tableView reloadData]; // } // self.content = self.bak_content; // [self reload_container_getdata:false]; } -(void) viewWillDisappear:(BOOL)animated { // self.ori_size=[[UIScreen mainScreen] bounds].size; // [[NSNotificationCenter defaultCenter] postNotificationName:CYCLESCROLL_STOPTIMMER object:nil]; // self.bak_content = self.content; // self.content= nil; //// self.homeTable re // [self.homeTable reloadData]; } -(void)manually_refresh { UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200]; if(self.isrefreshing) { [reF endRefreshing]; return; } reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"]; [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1]; // DebugLog(@"refresh!!!!!!!!"); } -(void) ReloadData { UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200]; [reF endRefreshing]; reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; [self LoadData]; } // //- (void)viewDidLoad //{ // [super viewDidLoad]; // // self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor; // self.label_net_err.layer.borderWidth = 2.0; // self.label_net_err.layer.cornerRadius=15; // self.label_net_err.layer.masksToBounds=true; // // // // // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)]; // // tap.minimumPressDuration = 0.8; //定义按的时间 // [self.label_net_err addGestureRecognizer:tap]; // // // // [[self navigationController] setNavigationBarHidden:YES animated:NO]; // // UIRefreshControl *ref = [[UIRefreshControl alloc]init]; // ref.tag = 200 ; // ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; // ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7]; // // ref.hidden = true; // [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged]; // [self.homeTable addSubview:ref]; // // // // // // //// //// RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.treeContainer.frame]; //// //// treeView.delegate = self; //// treeView.dataSource = self; //// treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine; //// //// [treeView reloadData]; //// // [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row //// [treeView setBackgroundColor:UIColorFromRGB(0xF7F7F7)]; //// //// self.treeView = treeView; //// [self.treeContainer addSubview:treeView]; // // // UIApplication * app = [UIApplication sharedApplication]; // // // //} -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // CGRect rt = self.treeContainer.bounds; // CGRect rt1 = self.treeContainer.frame; // self.treeView.frame = self.treeContainer.bounds; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id _Nonnull context) { // what ever you want to prepare } completion:^(id _Nonnull context) { [self.tableView reloadData]; }]; } #pragma mark - Table view data source - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath { if([cell isKindOfClass:[HomeTableViewCellSlide class]]) { HomeTableViewCellSlide* slidecell=(HomeTableViewCellSlide*)cell; [slidecell.imgScroll stopTimmer]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]]; NSString* type = [view_json valueForKeyPath:@"type"]; if([type isEqualToString:@"slideview"]) { int i_width = [[view_json valueForKey:@"width"]intValue]; int i_height = [[view_json valueForKey:@"height"]intValue]; if(i_width<=0||i_height<=0) { i_width=981; i_height=512; } int ui_height = (tableView.frame.size.width * i_height)/i_width; return ui_height; } else if([type isEqualToString:@"topicview"]) { return 310; } else if([type isEqualToString:@"banner"]) { int i_width = 498; int i_height = 197; int ui_height = (tableView.frame.size.width * i_height)/i_width; return ui_height; } else if([type isEqualToString:@"button_banner"]) { int i_width = [[view_json valueForKey:@"width"]intValue]; int i_height = [[view_json valueForKey:@"height"]intValue]; float ui_height = (tableView.frame.size.width * i_height)/(float)i_width; return ui_height; } return 44; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //-----debug!! // if(self.content !=nil) // return 2; return [[self.content valueForKeyPath:@"count"]intValue]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // // // // // // // // // // NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]]; // // // NSString* type = [view_json valueForKeyPath:@"type"]; // // NSString* action = [view_json valueForKeyPath:@"action"]; // // if([action isEqualToString:@"itemSearch"]) // { // // ItemSearchViewController* isvc =[ [UIStoryboard storyboardWithName:@"itemsearch" bundle:nil] instantiateViewControllerWithIdentifier:@"ItemSearchViewController"]; // NSString* covertype = [view_json valueForKeyPath:@"covertype"]; // // // // //// isvc.default_filter = nil; // isvc.covertype = covertype; //// isvc.saved_covertype = coverttype; // // // // isvc.p_available = nil; // isvc.p_price = nil; // isvc.p_QTY = nil; // isvc.p_bestseller = nil; // //// [ActiveViewController Notify:@"ItemSearchViewController" Message:RA_NOTIFICATION_RELOAD_DATA]; // // // [[self navigationController] pushViewController:isvc animated:true]; //// [ setNavigationBarHidden:YES animated:NO]; // // // //// //// UIApplication * app = [UIApplication sharedApplication]; //// AppDelegate *appDelegate = (AppDelegate *)[app delegate]; //// MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; //// //// [main_vc switchToItemSearch:covertype]; // } // } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; NSDictionary* view_json = [self.content objectForKey:[NSString stringWithFormat:@"view_%ld",(long)indexPath.section ]]; NSString* type = [view_json valueForKeyPath:@"type"]; if([type isEqualToString:@"slideview"]) { NSString *CellIdentifier = @"HomeTableViewCellSlide"; cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell; [slidecell.imgScroll stopTimmer]; [slidecell.imgScroll removeFromSuperview]; // for(UIView* v in cell.contentView.subviews) // { // [v removeFromSuperview]; // // } // int i_width = 981; // int i_height = 512; //如果模版有高度就用模版设置值,没有则用默认值。 int i_width = [[view_json valueForKey:@"width"]intValue]; int i_height = [[view_json valueForKey:@"height"]intValue]; if(i_width<=0||i_height<=0) { i_width=981; i_height=512; } int ui_height = (tableView.frame.size.width * i_height)/i_width; // HomeTableViewCellSlide* slidecell = (HomeTableViewCellSlide*)cell; // imgScroll.tag = 1394; // // // for (UIView *v in cell.contentView.subviews) { // if(v.tag==1394) // [v removeFromSuperview]; // } CycleScrollView *imgScroll = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, ui_height) animationDuration:5]; imgScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; int slide_item_count = [[view_json valueForKey:@"count"] intValue]; //imgScroll.frame =CGRectMake(0, 5, width, 300); [imgScroll reset]; // [imgScroll animationDuration:5]; imgScroll.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1]; for(int i = 0;i0) // // cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %@", [@(numberOfChildren) stringValue]]; // cell.textLabel.text = [((NSDictionary *)item) valueForKey:@"title"]; // cell.selectionStyle = UITableViewCellSelectionStyleNone; // // if (treeNodeInfo.treeDepthLevel == 0) { // cell.detailTextLabel.textColor = [UIColor blackColor]; // } // // return cell; //} // //- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item //{ // if (item == nil) { // // UIApplication * app = [UIApplication sharedApplication]; // AppDelegate *appDelegate = (AppDelegate *)[app delegate]; // return [[appDelegate.categoryMenu valueForKey:@"count" ] intValue]; // // return [self.data count]; // } // // NSDictionary* data = item; // return [[data valueForKey:@"count"] intValue]; // // RADataObject *data = item; // // return [data.children count]; //} // //- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item //{ // NSDictionary *data = item; // // if (item == nil) { // // UIApplication * app = [UIApplication sharedApplication]; // AppDelegate *appDelegate = (AppDelegate *)[app delegate]; // NSDictionary * ret =[appDelegate.categoryMenu objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index] ]; // return ret; // } // // return[data objectForKey:[NSString stringWithFormat:@"category_%ld",(long)index]]; // /* // // RADataObject *data = item; // if (item == nil) { // return [self.data objectAtIndex:index]; // } // // return [data.children objectAtIndex:index]; // */ //} #pragma mark - Topic cell delegate -(void) TopicItemClicked:(NSString*) product_id category:(NSString*) category { // // DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ]; // // dvc // // dvc.product_id=product_id; // dvc.category_id=category; // dvc.ispush=true; // [dvc reload]; // [self.navigationController pushViewController:dvc animated:true]; } //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //{ // // MainViewController* pvc = (MainViewController*)self.parentViewController; // // [pvc categoryMenuSelected:indexPath.row]; //} - (void)reRefreshView { [self.tableView reloadData]; } #pragma mark - RA_NOTIFICAITON -(void) refresh_ui { [self.tableView reloadData]; } -(void) reload_data { [self LoadData]; } @end