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
//
//  subregionView.m
//  BuWanVideo2.0
//
//  Created by weikou on 16/8/1.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "subregionView.h"
#import "SubregionViewCollectionViewCell.h"
 
#import "SubregionDetailViewController.h"
//#import "GDTNativeAd.h"//广点通原生广告
#import "ADCollectionReusableView.h"
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"
#import "FaxianCReusableView.h"
//#import "BannerViewController.h"
#import "GDTUnifiedBannerView.h"
 
@interface subregionView ()<UICollectionViewDelegate,UICollectionViewDataSource,GDTNativeExpressAdDelegete,GDTUnifiedBannerViewDelegate>{
    UICollectionView *_collview;
//    GDTNativeAd *_nativeAd;     //原生广告实例
//    NSArray *nativeArray;//存储请求下来的原生广告信息
    
}
@property (nonatomic , strong) NSMutableArray *dataClass;//分区数据
@property (nonatomic, strong) NSArray *expressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
@property (nonatomic, strong) GDTUnifiedBannerView *bannerView;
 
@end
@implementation subregionView
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //从file中读取数据
        [self loadAd];
        [self loadDataFromFile];
        //配置collectionView的属性
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向
        flowlayout.minimumLineSpacing = (KScreenW)/10;//行间距
        flowlayout.minimumInteritemSpacing=(KScreenW)/10;//item之间的距离
        flowlayout.itemSize = CGSizeMake((KScreenW)/6, (KScreenW)/6+30);//设置itme的大小
        flowlayout.sectionInset = UIEdgeInsetsMake((KScreenW)/10,(KScreenW)/9,(KScreenW)/10,(KScreenW)/9);//设置itme之间的间距
        flowlayout.headerReferenceSize = CGSizeMake(0, 60);
        //创建collectionView视图
        if (!_collview) {
            _collview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowlayout];
        }
        _collview.scrollEnabled = YES;
        _collview.backgroundColor = [UIColor whiteColor];
        _collview.delegate = self;
        _collview.dataSource = self;
        [self addSubview:_collview];
        
        //给collview视图注册cell
        [_collview registerNib:[UINib nibWithNibName:@"SubregionViewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SubregionViewCollectionViewCell"];
        [_collview registerNib:[UINib nibWithNibName:@"ADCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ADCollectionReusableView"];
 
        [_collview registerNib:[UINib nibWithNibName:@"FaxianCReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView"];
 
        //给collectionView设置下拉刷新
        _collview.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            //开始刷新
            [_collview.mj_header  beginRefreshing];
            //网络请求
            [self loadNewData];
 
        }];
      
    }
    return self;
}
 
/**
 *  下拉刷新调用的方法
 */
- (void)loadNewData{
    [self getchannelData];
}
 
/**
 *  请求分区数据
 */
- (void)getchannelData{
    [[YTHNetInterface startInterface] getClassWithUid:[YTHsharedManger startManger].Uid withSystem:@"1" withBlock:^(BOOL isSuccessful, id result, NSString *error) {
        if (isSuccessful) {
            NSDictionary *dic = (NSDictionary *)result;
            if (!_dataClass) {
                _dataClass = [[NSMutableArray alloc ] initWithCapacity:0];
            }
            [_dataClass removeAllObjects];
            NSArray *ar = [[dic objectForKey:@"Data"] objectForKey:@"data"];
            for (int i = 0; i<ar.count; i++) {
                if ([[ar objectAtIndex:i][@"Id"]integerValue] == 309 || [[ar objectAtIndex:i][@"Id"]integerValue] == 310) {
                    continue;
                }
                NSLog(@"---- %@ ----",[ar objectAtIndex:i]);
                [_dataClass addObject:[ar objectAtIndex:i]];
            }
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_dataClass];
            [data writeToFile:CHANNELfILE atomically:YES];
            //刷新collectionView视图
            [_collview reloadData];
            [self loadAd];
        }else{
            
        }
        //结束网络刷新
        [_collview.mj_header endRefreshing];
    }];
}
 
/**
 *  从沙盒里面加载数据
 */
-(void)loadDataFromFile{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:CHANNELfILE])
    {
        _dataClass = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:CHANNELfILE]];
    }else{
        [self loadNewData];
    }
}
 
/**
 *  返回当前视图的控制器
 */
- (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;
}
/**
 *  点击广点通广告的方法
 */
//-(void)getMoreInformation:(UIButton *)sender{
//    GDTNativeAdData * info;
//    if (sender.tag-456==0) {
//        info=nativeArray[0];
//    }
//
//    [_nativeAd clickAd:info];
//
//}
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
//
//    nativeArray = nativeAdDataArray;
//    [_collview reloadData];;
//}
//- (void)nativeAdFailToLoad:(NSError *)error{
//    NSLog(@"%@",error);
//}
/**
 *  加载广点通原生广告
 */
