|
//
// DetailPageController.swift
// PaiAi
//
// Created by zhengjianfei on 16/4/6.
// Copyright © 2016年 FFIB. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
import PaiaiDataKit
import PaiaiUIKit
let kPhotographerMark = 1
final class DetailPageController: UIViewController {
// MARK: Storyboard property
@IBOutlet var tableView: UITableView!
@IBOutlet var commentView: UIView!
@IBOutlet var commentHeight: NSLayoutConstraint!
@IBOutlet var commentTextField: UITextField!
@IBOutlet var sendBtn: UIButton!
@IBOutlet var thumbupView: UIView!
@IBOutlet var buyView: UIView!
@IBOutlet var shuiyinImage: UIImageView!
@IBOutlet var shuiyinLabel: UILabel!
@IBOutlet weak var waterMarkView: UIView!
@IBOutlet weak var shuiyinImageLeadingConstarint: NSLayoutConstraint!
// MARK: data property
var detailPageViewModel = DetailPageViewModel()
lazy var datas = [PhotoItem]()
lazy var currentPhotoIndex = 0
var isHiddenEnterView = false
let disposeBag = DisposeBag()
static let storyboardCtl = UIStoryboard.photoDetail.instantiateInitialViewController() as! DetailPageController
// MARK: view function
override func viewDidLoad() {
super.viewDidLoad()
// detailPageViewModel.tipDelegate = self
// tableView.tableFooterView = UIView()
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 100, right: 0)
configureNotification()
commentTextField.addLeftPadding(7)
commentTextField.rx.text
.map {!($0?.isEmpty)!}
.bind(to: sendBtn.rx.isEnabled)
.disposed(by: disposeBag)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
// titleWithbackBar = "详情"
navigationController?.isNavigationBarHidden = false
// refreshUI(index: currentPhotoIndex)
}
// override func backToController() {
// navigationController?.popViewController(animated: true)
// if let last = navigationController?.viewControllers[(navigationController?.viewControllers.count)! - 1] as? HomeViewController {
//// last.mainViewModel.models.value = datas
// }
//
// if let last = navigationController?.viewControllers[(navigationController?.viewControllers.count)! - 1] as? GroupPageController {
//// last.MineGroupViewModel.models.value = datas
// }
// }
func configureNotification() {
do {
// NotificationCenter.default.rx.notification(Notification.Name(rawValue: WXPayDidFinishNotification)).asObservable().subscribe { (notification) in
// FFToastView.showLoadingToast(inView: UIApplication.shared.keyWindow!, blockSuperView: true)
// self.detailPageViewModel.handleResult(errorCode: 0, success: {[weak self](PhotoItem) in
// if let weakself = self {
// weakself.datas[weakself.currentPhotoIndex].murl = PhotoItem.murl
// weakself.datas[weakself.currentPhotoIndex].rurl = PhotoItem.rurl
//// weakself.showBuyView()
// weakself.tableView.reloadRows(at: [IndexPath(item: 0, section: 1)], with: .none)
// let fullPicCtl = UIStoryboard.detailBoard.instantiateController(ShowFullPicController.self)
// fullPicCtl.datas = weakself.datas
// fullPicCtl.currentPhotoIndex = weakself.currentPhotoIndex
// fullPicCtl.showNomark = weakself.detailPageViewModel.watermarkPrice != -1
// fullPicCtl.showHD = weakself.detailPageViewModel.hdPrice != -1
// weakself.navigationController?.pushViewController(fullPicCtl, animated: true)
// }
// })
// }.disposed(by: disposeBag)
}
do {
// NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillShow)
// .asObservable()
// .subscribe({ (notification) in
// guard let info = notification.element?.userInfo, let avalue = info[UIKeyboardFrameEndUserInfoKey] else {
// return
// }
//
// let height = (avalue as AnyObject).cgRectValue.size.height
// self.returnKeyboarAction(notification.element!, height: height)
// }).disposed(by: disposeBag)
}
do {
NotificationCenter.default.rx.notification(UIResponder.keyboardWillHideNotification)
.asObservable()
.subscribe({ (notification) in
self.returnKeyboarAction(notification.element!, height: 0)
}).disposed(by: disposeBag)
}
}
// MARK: refresh interface
func refreshUI(index: Int) {
currentPhotoIndex = index
// detailPageViewModel.currentPhoto = datas[index]
detailPageViewModel.fetchThumbup(success: {[weak self] in
if let weakself = self {
var model = weakself.datas[index]
model.thumbup_num = weakself.detailPageViewModel.thumbups.count
weakself.datas[index] = model
// PhotoLocalStorage.instance.updateLocalData(PhotoItem: model)
weakself.reloadSection(inter: 3)
}
})
detailPageViewModel.fetchComment(success: {[weak self] in
if let weakself = self {
var model = weakself.datas[index]
model.comment_num = weakself.detailPageViewModel.comments.count
weakself.datas[index] = model
// PhotoLocalStorage.instance.updateLocalData(PhotoItem: model)
weakself.reloadSection(inter: 4)
}
})
reloadSection(inter: 0)
reloadSection(inter: 2)
showBuyView()
}
func showBuyView() {
// if detailPageViewModel.currentPhoto.photo_from == kPhotographerMark && detailPageViewModel.currentPhoto.display_payment_btn == 1 {
// buyView.isHidden = false
// detailPageViewModel.hdPrice = -0.01
// detailPageViewModel.watermarkPrice = -0.01
// shuiyinLabel.text = !detailPageViewModel.currentPhoto.murl.isEmpty ? "查看无水印图" : "去除水印"
// shuiyinImage.isHidden = false
// } else {
// buyView.isHidden = true
// }
}
// MARK: Storyboard button function
@IBAction func HDPay(_ sender: UIButton) {
detailPageViewModel.getHD (getPriceSuccess: { [weak self] (isExist) in
if let weakself = self {
if isExist {
let fullPicCtl = UIStoryboard(name: "Detail", bundle: nil).instantiateController(ShowFullPicController.self)
fullPicCtl.datas = [weakself.datas[weakself.currentPhotoIndex]]
fullPicCtl.showNomark = true
fullPicCtl.currentPhotoIndex = weakself.currentPhotoIndex
weakself.navigationController?.pushViewController(fullPicCtl, animated: true)
} else {
}
}
})
}
@IBAction func waterMarkPay(_ sender: UIButton) {
detailPageViewModel.getWatermark (getPriceSuccess: { [weak self] (isExist) in
if let weakself = self {
if isExist {
let fullPicCtl = UIStoryboard(name: "Detail", bundle: nil).instantiateController(ShowFullPicController.self)
fullPicCtl.datas = weakself.datas
fullPicCtl.showNomark = true
fullPicCtl.currentPhotoIndex = weakself.currentPhotoIndex
weakself.navigationController?.pushViewController(fullPicCtl, animated: true)
} else {
weakself.shuiyinImage.isHidden = true
weakself.shuiyinLabel.text = "¥\((weakself.detailPageViewModel.watermarkPrice/100))"
}
}
})
}
@IBAction func share() {
let ctl = UIStoryboard.photoDetail.instantiateController(ShareController.self)
ctl.shareContent = "我使用拍爱分享了一张美图,你也快来试试吧"
ctl.shareImgUrlThumb = datas[currentPhotoIndex].photo_thumbnail_url
ctl.shareUrl = datas[currentPhotoIndex].photo_share_url
presentController(ctl)
}
@IBAction func comment() {
commentView.isHidden = false
commentTextField.becomeFirstResponder()
}
@IBAction func sendComment() {
guard let text = commentTextField.text else {
return
}
detailPageViewModel.sendComment(content: text, success: { [weak self] in
if let weakself = self {
weakself.commentTextField.text = ""
weakself.commentTextField.resignFirstResponder()
weakself.commentView.isHidden = true
weakself.refreshUI(index: weakself.currentPhotoIndex)
}
})
}
@IBAction func thumbup() {
detailPageViewModel.sendThumbup(success: {[weak self] in
if let weakself = self {
weakself.refreshUI(index: weakself.currentPhotoIndex)
}
})
}
// MARK: custom function
func reloadSection(inter: Int) {
tableView.beginUpdates()
let indexSet = IndexSet(integer: inter)
tableView.reloadSections(indexSet, with: .none)
tableView.endUpdates()
}
@objc func showThumps() {
if detailPageViewModel.thumbupsCount > 0 {
detailPageViewModel.thumbupsCount = 0
} else {
detailPageViewModel.thumbupsCount = detailPageViewModel.thumbups.count
}
reloadSection(inter: 3)
}
@objc func showComments() {
if detailPageViewModel.commentsCount > 0 {
detailPageViewModel.commentsCount = 0
} else {
detailPageViewModel.commentsCount = detailPageViewModel.comments.count
}
reloadSection(inter: 4)
}
@objc func loadReportController() {
// let ctl = UIStoryboard.photoDetail.instantiateController(ReportController.self)
// presentController(ctl)
}
func returnKeyboarAction(_ notification: Notification, height: CGFloat) {
guard let info = (notification as NSNotification).userInfo, let duration = info[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else {
return
}
UIView.animate(withDuration: duration, animations: {() -> Void in
self.commentHeight.constant = height
self.commentView.superview!.layoutIfNeeded()
})
}
// MARK: deinit
deinit {
NotificationCenter.default.removeObserver(self)
}
}
// MARK: custom delegate function
extension DetailPageController: CellDelegate {
func selectIndex(indexpath: IndexPath) {
let ctl = UIStoryboard.photoDetail.instantiateController(ShowFullPicController.self)
ctl.datas = datas
ctl.currentPhotoIndex = currentPhotoIndex
show(ctl, sender: nil)
}
func returnCurrentIndex(index: Int) {
refreshUI(index: index)
}
func pushNext() {
let ctl = UIStoryboard.main.instantiateController(GroupPageController.self)
// ctl.groupModel = GroupModel(map: Map(mappingType: .fromJSON, JSON: datas[currentPhotoIndex].toJSON()))
show(ctl, sender: nil)
}
}
//MARK textField delegate
extension DetailPageController: UIGestureRecognizerDelegate {
// MARK: textField
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return commentTextField.isFirstResponder
}
@IBAction func ReturnKeyboard(_ sender: UITapGestureRecognizer) {
if !commentView.isHidden {
commentTextField.resignFirstResponder()
commentView.isHidden = true
}
}
}
// MARK: UITableView delegate
extension DetailPageController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "thumbupHeadCell")
if let label = cell?.viewWithTag(1001) as? UILabel {
label.text = "(\(detailPageViewModel.thumbups.count))"
}
if let button = cell?.viewWithTag(1011) as? UIButton {
button.addTarget(self, action: #selector(showThumps), for: .touchUpInside)
}
if let imageView = cell?.viewWithTag(1008) as? UIImageView {
let imageName = detailPageViewModel.thumbupsCount <= 0 ? "收起" : "列表箭头"
imageView.image = UIImage(named : imageName)
}
return cell?.contentView
} else if section == 4 {
let cell = tableView.dequeueReusableCell(withIdentifier: "comentHeadCell")
if let label = cell?.viewWithTag(1002) as? UILabel {
label.text = "(\(detailPageViewModel.comments.count))"
}
if let button = cell?.viewWithTag(1012) as? UIButton {
button.addTarget(self, action: #selector(showComments), for: .touchUpInside)
}
if let imageView = cell?.viewWithTag(1009) as? UIImageView {
let imageName = detailPageViewModel.commentsCount <= 0 ? "收起" : "列表箭头"
imageView.image = UIImage(named : imageName)
}
return cell?.contentView
}
return nil
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 3 || section == 4 {
return 44
}
return 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 3 {
return detailPageViewModel.thumbupsCount > 0 ? 1 : 0
} else if section == 4 {
return detailPageViewModel.commentsCount
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "headCell", for: indexPath) as! DetailPageHeadCell
cell.enterView.isHidden = isHiddenEnterView
// cell.setInfo(datas[currentPhotoIndex])
cell.delegate = self
if let reportBtn = cell.viewWithTag(40001) as? UIButton {
reportBtn.addTarget(self, action: #selector(loadReportController), for: .touchUpInside)
// reportBtn.isHidden = !(UserDefaults.Account.bool(forKey: .isAudit))
}
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailPagePhotoCell", for: indexPath) as! DetailPagePhotoCell
// cell.datas = datas
// cell.currentPhotoIndex = currentPhotoIndex
cell.delegate = self
cell.first = true
cell.collectionView.reloadData()
return cell
} else if indexPath.section == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell", for: indexPath) as! DetailPageNameCell
// cell.setInfo(datas[currentPhotoIndex])
return cell
} else if indexPath.section == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "thumbupCell", for: indexPath) as! DetailthumbupImagesCell
if detailPageViewModel.thumbups.count > 0 {
let headers = detailPageViewModel.thumbups.map {$0.avatar}
cell.setInfo(content: headers)
}
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "comentCell", for: indexPath) as! DetailCommentCell
cell.setInfo(detailPageViewModel.comments[indexPath.row])
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 48
} else if indexPath.section == 1 {
return 360
} else if indexPath.section == 2 {
return 36
} else if indexPath.section == 3 {
return 40
} else {
// return 40 + detailPageViewModel.comments[indexPath.row].cellHeigth
return 40
}
}
}
|