//
// DFTimeLineViewController.m
// DFTimelineView
//
// Created by Allen Zhong on 15/9/27.
// Copyright (c) 2015骞?Datafans, Inc. All rights reserved.
//
#import "DFTimeLineViewController.h"
#import "DFLineCellAdapterManager.h"
#import "DFTextImageLineCellAdapter.h"
#import "DFBaseLineCell.h"
#import "DFLineLikeItem.h"
#import "DFLineCommentItem.h"
#import "CommentInputView.h"
@interface DFTimeLineViewController ()
@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong) NSMutableDictionary *itemDic;
@property (nonatomic, strong) NSMutableDictionary *commentDic;
@property (strong, nonatomic) CommentInputView *commentInputView;
@property (assign, nonatomic) long long currentItemId;
@end
@implementation DFTimeLineViewController
#pragma mark - Lifecycle
- (instancetype)init
{
self = [super init];
if (self) {
_items = [NSMutableArray array];
_itemDic = [NSMutableDictionary dictionary];
_commentDic = [NSMutableDictionary dictionary];
DFLineCellAdapterManager *manager = [DFLineCellAdapterManager sharedInstance];
DFTextImageLineCellAdapter *textImageCellAdapter = [[DFTextImageLineCellAdapter alloc] init];
[manager registerAdapter:LineItemTypeTextImage adapter:textImageCellAdapter];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initCommentInputView];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_commentInputView addNotify];
[_commentInputView addObserver];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[_commentInputView removeNotify];
[_commentInputView removeObserver];
}
-(void) initCommentInputView
{
if (_commentInputView == nil) {
_commentInputView = [[CommentInputView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_commentInputView.hidden = YES;
_commentInputView.delegate = self;
[self.view addSubview:_commentInputView];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _items.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
DFBaseLineItem *item = [_items objectAtIndex:indexPath.row];
DFBaseLineCellAdapter *adapter = [self getAdapter:item.itemType];
return [adapter getCellHeight:item];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DFBaseLineItem *item = [_items objectAtIndex:indexPath.row];
DFBaseLineCellAdapter *adapter = [self getAdapter:item.itemType];
UITableViewCell *cell = [adapter getCell:tableView];
((DFBaseLineCell *)cell).delegate = self;
cell.separatorInset = UIEdgeInsetsZero;
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}
[adapter updateCell:cell message:item];
return cell;
}
#pragma mark - TabelViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//鐐瑰嚮鎵€鏈塩ell绌虹櫧鍦版柟 闅愯棌toolbar
NSInteger rows = [tableView numberOfRowsInSection:0];
for (int row = 0; row < rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
DFBaseLineCell *cell = (DFBaseLineCell *)[tableView cellForRowAtIndexPath:indexPath];
[cell hideLikeCommentToolbar];
}
}
#pragma mark - Method
-(DFBaseLineCellAdapter *) getAdapter:(LineItemType)itemType
{
DFLineCellAdapterManager *manager = [DFLineCellAdapterManager sharedInstance];
return [manager getAdapter:itemType];
}
-(void)addItem:(DFBaseLineItem *)item
{
[self genLikeAttrString:item];
[self genCommentAttrString:item];
[_items addObject:item];
[_itemDic setObject:item forKey:[NSNumber numberWithLongLong:item.itemId]];
[self.tableView reloadData];
}
-(DFBaseLineItem *) getItem:(long long) itemId
{
return [_itemDic objectForKey:[NSNumber numberWithLongLong:itemId]];
}
-(void)addLikeItem:(DFLineLikeItem *)likeItem itemId:(long long)itemId
{
DFBaseLineItem *item = [self getItem:itemId];
[item.likes insertObject:likeItem atIndex:0];
item.likesStr = nil;
item.cellHeight = 0;
[self genLikeAttrString:item];
[self.tableView reloadData];
}
-(void)addCommentItem:(DFLineCommentItem *)commentItem itemId:(long long)itemId replyCommentId:(long long)replyCommentId
{
DFBaseLineItem *item = [self getItem:itemId];
[链接已屏蔽ments addObject:commentItem];
if (replyCommentId > 0) {
DFLineCommentItem *replyCommentItem = [self getCommentItem:replyCommentId];
commentItem.replyUserId = replyCommentItem.userId;
commentItem.replyUserNick = replyCommentItem.userNick;
}
item.cellHeight = 0;
[self genCommentAttrString:item];
[self.tableView reloadData];
}
-(DFLineCommentItem *)getCommentItem:(long long)commentId
{
return [_commentDic objectForKey:[NSNumber numberWithLongLong:commentId]];
}
#pragma mark - DFLineCellDelegate
-(void)onComment:(long long)itemId
{
_currentItemId = itemId;
链接已屏蔽mentId = 0;
_commentInputView.hidden = NO;
[_commentInputView show];
}
-(void)onLike:(long long)itemId
{
}
-(void)onClickUser:(NSUInteger)userId
{
}
-(void)onClickComment:(long long)commentId itemId:(long long)itemId
{
_currentItemId = itemId;
_commentInputView.hidden = NO;
链接已屏蔽mentId = commentId;
[_commentInputView show];
DFLineCommentItem *comment = [_commentDic objectForKey:[NSNumber numberWithLongLong:commentId]];
[_commentInputView setPlaceHolder:[NSString stringWithFormat:@" 鍥炲: %@", comment.userNick]];
}
-(void)onCommentCreate:(long long)commentId text:(NSString *)text
{
[self onCommentCreate:commentId text:text itemId:_currentItemId];
}
-(void)onCommentCreate:(long long)commentId text:(NSString *)text itemId:(long long) itemId
{
}
-(void) genLikeAttrString:(DFBaseLineItem *) item
{
if (item.likes.count == 0) {
return;
}
if (item.likesStr == nil) {
NSMutableArray *likes = item.likes;
NSString *result = @"";
for (int i=0; i
DFLineLikeItem *like = [likes objectAtIndex:i];
if (i == 0) {
result = [NSString stringWithFormat:@"%@",like.userNick];
}else{
result = [NSString stringWithFormat:@"%@, %@", result, like.userNick];
}
}
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:result];
NSUInteger position = 0;
for (int i=0; i
DFLineLikeItem *like = [likes objectAtIndex:i];
[attrStr addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"%lu", (unsigned long)like.userId] range:NSMakeRange(position, like.userNick.length)];
position += like.userNick.length+2;
}
item.likesStr = attrStr;
}
}
-(void) genCommentAttrString:(DFBaseLineItem *)item
{
NSMutableArray *comments = 链接已屏蔽ments;
[链接已屏蔽mentStrArray removeAllObjects];
for (int i=0; i
DFLineCommentItem *comment = [comments objectAtIndex:i];
[_commentDic setObject:comment forKey:[NSNumber numberWithLongLong:链接已屏蔽mentId]];
NSString *resultStr;
if (comment.replyUserId == 0) {
resultStr = [NSString stringWithFormat:@"%@: %@",comment.userNick, comment.text];
}else{
resultStr = [NSString stringWithFormat:@"%@鍥炲%@: %@",comment.userNick, comment.replyUserNick, comment.text];
}
NSMutableAttributedString *commentStr = [[NSMutableAttributedString alloc]initWithString:resultStr];
if (comment.replyUserId == 0) {
[commentStr addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"%lu", (unsigned long)comment.userId] range:NSMakeRange(0, comment.userNick.length)];
}else{
NSUInteger localPos = 0;
[commentStr addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"%lu", (unsigned long)comment.userId] range:NSMakeRange(localPos, comment.userNick.length)];
localPos += comment.userNick.length + 2;
[commentStr addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"%lu", (unsigned long)comment.replyUserId] range:NSMakeRange(localPos, comment.replyUserNick.length)];
}
NSLog(@"ffff: %@", resultStr);
[链接已屏蔽mentStrArray addObject:commentStr];
}
}
@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// 链接已屏蔽 (cn) 链接已屏蔽 (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? 链接已屏蔽
更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明
Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有