NetNewsWire/iOS/Add/AddContainerViewController.swift

159 lines
3.8 KiB
Swift
Raw Normal View History

//
// AddContainerViewController.swift
// NetNewsWire
//
// Created by Maurice Parker on 4/16/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import UIKit
protocol AddContainerViewControllerChild: UIViewController {
var delegate: AddContainerViewControllerChildDelegate? {get set}
func cancel()
func add()
}
protocol AddContainerViewControllerChildDelegate: UIViewController {
func readyToAdd(state: Bool)
func processingDidBegin()
func processingDidCancel()
func processingDidEnd()
}
class AddContainerViewController: UIViewController {
2019-05-20 01:41:28 +02:00
static let preferredContentSizeForFormSheetDisplay = CGSize(width: 460.0, height: 400.0)
@IBOutlet weak var cancelButton: UIBarButtonItem!
@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
@IBOutlet weak var addButton: UIBarButtonItem!
@IBOutlet weak var typeSelectorSegmentedControl: UISegmentedControl!
@IBOutlet weak var containerView: UIView!
private var currentViewController: AddContainerViewControllerChild?
2019-04-26 01:06:53 +02:00
var initialFeed: String?
var initialFeedName: String?
override func viewDidLoad() {
super.viewDidLoad()
activityIndicatorView.isHidden = true
switchToFeed()
}
@IBAction func typeSelectorChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
switchToFeed()
default:
switchToFolder()
}
}
@IBAction func cancel(_ sender: Any) {
currentViewController?.cancel()
dismiss(animated: true)
}
@IBAction func add(_ sender: Any) {
currentViewController?.add()
}
}
extension AddContainerViewController: AddContainerViewControllerChildDelegate {
func readyToAdd(state: Bool) {
addButton.isEnabled = state
}
func processingDidBegin() {
addButton.isEnabled = false
typeSelectorSegmentedControl.isEnabled = false
activityIndicatorView.isHidden = false
activityIndicatorView.startAnimating()
}
func processingDidCancel() {
addButton.isEnabled = true
typeSelectorSegmentedControl.isEnabled = true
activityIndicatorView.isHidden = true
activityIndicatorView.stopAnimating()
}
func processingDidEnd() {
dismiss(animated: true)
}
}
private extension AddContainerViewController {
func switchToFeed() {
2019-04-26 01:06:53 +02:00
guard !(currentViewController is AddFeedViewController) else {
return
}
2019-04-26 01:06:53 +02:00
resetUI()
hideCurrentController()
2019-04-26 01:06:53 +02:00
let addFeedController = UIStoryboard.add.instantiateController(ofType: AddFeedViewController.self)
addFeedController.initialFeed = initialFeed
addFeedController.initialFeedName = initialFeedName
displayContentController(addFeedController)
}
func switchToFolder() {
2019-04-26 01:06:53 +02:00
guard !(currentViewController is AddFolderViewController) else {
return
}
2019-04-26 01:06:53 +02:00
resetUI()
hideCurrentController()
displayContentController(UIStoryboard.add.instantiateController(ofType: AddFolderViewController.self))
2019-04-26 01:06:53 +02:00
}
func resetUI() {
addButton.isEnabled = false
}
func displayContentController(_ controller: AddContainerViewControllerChild) {
currentViewController = controller
controller.delegate = self
addChild(controller)
containerView.addSubview(controller.view)
controller.view.translatesAutoresizingMaskIntoConstraints = false
controller.view.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
controller.didMove(toParent: self)
}
func hideCurrentController() {
guard let currentViewController = currentViewController else {
return
}
currentViewController.willMove(toParent: nil)
currentViewController.view.removeFromSuperview()
currentViewController.removeFromParent()
}
}