Skip to content

Instantly share code, notes, and snippets.

@zaimramlan
Last active March 27, 2018 09:22
Show Gist options
  • Save zaimramlan/bed6320c99e9efe884fbec63dfebe427 to your computer and use it in GitHub Desktop.
Save zaimramlan/bed6320c99e9efe884fbec63dfebe427 to your computer and use it in GitHub Desktop.
An iOS Quick Support screen represented using CleanSwift architecture
// Models File
// QuickSupportModels.swift
class QuickSupportModels {
...
enum CallSupport {
struct Request {
var viewController: UIViewController?
var supportPhoneNumber: String?
}
struct Response {
}
struct ViewModel {
}
}
...
}
// ViewController File
// QuickSupportViewController.swift
class QuickSupportViewController {
...
func callCustomerService(for number: String) {
let alertTitle = Localiser.sharedInstance.string(for: "Quick Support.Call.Alert.Title")
let alertDescription = Localiser.sharedInstance.string(for: "Quick Support.Call.Alert.Description")
let alertYes = Localiser.sharedInstance.string(for: "Quick Support.Call.Alert.Yes")
let alertNo = Localiser.sharedInstance.string(for: "Quick Support.Call.Alert.No")
var alertButtonAndActions: [UIAlertButtonAndAction] = []
alertButtonAndActions.append(UIAlertButtonAndAction(buttonLabel: alertNo, style: .cancel))
alertButtonAndActions.append(UIAlertButtonAndAction(buttonLabel: alertYes, style: .ok) {
let request = QuickSupportModels.CallSupport.Request(viewController: self, supportPhoneNumber: number)
self.interactor?.callSupport(with: request)
})
}
func displayCallSupportResult(with viewModel: QuickSupportModels.CallSupport.ViewModel) {
// display whatever you want after you've called the Phone app to call the Support Phone Number
}
...
}
// Interactor File
// QuickSupportInteractor.swift
class QuickSupportInteractor {
...
func callSupport(with request: QuickSupportModels.CallSupport.Request) {
worker = QuickSupportWorker(viewController: request.viewController!)
worker?.callSupport(number: request.supportPhoneNumber!, completionHandler: {
let response = QuickSupportModels.CallSupport.Response()
self.presenter?.presentCallSupportResult(with: response)
})
}
...
}
// Worker File
// QuickSupportWorker.swift
class QuickSupportWorker {
...
func callSupport(number: String, completionHandler: @escaping () -> Void) {
if let phoneCallURL:NSURL = NSURL(string: "tel://\(number)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL as URL)) {
application.openURL(phoneCallURL as URL);
}
}
completionHandler()
}
...
}
// Presenter File
// QuickSupportPresenter.swift
class QuickSupportPresenter {
func presentCallSupportResult(with response: QuickSupportModels.CallSupport.Response) {
let viewModel = QuickSupportModels.CallSupport.ViewModel()
viewController?.displayCallSupportResult(with: viewModel)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment