Last active
May 6, 2019 14:42
-
-
Save SURYAKANTSHARMA/dc96c67a29f3b0be30434105954e484c to your computer and use it in GitHub Desktop.
Decoupling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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