单军华
2018-03-29 89d748b77b478905732e60f0b4c5807c274b6565
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
//
//  SegmentedPage.m
//  BaseProject
//
//  Created by WindShan on 2016/12/9.
//  Copyright © 2016年 WindShan. All rights reserved.
//
 
#import "FileListPage.h"
#import "HMSegmentedControl.h"
#import "NetworkTestPage.h"
#import "FileStatusView.h"
#import "BaseNaviController.h"
#import "FileDownView.h"
#import "LHDB.h"
 
@interface FileListPage ()<UIScrollViewDelegate,SelectedDelegate,SelDetailDelegate,SelectedDownDelegate>
{
     NSInteger  currentPage;
}
@property (nonatomic, strong) UIScrollView        *scrollView;
@property (nonatomic, strong) HMSegmentedControl  *segmentedControl;
 
/*! views */
@property (nonatomic, strong) FileDownView * views1; // 已下 载
@property (nonatomic, strong) FileStatusView * views2; // 未下载
 
@end
 
@implementation FileListPage
 
#pragma mark -
#pragma mark View Controller
 
- (void)setupDocumentControllerWithURL:(NSURL *)url
{
    if (self.docInteractionController == nil)
    {
        self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
        self.docInteractionController.delegate = self;
    }
    else
    {
        self.docInteractionController.URL = url;
    }
}
 
#pragma mark -
#pragma mark File system support
 
- (NSString *)applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
 
- (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
{
    
}
 
 
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}
 
#pragma mark -
#pragma mark UIDocumentInteractionControllerDelegate
 
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController
{
    return self;
}
 
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"我的文件";
    [self setNavigationLeft:@"返回" sel:@selector(backAction)];
    /*! 设置CGRectZero从导航栏下开始计算 */
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self buildSegment];
 
    // Do any additional setup after loading the view.
}
 
-(void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
#pragma mark - ***** 配置segment
- (void)buildSegment
{
    self.segmentedControl.hidden = NO;
    self.scrollView.hidden       = NO;
    self.views1.hidden           = NO;
    self.views2.hidden           = NO;
}
 
 
 
- (HMSegmentedControl *)segmentedControl
{
    if (!_segmentedControl)
    {
        _segmentedControl                             = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        /*! 设置标题 */
        _segmentedControl.sectionTitles               = @[@"已下载",@"未下载"];
        /*! 自适应宽度,随着屏幕滑动自动滚动 */
        _segmentedControl.autoresizingMask            = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        /*! 默认选中第0个view */
        _segmentedControl.selectedSegmentIndex        = 0;
        /*! 标题背景颜色 */
        _segmentedControl.backgroundColor             = [UIColor clearColor];
        /*! 标题默认字体颜色 */
        _segmentedControl.titleTextAttributes         = @{NSForegroundColorAttributeName : RgbColor(25, 31, 35), NSFontAttributeName: BA_FontSize(16)};
        /*! 标题选中字体颜色 */
        _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : RgbColor(23, 172, 67), NSFontAttributeName: BA_FontSize(18)};
        /*! 标题选中的下划线的颜色 */
        _segmentedControl.selectionIndicatorColor     = RgbColor(23, 172, 67);
        /*! 标题选中的下划线的高度 */
        _segmentedControl.selectionIndicatorHeight    = 1.0f;
        /*! 标题选中的样式:本样式为下划线 */
        _segmentedControl.selectionStyle              = HMSegmentedControlSelectionStyleFullWidthStripe;
        /*! 标题选中的下划线的方向:本样式为向下 */
        _segmentedControl.selectionIndicatorLocation  = HMSegmentedControlSelectionIndicatorLocationDown;
        /*! 标题的中间的隔线:默认为:NO */
        _segmentedControl.verticalDividerEnabled      = YES;
        /*! 标题的中间的隔线颜色 */
        _segmentedControl.verticalDividerColor        = RgbColor(23, 172, 67);
        /*! 标题的中间的隔线宽度 */
        _segmentedControl.verticalDividerWidth        = 1.0f;
        
        [self.view addSubview:_segmentedControl];
        
        /*! 标题点击事件 */
        BA_WEAKSELF;
        [_segmentedControl setIndexChangeBlock:^(NSInteger index)
        {
            if(index == 0 )
            {
                [weakSelf.views1 reSetView];
            }
            else if( index == 1 )
            {
               [weakSelf.views2 reSetView];
            }
            
            [weakSelf.scrollView scrollRectToVisible:CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, weakSelf.scrollView.frame.size.height) animated:YES];
        }];
        
        //self.navigationItem.titleView = _segmentedControl;
    }
    
    return _segmentedControl;
}
 
 
- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        /*! 这里的frame按实际情况更改! */
        self.scrollView                                = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_HEIGHT-72-64)];
        self.scrollView.backgroundColor                = [UIColor whiteColor];
        self.scrollView.pagingEnabled                  = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.bounces                        = NO;
        self.scrollView.contentSize                    = CGSizeMake(SCREEN_WIDTH * 2, self.scrollView.frame.size.height);
        self.scrollView.delegate                       = self;
        self.scrollView.backgroundColor                = [UIColor whiteColor];
        [self.scrollView scrollRectToVisible:CGRectMake(0, 0, SCREEN_WIDTH, self.scrollView.frame.size.height) animated:NO];
        [self.view addSubview:self.scrollView];
        
    }
    return _scrollView;
}
 
#pragma mark - ***** UIScrollViewDelegate
 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    currentPage = scrollView.contentOffset.x / pageWidth;
    
    LOG_INFO(@"current page:%ld",currentPage);
    
    if(currentPage == 0 )
    {
        [self.views1 reSetView];
    }
    else if( currentPage == 1 )
    {
        [self.views2 reSetView];
    }
    
    [_segmentedControl setSelectedSegmentIndex:currentPage animated:YES];
}
 
- (FileDownView *)views1
{
    if (!_views1)
    {
        // start monitoring the document directory…
        self.docWatcher = [DirectoryWatcher watchFolderWithPath:[self applicationDocumentsDirectory] delegate:self];
        
        _views1 = [[FileDownView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.scrollView.frame.size.height)];
        
        _views1.delegate = self;
        [self.scrollView addSubview:_views1];
    }
    return _views1;
}
 
- (void)didSelectedDownSection:(FileStatus*)model
{
    NSURL *fileURL = [NSURL fileURLWithPath:model.filePath];
    [self setupDocumentControllerWithURL:fileURL];
 
    CGRect navRect = self.navigationController.navigationBar.frame;
    
    navRect.size = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [self.docInteractionController presentOptionsMenuFromRect:navRect inView:self.view  animated:YES];
}
 
- (FileStatusView *)views2
{
    if (!_views2)
    {
        _views2 = [[FileStatusView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.scrollView.frame.size.height)];
        _views2.delegate = self;
        _views2.detailDelegate = self;
        [self.scrollView addSubview:_views2];
    }
    return _views2;
}
 
- (void)didSelectedSection:(FileStatus*)model;
{
    //DeviceDetailPage* page = [[DeviceDetailPage alloc] initIsFirstPage:NO];
    
    //page.model = model;
    // 跳转界面
   // BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page];
   // [self presentViewController:baseNav animated:YES completion:nil];
    
}
 
- (void)didDetailSection:(FileStatus*)model;
{
    [LHDBPath instanceManagerWith:DEFAULT_PATH];
    
    [_views1 insertDataWithModel:model];
}
/*
#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