mirror of
https://github.com/wahyd4/libr-ios.git
synced 2026-08-09 05:06:46 +10:00
306 lines
11 KiB
Swift
306 lines
11 KiB
Swift
//
|
|
// StorysTableViewController.swift
|
|
// librx
|
|
//
|
|
// Created by Yunlong Zheng on 15/7/28.
|
|
// Copyright (c) 2015年 librx. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import SDWebImage
|
|
import SwiftyJSON
|
|
|
|
class StorysTableViewController: UITableViewController {
|
|
|
|
@IBOutlet weak var titleLabel: UILabel!
|
|
|
|
private var timer: NSTimer!
|
|
private var firstTime = true
|
|
private var postsLoader = PostsLoader(.Default)
|
|
private var loginStateChange: LoginStateHandler?
|
|
|
|
private var navigationToProfile: Int?
|
|
|
|
private var posts:[Post] = []
|
|
private var currentPage:Int = 1
|
|
private var isFirstTime = true
|
|
private var activityView: UIActivityIndicatorView?
|
|
|
|
|
|
var postSection : PostsLoader.PostSection = .Default {
|
|
didSet {
|
|
postsLoader = PostsLoader(postSection)
|
|
}
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
|
|
|
|
// mark: add refress control to tableview
|
|
self.refreshControl = UIRefreshControl()
|
|
self.refreshControl?.attributedTitle = NSAttributedString(string: "下拉可以刷新数据")
|
|
self.refreshControl?.tintColor = UIColor(red: 46/255.0, green: 204/255.0, blue: 113/255.0, alpha: 1.0)
|
|
self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
|
|
self.refreshControl?.backgroundColor = UIColor(white: 0.97, alpha: 1)
|
|
|
|
self.tableView.addSubview(refreshControl!)
|
|
self.tableView.separatorColor = UIColor(red:240.0/255.0,
|
|
green: 240.0/255.0, blue: 240.0/255.0,
|
|
alpha: 0.0)
|
|
self.tableView.estimatedRowHeight = 100
|
|
self.tableView.rowHeight = UITableViewAutomaticDimension
|
|
self.tableView.backgroundColor = UIColor(white: 0.97, alpha: 1)
|
|
|
|
self.posts = self.getLocalCachePosts()
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
override func viewDidAppear(animated: Bool) {
|
|
|
|
super.viewDidAppear(false)
|
|
if self.refreshControl?.refreshing == false{
|
|
self.refreshControl?.beginRefreshing()
|
|
refresh()
|
|
}
|
|
|
|
// reload data from cache
|
|
self.posts = self.getLocalCachePosts()
|
|
self.tableView.reloadData()
|
|
|
|
if isFirstTime {
|
|
self.refreshControl?.beginRefreshing()
|
|
refresh()
|
|
}
|
|
|
|
}
|
|
|
|
override func viewDidDisappear(animated: Bool) {
|
|
super.viewDidDisappear(animated)
|
|
endOfWork()
|
|
}
|
|
|
|
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return posts.count + 1
|
|
}
|
|
|
|
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
|
|
|
if (indexPath.row == posts.count ){
|
|
loadMore()
|
|
return createLoadMoreTableCell(cellForRowAtIndexPath: indexPath)
|
|
} else {
|
|
return createNormalTableCell(cellForRowAtIndexPath: indexPath)
|
|
}
|
|
|
|
}
|
|
|
|
|
|
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
|
|
|
|
if self.posts.count==0 {
|
|
|
|
let messageLabel: UILabel = UILabel.init(
|
|
frame: CGRect(x: 10, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)
|
|
)
|
|
|
|
messageLabel.text = self.postSection.tableString
|
|
messageLabel.font.fontWithSize(10.0)
|
|
messageLabel.textColor = UIColor.lightGrayColor()
|
|
messageLabel.numberOfLines = 0
|
|
messageLabel.textAlignment = NSTextAlignment.Center
|
|
messageLabel.sizeToFit()
|
|
|
|
self.tableView.backgroundView = messageLabel
|
|
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None;
|
|
|
|
}else {
|
|
self.tableView.backgroundView = nil
|
|
return 1;
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|
|
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
|
|
if let _ = self.tableView.indexPathForSelectedRow{
|
|
performSegueWithIdentifier("showWebView", sender: self)
|
|
}
|
|
}
|
|
|
|
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
|
|
|
|
if segue.identifier == "showWebView" {
|
|
if let indexPath = self.tableView.indexPathForSelectedRow{
|
|
let destinationController = segue.destinationViewController as! WebViewController
|
|
let post = self.posts[indexPath.row]
|
|
destinationController.post = post
|
|
}
|
|
}else if segue.identifier == "showUserProfile"{
|
|
if let userId = self.navigationToProfile{
|
|
let destinationController = segue.destinationViewController as! ProfileViewController
|
|
destinationController.isMe = false
|
|
destinationController.userId = userId
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func getLocalCachePosts() -> [Post] {
|
|
|
|
var posts: [Post] = []
|
|
|
|
if let data = PostsStore.getPostsCacheBySection(postSection) {
|
|
let json = JSON(data: data)
|
|
for index in 0..<json.count {
|
|
let postData = json[index]
|
|
let post: Post = JsonPaser.parsePost(postData)
|
|
posts.append(post)
|
|
}
|
|
}
|
|
|
|
print("get cache posts count:\(posts.count)")
|
|
return posts
|
|
}
|
|
|
|
func refresh() {
|
|
print("refreshing...")
|
|
postsLoader.load(completion: { [weak self] (posts, error) in
|
|
if let selfView = self {
|
|
selfView.isFirstTime = false
|
|
selfView.posts = posts
|
|
selfView.tableView.reloadData()
|
|
selfView.refreshControl?.endRefreshing()
|
|
}
|
|
})
|
|
timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: "endOfWork", userInfo: nil, repeats: true)
|
|
}
|
|
|
|
func endOfWork() {
|
|
refreshControl?.endRefreshing()
|
|
if timer != nil {
|
|
timer.invalidate()
|
|
timer = nil
|
|
}
|
|
}
|
|
|
|
func dismiss() {
|
|
dismissViewControllerAnimated(true, completion: nil)
|
|
}
|
|
|
|
func loadMore() {
|
|
postsLoader.load(currentPage+1, completion: { [weak self] (posts, error) in
|
|
if let selfView = self {
|
|
if posts.count == 0 {
|
|
print("没有数据了")
|
|
selfView.activityView?.stopAnimating()
|
|
} else{
|
|
selfView.posts += posts
|
|
selfView.currentPage += 1
|
|
selfView.tableView.reloadData()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func createLoadMoreTableCell(cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
|
|
|
let cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "moretag")
|
|
cell.backgroundColor = UIColor.clearColor()
|
|
|
|
self.activityView = UIActivityIndicatorView.init(frame: CGRectMake(0, 10 ,self.view.frame.width,30.0))
|
|
self.activityView?.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
|
|
self.activityView?.hidesWhenStopped = true
|
|
self.activityView?.startAnimating()
|
|
|
|
cell.contentView.addSubview(activityView!)
|
|
return cell
|
|
|
|
}
|
|
|
|
func createNormalTableCell(cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
|
|
|
|
let post = posts[indexPath.row] as Post
|
|
|
|
let cellIdentifier = "Cell"
|
|
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:indexPath) as! CustomTableCell
|
|
|
|
cell.delegate = self
|
|
|
|
cell.nameLabel.text = post.title
|
|
cell.nameLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
|
|
cell.nameLabel.numberOfLines = 2
|
|
|
|
cell.summary.text = post.summary
|
|
cell.summary.lineBreakMode = NSLineBreakMode.ByWordWrapping
|
|
cell.summary.numberOfLines = 1
|
|
|
|
cell.readNumLabel.text = String(post.viewedTimes)
|
|
cell.likeNumLabel.text = String(post.likeCount)
|
|
cell.timeDiffLabel.text = DateUtil.timeDiff(post.getFormatCreateDateTime())
|
|
|
|
let avatarUrl = NSURL(string: post.firstSharer.avatar)
|
|
let thumbnailUrl = NSURL(string: post.thumbnail)
|
|
|
|
if post.url.containsString("github") {
|
|
cell.thumbnilImageView.image = UIImage(named: "GitHub")
|
|
} else if post.url.containsString("zhihu"){
|
|
cell.thumbnilImageView.image = UIImage(named: "zhihu")
|
|
}else{
|
|
cell.thumbnilImageView.sd_setImageWithURL(thumbnailUrl, placeholderImage: UIImage(named: "default"))
|
|
}
|
|
|
|
cell.thumbnilImageView.layer.cornerRadius = 6
|
|
cell.thumbnilImageView.clipsToBounds = true
|
|
cell.thumbnilImageView.layer.borderWidth = 1
|
|
cell.thumbnilImageView.layer.borderColor = UIColor.clearColor().CGColor
|
|
|
|
cell.pageMarkLabel.layer.cornerRadius = 10
|
|
cell.pageMarkLabel.clipsToBounds = true
|
|
|
|
if !post.processed {
|
|
cell.pageMarkLabel.text = "处理"
|
|
cell.pageMarkLabel.textColor = UIColor(hexString: "#a1882b")
|
|
cell.pageMarkLabel.backgroundColor = UIColor(hexString: "#ffefc6")
|
|
} else{
|
|
if !post.goodFormat {
|
|
cell.pageMarkLabel.text = "网"
|
|
cell.pageMarkLabel.textColor = UIColor.whiteColor()
|
|
cell.pageMarkLabel.backgroundColor = UIColor(hexString: "#FB7F5A")
|
|
} else {
|
|
cell.pageMarkLabel.text = "文"
|
|
cell.pageMarkLabel.textColor = UIColor.whiteColor()
|
|
cell.pageMarkLabel.backgroundColor = DefaultTheme.themeColor
|
|
}
|
|
}
|
|
|
|
cell.avatarImageView.sd_setImageWithURL(avatarUrl, placeholderImage: UIImage(named: "default"))
|
|
cell.avatarImageView.layer.cornerRadius = cell.avatarImageView.frame.size.width / 2
|
|
cell.avatarImageView.clipsToBounds = true
|
|
|
|
cell.nameButton.tag = post.firstSharer.id
|
|
cell.shareNameLabel.text = post.firstSharer.name
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
@IBAction func startSharePost() {
|
|
let createPostViewController = UIStoryboard(name: "Main", bundle: nil)
|
|
.instantiateViewControllerWithIdentifier("NewPostNavigationController") as! UINavigationController
|
|
self.presentViewController(createPostViewController, animated: true, completion: nil)
|
|
}
|
|
|
|
}
|
|
|
|
extension StorysTableViewController:StoryTableNavigationDelegate{
|
|
|
|
func navigationTo(userId: Int){
|
|
self.navigationToProfile = userId
|
|
performSegueWithIdentifier("showUserProfile", sender: self)
|
|
}
|
|
|
|
}
|