AnnounceViewController.m 11.4 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 17
#import <MBProgressHUD.h>

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

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

@property (nonatomic, strong) NSMutableArray *allAnnoArray;
admin's avatar
admin committed
27 28 29 30 31 32 33
@end

@implementation AnnounceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
admin's avatar
admin committed
34
    self.view.backgroundColor = [UIColor whiteColor];
admin's avatar
admin committed
35
    
admin's avatar
admin committed
36 37 38 39 40 41 42
    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
43 44 45 46 47
    self.allAnnoArray = [NSMutableArray array];
    
    self.isRead = NO;
    self.stateStr = @"false";
    
admin's avatar
admin committed
48 49 50
    
    [self setNav];
    
admin's avatar
admin committed
51
    [self setup];
admin's avatar
admin committed
52 53
    
    [self requestAnnoList];
admin's avatar
admin committed
54 55
}

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

admin's avatar
admin committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
- (void)requestAnnoList
{
    
    NSString *user_uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_uuid"];
    int page_number = 0;
    int page_size = 10;
    NSString *url = [NSString stringWithFormat:@"%@%@%@?read=%@&page_number=%d&page_size=%d", kRedStarURL, kAnnounceListURL ,user_uuid ,_stateStr, page_number, page_size];
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    [httpClient getAnnounceListWithCompletion:^(id response, NSError *error) {
        if (response[@"success"]) {
            NSDictionary *dict = response[@"data"];
            NSArray *array = dict[@"records"];
            for (NSDictionary *annoDict in array) {
                AnnounceModel *anno = [AnnounceModel announceModelWithDict:annoDict];
                [_allAnnoArray addObject:anno];
                
                self.tableView.delegate = self;
                self.tableView.dataSource = self;
            }
            
        }
    }];
}

admin's avatar
admin committed
90 91 92 93
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
admin's avatar
admin committed
94 95
}

admin's avatar
admin committed
96
- (void)setNav
admin's avatar
admin committed
97
{
admin's avatar
admin committed
98 99 100 101 102
    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
103
    
admin's avatar
admin committed
104 105 106 107 108 109
    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
110

admin's avatar
admin committed
111
}
admin's avatar
admin committed
112

admin's avatar
admin committed
113
- (void)setup
admin's avatar
admin committed
114
{
admin's avatar
admin committed
115 116 117
    self.bgBar.backgroundColor = [UIColor whiteColor];
    [self.segmentedControl addTarget:self action:@selector(segmentChangedValue:) forControlEvents:UIControlEventValueChanged];

admin's avatar
admin committed
118 119
}

admin's avatar
admin committed
120
- (void)segmentChangedValue:(UISegmentedControl *)sender
admin's avatar
admin committed
121
{
admin's avatar
admin committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    NSString *url;
    NSString *user_uuid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_uuid"];
    int page_number = 0;
    int page_size = 10;
    if (sender.selectedSegmentIndex == 0) {
        self.stateStr = @"false";
        url = [NSString stringWithFormat:@"%@%@%@?read=%@&page_number=%d&page_size=%d", kRedStarURL, kAnnounceListURL ,user_uuid ,_stateStr, page_number, page_size];
    } else if (sender.selectedSegmentIndex == 1) {
        self.stateStr = @"true";
        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
138 139
    
    
admin's avatar
admin committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    [httpClient getAnnounceListWithCompletion:^(id response, NSError *error) {
        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];
            }
            _allAnnoArray = annoArray;
            [self.tableView reloadData];
        }
    }];
    

admin's avatar
admin committed
156 157
}

admin's avatar
admin committed
158 159
#pragma mark - UITableView Delegate/DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
admin's avatar
admin committed
160
{
admin's avatar
admin committed
161
    return _allAnnoArray.count;
admin's avatar
admin committed
162
}
admin's avatar
admin committed
163

admin's avatar
admin committed
164 165 166
// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
167
    AnnoTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
168
    if (!cell) {
admin's avatar
admin committed
169
        cell = [[AnnoTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
170
    }
admin's avatar
admin committed
171 172 173
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.currentState = self.stateStr;
    cell.announce = _allAnnoArray[indexPath.row];
admin's avatar
admin committed
174
    return cell;
admin's avatar
admin committed
175 176
}

admin's avatar
admin committed
177 178
// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
admin's avatar
admin committed
179
{
admin's avatar
admin committed
180 181 182 183
    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
184 185 186
}


admin's avatar
admin committed
187 188 189 190 191 192
#pragma mark - lazy laoding
- (UIView *)bgBar
{
    if (!_bgBar) {
        _bgBar = [[UIView alloc] init];
        _bgBar.translatesAutoresizingMaskIntoConstraints = NO;
admin's avatar
admin committed
193 194
        _bgBar.layer.borderColor = kSeparateLineCGColor;
        _bgBar.layer.borderWidth = 0.5;
admin's avatar
admin committed
195
        [self.view addSubview:_bgBar];
admin's avatar
admin committed
196
        
admin's avatar
admin committed
197 198
        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
199
        
admin's avatar
admin committed
200 201
        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
202
        
admin's avatar
admin committed
203 204
        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
205
        
admin's avatar
admin committed
206 207
        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
208
    }
admin's avatar
admin committed
209
    return _bgBar;
admin's avatar
admin committed
210 211
}

admin's avatar
admin committed
212
- (UISegmentedControl *)segmentedControl
admin's avatar
admin committed
213
{
admin's avatar
admin committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    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
236
    }
admin's avatar
admin committed
237
    return _segmentedControl;
admin's avatar
admin committed
238 239
}

admin's avatar
admin committed
240
- (UITableView *)tableView
admin's avatar
admin committed
241
{
admin's avatar
admin committed
242 243 244 245 246 247
    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
248 249 250
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 100.0;
        [_tableView registerClass:[AnnoTableViewCell class] forCellReuseIdentifier:kAnnoTableViewCell];
admin's avatar
admin committed
251 252
        
        [self.view addSubview:_tableView];
admin's avatar
admin committed
253
        
admin's avatar
admin committed
254 255
        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
256
        
admin's avatar
admin committed
257 258
        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
259
        
admin's avatar
admin committed
260 261
        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
262
        
admin's avatar
admin committed
263 264
        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
265
    }
admin's avatar
admin committed
266
    return _tableView;
admin's avatar
admin committed
267 268 269
}

@end