AnnounceViewController.m 14 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9
//
//  AnnounceViewController.m
//  redstar
//
//  Created by admin on 15/10/24.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "AnnounceViewController.h"
admin's avatar
admin committed
10 11 12 13
#import "AnnoTableViewCell.h"
#import "HttpClient.h"
#import "AnnounceModel.h"
#import "AnnoDetailViewController.h"
admin's avatar
admin committed
14

admin's avatar
admin committed
15 16
#import "NoDataView.h"

admin's avatar
admin committed
17 18 19
#import <MBProgressHUD.h>

#define kAnnoTableViewCell @"AnnoTableViewCell"
admin's avatar
admin committed
20 21 22
@interface AnnounceViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *bgBar;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
admin's avatar
admin committed
23

admin's avatar
admin committed
24 25
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *stateStr;
admin's avatar
admin committed
26 27
@property (nonatomic, assign) BOOL isRead;

admin's avatar
admin committed
28 29
@property (nonatomic, strong) NoDataView *noDataView;

admin's avatar
admin committed
30
@property (nonatomic, strong) NSMutableArray *allAnnoArray;
admin's avatar
admin committed
31 32 33 34 35 36 37
@end

@implementation AnnounceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
admin's avatar
admin committed
38
    self.view.backgroundColor = [UIColor whiteColor];
admin's avatar
admin committed
39
    
admin's avatar
admin committed
40 41 42 43 44 45 46
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.translucent = NO;
    }
    
admin's avatar
admin committed
47 48 49
    self.allAnnoArray = [NSMutableArray array];
    
    self.isRead = NO;
admin's avatar
admin committed
50
    self.stateStr = @"false";
admin's avatar
admin committed
51
    
admin's avatar
admin committed
52 53 54
    
    [self setNav];
    
admin's avatar
admin committed
55
    [self setup];
admin's avatar
admin committed
56 57
    
    [self requestAnnoList];
admin's avatar
admin committed
58 59
}

admin's avatar
admin committed
60
- (void)viewWillAppear:(BOOL)animated
admin's avatar
admin committed
61
{
admin's avatar
admin committed
62 63 64 65 66 67 68
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}
#pragma mark - Prative Methods
admin's avatar
admin committed
69

admin's avatar
admin committed
70 71 72 73 74 75
- (void)requestAnnoList
{
    
    NSString *user_uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_uuid"];
    int page_number = 0;
    int page_size = 10;
admin's avatar
admin committed
76
    NSString *url = [NSString stringWithFormat:@"%@%@%@?read=%@&page_number=%d&page_size=%d", kRedStarURL, kAnnounceListURL ,user_uuid ,_stateStr, page_number, page_size];
77

admin's avatar
admin committed
78
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
admin's avatar
admin committed
79 80
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    [httpClient getAnnounceListWithCompletion:^(id response, NSError *error) {
admin's avatar
admin committed
81
        if ([response[@"success"] boolValue]) {
admin's avatar
admin committed
82 83 84 85 86
            NSDictionary *dict = response[@"data"];
            NSArray *array = dict[@"records"];
            for (NSDictionary *annoDict in array) {
                AnnounceModel *anno = [AnnounceModel announceModelWithDict:annoDict];
                [_allAnnoArray addObject:anno];
admin's avatar
admin committed
87 88 89 90 91 92 93
            }
            
            
            if (_allAnnoArray.count == 0) {
                self.noDataView.backgroundColor = [UIColor whiteColor];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            } else {
admin's avatar
admin committed
94 95
                self.tableView.delegate = self;
                self.tableView.dataSource = self;
admin's avatar
admin committed
96
                [MBProgressHUD hideHUDForView:self.view animated:YES];
admin's avatar
admin committed
97 98 99 100 101
            }
        }
    }];
}

admin's avatar
admin committed
102 103 104 105
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
admin's avatar
admin committed
106 107
}

admin's avatar
admin committed
108
- (void)setNav
admin's avatar
admin committed
109
{
admin's avatar
admin committed
110 111 112 113 114
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"公告"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
admin's avatar
admin committed
115
    
admin's avatar
admin committed
116 117 118 119 120 121
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
admin's avatar
admin committed
122

admin's avatar
admin committed
123
}
admin's avatar
admin committed
124

