DWMainViewController.m 9.54 KB
Newer Older
Han Ruisong's avatar
Han Ruisong committed
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
//
//  DWMainViewController.m
//  Demo
//
//  Created by zwl on 2019/4/11.
//  Copyright © 2019 com.bokecc.www. All rights reserved.
//

#import "DWMainViewController.h"
#import "DWMainCollectionViewCell.h"
#import "DWMemberViewController.h"
#import "DWVodPlayViewController.h"
#import "DWDownloadManagerViewController.h"
#import "DWUploadViewController.h"

#define VIDEOINFOURL     @"https://p.bokecc.com/demo/videoinfo.json"

@interface DWMainViewController ()

@property(nonatomic,strong)UIImageView * headerImageView;
@property(nonatomic,strong)NSMutableArray * videoList;

@end

@implementation DWMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
Han Ruisong's avatar
Han Ruisong committed
31
    
Han Ruisong's avatar
Han Ruisong committed
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
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    //header
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.collectionView registerClass:[DWMainCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    // Do any additional setup after loading the view.
    
    //** !! **
    /*
     若需修改视频数据。步骤如下
     1.注释掉reloadNetworkData 网络请求的方法。
     2.修改DWConfigurationManager.m,中的账号ID,APIKey。
     3.在reloadLoaclData,填写自己账号下的具体数据
     */
  
//    [self reloadLoaclData];
    [self reloadNetworkData];
}

-(void)reloadNetworkData
{
    NSURLSession * session = [NSURLSession sharedSession];
    NSURL * url = [NSURL URLWithString:VIDEOINFOURL];
    
    NSURLSessionDataTask * task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [@"网络请求失败,请点击重试" showAlert];
            });
            return;
        }
        
        NSString * jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSArray * vodList = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];

Han Ruisong's avatar
Han Ruisong committed
71
//        NSLog(@"%@",vodList);
Han Ruisong's avatar
Han Ruisong committed
72
73
74
75
76
77
78
79
        for (NSDictionary * vodDict in vodList) {
            DWVodModel * vodModel = [[DWVodModel alloc]init];
            vodModel.videoId = [vodDict objectForKey:@"videoId"];
            vodModel.title = [vodDict objectForKey:@"videoTitle"];
            vodModel.time = [vodDict objectForKey:@"videoTime"];
            vodModel.imageUrl = [vodDict objectForKey:@"videoCover"];
            [self.videoList addObject:vodModel];
        }
Han Ruisong's avatar
Han Ruisong committed
80
                
Han Ruisong's avatar
Han Ruisong committed
81
        dispatch_async(dispatch_get_main_queue(), ^{
Han Ruisong's avatar
Han Ruisong committed
82
83
            DWAPPDELEGATE.videoList = self.videoList;

Han Ruisong's avatar
Han Ruisong committed
84
85
86
87
88
89
90
91
92
93
94
95
            DWVodModel * vodModel = self.videoList.firstObject;
            [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:vodModel.imageUrl] placeholderImage:[UIImage imageNamed:@"icon_placeholder.png"]];
            [self.collectionView reloadData];
        });
    }];
    
    [task resume];
}