-(void)loadAd{
//    _nativeAd = [[GDTNativeAd alloc]initWithAppId: GDTADkey placementId:GDTYSADkey5];
//    _nativeAd.controller = [self topViewController];
//    _nativeAd.delegate = self;
//    [_nativeAd loadAd:1];
    
   //  支持视频广告的 PlacementId 会混出视频与图片广告
    
    self.nativeExpressAd =[[GDTNativeExpressAd alloc] initWithPlacementId:@"6000418684535350" adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
    self.nativeExpressAd.delegate = self;
    // 配置视频播放属性
    self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
    self.nativeExpressAd.videoMuted = NO;
    [self.nativeExpressAd loadAd:1];
 
}
- (void)clickRemoveAd:(id)sender {
    [self.bannerView removeFromSuperview];
    self.bannerView = nil;
}
- (void)clickLoadAd:(id)sender {
 
}
// 请求广告条数据成功后调用
- (void)unifiedBannerViewDidLoad:(GDTUnifiedBannerView *)unifiedBannerView {
    
}
// 请求广告条数据失败后调用
- (void)unifiedBannerViewFailedToLoad:(GDTUnifiedBannerView *)unifiedBannerView error:(NSError *)error {
    
}
 
- (GDTUnifiedBannerView *)bannerView {
    if (_bannerView) {
        return _bannerView;
    }
    CGRect rect = CGRectMake(0, 0, KScreenW, 60);
    //_bannerView = [[GDTUnifiedBannerView alloc] initWithFrame:rect appId:GDTADkey placementId:GDTYSADkey5];
    _bannerView = [[GDTUnifiedBannerView alloc] initWithFrame:rect placementId:GDTYSADkey5 viewController:[self topViewController]];
    //设置广告轮播时间,范围为30-120秒,0表示不轮播
    _bannerView.autoSwitchInterval = 0;
    //设置Delegate
    _bannerView.delegate = self;
    return _bannerView;
}
 
- (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 后刷新 
    [_collview 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;
}
#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SubregionDetailViewController *subregionVC=[[SubregionDetailViewController alloc] init];
    subregionVC.Id=[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Id"];
    subregionVC.titles=[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Name"];
    [subregionVC setHidesBottomBarWhenPushed:YES];
 
    [[self viewController].navigationController pushViewController:subregionVC animated:YES];
}
 
#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataClass.count;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SubregionViewCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"SubregionViewCollectionViewCell" forIndexPath:indexPath];
    [cell.image setContentMode:UIViewContentModeScaleAspectFill];
    [cell.image setYthImageWithURL:[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Icon"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
    cell.text.text=[[_dataClass objectAtIndex:indexPath.row] objectForKey:@"Name"];
    return cell;
}
 
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader){
//        ADCollectionReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ADCollectionReusableView" forIndexPath:indexPath];
//
//        if (indexPath.section == 0) {
//            //返回广告视图
//            GDTNativeAdData *info;
//            info=nativeArray[0];
//            groupfootSection.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
//            groupfootSection.ADTitle.backgroundColor=[UIColor clearColor];
//            [groupfootSection.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:nil];
//            //把button设为透明
//            groupfootSection.ADClickButton.backgroundColor=[UIColor clearColor];
//            [groupfootSection.ADClickButton setTag:456+indexPath.section];
//            [groupfootSection.ADClickButton addTarget:self action:@selector(getMoreInformation:) forControlEvents:UIControlEventTouchUpInside];
//            [_nativeAd attachAd:info toView:groupfootSection];
//        }
//        return groupfootSection;
//        GDTNativeExpressAdView *adView ;
//        if (self.expressAdViews.count > 0) {
//            adView = (GDTNativeExpressAdView *)self.expressAdViews[0];
//        }
        FaxianCReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            [self clickRemoveAd:nil];
            
            [view addSubview:self.bannerView];
            [self.bannerView loadAdAndShow];
 
        }
        return view;
 
    }else{
        FaxianCReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView" forIndexPath:indexPath];
//        UIView *view frame=CGRectMake(0, 0, KScreenW,  KScreenW/2);
//        [groupfootSection addSubview:self.nativeAdView];
        return groupfootSection;
    }
  
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    if (section==0) {
//        return CGSizeMake(KScreenW, (KScreenW-20)/16*9);
//
//    }
//    return CGSizeMake(0, 0);
//}
@end