I'm trying to make my life a little bit easier and my app more maintainable while at the same time reducing the amount of duplicated code. So I thought it would be nice to put the some code to display a certain type of UIAlertController into its own class.
The thing is, that I have basically the same alert which only differs really slightly based on where I display it in my app. So I thought to myself: why not use a enum and every time I want to display an alert of that kind, just give it the enum value. Get rid of all the duplicated strings everywhere in my code.
I love enums in Swift. They are just so schwifty - ehh I mean swifty. So I came up with this example enum:
enum MyAlertType {
case a
case b
}
Next I created a simple class to handle the display:
class MyAlert {
static func showAlert(ofType type: MyAlertType, inViewController viewController: UIViewController, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
viewController.present(alert, animated: true, completion: completion)
}
}
//Somewhere in my code (obviously a UIViewController)
MyAlert.showAlert(ofType: .a), inViewController: self)
But wait, I still have to give this function the ViewController where I want to display the alert in. For me thats always the same (self) so the next logical step was to make this an extension:
extension UIViewController {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
present(alert, animated: true, completion: completion)
}
}
//Somewhere in my UIViewController)
showAlert(ofType: .a)
But this makes the code available for all UIViewControllers, even those where I don't explicitly need/want to display that kind of alert. I mean, yeah sure, I am the developer and I can decide not to use this but on the other hand isn't it always a good practice to hide everything as much as possible and as least as necessary? Who knows who might in the future join my dev team and start misusing my beautiful code in ways I haven't thought of?
The other day I learned about Swifts protocol oriented programming approach (which to be honest I still have not fully understood by now) and now I think, I maybe should make this a protocol with a default implementation and then let the only those UIViewControllers implement the protocol, where I need to show this alert.
protocol MyAlertProtocol {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())?, completion: (() -> ())?)
}
extension MyAlertProtocol where Self : UIViewController {
func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil, completion: (() -> ())? = nil) {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
present(alert, animated: true, completion: completion)
}
}
extension MyViewController: MyAlertProtocol {}
I know that this might sound as an opinion based question so I'm not asking you if you think if this or that is better but to tell me if there actually is a best practice for this scenario and how it looks like.
Protocol with default implementation via protocol extension? Simple UIViewController extension? Custom Enum, Struct or Class with static function (and if so, which one)? Or maybe even just a function somewhere in a Swift file? I feel overwhelmed. The agony of choice...
UPDATE/SOLUTION
After reading the given answers, I decided that my Sergeys answer was indeed the most suitable one. I wanted to make my like "easier" by having "less lines of duplicated code". For me this included the "presend(controller:animated:)" inside my ViewController.
However, I think you guys are right. I should use a struct (a class is really not necessary for one static function) with a static func to generate the alert man make it "ready to use" but still let the caller decide where to present it.
By doing it like this, I could use my alert-generation-struct anywhere I want and for example let a delegate present it or pass it around until I reach a UIViewController who can present it, if my caller isn't one.
Therefore in my very simple case, I'll go with:
enum MyAlertType {
case a
case b
}
struct MyAlert {
static func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -> ())? = nil) -> UIAlertController {
var message: String
switch type {
case .a:
message = "A is a nice letter!"
case .b:
message = "B is a nice letter!"
}
let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
return alert
}
}
Thanks to everybody participating in dissolving the blockade in my head.