-(void)reloadLoaclData
{
    //请自行替换数据
Han Ruisong's avatar
Han Ruisong committed
96
    NSArray * videos = @[];
Han Ruisong's avatar
Han Ruisong committed
97
    
Han Ruisong's avatar
Han Ruisong committed
98
99
100
    for (int i = 0; i < videos.count; i++) {
        DWVodModel * vodModel = [[DWVodModel alloc]init];
        vodModel.videoId = [videos objectAtIndex:i];
Han Ruisong's avatar
Han Ruisong committed
101
        vodModel.title = [videos objectAtIndex:i];
Han Ruisong's avatar
Han Ruisong committed
102
103
104
105
        vodModel.time = @"展示time";
        vodModel.imageUrl = @"icon_placeholder.png";
        [self.videoList addObject:vodModel];
    }
Han Ruisong's avatar
Han Ruisong committed
106
    DWAPPDELEGATE.videoList = self.videoList;
Han Ruisong's avatar
Han Ruisong committed
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
    
    [self.collectionView reloadData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark - action
-(void)userInfoButtonAction
{
    DWMemberViewController * memberVC = [[DWMemberViewController alloc]init];
    [self.navigationController pushViewController:memberVC animated:YES];
}

-(void)uploadButtonAction
{
    DWUploadViewController * uploadVC = [[DWUploadViewController alloc]init];
    [self.navigationController pushViewController:uploadVC animated:YES];
}

-(void)downloadButtonAction
{
    DWDownloadManagerViewController * downloadManagerVC = [[DWDownloadManagerViewController alloc]init];
    [self.navigationController pushViewController:downloadManagerVC animated:YES];
}

-(void)headerImageViewTap
{
    DWVodModel * vodModel = self.videoList.firstObject;
    if (!vodModel) {
        [@"暂无视频数据" showAlert];
        return;
    }

    DWVodPlayViewController * vodPlayVC = [[DWVodPlayViewController alloc]init];
    vodPlayVC.vodModel = vodModel;
    vodPlayVC.vidoeList = self.videoList;
    [self.navigationController pushViewController:vodPlayVC animated:YES];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.videoList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DWMainCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    // Configure the cell
    [cell setVideoModel:[self.videoList objectAtIndex:indexPath.row] AndIsLeft:indexPath.row % 2 == 0 ? YES : NO];
        
    return cell;

}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        
        if (!self.headerImageView) {
            self.headerImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_placeholder"]];
            self.headerImageView.userInteractionEnabled = YES;
            [headerView addSubview:self.headerImageView];
            
            UITapGestureRecognizer * headerImageViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerImageViewTap)];
            [self.headerImageView addGestureRecognizer:headerImageViewTap];
            
            [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(@0);
                make.left.equalTo(@10);
                make.right.equalTo(@(-10));
                make.bottom.equalTo(@(-10));
            }];
        }

        return headerView;
    }
    
    return nil;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Han Ruisong's avatar
Han Ruisong committed
202
{    
Han Ruisong's avatar
Han Ruisong committed
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
    DWVodModel * vodModel = [self.videoList objectAtIndex:indexPath.row];
    DWVodPlayViewController * vodPlayVC = [[DWVodPlayViewController alloc]init];
    vodPlayVC.vodModel = vodModel;
    vodPlayVC.vidoeList = self.videoList;
    [self.navigationController pushViewController:vodPlayVC animated:YES];
}

#pragma mark - init
-(void)initUI
{
    [self.navigationController.navigationBar setShadowImage:[[UIColor whiteColor] createImage]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:TitleFont(15),NSForegroundColorAttributeName:TitleColor_51}];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //导航功能按钮
    UIButton * userInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [userInfoButton setImage:[UIImage imageNamed:@"icon_photo.png"] forState:UIControlStateNormal];
    userInfoButton.frame = CGRectMake(0, 0, 40, 40);
    [userInfoButton addTarget:self action:@selector(userInfoButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]initWithCustomView:userInfoButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton * uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [uploadButton setImage:[UIImage imageNamed:@"icon_upload.png"] forState:UIControlStateNormal];
    uploadButton.frame = CGRectMake(0, 0, 40, 40);
    [uploadButton addTarget:self action:@selector(uploadButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem1 = [[UIBarButtonItem alloc]initWithCustomView:uploadButton];
    
    UIButton * downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [downloadButton setImage:[UIImage imageNamed:@"icon_download.png"] forState:UIControlStateNormal];
    downloadButton.frame = CGRectMake(0, 0, 40, 40);
    [downloadButton addTarget:self action:@selector(downloadButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem2 = [[UIBarButtonItem alloc]initWithCustomView:downloadButton];

    self.navigationItem.rightBarButtonItems = @[rightItem2,rightItem1];
    
}

-(NSMutableArray *)videoList
{
    if (!_videoList) {
        _videoList = [[NSMutableArray alloc]init];
    }
    return _videoList;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

/*
#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.
}
*/

@end