Skip to content

Commit

Permalink
- Final implementation using UIPageViewController.
Browse files Browse the repository at this point in the history
  • Loading branch information
pablocarmu committed Feb 28, 2019
2 parents 8f17df6 + 56cbbc1 commit bd207b8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 84 deletions.
12 changes: 0 additions & 12 deletions ownCloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@
6E586CFC2199A72600F680C4 /* OpenInAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E586CFB2199A72600F680C4 /* OpenInAction.swift */; };
6E586CFE2199A75900F680C4 /* MoveAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E586CFD2199A75900F680C4 /* MoveAction.swift */; };
6E586D002199A78E00F680C4 /* DeleteAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E586CFF2199A78E00F680C4 /* DeleteAction.swift */; };
6E5FC15B221563F200F60846 /* DisplayViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5FC15A221563F200F60846 /* DisplayViewer.swift */; };
6E5FC1622215653200F60846 /* DisplayViewerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5FC1612215653200F60846 /* DisplayViewerViewController.swift */; };
6E5FC1692215852700F60846 /* AlternativeCollectionCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5FC1682215852700F60846 /* AlternativeCollectionCollectionViewController.swift */; };
6E5FC172221590B000F60846 /* GalleryHostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E5FC171221590B000F60846 /* GalleryHostViewController.swift */; };
6E7EA15C221C5E52008F0F8A /* GalleryCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E7EA15B221C5E52008F0F8A /* GalleryCollectionViewCell.swift */; };
6E83C77E20A32C1B0066EC23 /* SettingsSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E83C77D20A32C1B0066EC23 /* SettingsSection.swift */; };
6E83C78420A33C180066EC23 /* LAContext+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E83C78320A33C180066EC23 /* LAContext+Extension.swift */; };
6E91F37E21ECA6FD009436D2 /* CopyAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E91F37D21ECA6FD009436D2 /* CopyAction.swift */; };
Expand Down Expand Up @@ -541,11 +537,7 @@
6E586CFB2199A72600F680C4 /* OpenInAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenInAction.swift; sourceTree = "<group>"; };
6E586CFD2199A75900F680C4 /* MoveAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveAction.swift; sourceTree = "<group>"; };
6E586CFF2199A78E00F680C4 /* DeleteAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteAction.swift; sourceTree = "<group>"; };
6E5FC15A221563F200F60846 /* DisplayViewer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayViewer.swift; sourceTree = "<group>"; };
6E5FC1612215653200F60846 /* DisplayViewerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayViewerViewController.swift; sourceTree = "<group>"; };
6E5FC1682215852700F60846 /* AlternativeCollectionCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlternativeCollectionCollectionViewController.swift; sourceTree = "<group>"; };
6E5FC171221590B000F60846 /* GalleryHostViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryHostViewController.swift; sourceTree = "<group>"; };
6E7EA15B221C5E52008F0F8A /* GalleryCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryCollectionViewCell.swift; sourceTree = "<group>"; };
6E83C77D20A32C1B0066EC23 /* SettingsSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsSection.swift; sourceTree = "<group>"; };
6E83C78320A33C180066EC23 /* LAContext+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "LAContext+Extension.swift"; sourceTree = "<group>"; };
6E91F37D21ECA6FD009436D2 /* CopyAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CopyAction.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -973,11 +965,7 @@
6E8E3CAB2213287500BF8BDB /* Gallery */ = {
isa = PBXGroup;
children = (
6E5FC15A221563F200F60846 /* DisplayViewer.swift */,
6E5FC1612215653200F60846 /* DisplayViewerViewController.swift */,
6E5FC1682215852700F60846 /* AlternativeCollectionCollectionViewController.swift */,
6E5FC171221590B000F60846 /* GalleryHostViewController.swift */,
6E7EA15B221C5E52008F0F8A /* GalleryCollectionViewCell.swift */,
);
path = Gallery;
sourceTree = "<group>";
Expand Down
114 changes: 60 additions & 54 deletions ownCloud/Gallery/GalleryHostViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@ import ownCloudSDK

class GalleryHostViewController: UIPageViewController {

typealias Filter = ([OCItem]) -> [OCItem]

// MARK: - Constants
let hasChangesAvailableKeyPath: String = "hasChangesAvailable"
let imageFilterRegexp: String = "\\A((image/(?!(gif|svg*))))"

// MARK: - Instance Variables
weak private var core: OCCore?
private var selectedItem: OCItem
private var items: [OCItem]?
private var query: OCQuery
private weak var viewControllerToTansition: DisplayViewController?
private var selectedFilter: Filter?

private var itemsFilter: OCQueryFilter = {
var itemFilterHandler: OCQueryFilterHandler = { (_, _, item) -> Bool in
if let item = item {
if item.type == .collection {return false}
}
return true
}
// MARK: - Filters
lazy var filterImageFiles: Filter = { items in
let filteredItems = items.filter({$0.type != .collection && $0.mimeType?.matches(regExp: self.imageFilterRegexp) ?? false})
return filteredItems
}

return OCQueryFilter(handler: itemFilterHandler)
}()
lazy var filterOneItem: Filter = { items in
let filteredItems = items.filter({$0.type != .collection && $0.fileID == self.selectedItem.fileID})
return filteredItems
}

// MARK: - Init & deinit
init(core: OCCore, selectedItem: OCItem, query: OCQuery) {
Expand All @@ -36,18 +43,15 @@ class GalleryHostViewController: UIPageViewController {
self.query = query

super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

query.addObserver(self, forKeyPath: "hasChangesAvailable", options: [.initial, .new], context: nil)
query.addFilter(itemsFilter, withIdentifier: "items-filter")

query.addObserver(self, forKeyPath: hasChangesAvailableKeyPath, options: [.initial, .new], context: nil)
}

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

deinit {
query.removeObserver(self, forKeyPath: "hasChangesAvailable")
query.removeObserver(self, forKeyPath: hasChangesAvailableKeyPath)
}

// MARK: - ViewController lifecycle
Expand All @@ -56,41 +60,39 @@ class GalleryHostViewController: UIPageViewController {
dataSource = self
delegate = self

if selectedItem.mimeType?.matches(regExp: imageFilterRegexp) ?? false {
selectedFilter = filterImageFiles
} else {
selectedFilter = filterOneItem
}

query.requestChangeSet(withFlags: .onlyResults) { [weak self] ( _, changeSet) in
guard let `self` = self else {
return
}
guard let `self` = self else { return}
guard let queryResult = changeSet?.queryResult else { return }

self.items = changeSet?.queryResult
self.items = self.selectedFilter?(queryResult)

if let items = self.items, let index = items.firstIndex(where: {$0.fileID == self.selectedItem.fileID}) {
let itemToDisplay = items[index]

guard let mimeType = itemToDisplay.mimeType else { return }
OnMainThread {
let viewController = self.selectDisplayViewControllerBasedOn(mimeType: mimeType)
let shouldDownload = viewController is DisplayExtension ? true : false
let configurationState: DisplayViewState = shouldDownload ? .hasNetworkConnection : .notSupportedMimeType
let configuration = DisplayViewConfiguration(item: itemToDisplay, core: self.core, state: configurationState)
let configuration = self.configurationFor(itemToDisplay, viewController: viewController)

viewController.configure(configuration)
self.addChild(viewController)
viewController.didMove(toParent: self)

viewController.configure(configuration)
viewController.present(item: itemToDisplay)
viewController.setupStatusBar()

self.setViewControllers([viewController], direction: .forward, animated: false, completion: nil)

viewController.present(item: itemToDisplay)
viewController.updateNavigationBarItems()
}
}
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
query.removeFilter(itemsFilter)
}

override var childForHomeIndicatorAutoHidden : UIViewController? {
if let childViewController = self.children.first {
return childViewController
Expand All @@ -104,10 +106,11 @@ class GalleryHostViewController: UIPageViewController {
if (object as? OCQuery) === query {
query.requestChangeSet(withFlags: .onlyResults) { ( _, changeSet) in
guard changeSet != nil else { return }
self.items = changeSet!.queryResult.filter({$0.mimeType != nil})
self.items = self.selectedFilter?(changeSet!.queryResult)
}
}
}
// swiftlint:enable block_based_kvo

// MARK: - Extension selection
private func selectDisplayViewControllerBasedOn(mimeType: String) -> (DisplayViewController) {
Expand Down Expand Up @@ -139,6 +142,7 @@ class GalleryHostViewController: UIPageViewController {
return controllerType
}

// MARK: - Helper methods
private func viewControllerAtIndex(index: Int) -> UIViewController? {
guard let items = items else { return nil }

Expand All @@ -147,39 +151,42 @@ class GalleryHostViewController: UIPageViewController {
let item = items[index]

let newViewController = selectDisplayViewControllerBasedOn(mimeType: item.mimeType!)
let shouldDownload = newViewController is (DisplayViewController & DisplayExtension) ? true : false
let configuration = configurationFor(item, viewController: newViewController)

newViewController.configure(configuration)
newViewController.present(item: item)
return newViewController
}

private func configurationFor(_ item: OCItem, viewController: UIViewController) -> DisplayViewConfiguration {
let shouldDownload = viewController is (DisplayViewController & DisplayExtension) ? true : false
var configuration: DisplayViewConfiguration
if !shouldDownload {
configuration = DisplayViewConfiguration(item: item, core: core, state: .notSupportedMimeType)
} else {
configuration = DisplayViewConfiguration(item: item, core: core, state: .hasNetworkConnection)
}

newViewController.configure(configuration)
newViewController.present(item: item)
return newViewController
return configuration
}
}

extension GalleryHostViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
print("LOG ---> pasa after")
if let displayViewController = viewController as? DisplayViewController {

if let item = displayViewController.item, let index =
items?.firstIndex(where: {$0.fileID == item.fileID}) {
return viewControllerAtIndex(index: index + 1)
}
if let displayViewController = viewControllers?.first as? DisplayViewController,
let item = displayViewController.item,
let index = items?.firstIndex(where: {$0.fileID == item.fileID}) {
return viewControllerAtIndex(index: index + 1)
}

return nil

}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
print("LOG ---> pasa before")
if let displayViewController = viewController as? DisplayViewController, let item = displayViewController.item, let index =
items?.firstIndex(where: {$0.fileID == item.fileID}) {

if let displayViewController = viewControllers?.first as? DisplayViewController,
let item = displayViewController.item,
let index = items?.firstIndex(where: {$0.fileID == item.fileID}) {
return viewControllerAtIndex(index: index - 1)
}

Expand All @@ -193,19 +200,18 @@ extension GalleryHostViewController: UIPageViewControllerDelegate {
let previousViewController = previousViewControllers[0]
previousViewController.didMove(toParent: nil)

if completed, let vc = self.viewControllerToTansition {
if self.children.contains(vc) {
} else {
self.addChild(vc)
if completed, let viewControllerToTransition = self.viewControllerToTansition {
if self.children.contains(viewControllerToTransition) == false {
self.addChild(viewControllerToTransition)
}
vc.didMove(toParent: self)
vc.setupStatusBar()
viewControllerToTransition.didMove(toParent: self)
viewControllerToTransition.updateNavigationBarItems()
}
}

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
if let vc = pendingViewControllers[0] as? DisplayViewController {
self.viewControllerToTansition = vc
if let viewControllerToTransition = pendingViewControllers[0] as? DisplayViewController {
self.viewControllerToTansition = viewControllerToTransition
}
}
}
6 changes: 6 additions & 0 deletions ownCloud/UIKit Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ extension String {
let nonDigitsCharacterSet = CharacterSet.decimalDigits.inverted
return !self.isEmpty && rangeOfCharacter(from: nonDigitsCharacterSet) == nil
}

func matches (regExp: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: regExp) else { return false }
let range = NSRange(location: 0, length: self.count)
return regex.firstMatch(in: self, options: [], range: range) != nil
}
}
25 changes: 10 additions & 15 deletions ownCloud/Viewer/DisplayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import UIKit
import ownCloudSDK

struct DisplayViewConfiguration {
weak var item: OCItem!
var item: OCItem!
weak var core: OCCore!
let state: DisplayViewState
}
Expand All @@ -46,11 +46,7 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
private let progressViewVerticalSpacing: CGFloat = 20.0

// MARK: - Configuration
weak var item: OCItem? {
didSet {
print("LOG ---> new value of item = \(item)")
}
}
var item: OCItem?
weak var core: OCCore? {
willSet {
if let core = core {
Expand Down Expand Up @@ -108,6 +104,12 @@ 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 @@ -128,13 +130,11 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
override func loadView() {
super.loadView()

iconImageView = UIImageView()
iconImageView.translatesAutoresizingMaskIntoConstraints = false
iconImageView.contentMode = .scaleAspectFit

view.addSubview(iconImageView)

metadataInfoLabel = UILabel()
metadataInfoLabel?.translatesAutoresizingMaskIntoConstraints = false
metadataInfoLabel?.isHidden = false
metadataInfoLabel?.textAlignment = .center
Expand All @@ -151,22 +151,19 @@ class DisplayViewController: UIViewController, OCQueryDelegate {

view.addSubview(progressView!)

cancelButton = ThemeButton(type: .custom)
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!)

showPreviewButton = ThemeButton(type: .custom)
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 = UILabel()
noNetworkLabel?.translatesAutoresizingMaskIntoConstraints = false
noNetworkLabel?.isHidden = true
noNetworkLabel?.adjustsFontForContentSizeCategory = true
Expand Down Expand Up @@ -242,7 +239,7 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}
}

func setupStatusBar() {
func updateNavigationBarItems() {
if let parent = parent, let item = item, let itemName = item.name {
parent.navigationItem.title = itemName

Expand Down Expand Up @@ -276,10 +273,8 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
}
return
}
// OnMainThread {
self?.item = latestItem
self?.source = file!.url
// }
}) {
self.state = .downloading(progress: downloadProgress)
}
Expand Down Expand Up @@ -443,8 +438,8 @@ class DisplayViewController: UIViewController, OCQueryDelegate {
// MARK: - Public API
extension DisplayViewController {
func configure(_ configuration: DisplayViewConfiguration) {
self.core = configuration.core
self.item = configuration.item
self.core = configuration.core
self.state = configuration.state
}
}
Expand Down
13 changes: 13 additions & 0 deletions ownCloud/Viewer/ImageDisplayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ class ImageDisplayViewController : DisplayViewController {
var tapToZoomGestureRecognizer : UITapGestureRecognizer!
var tapToHideBarsGestureRecognizer: UITapGestureRecognizer!

// MARK: - View controller lifecycle
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

guard let navigationController = navigationController else {
return
}

navigationController.setNavigationBarHidden(false, animated: true)

setNeedsUpdateOfHomeIndicatorAutoHidden()
}

// MARK: - Specific view
override func renderSpecificView() {
scrollView = ImageScrollView(frame: self.view.bounds)
Expand Down
Loading

0 comments on commit bd207b8

Please sign in to comment.