developer
2023-05-20 c1ffd99c4b60066774eb2c97b31e4aaa014e7f51
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
//
//  MainViewController.m
//  BuWanVideo2.0
//
//  Created by weikou on 16/7/29.
//  Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "MainViewController.h"
 
#import "recommendView.h"
#import "liveOnLineView.h"
#import "subregionView.h"
#import "discoverView.h"
#import "attentionView.h"
 
#import "searchViewController.h"
#import "LookNoteController.h"
#import "UIButton+YYWebImage.h"
 
#import "SlideMenuController.h"
 
//#import "XGPush.h"
 
#import "XYRDetailViewController.h"
#import "XYRVideoInfoModel.h"
 
#import "AppDelegate.h"
#import "MymessageContrlloer.h"
#import "OnlySpecialController.h"
 
 
@interface MainViewController ()<UIScrollViewDelegate>{
    recommendView *_recommend;
    liveOnLineView *_liveOnLine;
    subregionView *_subregion;
    discoverView *_discover;
    attentionView *_attention;
}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) HMSegmentedControl *segmentedControl;
@property (nonatomic, assign) BOOL userOnLine;//用户是否登录
@property (nonatomic, strong) UIView *Point;  //提示红点
@property (nonatomic, strong) NSMutableArray *dataClass;//分区数据
 
 
@end
 
@implementation MainViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    _dataClass = @[].mutableCopy;
    //加载导航栏
    [self loadNavigationbar];
    //检查当前用户是否登录
    [self whetherLogin];
    //添加主视图的segment
    [self creatSegmentView];
    
    //添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushMessageVC:) name:@"pushMessageNotification" object:nil];
}
 
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = kGlobalBackgroundColor;
    //[MobClick beginLogPageView:@"主四页"];
    
    //强制竖屏
    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
    
    NSNumber *PortraitorientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:PortraitorientationUnknown forKey:@"orientation"];
}
 
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //[MobClick endLogPageView:@"主四页"];
}
 
-(BOOL)shouldAutorotate{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait ;
}
 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait ;
}
 
/**
 *  加载导航栏
 */
-(void)loadNavigationbar{
    //去掉titlie
    UIView *tview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.01, 0.01)];
    self.navigationItem.titleView = tview;
    
    //观看记录
    UIButton *recordBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [recordBtn setImage:[UIImage imageNamed:@"历史记录"] forState:UIControlStateNormal];
    [recordBtn addTarget:self action:@selector(JumpToRecordView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *recordBarItem=[[UIBarButtonItem alloc] initWithCustomView:recordBtn];
    //    //下载
    //    UIButton *downLoadBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    //    downLoadBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 4);
    //    [downLoadBtn setImage:[UIImage imageNamed:@"缓存"] forState:UIControlStateNormal];
    //    [downLoadBtn addTarget:self action:@selector(JumpToDownloadView) forControlEvents:UIControlEventTouchUpInside];
    //    UIBarButtonItem *downLoadBarItem=[[UIBarButtonItem alloc] initWithCustomView:downLoadBtn];
    //搜索
    UIButton *SearchBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [SearchBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [SearchBtn addTarget:self action:@selector(JumpToSearchView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *SearchBarItem=[[UIBarButtonItem alloc] initWithCustomView:SearchBtn];
    //空白间距
    UILabel *WhiteLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 8, 10)];
    WhiteLabel.backgroundColor=[UIColor clearColor];
    UIBarButtonItem *WhiteBarItem=[[UIBarButtonItem alloc] initWithCustomView:WhiteLabel];
    //添加 观看记录/下载/搜索 到导航栏
    self.navigationItem.rightBarButtonItems=[[NSArray alloc] initWithObjects:SearchBarItem,WhiteBarItem,recordBarItem,nil];
}
 
/**
 *  当前是否登录
 */
-(void)whetherLogin{
    //调整头像和用户名
    [self loadDATA];
    //添加一个观察者监听登录和退出登录
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadDATA) name:@"RELOAD_DATA" object:nil];
}
 
/**
 *  响应登录监听方法的函数
 */
-(void)loadDATA{
    //重新获取登录状态
    _userOnLine= [[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"];
    //获取头像和用户名的按钮
    //定制显示头像的Button
    UIButton *iconBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    iconBtn.imageView.contentMode=UIViewContentModeScaleAspectFit;
    iconBtn.layer.masksToBounds=YES;
    iconBtn.layer.cornerRadius=22.0;
    [iconBtn addTarget:self action:@selector(presentLeftMenuViewController) forControlEvents:UIControlEventTouchUpInside];
    
    //定制显示用户名的Button
    UIButton *nameBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    nameBtn.backgroundColor=[UIColor clearColor];
    nameBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [nameBtn setTintColor:[UIColor whiteColor]];
    [nameBtn addTarget:self action:@selector(presentLeftMenuViewController) forControlEvents:UIControlEventTouchUpInside];
    nameBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
    
    if(_userOnLine){//如果当前状态是登录状态,说明用户进行了登录操作
        [iconBtn yy_setImageWithURL:[[NSUserDefaults standardUserDefaults] objectForKey:@"iconURL"]
                           forState:UIControlStateNormal
                        placeholder:[UIImage imageNamed:@"默认头像forios11"]];
        [nameBtn setTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"QQUserName"] forState:UIControlStateNormal];
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"iconURL"]);
        //设置推送
//        [XGPush setAccount:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"]];
    }else{//否则用户进行了退出登录的操作
        [iconBtn setImage:[UIImage imageNamed:@"默认头像forios11"] forState:UIControlStateNormal];
        [nameBtn setTitle:@"立即登录" forState:UIControlStateNormal];
        //取消账号推送
//        [XGPush setAccount:nil];
    }
    UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:iconBtn];
    UIBarButtonItem *nameBarItem=[[UIBarButtonItem alloc] initWithCustomView:nameBtn];
     self.navigationItem.leftBarButtonItems=[[NSArray alloc] initWithObjects:iconBarItem,nameBarItem,nil];
}
 
 
/**
 *  添加主视图的segment
 */
-(void)creatSegmentView{
 
    
    
    self.segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"推荐 ", @"分区 ", @"发现 ", @"关注 "]];
    [self.segmentedControl setFrame:CGRectMake(0, KScreenH - 40, KScreenW, 40)];
    self.segmentedControl.selectionIndicatorHeight = 2.0f;
    self.segmentedControl.backgroundColor = kGlobalMainColor;
    self.segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName :[UIFont systemFontOfSize:15]};
    self.segmentedControl.selectedTitleTextAttributes=@{NSForegroundColorAttributeName : kGlobalYellowColor,NSFontAttributeName :[UIFont systemFontOfSize:15]};
    self.segmentedControl.selectionIndicatorColor = kGlobalYellowColor;
    self.segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
    self.segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segmentedControl.selectedSegmentIndex=0;
    self.segmentedControl.shouldAnimateUserSelection = YES;
    
    __weak typeof(self) weakSelf = self;
    [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
        [weakSelf.scrollView scrollRectToVisible:CGRectMake(KScreenW * index, kNavigationBarH+40, KScreenW, KScreenH-kNavigationBarH-40) animated:YES];
        if(index == 3 && _Point){
            [weakSelf.Point removeFromSuperview];
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"HaveAttention"];
        }
    }];
    
    [self.view addSubview:self.segmentedControl];
    
    //配置滑动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kNavigationBarH, KScreenW, KScreenH-kNavigationBarH-40)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(KScreenW * 4, KScreenH-kNavigationBarH-40);
    self.scrollView.delegate = self;
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, KScreenW, KScreenH-kNavigationBarH-40) animated:NO];
    [self.view addSubview:self.scrollView];
    //推荐
    _recommend=[[recommendView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH-kNavigationBarH-40)];
    [self.scrollView addSubview:_recommend];
    //直播
