Skip to content

Commit

Permalink
- Clean up DisplayViewController and remove force-casts
Browse files Browse the repository at this point in the history
- Fix (47) "the non-openable files prompt the download progress bar in the "details" view" in #237
  • Loading branch information
felix-schwarz committed Apr 25, 2019
1 parent 14dbecb commit 0dfdac6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 76 deletions.
2 changes: 1 addition & 1 deletion ownCloud/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@

/* Preview */
"Open file" = "Open file";
"There is no network" = "There is no network";
"Network unavailable" = "Network unavailable";
"Error" = "Error";
"Could not get the picture" = "Could not get the picture";
"Downloading" = "Downloading";
Expand Down
146 changes: 71 additions & 75 deletions ownCloud/Viewer/DisplayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import UIKit
import ownCloudSDK

struct DisplayViewConfiguration {
var item: OCItem!
weak var core: OCCore!
var item: OCItem?
weak var core: OCCore?
let state: DisplayViewState
}

Expand All @@ -47,23 +47,33 @@ class DisplayViewController: UIViewController, OCQueryDelegate {

// MARK: - Configuration
var item: OCItem?
private var coreConnectionStatusObservation : NSKeyValueObservation?
weak var core: OCCore? {
willSet {
if let core = core {
core.removeObserver(self, forKeyPath: "connectionStatus")
}
coreConnectionStatusObservation?.invalidate()
coreConnectionStatusObservation = nil
}
didSet {
if let core = core {
core.addObserver(self, forKeyPath: "connectionStatus", options: [.initial, .new], context: nil)
coreConnectionStatusObservation = core.observe(\OCCore.connectionStatus, options: [.initial, .new]) { [weak self] (core, _) in
guard let state = self?.state, case DisplayViewState.notSupportedMimeType = state else {
if core.connectionStatus == .online {
self?.state = .hasNetworkConnection
} else {
self?.state = .noNetworkConnection
}

return
}
}
}
}
}

var source: URL? {
didSet {
OnMainThread(inline: true) {
self.iconImageView.isHidden = true
self.iconImageView?.isHidden = true
self.hideItemMetadataUIElements()
self.renderSpecificView()
}
Expand Down Expand Up @@ -92,7 +102,7 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}

// MARK: - Views
private var iconImageView: UIImageView!
private var iconImageView: UIImageView?
private var progressView : UIProgressView?
private var cancelButton : ThemeButton?
private var metadataInfoLabel: UILabel?
Expand All @@ -104,12 +114,6 @@ class DisplayViewController: UIViewController, OCQueryDelegate {

// MARK: - Init & Deinit
required init() {
iconImageView = UIImageView()
metadataInfoLabel = UILabel()
cancelButton = ThemeButton(type: .custom)
showPreviewButton = ThemeButton(type: .custom)
noNetworkLabel = UILabel()

super.init(nibName: nil, bundle: nil)
}

Expand All @@ -118,65 +122,68 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}

deinit {
coreConnectionStatusObservation?.invalidate()

Theme.shared.unregister(client: self)
self.downloadProgress?.cancel()
self.stopQuery()
if let core = core {
core.removeObserver(self, forKeyPath: "connectionStatus")
}
}

// MARK: - Controller lifecycle
override func loadView() {
super.loadView()

iconImageView = UIImageView()
metadataInfoLabel = UILabel()
cancelButton = ThemeButton(type: .custom)
showPreviewButton = ThemeButton(type: .custom)
noNetworkLabel = UILabel()
progressView = UIProgressView(progressViewStyle: .bar)

guard let iconImageView = iconImageView, let metadataInfoLabel = metadataInfoLabel, let progressView = progressView, let cancelButton = cancelButton, let showPreviewButton = showPreviewButton, let noNetworkLabel = noNetworkLabel else {
return
}

iconImageView.translatesAutoresizingMaskIntoConstraints = false
iconImageView.contentMode = .scaleAspectFit

view.addSubview(iconImageView)

metadataInfoLabel?.translatesAutoresizingMaskIntoConstraints = false
metadataInfoLabel?.isHidden = false
metadataInfoLabel?.textAlignment = .center
metadataInfoLabel?.adjustsFontForContentSizeCategory = true
metadataInfoLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
metadataInfoLabel.translatesAutoresizingMaskIntoConstraints = false
metadataInfoLabel.isHidden = false
metadataInfoLabel.textAlignment = .center
metadataInfoLabel.adjustsFontForContentSizeCategory = true
metadataInfoLabel.font = UIFont.preferredFont(forTextStyle: .headline)

view.addSubview(metadataInfoLabel!)
view.addSubview(metadataInfoLabel)

progressView = UIProgressView(progressViewStyle: .bar)
progressView?.translatesAutoresizingMaskIntoConstraints = false
progressView?.progress = 0
progressView?.observedProgress = downloadProgress
progressView?.isHidden = (downloadProgress != nil)
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.progress = 0
progressView.observedProgress = downloadProgress
progressView.isHidden = (downloadProgress != nil)

view.addSubview(progressView!)
view.addSubview(progressView)

cancelButton?.translatesAutoresizingMaskIntoConstraints = false
cancelButton?.setTitle("Cancel".localized, for: .normal)
cancelButton?.isHidden = (downloadProgress != nil)
cancelButton?.addTarget(self, action: #selector(cancelDownload(sender:)), for: UIControl.Event.touchUpInside)
cancelButton.translatesAutoresizingMaskIntoConstraints = false
cancelButton.setTitle("Cancel".localized, for: .normal)
cancelButton.isHidden = (downloadProgress != nil)
cancelButton.addTarget(self, action: #selector(cancelDownload(sender:)), for: UIControl.Event.touchUpInside)

view.addSubview(cancelButton!)
view.addSubview(cancelButton)

showPreviewButton?.translatesAutoresizingMaskIntoConstraints = false
showPreviewButton?.setTitle("Open file".localized, for: .normal)
showPreviewButton?.isHidden = true
showPreviewButton?.addTarget(self, action: #selector(downloadItem), for: UIControl.Event.touchUpInside)
view.addSubview(showPreviewButton!)
showPreviewButton.translatesAutoresizingMaskIntoConstraints = false
showPreviewButton.setTitle("Open file".localized, for: .normal)
showPreviewButton.isHidden = true
showPreviewButton.addTarget(self, action: #selector(downloadItem), for: UIControl.Event.touchUpInside)
view.addSubview(showPreviewButton)

noNetworkLabel?.translatesAutoresizingMaskIntoConstraints = false
noNetworkLabel?.isHidden = true
noNetworkLabel?.adjustsFontForContentSizeCategory = true
noNetworkLabel?.text = "There is no network".localized
noNetworkLabel?.textAlignment = .center
noNetworkLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
view.addSubview(noNetworkLabel!)

guard let iconImageView = iconImageView, let metadataInfoLabel = metadataInfoLabel, let progressView = progressView,
let cancelButton = cancelButton, let showPreviewButton = showPreviewButton, let noNetworkLabel = noNetworkLabel
else {
return
}
noNetworkLabel.translatesAutoresizingMaskIntoConstraints = false
noNetworkLabel.isHidden = true
noNetworkLabel.adjustsFontForContentSizeCategory = true
noNetworkLabel.text = "Network unavailable".localized
noNetworkLabel.textAlignment = .center
noNetworkLabel.font = UIFont.preferredFont(forTextStyle: .headline)
view.addSubview(noNetworkLabel)

NSLayoutConstraint.activate([
iconImageView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor),
Expand Down Expand Up @@ -211,7 +218,7 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
Theme.shared.register(client: self)

if let item = item {
iconImageView.image = item.icon(fitInSize:iconImageSize)
iconImageView?.image = item.icon(fitInSize:iconImageSize)

if item.thumbnailAvailability != .none {
let displayThumbnail = { (thumbnail: OCItemThumbnail?) in
Expand All @@ -220,8 +227,8 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
image != nil,
item.itemVersionIdentifier == thumbnail?.itemVersionIdentifier {
OnMainThread {
if !self.iconImageView.isHidden {
self.iconImageView.image = image
if self.iconImageView?.isHidden == false {
self.iconImageView?.image = image
}
}
}
Expand All @@ -237,6 +244,8 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}
}
}

self.render()
}

func updateNavigationBarItems() {
Expand Down Expand Up @@ -273,8 +282,8 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}
return
}
self?.item = latestItem
self?.source = file!.url
self?.item = latestItem
self?.source = file?.url
}) {
self.state = .downloading(progress: downloadProgress)
}
Expand All @@ -285,31 +294,14 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}

func hideItemMetadataUIElements() {
iconImageView.isHidden = true
iconImageView?.isHidden = true
progressView?.isHidden = true
cancelButton?.isHidden = true
metadataInfoLabel?.isHidden = true
showPreviewButton?.isHidden = true
noNetworkLabel?.isHidden = true
}

// MARK: - KVO observing
// swiftlint:disable block_based_kvo
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[NSKeyValueChangeKey.newKey] as? OCCoreConnectionStatus {
if case DisplayViewState.notSupportedMimeType = self.state {

} else {
if newValue == .online {
self.state = .hasNetworkConnection
} else {
self.state = .noNetworkConnection
}
}
}
}
// swiftlint:enable block_based_kvo

private func render() {
print("LOG --> State changed to \(state)")
switch state {
Expand Down Expand Up @@ -416,6 +408,10 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}

func present(item: OCItem) {
guard self.view != nil else {
return
}

self.item = item

metadataInfoLabel?.text = item.sizeLocalized + " - " + item.lastModifiedLocalized
Expand Down

0 comments on commit 0dfdac6

Please sign in to comment.