Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.