admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
//  discoverView.m
//  BuWanVideo2.0
//
//  Created by weikou on 16/8/1.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "discoverView.h"
#import "CellOne.h"
#import "CellHead.h"
#import "WebControllerView.h"
#import "TWScontroller.h"
#import "StarsController.h"
 
#import "findGoodsTableViewCell.h"
#import "OnlySpecialController.h"
#import "IndividualStarController.h"
#import "discoverGoodsDetailViewController.h"
#import "allCommentsViewController.h"
#import "DisCoverTableViewCell.h"
#import "DisCoverADView.h"
//#import "GDTNativeAd.h"//广点通原生广告
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"
 
@interface discoverView()<UITableViewDataSource,UITableViewDelegate ,GDTNativeExpressAdDelegete>{
    UILabel * SDYname;   //专题换的文字
    int goodslist_page;  //请求发现页面的商品页数
//    GDTNativeAd *_nativeAd;     //原生广告实例
//    NSArray *nativeArray;//存储请求下来的原生广告信息
 
}
 
@property(nonatomic,strong) UITableView *mytableview;//发现页的主视图
@property(nonatomic,strong) NSMutableArray *dataSDY;//专题数据
@property(nonatomic,strong) NSMutableArray *dataGoodsList;//商品列表
@property (nonatomic, strong) NSArray *expressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
@end
 
@implementation discoverView
 
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        _mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
        _mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStyleGrouped];
        _mytableview.delegate = self;
        _mytableview.dataSource = self;
        _mytableview.backgroundColor = kGlobalBackgroundColor;
        _mytableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mytableview.showsVerticalScrollIndicator = NO;
        _mytableview.estimatedRowHeight = 0;
        _mytableview.estimatedSectionFooterHeight = 0;
        _mytableview.estimatedSectionHeaderHeight=0  ;
        //默认请求第一页的数据
        goodslist_page=1;
        _mytableview.rowHeight = KScreenW * 0.437 + 50;
        //注册cell
        [_mytableview registerNib:[UINib nibWithNibName:@"CellOne" bundle:nil] forCellReuseIdentifier:@"CellOne"];
        [_mytableview registerNib:[UINib nibWithNibName:@"findGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"findGoodsTableViewCell"];
        [_mytableview registerNib:[UINib nibWithNibName:@"DisCoverTableViewCell" bundle:nil] forCellReuseIdentifier:@"DisCoverTableViewCell"];
 
        
        _mytableview.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNetData)];
        
 
        _mytableview .mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadFoundHomeBanner];
        }];
        //将tableView添加到发现页面的视图
        [self addSubview:_mytableview];
        [self loadAd];
 
        //预加载数据
        [self PreLoad];
 
    }
    return self;
}
 
//懒加载发现页面的数据
-(void)PreLoad{
    //1.获取旧的发现页面轮播图页面
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //2.判断本地是否有轮播图数据,如果有数据就进行预加载
    if ([fileManager fileExistsAtPath:DISCOVERSDY]){
        _dataSDY=[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:DISCOVERSDY]];
        [_mytableview reloadData];
    }
    //3.请求发现页面的网络数据
    [self loadNetData];
}
 
/**
 加载网络数据
 */
-(void)loadNetData{
    goodslist_page=1;
    [self loadFoundHomeBanner];
    [self loadGoodsItemList];
//    [self loadAd];
}
 
/**
 请求轮播图数据
 */
-(void)loadFoundHomeBanner{
    [[YTHNetInterface startInterface] getFoundHomeBanner:[YTHsharedManger startManger].Uid WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
        if(isSuccessful){
            if (!_dataSDY) {
                _dataSDY=[[NSMutableArray alloc] initWithCapacity:0];
            }else{
                //清空之前的数据
                [_dataSDY removeAllObjects];
            }
            _dataSDY = [[result objectForKey:@"Data"] objectForKey:@"data"];
            [_mytableview.mj_footer endRefreshing];
            if (_dataSDY.count < 20) {
                [_mytableview.mj_footer endRefreshingWithNoMoreData];
            }
            NSLog(@"%@",_dataSDY);
            //存储获取到的数据
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_dataSDY];
            [data writeToFile:DISCOVERSDY atomically:YES];
            [_mytableview reloadData];
        }else{
            NSLog(@"%@",error);
        }
        [_mytableview.mj_header endRefreshing];
    }];
}
 
/**
 请求商品列表数据
 */
-(void)loadGoodsItemList{
    [[YTHNetInterface startInterface] getGoodsItemListWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withPage:[NSString stringWithFormat:@"%d",goodslist_page] withBlock:^(BOOL isSuccessful, id result, NSString *error) {
        if(isSuccessful){
            if (!_dataGoodsList) {
                _dataGoodsList=[[NSMutableArray alloc] initWithCapacity:0];
            }
            if(goodslist_page==1){
                [_dataGoodsList removeAllObjects];
            }
            NSArray *arr=[[result objectForKey:@"Data"] objectForKey:@"data"];
            [_mytableview.mj_header endRefreshing];
            [_mytableview.mj_footer endRefreshing];
            if ([arr count]>0) {
                ++goodslist_page;
                [_dataGoodsList addObjectsFromArray:arr];
                [_mytableview reloadData];
            }else{
                [_mytableview.mj_footer endRefreshingWithNoMoreData];
            }
            [self loadAd];
 
        }else{
            NSLog(@"%@",error);
            [_mytableview.mj_header endRefreshing];
            [_mytableview.mj_footer endRefreshing];
        }
 
    }];
}
 
/**
 点击收藏按钮
 
 @param sender 按钮
 */
-(void)clickCollectionBtn:(UIButton *)sender{
    if([sender isSelected]){
        sender.selected=NO;
        NSString *goodssId=[NSString stringWithFormat:@"%d",[[_dataGoodsList[sender.tag-100] objectForKey:@"Id"] intValue]];
        [[YTHNetInterface startInterface] cancelCollectWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withId:goodssId withBlock:^(BOOL isSuccessful, id result, NSString *error) {
            if (isSuccessful) {
                if ([result objectForKey:@"IsPost"]) {
                    sender.selected=NO;
                    NSNumber *boolNumber = [NSNumber numberWithBool:NO];
                    [_dataGoodsList[sender.tag-100] setValue:boolNumber forKey:@"Collect"];
                }else{
                    sender.selected=YES;
                }
            }else{
                sender.selected=YES;
            }
        }];
    }else{
        sender.selected=YES;
        NSString *goodssId=[NSString stringWithFormat:@"%d",[[_dataGoodsList[sender.tag-100] objectForKey:@"Id"] intValue]];
        [[YTHNetInterface startInterface] addCollectWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withId:goodssId withBlock:^(BOOL isSuccessful, id result, NSString *error) {
            if (isSuccessful) {
                if ([result objectForKey:@"IsPost"]) {
                    sender.selected=YES;
                    NSNumber *boolNumber = [NSNumber numberWithBool:YES];
                    [_dataGoodsList[sender.tag-100] setValue:boolNumber forKey:@"Collect"];
                }else{
                    sender.selected=NO;
                }
            }else{
                sender.selected=NO;
            }
        }];
    }
}
 
/**
 跳转到评论页面
 @param sender 按钮
 */
-(void)clickCommitBtn:(UIButton *)sender{
    NSLog(@"%ld",(long)sender.tag);
    allCommentsViewController *allCommentVC=[[allCommentsViewController alloc] init];
    allCommentVC.goodsId=[_dataGoodsList[sender.tag-100] objectForKey:@"Id"];
    [allCommentVC setHidesBottomBarWhenPushed:YES];
    [[self viewController].navigationController pushViewController:allCommentVC animated:YES];
}
 
#pragma mark  UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSDY.count;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if(section==1){
//        return _dataGoodsList.count;
//    }else{
//        return 1;
//    }
    return 1;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DisCoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DisCoverTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0;  i < _dataSDY.count; ++i) {
        [arr addObject:[_dataSDY[i] objectForKey:@"Picture"]];
        NSLog(@"%@",_dataSDY[i]);
    }
    NSLog(@"%lu",(unsigned long)arr.count);
    [cell.pic setYthImageWithURL:[_dataSDY[indexPath.section] objectForKey:@"Picture"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",[_dataSDY[indexPath.section] objectForKey:@"Name"]];
 
    return cell;
 
}
 
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (section == 1){
//        CellHead * head = [CellHead getCellHead:CGRectMake(0, 0, self.frame.size.width, 50)];
//        head.image.image = [UIImage imageNamed:@"发现东西"];
//        head.name.text = @"发现东西";
//        head.backgroundColor = kGlobalBackgroundColor;
//        return head;
//    }
//    return nil;
//}
 
#pragma mark  UITableViewDelegate
 
 
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section % 5 == 0 && self.expressAdViews.count != 0) {
        NSInteger infoIndex;
        if (self.expressAdViews.count <= (section / 5)) {
            infoIndex = (section/5)%self.expressAdViews.count;
        }else{
            infoIndex = section / 5;
        }
        UIView *view = self.expressAdViews[infoIndex];
        return view.bounds.size.height + 10 ;
    }else{
        return CGFLOAT_MIN;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section % 5 == 0 && self.expressAdViews.count != 0) {
        
        NSInteger infoIndex;
        if (self.expressAdViews.count <= (section / 5)) {
            infoIndex = (section/5)%self.expressAdViews.count;
        }else{
            infoIndex = section / 5;
        }
        
        GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.expressAdViews[infoIndex];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, adView.bounds.size.height + 10 )];
        view.backgroundColor = kGlobalBackgroundColor;
        [view addSubview:adView];
        return view;
    }else{
        UIView *view = [[UIView alloc]init];
        return view;
    }
}
/**
 *  点击广点通广告的方法
 */
//-(void)getMoreInformation:(UIButton *)sender{
//    GDTNativeAdData * info;
//    NSInteger infoIndex = sender.tag - 456;
//    info=nativeArray[infoIndex];
//    [_nativeAd clickAd:info];
//
//}
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
//
//    nativeArray = nativeAdDataArray;
//    [self.mytableview reloadData];
//
//}
//- (void)nativeAdFailToLoad:(NSError *)error{
//    NSLog(@"%@",error);
//}
/**
 *  加载广点通原生广告
 */
-(void)loadAd{
//    _nativeAd = [[GDTNativeAd alloc]initWithAppId: GDTADkey placementId:GDTYSADkey1];
//    _nativeAd.controller = [self topViewController];
//    _nativeAd.delegate = self;
//    [_nativeAd loadAd:10];
 
    // 支持视频广告的 PlacementId 会混出视频与图片广告
    self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithPlacementId:GDTYSADkey1 adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
    
    
    self.nativeExpressAd.delegate = self;
    // 配置视频播放属性
    self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
    self.nativeExpressAd.videoMuted = NO;
    [self.nativeExpressAd loadAd:10];
}
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd
                                      *)nativeExpressAd views:(NSArray<__kindof
                                                               GDTNativeExpressAdView *> *)views
{
    self.expressAdViews = [NSArray arrayWithArray:views];
    if (self.expressAdViews.count) {
        [self.expressAdViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
            expressView.controller = [self topViewController];
            [expressView render];
        }];
    }
    // 广告位 render 后刷新 tableView
    [self.mytableview reloadData];
}
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
    NSLog(@"%@",error);
}
 
- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
- (UIViewController *)topViewController {
    
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
 
    NSDictionary *dic =_dataSDY[indexPath.section];
    if([[dic allKeys] containsObject:@"CommonAd"]){  //跳转网页
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[dic objectForKey:@"Link"]]];
    }else{  //跳转内部
        OnlySpecialController * oscontroller = [OnlySpecialController new];
        oscontroller.Id = ((NSString*)[dic objectForKey:@"Id"]).intValue;
        [oscontroller setHidesBottomBarWhenPushed:YES];
        [[self viewController].navigationController pushViewController:oscontroller animated:YES];
    }
}
 
 
 
/**
 *  返回当前视图的控制器
 */
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
 
 
@end