zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
//
//  PBProjectListViewController.m
//  IphoneBIMe
//
//  Created by zjf on 2018/7/13.
//  Copyright © 2018年 ProBIM. All rights reserved.
//
 
#import "PBProjectListViewController.h"
#import "PBLoginViewController.h"
#import "PBProjectModel.h"
#import "PBProjectTableViewCell.h"
#import "PBMineView.h"
#import "MenuView.h"
#import "PBTabBarController.h"
#import "PBPromptView.h"
#import "PBMessageController.h"
#import "PBChangePWController.h"
#import "PBRefreshGifHeader.h"
static NSString *const cellID = @"cellID";
@interface PBProjectListViewController ()<UITableViewDataSource, UITableViewDelegate, HomeMenuViewDelegate, UISearchBarDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, weak) PBMineView *mineView;
@property (nonatomic, strong) MenuView *menu;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UIButton *maskBtn;
@property (nonatomic, copy) NSString *fullName;
@property (nonatomic, strong) PBPromptView *promptView;
@property (nonatomic, strong) PBRefreshGifHeader *headerView;
@end
 
@implementation PBProjectListViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self setupUI];
    [self setupRefresh];
    if ([NSString getLoginData]) {
        [self.tableView.mj_header beginRefreshing];
    }else{
        [self toLogin];
    }
}
- (void)toLogin {
    self.dataList = nil;
    [self.tableView reloadData];
    PBLoginViewController *loginVC = [[PBLoginViewController alloc] init];
    loginVC.LoginSuccessfulBlock = ^{
        self.mineView.realName = Realname;
        self.searchBar.text = nil;
        self.fullName = nil;
        [self.tableView.mj_header beginRefreshing];
    };
    loginVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:loginVC animated:NO completion:nil];
}
- (void)setupNav {
    UIBarButtonItem *leftNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Project_list_mine"] style:UIBarButtonItemStylePlain target:self action:@selector(leftNavItemAction)];
    UIBarButtonItem *rightNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Project_list_news"] style:UIBarButtonItemStylePlain target:self action:@selector(rightNavItemAction)];
    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.placeholder = @"请搜索项目名称";
    self.searchBar.delegate = self;
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.navigationItem.leftBarButtonItem = leftNavItem;
    self.navigationItem.rightBarButtonItem = rightNavItem;
    self.navigationItem.titleView = self.searchBar;
}
- (void)leftNavItemAction {
    [self.menu show];
}
- (void)rightNavItemAction {
    PBMessageController *mesVC = [[PBMessageController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mesVC];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}
- (void)setupRefresh {
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self loadProjectList];
//    }];
//    header.lastUpdatedTimeLabel.hidden = YES;
//    header.stateLabel.textColor = TitleColor;
//    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
//    [header setTitle:@"释放更新" forState:MJRefreshStatePulling];
//    [header setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
//    self.tableView.mj_header = header;
    self.tableView.mj_header = self.headerView;
}
 
- (void)loadProjectList {
    if (self.fullName == nil) {
        self.fullName = @"";
    }
    [[PBNetworkTools sharedTools] getProjectListWithKeyword:self.fullName andIsPublic:@"" andSort:@"CreateDate desc" andSkip:@"0" andtake:@"999" andOnlyFavorite:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if(error) {
            NSLog(@"%@",error);
            [self.tableView.mj_header endRefreshing];
            [YJProgressHUD showMessage:@"加载项目列表失败" inView:self.view];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            self.dataList = [NSArray yy_modelArrayWithClass:[PBProjectModel class] json:[networkModel.Data valueForKey:@"rows"]];
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            if (self.dataList.count == 0) {
                self.promptView.hidden = NO;
            }else {
                self.promptView.hidden = YES;
            }
        }else {
            [self.tableView.mj_header endRefreshing];
            [YJProgressHUD showMessage:networkModel.Msg inView:self.view];
        }
    }];
}
#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.fullName = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
    [self.searchBar resignFirstResponder];
}
//- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//    self.fullName = searchBar.text;
//    [self.tableView.mj_header beginRefreshing];
//}
 
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [self.view addSubview:self.maskBtn];
    self.navigationItem.leftBarButtonItem.enabled = NO;
    self.navigationItem.rightBarButtonItem.enabled = NO;
    return YES;
}
 
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self.maskBtn removeFromSuperview];
    self.navigationItem.leftBarButtonItem.enabled = YES;
    self.navigationItem.rightBarButtonItem.enabled = YES;
    if ([searchBar.text isEqualToString:@""]) {
        self.fullName = @"";
        [self.tableView.mj_header beginRefreshing];
    }
}
 
- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] init];
    self.tableView.rowHeight = 120;
    [self.tableView registerClass:[PBProjectTableViewCell class] forCellReuseIdentifier:cellID];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self.view);
    }];
    PBMineView *mineView = [[PBMineView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth * 0.82, MainScreenHeight)];
    mineView.customDelegate = self;
    mineView.realName = Realname;
    self.mineView = mineView;
    self.menu = [[MenuView alloc]initWithDependencyView:self.view MenuView:mineView isShowCoverView:YES];
    
    [self.view addSubview:self.promptView];
    self.promptView.imageV.image = [UIImage imageNamed:@"Doc_unable_preview"];
    self.promptView.textL.text = @"抱歉,暂时没有项目";
    self.promptView.hidden = YES;
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.projectModel = self.dataList[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBTabBarController *tabBarController = [[PBTabBarController alloc] init];
    tabBarController.projectModel = self.dataList[indexPath.row];
    tabBarController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:tabBarController animated:YES completion:nil];
}
 
#pragma mark - HomeMenuViewDelegate
- (void)LeftMenuViewClick:(NSInteger)tag{
    [self.menu hidenWithAnimation];
    switch (tag) {
        case 0:{
            //修改密码
            PBChangePWController  *changePwVC = [[PBChangePWController alloc] init];
            changePwVC.ChangePWCompleteBlock = ^{
                [self toLogin];
            };
            [self.navigationController pushViewController:changePwVC animated:YES];
        }
            break;
        case 1:{
            //清除缓存
            [self clearCache];
        }
            break;
        case 10:{
            [self toLogin];
        }
            break;
        default:
            break;
    }
}
- (void)clearCache {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"将删除所有缓存数据" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *clearAction = [UIAlertAction actionWithTitle:@"清除缓存" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        [YJProgressHUD showProgress:@"正在删除" inView:self.view];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@",cachePath);
        NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtPath:cachePath];
        for (NSString *fileName in fileEnumerator) {
            NSString *filePath = [cachePath stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:filePath error:nil];
        }
        [YJProgressHUD showMessage:@"删除完成" inView:self.view];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:clearAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
 
- (UIButton *)maskBtn {
    if (_maskBtn == nil) {
        _maskBtn = [[UIButton alloc] initWithFrame:self.view.bounds];
        _maskBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        [_maskBtn addTarget:self action:@selector(cancelMaskBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskBtn;
}
 
- (void)cancelMaskBtn {
    [self.searchBar resignFirstResponder];
}
- (PBPromptView *)promptView {
    if (_promptView == nil) {
        _promptView = [[PBPromptView alloc] initWithFrame:self.view.bounds];
    }
    return _promptView;
}
- (PBRefreshGifHeader *)headerView {
    if(_headerView == nil) {
        _headerView = [[PBRefreshGifHeader alloc] init];
        __weak typeof(self) weakSelf = self;
        [self.headerView setRefreshingBlock:^{
            [weakSelf loadProjectList];
        }];
    }
    return _headerView;
}
@end