Skip to content
This repository has been archived by the owner on Aug 16, 2019. It is now read-only.

Feature/scroll to view controller #19

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Matrioska.xcodeproj/project.pbxproj
Expand Up @@ -36,6 +36,7 @@
DE85E5AF1DEC842700DFF34A /* AppearanceSpyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE85E5AE1DEC842700DFF34A /* AppearanceSpyViewController.swift */; };
DEA64C8B1E017D8100687658 /* UIColor+HEX.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEA64C8A1E017D8100687658 /* UIColor+HEX.swift */; };
DEA64C8D1E01810500687658 /* UIColorHEXTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEA64C8C1E01810500687658 /* UIColorHEXTests.swift */; };
EDC724671E784EA5004EFEC4 /* Focusable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDC724661E784EA5004EFEC4 /* Focusable.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -86,6 +87,7 @@
DEA64C8C1E01810500687658 /* UIColorHEXTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIColorHEXTests.swift; sourceTree = "<group>"; };
DEECB3B21DD47C4A00F7DFB1 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; lineEnding = 0; path = README.md; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.markdown; };
E08AB4E968331E4711F8DCD9 /* Pods_MatrioskaTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_MatrioskaTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
EDC724661E784EA5004EFEC4 /* Focusable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Focusable.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -228,6 +230,7 @@
DE3E8D9A1DD38D1C00417AE5 /* Source */ = {
isa = PBXGroup;
children = (
EDC724651E784E74004EFEC4 /* Protocols */,
8BCFF8AB1E28D4340053AD98 /* Components */,
8BCFF8A21E28D3830053AD98 /* Factories */,
DEA64C891E017D7300687658 /* Helpers */,
Expand Down Expand Up @@ -256,6 +259,14 @@
path = Helpers;
sourceTree = "<group>";
};
EDC724651E784E74004EFEC4 /* Protocols */ = {
isa = PBXGroup;
children = (
EDC724661E784EA5004EFEC4 /* Focusable.swift */,
);
path = Protocols;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -472,6 +483,7 @@
8BCFF8B51E28D4340053AD98 /* TabBarCluster.swift in Sources */,
8BB2842A1E31754400243C0A /* Rule.swift in Sources */,
8BCFF8B31E28D4340053AD98 /* ComponentMeta.swift in Sources */,
EDC724671E784EA5004EFEC4 /* Focusable.swift in Sources */,
DEA64C8B1E017D8100687658 /* UIColor+HEX.swift in Sources */,
DE067DCC1DDE017700641D1A /* IntrinsicSizeAwareScrollView.swift in Sources */,
8BCFF8B21E28D4340053AD98 /* Component.swift in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions Source/Components/TabBarCluster.swift
Expand Up @@ -132,3 +132,9 @@ extension ClusterLayout {
}
}
}

extension UITabBarController: Focusable {
func focus(on viewController: UIViewController) {
selectedViewController = viewController
}
}
21 changes: 21 additions & 0 deletions Source/Protocols/Focusable.swift
@@ -0,0 +1,21 @@
//
// Focusable.swift
// Matrioska
//
// Created by Andreas Thenn on 14/03/2017.
// Copyright © 2017 runtastic. All rights reserved.
//

import Foundation

/** This protocol should be implemented by every UIViewController that has some kind of focus,
highlight or select method. We want these to be adressable in a more generic way and
we don't need to care about the actual underlying method that's being called.
*/
protocol Focusable {

/** Try to focus on the given UIViewController, otherwise it fails silently */
@discardableResult
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@discardableResult on a function with return type Void is kind of pointless, wouldn't you agree?

func focus(on viewController: UIViewController)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second @joanromano's position: "I would expect Matrioska to support navigation to some Component or child Component in a generic way". Matrioska is basically a "Component tree API", so the API should allow clients to focus Components, not UIViewControllers.

But before that, I think we need to introduce an identity concept for Components, a means by which Components can be looked up, so that you could find the Component to focus.

The question is also whether the focus request should be dispatched to the root of the Component tree, i.e.

root.focus(componentId: someId)

or the Component to focus:

if let someNestedComponent = root.find(by: someId) {
    someNestedComponent.focus()
}

To me it looks like the focusing will require a top-down approach anyway (stuff higher up the hierarchy potentially needs to be scrolled into position before focusing a nested child), so the first alternative seems the more natural thing to do.


}
75 changes: 72 additions & 3 deletions Source/Views/StackViewController.swift
Expand Up @@ -9,8 +9,16 @@
import UIKit
import SnapKit

/// An enum that describes the relative position of an element. The enum should work horizontally as well as vertically.
/// Orientation depending meaning: begin - top/left, end - bottom/right
public enum RelativePosition {
case begin
case center
case end
}

/// A ViewController that contains a stackView
final class StackViewController: UIViewController {
final class StackViewController: UIViewController, Focusable {

private let preserveParentWidth: Bool
private let scrollView = IntrinsicSizeAwareScrollView()
Expand All @@ -24,7 +32,7 @@ final class StackViewController: UIViewController {
self.preserveParentWidth = configuration.preserveParentWidth

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to avoid these next time ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we removed trailing space from swiftlint and @AndreasThenn has a different Xcode setting

title = configuration.title
stackView.distribution = .equalSpacing
stackView.axis = configuration.axis
Expand All @@ -33,7 +41,7 @@ final class StackViewController: UIViewController {
if !preserveParentWidth {
stackView.alignment = .leading
}

scrollView.backgroundColor = configuration.backgroundColor
}

Expand Down Expand Up @@ -69,4 +77,65 @@ final class StackViewController: UIViewController {
stackView.addArrangedSubview(childViewController.view)
childViewController.didMove(toParentViewController: self)
}

public func focus(on viewController: UIViewController) {
scroll(to: viewController, at: .center, animated: true)
}

/// A method to access the scrollView from outside and scroll to a given target ViewController.
/// If no relativePosition parameter is provided, it will try to center the target.
///
/// - Parameters:
/// - targetViewController: The ViewController which should scroll into focus
/// - position: An optional parameter to specify the alignement of the target. Depending of
/// the stackViews' orientation position values have different meaning:
/// begin - top/left, end - bottom/right
private func scroll(to targetViewController: UIViewController,
at position: RelativePosition = .center,
animated: Bool = false) {

guard childViewControllers.contains(targetViewController) else {
return
}

var targetOffset = CGPoint.zero
let targetFrame = targetViewController.view.frame

if stackView.axis == .vertical {
// keep current horizontal offset
targetOffset.x = scrollView.contentOffset.x
let topOffset = targetFrame.origin.y - scrollView.contentInset.top

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the calculation in the respected case

let bottomOffset = targetFrame.maxY - scrollView.bounds.height
let maxOffset = scrollView.contentSize.height - scrollView.bounds.height

switch position {
case .begin:
targetOffset.y = topOffset

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about targetOffset.y = min(maxOffset, topOffset)

case .center:
targetOffset.y = topOffset - (topOffset - bottomOffset) / 2
case .end:
targetOffset.y = bottomOffset
}

targetOffset.y = min(maxOffset, max(scrollView.contentInset.top, targetOffset.y))
} else {
// keep current vertical offset
targetOffset.y = scrollView.contentOffset.y
let leftOffset = targetFrame.origin.x - scrollView.contentInset.left

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as vertical, move it in the cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChristianOrgler Why? I then I need to duplicate code, that I need in multiple cases.

let rightOffset = targetFrame.maxX - scrollView.bounds.width
let maxOffset = scrollView.contentSize.width - scrollView.bounds.width
switch position {
case .begin:
targetOffset.x = leftOffset
case .center:
targetOffset.x = leftOffset - (leftOffset - rightOffset) / 2
case .end:
targetOffset.x = rightOffset
}

targetOffset.x = min(maxOffset, max(scrollView.contentInset.left, targetOffset.x))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a general look here and also seems to me that some ensureRangeFor calls are not needed.


scrollView.setContentOffset(targetOffset, animated: animated)
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.