Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typealias completionHandler = ()->()
- enum TypeOfComplietion: String{
- case ok
- case cancel
- }
- protocol AlertViewPlayground{
- var title: String {get}
- var message: String {get}
- var dictionary: [TypeOfComplietion: completionHandler] {get set}
- func show()
- func ok()
- func addAction(key: TypeOfComplietion, closure: @escaping ()->())
- }
- protocol CancelAlertViewPlayground : AlertViewPlayground{
- func cancel()
- }
- enum TypeOfAlert {
- case unknownError
- case dbError
- case phoneError
- }
- class ErrorUnknownAlert: AlertViewPlayground{
- var title: String
- var message: String
- var dictionary: [TypeOfComplietion : completionHandler] = [:]
- init(title: String, message: String) {
- self.title = title
- self.message = message
- }
- func show() {
- print("Title: \(title), message: \(message)")
- }
- func ok(){
- guard !dictionary.isEmpty else{
- print("dismiss")
- return
- }
- print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
- }
- func addAction(key: TypeOfComplietion, closure: @escaping ()->()){
- dictionary.updateValue(closure, forKey:key)
- }
- }
- class ErrorDBAlert: AlertViewPlayground{
- var title: String
- var message: String
- var dictionary: [TypeOfComplietion : completionHandler] = [:]
- init(title: String, message: String) {
- self.title = title
- self.message = message
- }
- func show() {
- print("Title: \(title), message: \(message)")
- }
- func ok() {
- guard !dictionary.isEmpty else{
- print("dismiss")
- return
- }
- print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
- }
- func addAction(key: TypeOfComplietion, closure: @escaping () -> ()) {
- dictionary.updateValue(closure, forKey:key)
- }
- }
- class ErrorInPhoneAlert: CancelAlertViewPlayground{
- var title: String
- var message: String
- init(title: String, message: String) {
- self.title = title
- self.message = message
- }
- var dictionary: [TypeOfComplietion : completionHandler] = [:]
- func show() {
- print("Title: \(title), message: \(message)")
- }
- func ok() {
- if (dictionary.index(forKey: TypeOfComplietion.ok) != nil){
- print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
- }else {
- print("Dismiss")
- }
- }
- func cancel() {
- if (dictionary.index(forKey: TypeOfComplietion.cancel) != nil){
- print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
- }else {
- print("Dismiss")
- }
- }
- func addAction(key: TypeOfComplietion, closure: @escaping () -> ()) {
- dictionary.updateValue(closure, forKey:key)
- }
- }
- class AlertFactory {
- static let shared = AlertFactory()
- func getAlert(alertType: TypeOfAlert) -> AlertViewPlayground{
- switch alertType {
- case .unknownError:
- return ErrorUnknownAlert(title: "Error unknown", message: "It is not so important but something went wrong.")
- case .dbError:
- return ErrorDBAlert(title: "Error in DB", message: "The database is wrong and the app need to restart now")
- case .phoneError:
- return ErrorInPhoneAlert(title: "Error in Phone", message: "The phone is not working properly and it should be restarted.Do you want to restart it now?")
- }
- }
- }
- let unknownAlert = AlertFactory.shared.getAlert(alertType: .unknownError)
- unknownAlert.show()
- var okAction: () -> () = {
- print("asd")
- }
- unknownAlert.addAction(key: TypeOfComplietion.ok, closure: okAction)
- unknownAlert.ok()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement