|
//
// PhotoPreviewViewController.swift
// PaiAi
//
// Created by zhengjianfei on 16/4/9.
// Copyright © 2016年 FFIB. All rights reserved.
//
import UIKit
import PaiaiDataKit
import PaiaiUIKit
import RxCocoa
import RxSwift
import RxDataSources
final class PhotoPreviewViewController: UIViewController {
/// MARK: Storyboard property
@IBOutlet weak var collectionView: UICollectionView!
var viewModel: PhotoDetailListViewModel!
var disposeBag = DisposeBag()
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
binding()
scrollToSpecifiedImage()
navigationController?.setNavigationBarHidden(true, animated: true)
}
func scrollToSpecifiedImage() {
collectionView.layoutIfNeeded()
collectionView.scrollToItem(at: IndexPath(item: viewModel.currIndex, section: 0), at: .right, animated: false)
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
bindCollectionViewToViewModel()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
@objc func image(_ image: UIImage?, didFinishSavingWithError error: NSError?, contextInfo info: UnsafeMutableRawPointer) {
if error != nil {
Toast.show(message: "保存照片失败")
} else {
Toast.show(message: "已保存照片到相册中", image: UIImage(named: "icon-success"))
}
}
var angle: CGFloat = 0
}
/// binding
extension PhotoPreviewViewController {
var dataSource: RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>> {
return RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<Int, PhotoItem>>(configureCell: { (dataSource, collectionView, indexPath, item) in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
cell.setModel(url: item.murl.isEmpty ? item.photo_url : item.murl)
return cell
})
}
func binding() {
bindViewModelToCollectionView()
bindCollectionViewDelegate()
}
func bindViewModelToCollectionView() {
viewModel.content
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
func bindCollectionViewDelegate() {
collectionView.rx.setDelegate(self).disposed(by: disposeBag)
}
func bindCollectionViewToViewModel() {
collectionView.rx.willDisplayCell
.asDriver()
.drive(onNext: { [unowned self] in self.viewModel.willShow(index: $0.at.row) })
.disposed(by: disposeBag)
}
}
/// storyboard button action
extension PhotoPreviewViewController {
@IBAction func back() {
dismissController()
}
@IBAction func rotateTheImage(_ sender: UIButton) {
guard let cell = collectionView.cellForItem(at: IndexPath(item: viewModel.currIndex, section: 0)) as? ImageCell else {
return
}
cell.rotate()
}
@IBAction func download(_ sender: UIButton) {
Toast.showActivity(message: "正在保存照片到相册")
guard let cell = collectionView.cellForItem(at: IndexPath(item: viewModel.currIndex, section: 0)) as? ImageCell,
let image = cell.photoImage.image else {
Toast.show(message: "未检测到照片")
return
}
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
// MARK: UICollectionView delegate
extension PhotoPreviewViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.width, height: collectionView.height - 20)
}
}
|