Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created June 24, 2020 16:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianselig/f1f9187d8ad6d3e9bc3328dfb0bc6f71 to your computer and use it in GitHub Desktop.
Save christianselig/f1f9187d8ad6d3e9bc3328dfb0bc6f71 to your computer and use it in GitHub Desktop.
func checkClipboardForRedditURL() {
guard let url = UIPasteboard.general.url else { return }
guard url != UserDefaults.standard.url(forKey: DefaultsKeys.previouslyAskedLaunchURL) && url != UserDefaults.standard.url(forKey: DefaultsKeys.mostRecentSharedURLSoNotToAskOnLaunch) else { return }
guard let contentType = ContentIdentifier.shared.contentType(forURL: url, link: nil, moreTimeNeededHandler: nil), contentType.isRedditType else { return }
let alertController = ApolloUIAlertController(title: "Reddit Link Detected", message: "You have a Reddit link on your clipboard. Would you like to open it?", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Open", style: .default, handler: { [weak self] (action) in
guard let self = self else { return }
switch contentType {
case .redditComments(let linkID, let subreddit, let commentID, let context):
let commentsViewController = CommentsViewController(linkID: linkID, commentID: commentID, subreddit: subreddit, context: context)
self.open(viewController: commentsViewController)
case .redditUser(let username):
let profileViewController = ProfileViewController(profileType: .user(username: username))
self.open(viewController: profileViewController)
case .subreddit(let subredditName):
let postsViewController = PostsViewController(initialPostsType: .subreddit(subredditName: subredditName))
self.open(viewController: postsViewController)
case .multireddit(let username, let name):
let postsViewController = PostsViewController(initialPostsType: .multireddit(multiredditName: name, username: username))
self.open(viewController: postsViewController)
default:
return
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
visibleViewController()?.present(alertController, animated: true, completion: nil)
UserDefaults.standard.set(url, forKey: DefaultsKeys.previouslyAskedLaunchURL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment