Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.36 KB | None | 0 0
  1. typealias completionHandler = ()->()
  2. enum TypeOfComplietion: String{
  3.     case ok
  4.     case cancel
  5. }
  6.  
  7. protocol AlertViewPlayground{
  8.     var title: String {get}
  9.     var message: String {get}
  10.     var dictionary: [TypeOfComplietion: completionHandler] {get set}
  11.    
  12.     func show()
  13.     func ok()
  14.     func addAction(key: TypeOfComplietion, closure: @escaping ()->())
  15. }
  16. protocol CancelAlertViewPlayground : AlertViewPlayground{
  17.     func cancel()
  18. }
  19.  
  20. enum TypeOfAlert {
  21.     case unknownError
  22.     case dbError
  23.     case phoneError
  24. }
  25.  
  26. class ErrorUnknownAlert: AlertViewPlayground{
  27.     var title: String
  28.     var message: String
  29.    
  30.     var dictionary: [TypeOfComplietion : completionHandler] = [:]
  31.    
  32.     init(title: String, message: String) {
  33.         self.title = title
  34.         self.message = message
  35.     }
  36.    
  37.     func show() {
  38.         print("Title: \(title), message: \(message)")
  39.     }
  40.    
  41.     func ok(){
  42.         guard !dictionary.isEmpty else{
  43.             print("dismiss")
  44.             return
  45.         }
  46.         print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
  47.     }
  48.    
  49.     func addAction(key: TypeOfComplietion, closure: @escaping ()->()){
  50.         dictionary.updateValue(closure, forKey:key)
  51.     }
  52. }
  53.  
  54. class ErrorDBAlert: AlertViewPlayground{
  55.     var title: String
  56.     var message: String
  57.    
  58.     var dictionary: [TypeOfComplietion : completionHandler] = [:]
  59.    
  60.     init(title: String, message: String) {
  61.         self.title = title
  62.         self.message = message
  63.     }
  64.    
  65.    
  66.     func show() {
  67.         print("Title: \(title), message: \(message)")
  68.     }
  69.    
  70.     func ok() {
  71.         guard !dictionary.isEmpty else{
  72.             print("dismiss")
  73.             return
  74.         }
  75.         print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
  76.     }
  77.    
  78.     func addAction(key: TypeOfComplietion, closure: @escaping () -> ()) {
  79.         dictionary.updateValue(closure, forKey:key)
  80.     }
  81. }
  82.  
  83. class ErrorInPhoneAlert: CancelAlertViewPlayground{
  84.    
  85.     var title: String
  86.     var message: String
  87.    
  88.     init(title: String, message: String) {
  89.         self.title = title
  90.         self.message = message
  91.     }
  92.    
  93.     var dictionary: [TypeOfComplietion : completionHandler] = [:]
  94.    
  95.     func show() {
  96.         print("Title: \(title), message: \(message)")
  97.     }
  98.    
  99.     func ok() {
  100.         if (dictionary.index(forKey: TypeOfComplietion.ok) != nil){
  101.             print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
  102.         }else {
  103.             print("Dismiss")
  104.         }
  105.     }
  106.    
  107.     func cancel() {
  108.         if (dictionary.index(forKey: TypeOfComplietion.cancel) != nil){
  109.             print("\(String(describing: dictionary[TypeOfComplietion.ok]))")
  110.         }else {
  111.             print("Dismiss")
  112.         }
  113.     }
  114.    
  115.     func addAction(key: TypeOfComplietion, closure: @escaping () -> ()) {
  116.         dictionary.updateValue(closure, forKey:key)
  117.     }
  118. }
  119.  
  120.  
  121.  
  122. class AlertFactory {
  123.     static let shared = AlertFactory()
  124.    
  125.     func getAlert(alertType: TypeOfAlert) -> AlertViewPlayground{
  126.         switch alertType {
  127.         case .unknownError:
  128.             return ErrorUnknownAlert(title: "Error unknown", message: "It is not so important but something went wrong.")
  129.         case .dbError:
  130.             return ErrorDBAlert(title: "Error in DB", message: "The database is wrong and the app need to restart now")
  131.         case .phoneError:
  132.             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?")
  133.         }
  134.        
  135.     }
  136. }
  137.  
  138. let unknownAlert = AlertFactory.shared.getAlert(alertType: .unknownError)
  139. unknownAlert.show()
  140. var okAction: () -> () = {
  141.      print("asd")
  142. }
  143.  
  144. unknownAlert.addAction(key: TypeOfComplietion.ok, closure: okAction)
  145. unknownAlert.ok()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement