Files
libr-ios/librx/ProfileViewController.swift
2015-11-30 23:07:13 +08:00

210 lines
7.2 KiB
Swift

//
// ProfileViewController.swift
// librx
//
// Created by Yunlong Zheng on 15/8/5.
// Copyright (c) 2015年 librx. All rights reserved.
//
import UIKit
class ProfileViewController: UIViewController{
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var subTitleLabel: UILabel!
@IBOutlet weak var followerNumLabel: UILabel!
@IBOutlet weak var follosNumLabel: UILabel!
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var followButton: UIButton!
@IBOutlet weak var settingButton: UIButton!
@IBOutlet weak var backButton: UIButton!
private var containerViewController: ContainerViewController!
private var screenSize: CGRect!
private var screenWidth: CGFloat!
private var screenHeight: CGFloat!
private var isFollowed: Bool = true
private var profile: Profile?
// 其他用户个人中心
var forceHiddenBackButton: Bool = false
var isMe:Bool = true
var userId:Int?
var lastOffsetY :CGFloat = 0
private var loginAction : LoginAction?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
screenSize = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.width / 2
self.avatarImageView.clipsToBounds = true
self.avatarImageView.layer.borderWidth = 2
self.avatarImageView.layer.borderColor = UIColor.whiteColor().CGColor
self.followButton.layer.borderWidth = 1
self.followButton.layer.borderColor = UIColor.whiteColor().CGColor
self.followButton.hidden = true
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBarHidden = false
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.settingButton.hidden = true
self.backButton.hidden = true
}
override func viewDidAppear(animated: Bool) {
super.viewDidDisappear(animated)
if self.isMe {
self.followButton.hidden = true
loadMeProfile()
self.settingButton.hidden = false
} else{
loadUserProfile(userId!)
if !self.forceHiddenBackButton{
self.backButton.hidden = false
}
}
}
override func prefersStatusBarHidden() -> Bool {
return false
}
func loadUserProfile(userId: Int){
LibrxService.profile(userId) { [weak self](profile) -> () in
if let selfView = self {
if let thatProfile = profile {
selfView.profile = thatProfile
let avatar = NSURL(string: thatProfile.avatar)
selfView.avatarImageView.sd_setImageWithURL(avatar, placeholderImage: UIImage(named: "default"))
selfView.nameLabel.text = thatProfile.name
selfView.subTitleLabel.text = "简介:\(thatProfile.email)"
selfView.followerNumLabel.text = String(thatProfile.followersCount)
selfView.follosNumLabel.text = String(thatProfile.followsCount)
if let loginEdUserId = LocalStore.accessUserId() {
if selfView.userId != loginEdUserId {
selfView.isFollowed = thatProfile.followed
if thatProfile.followed {
selfView.followButton.setTitle("已关注", forState: .Normal)
} else {
selfView.followButton.setTitle("关注", forState: .Normal)
}
if let _ = LocalStore.accessToken() {
selfView.followButton.hidden = false
}
else{
selfView.followButton.hidden = true
}
}
}
}
}
}
}
func loadMeProfile(){
if let _ = LocalStore.accessToken() {
LibrxService.me { [weak self](profile) -> () in
if let selfView = self {
if let _ = profile {
self?.profile = profile
let avatar = NSURL(string: profile!.avatar)
selfView.avatarImageView.sd_setImageWithURL(avatar, placeholderImage: UIImage(named: "default"))
selfView.nameLabel.text = profile!.name
selfView.subTitleLabel.text = "简介:\(profile!.email)"
selfView.followerNumLabel.text = String(profile!.followersCount)
selfView.follosNumLabel.text = String(profile!.followsCount)
}
}
}
}else{
loginAction = LoginAction(viewController: self, completion: { [weak self]() -> () in
if let strongSelf = self {
strongSelf.dismissViewControllerAnimated(true, completion: nil)
}
})
}
}
@IBAction func unwindToProfile(segue: UIStoryboardSegue) {
}
@IBAction func toggleFollowUser(sender: AnyObject) {
if self.isFollowed {
self.isFollowed = false
self.followButton.setTitle("关注", forState: .Normal)
self.follosNumLabel.text = String(self.profile!.followersCount-1)
LibrxService.unFollow(userId!)
return
} else {
self.isFollowed = true
self.followButton.setTitle("已关注", forState: .Normal)
self.follosNumLabel.text = String(self.profile!.followersCount+1)
LibrxService.follow(userId!)
return
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerSegue" {
self.containerViewController = segue.destinationViewController as! ContainerViewController
self.containerViewController.isMe = isMe
self.containerViewController.userId = userId
}
}
@IBAction func segmentSwicth(sender: UISegmentedControl) {
self.containerViewController.turnToPage(sender.selectedSegmentIndex)
}
}
extension ProfileViewController: UIScrollViewDelegate{}
extension ProfileViewController: UIPageViewControllerDelegate{}