|
//
// GroupPageViewModel.swift
// PaiAi
//
// Created by zhengjianfei on 2017/1/4.
// Copyright © 2017年 FFIB. All rights reserved.
//
import Foundation
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
public protocol GroupPageViewModelDelegate: class {
func didSelect(_ item: PhotoItem)
func navigateToGroupDetail()
func navigateToGroupQR()
}
public class GroupPageViewModel {
private let respository: GroupPhotoRepository
private let items: BehaviorRelay<[PhotoItem]>
weak var delegate: GroupPageViewModelDelegate?
init(groupId: String) {
respository = GroupPhotoRepository(groupId: groupId)
items = BehaviorRelay<[PhotoItem]>(value: [])
}
public var groupPhotoContents: Observable<[AnimatableSectionModel<Int, PhotoItem>]> {
return items.map({ model in
return [AnimatableSectionModel(model: 0, items: model)]
})
}
public func submit(image: UIImage) {
let edge = image.size.width > image.size.height ? image.size.height : image.size.width
// let newImage = image.scaledImage(1280 / edge),
guard let data = image.jpegData(compressionQuality: 0.4) else { return }
respository.upload(data: data)
}
public func reload() {
respository.load(page: 1)
}
public func layoutSizeForIndexPath(_ indexPath: IndexPath) -> CGSize {
let item = items.value[indexPath.row]
let w = item.photo_thumbnail_w
let h = item.photo_thumbnail_h
return CGSize(width: w, height: h)
}
}
extension GroupPageViewModel {
public func didSelect(_ item: PhotoItem) {
delegate?.didSelect(item)
}
public func navigateToGroupDetail() {
delegate?.navigateToGroupDetail()
}
public func navigateToGroupQR() {
delegate?.navigateToGroupDetail()
}
}
|