Skip to content

Commit

Permalink
FEAT: Display stream images in printer list
Browse files Browse the repository at this point in the history
  • Loading branch information
josefdolezal committed May 9, 2017
1 parent 91fe46b commit c2ac5bc
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 47 deletions.
82 changes: 73 additions & 9 deletions OctoPhone/View Related/Printer List/PrinterListCellViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,89 @@
//

import Foundation
import ReactiveSwift
import Icons
import class UIKit.UIImage

/// View model for printer list cell
protocol PrinterListCellViewModelType {
/// Cell inputs
protocol PrinterListCellViewModelInputs {

}

/// Cell logic outputs
protocol PrinterListCellViewModelOutputs {
/// Name of stored printer
var printerName: String { get }
var printerName: Property<String> { get }

/// URL of printer
var printerURL: String { get }
var printerURL: Property<String> { get }

/// Image of printer stream
var printerStream: Property<UIImage?> { get }
}

/// View model for printer list cell
final class PrinterListCellViewModel: PrinterListCellViewModelType {
protocol PrinterListCellViewModelType {
/// Available inputs
var inputs: PrinterListCellViewModelInputs { get }

/// Available outputs
var outputs: PrinterListCellViewModelOutputs { get }
}

/// View model for printer list cell
final class PrinterListCellViewModel: PrinterListCellViewModelInputs, PrinterListCellViewModelOutputs,
PrinterListCellViewModelType {

var inputs: PrinterListCellViewModelInputs { return self }

var printerName: String
var outputs: PrinterListCellViewModelOutputs { return self }

var printerURL: String
// MARK: Outputs

let printerName: Property<String>

let printerURL: Property<String>

let printerStream: Property<UIImage?>

// MARK: Private properties

/// Octoprint requests provider
private let provider = StaticContentProvider()

/// Holds current value of stream illustration
private let streamProperty = MutableProperty<UIImage?>(PrinterListCellViewModel.imageForIcon(.questionIcon))

init(printer: Printer) {
self.printerName = printer.name
self.printerURL = printer.url.absoluteString
self.printerName = Property(value: printer.name)
self.printerURL = Property(value: printer.url.absoluteString)
self.printerStream = Property(capturing: streamProperty)

if let url = printer.streamUrl {
requestStreamImage(url: url)
}
}

/// Requests stream photo from given URL
///
/// - Parameter url: URL of streamg
private func requestStreamImage(url: URL) {
provider.request(.get(url))
.filterSuccessfulStatusCodes()
.startWithResult { [weak self] result in
switch result {
case let .success(response): self?.streamProperty.value = UIImage(data: response.data)
case .failure: self?.streamProperty.value = PrinterListCellViewModel.imageForIcon(.minusSignIcon)
}
}
}

/// Creates preconfigured image from given icon
///
/// - Parameter icon: Icon to be converted to image
/// - Returns: New image generated from icon
private static func imageForIcon(_ icon: FontAwesomeIcon) -> UIImage {
return icon.image(ofSize: CGSize(width: 100, height: 100), color: Colors.Pallete.greyHue3)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

import UIKit
import ReactiveSwift
import ReactiveCocoa

/// Real word printer overview
class PrinterListCollectionViewCell: UICollectionViewCell {
Expand All @@ -24,15 +26,7 @@ class PrinterListCollectionViewCell: UICollectionViewCell {
let printProgress = UIProgressView()

/// Cell view model
var viewModel: PrinterListCellViewModelType? {
didSet {
// guard let viewModel = viewModel else { return }

// nameLabel.text = viewmModel.printerName
// urlLabel.text = viewModel.printerURL
backgroundColor = .red
}
}
let viewModel = MutableProperty<PrinterListCellViewModelType?>(nil)

/// Holds all view size constants
struct Sizes {
Expand All @@ -46,43 +40,47 @@ class PrinterListCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)

configureView()
bindViewModel()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

/// Configure cell subviews
private func configureView() {
let subviews = [photo, statusLabel, printProgress]
override func layoutSubviews() {
super.layoutSubviews()

let textBackgroundView = UIView()

contentView.addSubview(photo)
contentView.addSubview(textBackgroundView)
contentView.addSubview(statusLabel)

subviews.forEach { view in
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
photo.snp.makeConstraints { make in
make.edges.equalToSuperview()
make.width.top.equalToSuperview()
}

let constraints = [
photo.topAnchor.constraint(equalTo: topAnchor),
photo.leadingAnchor.constraint(equalTo: leadingAnchor),
photo.widthAnchor.constraint(equalTo: widthAnchor),
photo.heightAnchor.constraint(equalToConstant: Sizes.photoHeight),
statusLabel.topAnchor.constraint(equalTo: photo.bottomAnchor),
statusLabel.leadingAnchor.constraint(
equalTo: leadingAnchor,
constant: Sizes.leftMargin
)
]

constraints.forEach { $0.isActive = true }
}
statusLabel.snp.makeConstraints { make in
make.bottom.equalTo(photo).offset(-7)
make.leading.trailing.equalToSuperview().inset(15)
}

override func prepareForReuse() {
super.prepareForReuse()
textBackgroundView.snp.makeConstraints { make in
make.bottom.leading.trailing.equalToSuperview()
make.top.equalTo(statusLabel).offset(-7)
}

contentView.backgroundColor = .white
textBackgroundView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.7)
photo.contentMode = .scaleAspectFit
}

viewModel = nil
/// Binds ViewModel to UI
private func bindViewModel() {
let vm = viewModel.producer.skipNil()

// nameLabel.text = viewmModel.printerName
// urlLabel.text = viewModel.printerURL
photo.reactive.image <~ vm.flatMap(.latest) { $0.outputs.printerStream }
statusLabel.reactive.text <~ vm.flatMap(.latest) { $0.outputs.printerName }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension PrinterListViewController {
withReuseIdentifier: PrinterListCollectionViewCell.identifier, for: indexPath
) as! PrinterListCollectionViewCell

cell.viewModel = viewModel.outputs.storedPrinterCellViewModel(for: indexPath.row)
cell.viewModel.value = viewModel.outputs.storedPrinterCellViewModel(for: indexPath.row)

return cell
}
Expand All @@ -91,10 +91,15 @@ extension PrinterListViewController {
}

extension PrinterListViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {

return CGSize(width: collectionView.frame.width, height: 150)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {

return 1
}
}

0 comments on commit c2ac5bc

Please sign in to comment.