Skip to content

Instantly share code, notes, and snippets.

@mactive
Created April 1, 2014 08:25
Show Gist options
  • Save mactive/9910079 to your computer and use it in GitHub Desktop.
Save mactive/9910079 to your computer and use it in GitHub Desktop.
类簇 抽象工厂
//
// //
// //
// CommonListViewController.h
// bitmedia
//
// Created by meng qian on 14-1-22.
// Copyright (c) 2014年 thinktube. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MMViewController.h"
@protocol CommonListDelegate <NSObject>
@optional
- (void)setupDataSource;
- (void)recomendNewItems;
- (void)recomendOldItems;
@end
@interface CommonListViewController : MMViewController
@property(nonatomic, strong)NSString *categoryID;
@property(nonatomic, strong)NSString *categoryType;
@property(nonatomic, assign)NSObject<CommonListDelegate> *commonListDelegate;
- (void)initDataSource;
- (void)showSetupDataSource:(id)responseObject andError:(NSError *)error;
- (void)showRecomendNewItems:(id)responseObject andError:(NSError *)error;
- (void)showRecomendOldItems:(id)responseObject andError:(NSError *)error;
- (NSString *)getFirstOrLastArticleID:(BOOL)isFirstOne;
- (NSUInteger)getTimeWithFirstOne:(BOOL)isFirstOne;
@end
// bitmedia
//
// Created by meng qian on 14-1-22.
// Copyright (c) 2014年 thinktube. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MMViewController.h"
@protocol CommonListDelegate <NSObject>
@optional
- (void)setupDataSource;
- (void)recomendNewItems;
- (void)recomendOldItems;
@end
@interface CommonListViewController : MMViewController
@property(nonatomic, strong)NSString *categoryID;
@property(nonatomic, strong)NSString *categoryType;
@property(nonatomic, assign)NSObject<CommonListDelegate> *commonListDelegate;
- (void)initDataSource;
- (void)showSetupDataSource:(id)responseObject andError:(NSError *)error;
- (void)showRecomendNewItems:(id)responseObject andError:(NSError *)error;
- (void)showRecomendOldItems:(id)responseObject andError:(NSError *)error;
- (NSString *)getFirstOrLastArticleID:(BOOL)isFirstOne;
- (NSUInteger)getTimeWithFirstOne:(BOOL)isFirstOne;
@end
// bitmedia
//
// Created by meng qian on 14-1-22.
// Copyright (c) 2014年 thinktube. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MMViewController.h"
@protocol CommonListDelegate <NSObject>
@optional
- (void)setupDataSource;
- (void)recomendNewItems;
- (void)recomendOldItems;
@end
@interface CommonListViewController : MMViewController
@property(nonatomic, strong)NSString *categoryID;
@property(nonatomic, strong)NSString *categoryType;
@property(nonatomic, assign)NSObject<CommonListDelegate> *commonListDelegate;
- (void)initDataSource;
- (void)showSetupDataSource:(id)responseObject andError:(NSError *)error;
- (void)showRecomendNewItems:(id)responseObject andError:(NSError *)error;
- (void)showRecomendOldItems:(id)responseObject andError:(NSError *)error;
- (NSString *)getFirstOrLastArticleID:(BOOL)isFirstOne;
- (NSUInteger)getTimeWithFirstOne:(BOOL)isFirstOne;
@end
//
// CommonListViewController.m
// bitmedia
//
// Created by meng qian on 13-12-24.
// Copyright (c) 2013年 thinktube. All rights reserved.
//
#import "CommonListViewController.h"
#import "ArticleViewController.h"
#import "ArticleTableViewCell.h"
#import "SVPullToRefresh.h"
#import "AppRequestManager.h"
#import <MMDrawerController/MMDrawerBarButtonItem.h>
#import <MMDrawerController/UIViewController+MMDrawerController.h>
#import <TSMessages/TSMessage.h>
#import "FAHoverButton.h"
#import "NoDataView.h"
#import "NSString+Size.h"
#import "LoadingNoticeView.h"
@interface CommonListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSString *latestID;
@end
@implementation CommonListViewController
@synthesize tableView = tableView;
@synthesize dataSource;
@synthesize categoryID;
@synthesize latestID;
@synthesize categoryType;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = BGCOLOR;
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.backgroundColor = BGCOLOR;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.separatorColor = SEPCOLOR;
self.tableView.delegate = self;
self.tableView.dataSource = self;
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
UIEdgeInsets currentInset = self.tableView.contentInset;
// Manully set contentInset.
if (OSVersionIsAtLeastiOS7()) {
currentInset.top = self.navigationController.navigationBar.bounds.size.height;
self.automaticallyAdjustsScrollViewInsets = NO;
// On iOS7, you need plus the height of status bar.
currentInset.top = 0;
// currentInset.bottom += IOS7_CONTENT_OFFSET_Y ;
self.tableView.height = self.tableView.height-IOS7_CONTENT_OFFSET_Y-TABS_VIEW_HEIGHT;
}else{
DDLogVerbose(@"ios 6");
currentInset.bottom += TABBAR_HEIGHT;
}
self.tableView.contentInset = currentInset;
[self.view addSubview:self.tableView];
[self initTableViewBgView];
self.latestID = @"";
__weak CommonListViewController *weakSelf = self;
// self.tableView
// setup pull-to-refresh
[self.tableView addPullToRefreshWithActionHandler:^{
[weakSelf refreshTable];
}];
//setup infinite scrolling
[self.tableView addInfiniteScrollingWithActionHandler:^{
[weakSelf getMoreData];
}];
// if no network set enable
}
- (void)initDataSource
{
[self.commonListDelegate setupDataSource];
}
- (void)initTableViewBgView
{
// loading label
UIView *bgView = [[UIView alloc]initWithFrame:self.tableView.frame];
[bgView setBackgroundColor:BGCOLOR];
NoDataView *noDataView = [[NoDataView alloc] initWithFrame:
CGRectMake((TOTAL_WIDTH-NO_DATA_WIDTH)/2, (bgView.frame.size.height-NO_DATA_WIDTH)/2, NO_DATA_WIDTH, NO_DATA_WIDTH)];
[noDataView setTitleString:[NSString stringWithFormat:T(@"%@\n正在为你加载\n......"),APP_NAME]];
[bgView addSubview:noDataView];
self.tableView.backgroundView = bgView;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// [self.tableView triggerPullToRefresh];
// [XFox logEvent:[NSString stringWithFormat:@"%@#%@",EVENT_CATEGORY_TIMER,self.categoryID] timed:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
// [XFox endTimedEvent:[NSString stringWithFormat:@"%@#%@",EVENT_CATEGORY_TIMER,self.categoryID] withParameters:@{@"id": self.categoryID}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
////////////////////////////////////////////////////////////
#pragma mark - getFirstOrLastArticleID Actions
////////////////////////////////////////////////////////////
- (NSString *)getFirstOrLastArticleID:(BOOL)isFirstOne
{
NSString* articleID = @"";
if ([self.dataSource count] > 0) {
if (isFirstOne) {
for (NSDictionary* item in self.dataSource) {
if([ServerDataTransformer getArticleIsFirst:item]){
articleID = [ServerDataTransformer getArticleID:item];
}
}
}else{
for (NSDictionary* item in self.dataSource) {
if([ServerDataTransformer getArticleIsLast:item]){
articleID = [ServerDataTransformer getArticleID:item];
}
}
}
}else{
articleID = @"";
}
NSLog(@"getFirstOrLastArticleID: %@",articleID);
return articleID;
}
- (NSUInteger)getTimeWithFirstOne:(BOOL)isFirstOne
{
NSUInteger time = 0;
if ([self.dataSource count] > 0) {
if (isFirstOne) {
NSDictionary *firstOne = [self.dataSource firstObject];
NSDate *timeDate = [ServerDataTransformer getArticleDate:firstOne];
time = [timeDate timeIntervalSince1970];
}else{
NSDictionary *lastOne = [self.dataSource lastObject];
NSDate *timeDate = [ServerDataTransformer getArticleDate:lastOne];
time = [timeDate timeIntervalSince1970];
}
}else{
time = [[NSDate date]timeIntervalSince1970];
}
NSLog(@"getTimeWithFirstOne: %i",time);
return time;
}
- (void)showSetupDataSource:(id)responseObject andError:(NSError *)error
{
self.dataSource = [[NSMutableArray alloc] init];
self.latestID = @"";
self.dataSource = [ServerDataTransformer getDataArray:responseObject withArticleMode:ArticleModeUnreaded];
NSDictionary *latestItem = [self.dataSource lastObject];
self.latestID = [ServerDataTransformer getArticleID:latestItem];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
self.tableView.backgroundView = nil;
[self.tableView reloadData];
DDLogVerbose(@"setupDataSource %d 条", [self.dataSource count]);
}
- (void)showRecomendNewItems:(id)responseObject andError:(NSError *)error
{
__weak CommonListViewController *weakSelf = self;
NSMutableArray *data = [ServerDataTransformer getDataArray:responseObject withArticleMode:ArticleModeUnreaded];
NSLog(@"new data : %d",[data count]);
// 大于10条清空列表
if ([data count] >= 20) {
self.dataSource = [[NSMutableArray alloc]init];
}
if ([data count] <= 0) {
DDLogError(@"no data");
[TSMessage setDefaultViewController:self];
[TSMessage showNotificationWithTitle:T(@"暂无更新,休息一会儿") type:TSMessageNotificationTypeSuccess];
return;
}else{
/**
* insert new article
*/
[weakSelf.tableView beginUpdates];
NSMutableArray *indexPaths = [[NSMutableArray alloc]init];
NSInteger _count = [data count]-1;
for (int idx = _count; idx >= 0; idx--) { //[data count]
NSDictionary *object = [data objectAtIndex:idx];
NSLog(@"insert top %@", [ServerDataTransformer getArticleTitle:object]);
[weakSelf.dataSource insertObject:object atIndex:0];
[indexPaths addObject:[NSIndexPath indexPathForRow:idx inSection:0]];
}
[weakSelf.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[weakSelf.tableView endUpdates];
NSString *message = [NSString stringWithFormat:T(@"为您推荐了%d篇文章"),[data count]];
[TSMessage setDefaultViewController:self];
[TSMessage showNotificationWithTitle:message type:TSMessageNotificationTypeSuccess];
}
}
- (void)showRecomendOldItems:(id)responseObject andError:(NSError *)error
{
__weak CommonListViewController *weakSelf = self;
NSMutableArray *data = [ServerDataTransformer getDataArray:responseObject withArticleMode:ArticleModeUnreaded];
NSLog(@"old data : %d",[data count]);
if ([data count] > 0) {
[weakSelf.tableView beginUpdates];
for (NSDictionary *object in data) {
[weakSelf.dataSource addObject:object];
[weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:weakSelf.dataSource.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
}
[weakSelf.tableView endUpdates];
}else{
// 找不到更旧的数据时
[weakSelf.tableView.infiniteScrollingView setTitle:T(@"全部加载完成") forState:SVPullToRefreshStateStopped];
// [weakSelf.tableView.infiniteScrollingView setEnabled:NO];
}
}
////////////////////////////////////////////////////////////
#pragma mark - tableview and datasource action
////////////////////////////////////////////////////////////
- (void)refreshTable
{
__weak CommonListViewController *weakSelf = self;
// [weakSelf.tableView.pullToRefreshView setTitle:T(@">_< 努力加载中..") forState:SVPullToRefreshStateAll];
int64_t delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.commonListDelegate recomendNewItems];
[weakSelf.tableView.pullToRefreshView stopAnimating];
});
}
- (void)getMoreData {
__weak CommonListViewController *weakSelf = self;
int64_t delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.commonListDelegate recomendOldItems];
[weakSelf.tableView.infiniteScrollingView stopAnimating];
});
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *cellData = [self.dataSource objectAtIndex:indexPath.row];
CGFloat middle_column_width = MIDDLE_COLUMN_FULL_WIDTH;
if (StringHasValue([ServerDataTransformer getArticleCover:cellData])) {
middle_column_width = MIDDLE_COLUMN_WIDTH;
}
NSString *nameString = [ServerDataTransformer getArticleTitle:cellData];
CGSize nameSize = [nameString sizeWithWidth:middle_column_width andFont:FONT_16];
if (nameSize.height + TOP_PADDING*3 > ARTICLE_CELL_HEIGHT) {
return nameSize.height + TOP_PADDING*3;
}else{
return ARTICLE_CELL_HEIGHT;
}
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"ListCell";
NSDictionary *cellData = [self.dataSource objectAtIndex:indexPath.row];
ArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ArticleTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell setNewData:cellData];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ArticleViewController *viewController = [[ArticleViewController alloc]initWithNibName:nil bundle:nil];
NSMutableDictionary *cellData = [self.dataSource objectAtIndex:indexPath.row];
[cellData setValue:INT(ArticleModeReaded) forKey:@"articleMode"];
viewController.baseInfo = cellData;
[self.navigationController pushViewController:viewController animated:YES];
ArticleTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setArticleMode];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
//
// ListViewController.h
// bitmedia
//
// Created by meng qian on 14-1-22.
// Copyright (c) 2014年 thinktube. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CommonListViewController.h"
@interface ListViewController : CommonListViewController
@property(nonatomic, strong)NSString *categoryID;
@property(nonatomic, strong)NSString *categoryType;
@end
//
// //
// ListViewController.m
// bitmedia
//
// Created by meng qian on 13-12-24.
// Copyright (c) 2013年 thinktube. All rights reserved.
//
#import "ListViewController.h"
#import "AppRequestManager.h"
@interface ListViewController ()<UITableViewDataSource, UITableViewDelegate, CommonListDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSString *latestID;
@end
@implementation ListViewController
@synthesize tableView = tableView;
@synthesize dataSource;
@synthesize categoryID;
@synthesize latestID;
@synthesize categoryType;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.commonListDelegate = self;
[self initDataSource];
}
- (void)initDataSource
{
[super initDataSource];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// [self.tableView triggerPullToRefresh];
// [XFox logEvent:[NSString stringWithFormat:@"%@#%@",EVENT_CATEGORY_TIMER,self.categoryID] timed:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
// [XFox endTimedEvent:[NSString stringWithFormat:@"%@#%@",EVENT_CATEGORY_TIMER,self.categoryID] withParameters:@{@"id": self.categoryID}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
////////////////////////////////////////////////////////////
#pragma mark - Network Actions
////////////////////////////////////////////////////////////
/**
* 初始化文章
*/
- (void)setupDataSource {
NSString *firstID = @"";
[[AppRequestManager sharedManager]getArticleListWithCategoryID:self.categoryID request:@"new" latest_id:firstID andBlock:^(id responseObject, NSError *error) {
[super showSetupDataSource:responseObject andError:(NSError *)error];
}];
}
/**
* 推荐新的文章
*/
- (void)recomendNewItems
{
NSString *firstID = [self getFirstOrLastArticleID:YES];
[[AppRequestManager sharedManager]getArticleListWithCategoryID:self.categoryID request:@"new" latest_id:firstID andBlock:^(id responseObject, NSError *error) {
[super showRecomendNewItems:responseObject andError:error];
}];
}
/**
* 推荐旧的文章
*/
- (void)recomendOldItems
{
NSString *lastID = [self getFirstOrLastArticleID:NO];
[[AppRequestManager sharedManager]getArticleListWithCategoryID:self.categoryID request:@"old" latest_id:lastID andBlock:^(id responseObject, NSError *error) {
[super showRecomendOldItems:responseObject andError:error];
}];
}
@end
// bitmedia
//
// Created by meng qian on 13-12-24.
// Copyright (c) 2013年 thinktube. All rights reserved.
//
#import "ListViewController.h"
#import "AppRequestManager.h"
@interface ListViewController ()<UITableViewDataSource, UITableViewDelegate, CommonListDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger start;
@property (nonatomic, assign)NSString *latestID;
@end
@implementation ListViewController
@synthesize tableView = tableView;
@synthesize dataSource;
@synthesize categoryID;
@synthesize latestID;
@synthesize categoryType;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.commonListDelegate = self;
[self initDataSource];
}
- (void)initDataSource
{
[super initDataSource];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// [self.tableView triggerPullToRefresh];
// [XFox logEvent:[NSString stringWithFormat:@"%@#%@",EVENT_CATEGORY_TIMER,self.categoryID] timed:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
// [XFox endTimedEvent:[NSString stringWithFormat:@"%@#%@",EVENT_CATEGORY_TIMER,self.categoryID] withParameters:@{@"id": self.categoryID}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
////////////////////////////////////////////////////////////
#pragma mark - Network Actions
////////////////////////////////////////////////////////////
/**
* 初始化文章
*/
- (void)setupDataSource {
NSString *firstID = @"";
[[AppRequestManager sharedManager]getArticleListWithCategoryID:self.categoryID request:@"new" latest_id:firstID andBlock:^(id responseObject, NSError *error) {
[super showSetupDataSource:responseObject andError:(NSError *)error];
}];
}
/**
* 推荐新的文章
*/
- (void)recomendNewItems
{
NSString *firstID = [self getFirstOrLastArticleID:YES];
[[AppRequestManager sharedManager]getArticleListWithCategoryID:self.categoryID request:@"new" latest_id:firstID andBlock:^(id responseObject, NSError *error) {
[super showRecomendNewItems:responseObject andError:error];
}];
}
/**
* 推荐旧的文章
*/
- (void)recomendOldItems
{
NSString *lastID = [self getFirstOrLastArticleID:NO];
[[AppRequestManager sharedManager]getArticleListWithCategoryID:self.categoryID request:@"old" latest_id:lastID andBlock:^(id responseObject, NSError *error) {
[super showRecomendOldItems:responseObject andError:error];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment