Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Fix: Collection view focus #102

Merged
merged 16 commits into from Mar 29, 2017
Merged

Fix: Collection view focus #102

merged 16 commits into from Mar 29, 2017

Conversation

3lvis
Copy link
Owner

@3lvis 3lvis commented Mar 28, 2017

Viewer currently wasn't handling focus quite well changing the focus of the selected cell when you swipe between viewable objects, this PR aims to fix that.

Before

before

After

after

@3lvis 3lvis changed the title Fix: Jumping collection view fous Fix: Jumping collection view focus Mar 28, 2017
@3lvis 3lvis changed the title Fix: Jumping collection view focus Fix: Collection view focus Mar 28, 2017
@3lvis
Copy link
Owner Author

3lvis commented Mar 28, 2017

@marijnschilling what about this one?

@marijnschilling
Copy link
Contributor

I feel like I cant really review it, really dont know what is going on in the code :(

@3lvis
Copy link
Owner Author

3lvis commented Mar 28, 2017

@marijnschilling Maybe I can help, anything in special that feels difficult to understand?

@marijnschilling
Copy link
Contributor

Yeah maybe explain what changed code-wise. In the context of the classes that are changed.

I also don't see from the gifs what has changed in functionality.

@3lvis
Copy link
Owner Author

3lvis commented Mar 28, 2017

@marijnschilling For the gif, check the last part when the viewer is dismissed, then you will see the difference. In one it goes back to the place that it should go and in the other one it jumps to another cell.

@@ -50,6 +50,20 @@ class PhotosController: UICollectionViewController {
}
}

#if os(tvOS)
override var preferredFocusEnvironments: [UIFocusEnvironment] {
Copy link
Owner Author

Choose a reason for hiding this comment

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

In this class the main changes are improving the control of the focus engine. preferredFocusEnvironments should return the views that you want to focus, in our case it will return the currently selected cell.

self.present(self.viewerController!, animated: false, completion: nil)
}

#if os(tvOS)
override public func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
Copy link
Owner Author

Choose a reason for hiding this comment

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

This is where we tell the collection view if it should focus or not its items.

Copy link
Owner Author

Choose a reason for hiding this comment

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

If the viewer is dismissed it will work as usual, otherwise it ignores the focus changes, this disables the default behavior where the collection view will try to figure out which view to focus on, and instead we will tell the collection view which view to focus thanks to the preferredFocusEnvironments.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really understand what these focus changes are.. and UIFocusEnvironment I might have to read up on that before i understand what it does.

extension PhotosController: ViewerControllerDelegate {
func viewerController(_ viewerController: ViewerController, didChangeFocusTo indexPath: IndexPath) {}

func viewerControllerDidDismiss(_ viewerController: ViewerController) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

When the controller is dismissed we tell the collection view to figure out what do focus on, this will trigger a call to preferredFocusEnvironments.

@@ -94,12 +94,12 @@ public class ViewerController: UIViewController {
/**
Copy link
Owner Author

Choose a reason for hiding this comment

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

Changes on ViewerController are mainly to support the functionality on PhotosController.

@3lvis
Copy link
Owner Author

3lvis commented Mar 28, 2017

@marijnschilling let me know if I can clarify anything else.

@3lvis
Copy link
Owner Author

3lvis commented Mar 29, 2017

Gonna merge this to update the whole project to Swift 3.1. Will continue with code-review comments on a new PR.

@3lvis 3lvis merged commit b6e7ff7 into master Mar 29, 2017
@3lvis 3lvis deleted the fix/jumping-collection-view branch March 29, 2017 05:39

#if os(tvOS)
override public func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
let isViewerIsVisible = self.viewerController?.isPresented ?? false
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be called isViewerVisible right?

Copy link
Owner Author

Choose a reason for hiding this comment

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

duh!

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fixed de66baf

self.present(self.viewerController!, animated: false, completion: nil)
}

#if os(tvOS)
override public func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really understand what these focus changes are.. and UIFocusEnvironment I might have to read up on that before i understand what it does.


/**
A helper to prevent the paginated scroll view to be set up twice when is presented
*/
fileprivate var presented = false
fileprivate(set) public var isPresented = false
Copy link
Contributor

Choose a reason for hiding this comment

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

Oooh what does this do? fileprivate(set) public

Copy link
Owner Author

@3lvis 3lvis Mar 29, 2017

Choose a reason for hiding this comment

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

Means you can read it but not write on it.

@marijnschilling
Copy link
Contributor

The code looks good!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants