Skip to content

Instantly share code, notes, and snippets.

@SURYAKANTSHARMA
Last active May 6, 2019 14:42
Show Gist options
  • Save SURYAKANTSHARMA/dc96c67a29f3b0be30434105954e484c to your computer and use it in GitHub Desktop.
Save SURYAKANTSHARMA/dc96c67a29f3b0be30434105954e484c to your computer and use it in GitHub Desktop.
Decoupling
struct Document {
let identifier: Int
}
enum Mode: String {
case edit = "edit"
case view = "view"
}
protocol URLOpening {
func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any], completionHandler completion: ((Bool) -> Void)?)
}
extension UIApplication: URLOpening {
// Nothing needed here!
}
class DocumentOpener {
let urlOpener: URLOpening
init(urlOpener: URLOpening = UIApplication.shared) {
self.urlOpener = urlOpener
}
func openUrl(document: Document, mode: Mode) {
let stringURL = "myappscheme://open?id=\(document.identifier)&mode=\(mode.rawValue)"
guard let url = URL(string: stringURL) else {
return
}
urlOpener.open(url, options: [:], completionHandler: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment