Files
WebApp/ShenQi/NotiCenterViewController.m
2025-01-11 14:58:16 +08:00

212 lines
7.9 KiB
Objective-C

//
// NotiCenterViewController.m
// ShenQi
//
// Created by twotutoo on 2025/1/11.
//
#import "NotiCenterViewController.h"
#import "NotiCenterTableViewCell.h"
#import "NotiCenterContentTableViewCell.h"
#import "NotiCenterImageTableViewCell.h"
#import "NotiCenterJumpLinkTableViewCell.h"
#import "MacroDefine.h"
#import <SVProgressHUD.h>
#import <SDWebImage.h>
#import "DataModels.h"
#import "UIButton+DSButtonBlock.h"
#import "MainOldViewController.h"
static NSString *identifier = @"NotiCenterTableViewCell";
static NSString *contentidentifier = @"NotiCenterContentTableViewCell";
static NSString *imageidentifier = @"NotiCenterImageTableViewCell";
static NSString *linkidentifier = @"NotiCenterJumpLinkTableViewCell";
@interface NotiCenterViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSString *mainUserId;
@property (nonatomic, strong) NSMutableArray *notiArray;
@end
@implementation NotiCenterViewController
- (IBAction)gobackAction:(id)sender
{
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mainUserId = TESTUserID;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.sectionHeaderHeight = 10;
self.tableView.sectionFooterHeight = 0;
self.tableView.backgroundColor = UIColorFromRGB(0xFAFAFA);
[self.tableView registerNib:[UINib nibWithNibName:@"NotiCenterTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
[self.tableView registerNib:[UINib nibWithNibName:@"NotiCenterContentTableViewCell" bundle:nil] forCellReuseIdentifier:contentidentifier];
[self.tableView registerNib:[UINib nibWithNibName:@"NotiCenterImageTableViewCell" bundle:nil] forCellReuseIdentifier:imageidentifier];
[self.tableView registerNib:[UINib nibWithNibName:@"NotiCenterJumpLinkTableViewCell" bundle:nil] forCellReuseIdentifier:linkidentifier];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.tableFooterView = [UIView new];
self.notiArray = @[].mutableCopy;
[self setupDatasource];
}
-(void)setupDatasource
{
NSString *urlstring = [NSString stringWithFormat:@"%@?userId=%@&page=1&size=100",PushRecordsURL,self.mainUserId];
NSURL *url = [NSURL URLWithString:urlstring];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[SVProgressHUD showInfoWithStatus:error.localizedDescription];
}else{
NSError *error;
NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (error) {
[SVProgressHUD showInfoWithStatus:error.localizedDescription];
}else{
if (ISNULL(error) && !ISNULL(responseObject) && [[responseObject objectForKey:@"code"] integerValue] == 1) {
PushRecordDataModel *model = [[PushRecordDataModel alloc] initWithDictionary:responseObject];
for (PushRecordList *list in self.notiArray) {
if (list.type == 2) {
list.imageHeight = SCREEN_WIDTH-30;
}
}
[self.notiArray addObjectsFromArray:model.data.list];
[self.tableView reloadData];
[self refreshImageDatasource];
}
}
}
});
}];
[dataTask resume];
}
-(void)refreshImageDatasource
{
for (PushRecordList *list in self.notiArray) {
NSURL *url = [NSURL URLWithString:list.image];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (image && error == nil) {
list.imageHeight = (SCREEN_WIDTH-30)*image.size.height/image.size.width;
}
}];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.notiArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
PushRecordList *list = self.notiArray[section];
if (list.isOpen == YES) {
return 2;
}else{
return 1;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// PushRecordList *list = self.notiArray[indexPath.section];
// if (list.isOpen == YES) {
// if (indexPath.row == 0) {
// return UITableViewAutomaticDimension;
// }else{
// if (list.type == 2) {
// return list.imageHeight;
// }else{
// return UITableViewAutomaticDimension;
// }
// }
// }else{
// return UITableViewAutomaticDimension;
// }
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return CGFLOAT_MIN;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PushRecordList *list = self.notiArray[indexPath.section];
if (indexPath.row == 0) {
NotiCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.list = list;
if (list.isOpen == YES) {
[cell.arrowBtn setImage:[UIImage imageNamed:@"s"] forState:(UIControlStateNormal)];
}else{
[cell.arrowBtn setImage:[UIImage imageNamed:@"xaingxia"] forState:(UIControlStateNormal)];
}
[cell.arrowBtn addAction:^(UIButton * _Nullable btn) {
list.isOpen = !list.isOpen;
[self.tableView reloadData];
}];
return cell;
}else if (indexPath.row == 1) {
if (list.type == 1) {
NotiCenterContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contentidentifier];
cell.list = list;
return cell;
}else if (list.type == 2) {
NotiCenterImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imageidentifier];
cell.list = list;
return cell;
}else if (list.type == 3) {
NotiCenterJumpLinkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:linkidentifier];
cell.list = list;
[cell.linkBtn addAction:^(UIButton * _Nullable btn) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MainOldViewController *view = [sb instantiateViewControllerWithIdentifier:@"MainOldViewController"];
view.isPresent = YES;
view.gameURLString = list.jumpUrl;
view.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:view animated:YES completion:^{}];
}];
return cell;
}
}
return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PushRecordList *list = self.notiArray[indexPath.section];
if (indexPath.row == 0) {
list.isOpen = !list.isOpen;
[self.tableView reloadData];
}
}
@end