//    _liveOnLine=[[liveOnLineView alloc] initWithFrame:CGRectMake(KScreenW, 0, KScreenW, KScreenH-kNavigationBarH-40)];
    [self.scrollView addSubview:_liveOnLine];
    //分区
    _subregion=[[subregionView alloc] initWithFrame:CGRectMake(KScreenW*1, 0, KScreenW, KScreenH-kNavigationBarH-40)];
    [self.scrollView addSubview:_subregion];
    //发现
    _discover=[[discoverView alloc] initWithFrame:CGRectMake(KScreenW*2, 0, KScreenW, KScreenH-kNavigationBarH-40)];
    _discover.mycontroller = self;
    [self.scrollView addSubview:_discover];
    //关注
    _attention=[[attentionView alloc] initWithFrame:CGRectMake(KScreenW*3, 0, KScreenW, KScreenH-kNavigationBarH-40)];
    _attention.temp = self;
    [self.scrollView addSubview:_attention];
    
    //延迟加载  需要读取segmentedControl 的位置直接加载没有值
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"HaveAttention"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]){
        [self performSelector:@selector(initPont) withObject:nil afterDelay:4];
    }
    
    //登录后进行是否有新的关注提醒
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadScene) name:@"RELOAD_DATA" object:nil];
}
 
-(void)ReloadScene{
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"HaveAttention"]){  //用户已经登录
        [self initPont];
    }
}
 
-(void)initPont{
    _Point = [UIView new];
    _Point.backgroundColor = [UIColor redColor];
    _Point.frame = CGRectMake(self.segmentedControl.ThereRect.origin.x + self.segmentedControl.ThereRect.size.width,(self.segmentedControl.frame.size.height - 8) / 2, 6, 6);
    _Point.layer.cornerRadius = 3;
    _Point.layer.masksToBounds =YES;
    [self.segmentedControl addSubview:_Point];
}
 
 
//展开侧边栏
-(void)presentLeftMenuViewController{
    [self.slideMenuController openLeft];
}
 
/**
 *  跳转到历史记录
 */
-(void)JumpToRecordView{
    LookNoteController *LookNoteLookNoteVC=[[LookNoteController alloc] init];
    [LookNoteLookNoteVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:LookNoteLookNoteVC animated:YES];
}
 
/**
 *  跳转到搜索
 */
-(void)JumpToSearchView{
    searchViewController *searchVC=[[searchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
 
- (void)pushMessageVC:(NSNotification *)sender{
    NSDictionary *dic=sender.userInfo;
    if([[dic objectForKey:@"PushType"] isEqualToString:@"pushVideoUpdateByAccount"]){//跳转到详细界面
        NSString *userInfo=[dic objectForKey:@"Params"];
        NSDictionary *Params = [self dictionaryWithJsonString:userInfo];
        XYRDetailViewController *play = [[XYRDetailViewController alloc]init];
        [YTHsharedManger startManger].preController = self;
        play.modalPresentationStyle = 0;
        play.Model = [XYRVideoInfoModel yy_modelWithDictionary:Params];
        [self presentViewController:play animated:YES completion:^{
            
        }];
    }else if([[dic objectForKey:@"PushType"] isEqualToString:@"pushSystemMessage"]){//跳转到系统消息
        MymessageContrlloer *message = [MymessageContrlloer new];
        [self.navigationController pushViewController:message animated:YES];
    }else if([[dic objectForKey:@"PushType"] isEqualToString:@"pushCommentMessage"]){//跳转到我的评论
        MymessageContrlloer *message = [MymessageContrlloer new];
        [self.navigationController pushViewController:message animated:YES];
    }else if([[dic objectForKey:@"PushType"] isEqualToString:@"pushSpecialMessage"]){//跳转到专辑详情页面
        OnlySpecialController *OnlySpecial =[OnlySpecialController new];
        OnlySpecial.Id=[[dic objectForKey:@"Id"] intValue];
        [self.navigationController pushViewController:OnlySpecial animated:YES];
    }
}
 
 
#pragma mark 把json格式的字符串转换为字典格式
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败:%@",err);
        return nil;
    }
    return dic;
}
 
 
 
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth =KScreenW;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    
    if(page == 4 && _Point){
        [_Point removeFromSuperview];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"HaveAttention"];
    }
 
    [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
}
 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView.contentOffset.x < -50){
        [self presentLeftMenuViewController];
    }
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end