admin's avatar
admin committed
125
- (void)setup
admin's avatar
admin committed
126
{
admin's avatar
admin committed
127 128 129
    self.bgBar.backgroundColor = [UIColor whiteColor];
    [self.segmentedControl addTarget:self action:@selector(segmentChangedValue:) forControlEvents:UIControlEventValueChanged];

admin's avatar
admin committed
130 131
}

admin's avatar
admin committed
132
- (void)segmentChangedValue:(UISegmentedControl *)sender
admin's avatar
admin committed
133
{
admin's avatar
admin committed
134 135 136 137 138
    NSString *url;
    NSString *user_uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_uuid"];
    int page_number = 0;
    int page_size = 10;
    if (sender.selectedSegmentIndex == 0) {
admin's avatar
admin committed
139
        self.stateStr = @"false";
admin's avatar
admin committed
140 141
        url = [NSString stringWithFormat:@"%@%@%@?read=%@&page_number=%d&page_size=%d", kRedStarURL, kAnnounceListURL ,user_uuid ,_stateStr, page_number, page_size];
    } else if (sender.selectedSegmentIndex == 1) {
admin's avatar
admin committed
142
        self.stateStr = @"true";
admin's avatar
admin committed
143 144 145 146 147 148 149
        url = [NSString stringWithFormat:@"%@%@%@?read=%@&page_number=%d&page_size=%d", kRedStarURL, kAnnounceListURL ,user_uuid ,_stateStr, page_number, page_size];
    } else {
        self.stateStr = @"";
        url = [NSString stringWithFormat:@"%@%@%@?page_number=%d&page_size=%d", kRedStarURL, kAnnounceListURL ,user_uuid , page_number, page_size];
    }
    
    
admin's avatar
admin committed
150 151
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

admin's avatar
admin committed
152 153
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    [httpClient getAnnounceListWithCompletion:^(id response, NSError *error) {
admin's avatar
admin committed
154
        if ([response[@"success"] boolValue]) {
admin's avatar
admin committed
155 156 157 158 159 160 161
            NSDictionary *dict = response[@"data"];
            NSArray *array = dict[@"records"];
            NSMutableArray *annoArray = [NSMutableArray array];
            for (NSDictionary *annoDict in array) {
                AnnounceModel *anno = [AnnounceModel announceModelWithDict:annoDict];
                [annoArray addObject:anno];
            }
admin's avatar
admin committed
162
            _allAnnoArray = [NSMutableArray arrayWithArray:annoArray];
admin's avatar
admin committed
163
        }
admin's avatar
admin committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        
        if (_allAnnoArray.count == 0) {
            if (_tableView) {
                [_tableView removeFromSuperview];
                _tableView = nil;
            }
            self.noDataView.backgroundColor = [UIColor whiteColor];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } else {
            if (_noDataView) {
                [_noDataView removeFromSuperview];
                _noDataView = nil;

            }
            self.tableView.delegate = self;
            self.tableView.dataSource = self;
            [self.tableView reloadData];
181

admin's avatar
admin committed
182 183 184
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
       
admin's avatar
admin committed
185 186 187
    }];
    

admin's avatar
admin committed
188 189
}

admin's avatar
admin committed
190 191
#pragma mark - UITableView Delegate/DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
admin's avatar
admin committed
192
{
admin's avatar
admin committed
193
    return _allAnnoArray.count;
admin's avatar
admin committed
194
}
admin's avatar
admin committed
195

admin's avatar
admin committed
196 197 198
// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
199
    AnnoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
200
    if (!cell) {
admin's avatar
admin committed
201
        cell = [[AnnoTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
202
    }
admin's avatar
admin committed
203 204 205
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.currentState = self.stateStr;
    cell.announce = _allAnnoArray[indexPath.row];
admin's avatar
admin committed
206
    return cell;
admin's avatar
admin committed
207 208
}

admin's avatar
admin committed
209 210
// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
admin's avatar
admin committed
211
{
admin's avatar
admin committed
212 213 214 215
    AnnounceModel *announce = _allAnnoArray[indexPath.row];
    AnnoDetailViewController *detailVC = [[AnnoDetailViewController alloc] init];
    detailVC.affiche_uuid = announce.uuid;
    [self.navigationController pushViewController:detailVC animated:YES];
admin's avatar
admin committed
216 217 218
}


admin's avatar
admin committed
219 220 221 222 223 224
#pragma mark - lazy laoding
- (UIView *)bgBar
{
    if (!_bgBar) {
        _bgBar = [[UIView alloc] init];
        _bgBar.translatesAutoresizingMaskIntoConstraints = NO;
admin's avatar
admin committed
225 226
        _bgBar.layer.borderColor = kSeparateLineCGColor;
        _bgBar.layer.borderWidth = 0.5;
admin's avatar
admin committed
227
        [self.view addSubview:_bgBar];
admin's avatar
admin committed
228
        
admin's avatar
admin committed
229 230
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_bgBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self.view addConstraint:tableTop];
admin's avatar
admin committed
231
        
admin's avatar
admin committed
232 233
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_bgBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
admin's avatar
admin committed
234
        
admin's avatar
admin committed
235 236
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_bgBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
admin's avatar
admin committed
237
        
admin's avatar
admin committed
238 239
        NSLayoutConstraint *tableHeight = [NSLayoutConstraint constraintWithItem:_bgBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60];
        [self.view addConstraint:tableHeight];
admin's avatar
admin committed
240
    }
admin's avatar
admin committed
241
    return _bgBar;
admin's avatar
admin committed
242 243
}

admin's avatar
admin committed
244
- (UISegmentedControl *)segmentedControl
admin's avatar
admin committed
245
{
admin's avatar
admin committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if (!_segmentedControl) {
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"未读", @"已读", @"历史", nil]];
        _segmentedControl.translatesAutoresizingMaskIntoConstraints = NO;
        _segmentedControl.selectedSegmentIndex = 0;
        UIFont *font = [UIFont systemFontOfSize:16.0f];
        NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                               forKey:NSFontAttributeName];
        [_segmentedControl setTitleTextAttributes:attributes
                                   forState:UIControlStateNormal];
        [self.bgBar addSubview:_segmentedControl];
        
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_segmentedControl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.bgBar attribute:NSLayoutAttributeTop multiplier:1.0 constant:15];
        [self.bgBar addConstraint:tableTop];
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_segmentedControl attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.bgBar attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        [self.bgBar addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_segmentedControl attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.bgBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        [self.bgBar addConstraint:tableRight];
        
        NSLayoutConstraint *tableHeight = [NSLayoutConstraint constraintWithItem:_segmentedControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];
        [self.bgBar addConstraint:tableHeight];
admin's avatar
admin committed
268
    }
admin's avatar
admin committed
269
    return _segmentedControl;
admin's avatar
admin committed
270 271
}

admin's avatar
admin committed
272
- (UITableView *)tableView
admin's avatar
admin committed
273
{
admin's avatar
admin committed
274 275 276 277 278 279
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [[UIView alloc] init];
admin's avatar
admin committed
280 281 282
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100.0;
        [_tableView registerClass:[AnnoTableViewCell class] forCellReuseIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
283 284
        
        [self.view addSubview:_tableView];
admin's avatar
admin committed
285
        
admin's avatar
admin committed
286 287
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.bgBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableTop];
admin's avatar
admin committed
288
        
admin's avatar
admin committed
289 290
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
admin's avatar
admin committed
291
        
admin's avatar
admin committed
292 293
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
admin's avatar
admin committed
294
        
admin's avatar
admin committed
295 296
        NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableBottom];
admin's avatar
admin committed
297
    }
admin's avatar
admin committed
298
    return _tableView;
admin's avatar
admin committed
299 300
}

admin's avatar
admin committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
- (NoDataView *)noDataView
{
    if (!_noDataView) {
        _noDataView = [[NoDataView alloc] init];
        _noDataView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_noDataView];
        
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_noDataView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.bgBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableTop];
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_noDataView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_noDataView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
        
        NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_noDataView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableBottom];
    }
    return _noDataView;
}

admin's avatar
admin committed
323
@end