Skip to content

Commit

Permalink
Added view comments action item to peek
Browse files Browse the repository at this point in the history
  • Loading branch information
weiran committed Apr 30, 2016
1 parent dab2902 commit 64b017a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
21 changes: 19 additions & 2 deletions Client/Post List/NewsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import Foundation
import UIKit
import SafariServices

class NewsViewController : UITableViewController, UISplitViewControllerDelegate, PostTitleViewDelegate, SFSafariViewControllerDelegate, UIViewControllerPreviewingDelegate {
class NewsViewController : UITableViewController, UISplitViewControllerDelegate, PostTitleViewDelegate, SFSafariViewControllerDelegate, SFSafariViewControllerPreviewActionItemsDelegate, UIViewControllerPreviewingDelegate {

var posts: [HNPost] = [HNPost]()
private var collapseDetailViewController = true
private var peekedIndexPath: NSIndexPath?

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -96,7 +97,9 @@ class NewsViewController : UITableViewController, UISplitViewControllerDelegate,
// MARK: - PostCellDelegate

func getSafariViewController(URL: String) -> SFSafariViewController {
return SFSafariViewController(URL: NSURL(string: URL)!)
let safariViewController = SFSafariViewController(URL: NSURL(string: URL)!)
safariViewController.previewActionItemsDelegate = self
return safariViewController
}

func didPressLinkButton(post: HNPost) {
Expand All @@ -108,6 +111,7 @@ class NewsViewController : UITableViewController, UISplitViewControllerDelegate,

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let indexPath = tableView.indexPathForRowAtPoint(location) {
peekedIndexPath = indexPath
previewingContext.sourceRect = tableView.rectForRowAtIndexPath(indexPath)
let post = posts[indexPath.row]
return getSafariViewController(post.UrlString)
Expand All @@ -118,4 +122,17 @@ class NewsViewController : UITableViewController, UISplitViewControllerDelegate,
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
presentViewController(viewControllerToCommit, animated: true, completion: nil)
}

func safariViewControllerPreviewActionItems(controller: SFSafariViewController) -> [UIPreviewActionItem] {
let indexPath = self.peekedIndexPath!
let post = posts[indexPath.row]
let commentsPreviewActionTitle = post.CommentCount > 0 ? "View \(post.CommentCount) comments" : "View comments"

let viewCommentsPreviewAction = UIPreviewAction(title: commentsPreviewActionTitle, style: .Default) {
[unowned self, indexPath = indexPath] (action, viewController) -> Void in
self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .None)
self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath)
}
return [viewCommentsPreviewAction]
}
}
52 changes: 52 additions & 0 deletions Client/SFSafariViewController+PreviewActionItems.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// SFSafariViewController+PreviewActionItems.swift
// Hackers
//
// Created by Weiran Zhang on 30/04/2016.
// Copyright © 2016 Glass Umbrella. All rights reserved.
//

import SafariServices
import ObjectiveC

private struct AssociatedKeys {
static var PreviewActionItemsDelegateName = "previewActionItemsDelegate"
static var InitialURL = "initialURL"
}

extension SFSafariViewController {
private(set) public var initialURL: NSURL? {
set {
objc_setAssociatedObject(self, &AssociatedKeys.InitialURL, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, &AssociatedKeys.InitialURL) as? NSURL
}
}

public convenience init(initialURL: NSURL, entersReaderIfAvailable: Bool) {
self.init(URL: initialURL, entersReaderIfAvailable: entersReaderIfAvailable)
self.initialURL = initialURL
}
}


// Preview action items
extension SFSafariViewController {
weak var previewActionItemsDelegate: SFSafariViewControllerPreviewActionItemsDelegate? {
set {
objc_setAssociatedObject(self, &AssociatedKeys.PreviewActionItemsDelegateName, newValue, .OBJC_ASSOCIATION_ASSIGN)
}
get {
return objc_getAssociatedObject(self, &AssociatedKeys.PreviewActionItemsDelegateName) as? SFSafariViewControllerPreviewActionItemsDelegate
}
}

public override func previewActionItems() -> [UIPreviewActionItem] {
return previewActionItemsDelegate?.safariViewControllerPreviewActionItems(self) ?? []
}
}

public protocol SFSafariViewControllerPreviewActionItemsDelegate: class {
func safariViewControllerPreviewActionItems(controller: SFSafariViewController) -> [UIPreviewActionItem]
}
4 changes: 2 additions & 2 deletions Client/Supporting Files/Hackers-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.0.1</string>
<string>3.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>11</string>
<string>12</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
Expand Down
4 changes: 4 additions & 0 deletions Hackers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
240C5BE018D267C70011177A /* Icons.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240C5BDF18D267C70011177A /* Icons.xcassets */; };
24191F8D1944C991003C0D98 /* CommentsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24191F8C1944C991003C0D98 /* CommentsController.swift */; };
241E8DAA1944AF870004F457 /* CommentModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 241E8DA91944AF870004F457 /* CommentModel.swift */; };
245DCAB01CD4DA2A00152AA3 /* SFSafariViewController+PreviewActionItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245DCAAF1CD4DA2A00152AA3 /* SFSafariViewController+PreviewActionItems.swift */; };
249A7E32194374550059C079 /* CommentTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 249A7E31194374550059C079 /* CommentTableViewCell.swift */; };
24B5BFEF1B52CE8900328C62 /* PostTitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24B5BFEE1B52CE8900328C62 /* PostTitleView.swift */; };
24C20D391A7E42D5005B759B /* MainSplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24C20D381A7E42D5005B759B /* MainSplitViewController.swift */; };
Expand All @@ -33,6 +34,7 @@
241387A51945A6E100D71220 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
24191F8C1944C991003C0D98 /* CommentsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommentsController.swift; sourceTree = "<group>"; };
241E8DA91944AF870004F457 /* CommentModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommentModel.swift; sourceTree = "<group>"; };
245DCAAF1CD4DA2A00152AA3 /* SFSafariViewController+PreviewActionItems.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SFSafariViewController+PreviewActionItems.swift"; sourceTree = "<group>"; };
249A7E31194374550059C079 /* CommentTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommentTableViewCell.swift; sourceTree = "<group>"; };
24B5BFEE1B52CE8900328C62 /* PostTitleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PostTitleView.swift; sourceTree = "<group>"; };
24C20D381A7E42D5005B759B /* MainSplitViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainSplitViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -159,6 +161,7 @@
24F39F8F18AFB1150055F8DC /* Supporting Files */,
24B5BFEE1B52CE8900328C62 /* PostTitleView.swift */,
24D6B8C91C1EDBFC00618422 /* Theme.swift */,
245DCAAF1CD4DA2A00152AA3 /* SFSafariViewController+PreviewActionItems.swift */,
);
path = Client;
sourceTree = "<group>";
Expand Down Expand Up @@ -310,6 +313,7 @@
24CE5FF419435061009D2677 /* NewsViewController.swift in Sources */,
24E4F26119B4DA9900574B52 /* CommentDelegate.swift in Sources */,
24D6B8CA1C1EDBFC00618422 /* Theme.swift in Sources */,
245DCAB01CD4DA2A00152AA3 /* SFSafariViewController+PreviewActionItems.swift in Sources */,
24B5BFEF1B52CE8900328C62 /* PostTitleView.swift in Sources */,
241E8DAA1944AF870004F457 /* CommentModel.swift in Sources */,
24191F8D1944C991003C0D98 /* CommentsController.swift in Sources */,
Expand Down

0 comments on commit 64b017a

Please sign in to comment.