Last active
August 27, 2018 01:16
-
-
Save lacyrhoades/b9caa00cbf56cfd8cb3645cba2ae9be7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PrintConfigViewController.swift | |
// Fobo | |
// | |
// Created by Lacy Rhoades on 8/24/18. | |
// | |
import UIKit | |
protocol PrintConfigViewControllerDelegate: class { | |
func printConfigDidCancel() | |
func save(printConfig: PrintConfig) | |
} | |
class PrintConfigViewController: UINavigationController { | |
var settingsDelegate: PrintConfigViewControllerDelegate? | |
var form: PrintConfigFormViewController | |
init(_ config: PrintConfig) { | |
form = PrintConfigFormViewController(config) | |
super.init(nibName: nil, bundle: nil) | |
form.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(didTapDone)) | |
form.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didTapCancel)) | |
self.viewControllers = [form] | |
} | |
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { | |
fatalError("Use init(config:)") | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("Use init(config:)") | |
} | |
@objc func didTapDone() { | |
if let newValue = self.form.editedConfig { | |
self.settingsDelegate?.save(printConfig: newValue) | |
} | |
} | |
@objc func didTapCancel() { | |
self.settingsDelegate?.printConfigDidCancel() | |
} | |
} | |
extension PrintConfigViewController: InteriorPresentedVC { | |
var sizeForPresentation: CGSize { | |
return UIScreen.smallFormControllerSize | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PrintConfigFormViewController.swift | |
// Fobo | |
// | |
// Created by Lacy Rhoades on 8/24/18. | |
// | |
import Eureka | |
class PrintConfigFormViewController: FormViewController { | |
var config: PrintConfig | |
init(_ config: PrintConfig) { | |
self.config = config | |
super.init(nibName: nil, bundle: nil) | |
self.title = "Print Settings" | |
form +++ Section() { | |
section in | |
} <<< SwitchRow("Enable") { | |
row in | |
row.title = "Enabled" | |
row.value = self.config.isEnabled | |
} | |
<<< StepperRow("Quantity") { | |
row in | |
row.title = "Number of Prints" | |
row.value = Double(self.config.quantity) | |
row.displayValueFor = { | |
value in | |
return String(format: "%.0f", value ?? 1) | |
} | |
}.cellSetup({ (cell, row) in | |
cell.stepper.stepValue = 1 | |
cell.stepper.minimumValue = 1 | |
cell.stepper.maximumValue = 10 | |
}) | |
<<< PickerInlineRow<PrintMode>("Mode") { | |
row in | |
row.title = "Print Button" | |
row.value = self.config.mode | |
row.options = PrintMode.allValues | |
} | |
<<< PickerInlineRow<PaperSize>("Size") { | |
row in | |
row.title = "Paper Size (when available)" | |
row.value = self.config.paperSize | |
row.options = PaperSize.allValues | |
} | |
} | |
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { | |
fatalError("Nib init not allowed") | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("Coder init not allowed") | |
} | |
var editedConfig: PrintConfig? { | |
let sizeRow = form.rowBy(tag: "Size") as? PickerInlineRow<PaperSize> | |
let quantityRow = form.rowBy(tag: "Quantity") as? StepperRow | |
let modeRow = form.rowBy(tag: "Mode") as? PickerInlineRow<PrintMode> | |
guard let size: PaperSize = sizeRow?.value, | |
let quantity: Double = quantityRow?.value, | |
let mode: PrintMode = modeRow?.value else { | |
return nil | |
} | |
return PrintConfig(paperSize: size, quantity: Int(quantity), mode: mode) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment