AnnounceViewController.m 14.1 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
        NSLog(@"公告response = %@", response);
admin's avatar
admin committed
82 83 84 85 86 87
        if (response[@"success"]) {
            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
88 89 90 91 92 93 94
            }
            
            
            if (_allAnnoArray.count == 0) {
                self.noDataView.backgroundColor = [UIColor whiteColor];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            } else {
admin's avatar
admin committed
95 96
                self.tableView.delegate = self;
                self.tableView.dataSource = self;
admin's avatar
admin committed
97
                [MBProgressHUD hideHUDForView:self.view animated:YES];
admin's avatar
admin committed
98 99 100 101 102
            }
        }
    }];
}

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

admin's avatar
admin committed
109
- (void)setNav
admin's avatar
admin committed
110
{
admin's avatar
admin committed
111 112 113 114 115
    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
116
    
admin's avatar
admin committed
117 118 119 120 121 122
    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
123

admin's avatar
admin committed
124
}
admin's avatar
admin committed
125

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

admin's avatar
admin committed
131 132
}

admin's avatar
admin committed
133
- (void)segmentChangedValue:(UISegmentedControl *)sender
admin's avatar
admin committed
134
{
admin's avatar
admin committed
135 136 137 138 139
    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
140
        self.stateStr = @"false";
admin's avatar
admin committed
141 142
        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
143
        self.stateStr = @"true";
admin's avatar
admin committed
144 145 146 147 148 149 150
        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
151 152 153
    NSLog(@"22url ==== %@", url);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

admin's avatar
admin committed
154 155
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    [httpClient getAnnounceListWithCompletion:^(id response, NSError *error) {
admin's avatar
admin committed
156
        NSLog(@"segemnt 公告 = %@ error = %@", response, error);
admin's avatar
admin committed
157 158 159 160 161 162 163 164
        if (response[@"success"]) {
            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
165
            _allAnnoArray = [NSMutableArray arrayWithArray:annoArray];
admin's avatar
admin committed
166
        }
admin's avatar
admin committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        
        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];
184

admin's avatar
admin committed
185 186 187
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
       
admin's avatar
admin committed
188 189 190
    }];
    

admin's avatar
admin committed
191 192
}

admin's avatar
admin committed
193 194
#pragma mark - UITableView Delegate/DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
admin's avatar
admin committed
195
{
196
    NSLog(@"cooount = %d", (int)_allAnnoArray.count);
admin's avatar
admin committed
197
    return _allAnnoArray.count;
admin's avatar
admin committed
198
}
admin's avatar
admin committed
199

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

admin's avatar
admin committed
213 214
// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
admin's avatar
admin committed
215
{
admin's avatar
admin committed
216 217 218 219
    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
220 221 222
}


admin's avatar
admin committed
223 224 225 226 227 228
#pragma mark - lazy laoding
- (UIView *)bgBar
{
    if (!_bgBar) {
        _bgBar = [[UIView alloc] init];
        _bgBar.translatesAutoresizingMaskIntoConstraints = NO;
admin's avatar
admin committed
229 230
        _bgBar.layer.borderColor = kSeparateLineCGColor;
        _bgBar.layer.borderWidth = 0.5;
admin's avatar
admin committed
231
        [self.view addSubview:_bgBar];
admin's avatar
admin committed
232
        
admin's avatar
admin committed
233 234
        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
235
        
admin's avatar
admin committed
236 237
        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
238
        
admin's avatar
admin committed
239 240
        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
241
        
admin's avatar
admin committed
242 243
        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
244
    }
admin's avatar
admin committed
245
    return _bgBar;
admin's avatar
admin committed
246 247
}

admin's avatar
admin committed
248
- (UISegmentedControl *)segmentedControl
admin's avatar
admin committed
249
{
admin's avatar
admin committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    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
272
    }
admin's avatar
admin committed
273
    return _segmentedControl;
admin's avatar
admin committed
274 275
}

admin's avatar
admin committed
276
- (UITableView *)tableView
admin's avatar
admin committed
277
{
admin's avatar
admin committed
278 279 280 281 282 283
    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
284 285 286
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100.0;
        [_tableView registerClass:[AnnoTableViewCell class] forCellReuseIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
287 288
        
        [self.view addSubview:_tableView];
admin's avatar
admin committed
289
        
admin's avatar
admin committed
290 291
        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
292
        
admin's avatar
admin committed
293 294
        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
295
        
admin's avatar
admin committed
296 297
        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
298
        
admin's avatar
admin committed
299 300
        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
301
    }
admin's avatar
admin committed
302
    return _tableView;
admin's avatar
admin committed
303 304
}

admin's avatar
admin committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
- (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
327
@end