Skip to content

Instantly share code, notes, and snippets.

@wassim93
Created July 22, 2020 11:36
Show Gist options
  • Save wassim93/3f6f6848156f0b09f143e8950b6c9b33 to your computer and use it in GitHub Desktop.
Save wassim93/3f6f6848156f0b09f143e8950b6c9b33 to your computer and use it in GitHub Desktop.
Reusable alert view controller with multiple types
import UIKit
//swiftlint:disable superfluous_disable_command identifier_name line_length
enum AlertType {
case ERROR, INFO, ONOFF_INFOCOUPON, NO_NETWORK, LOGOUT
}
class AlertViewController: UIViewController {
@IBOutlet weak var alertContainerWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var saveBtnViewHeight: NSLayoutConstraint!
@IBOutlet weak var saveBtn: UIButton!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var cancelBtnViewHeight: NSLayoutConstraint!
@IBOutlet weak var stackView: UIStackView!
@IBOutlet weak var titleHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var btnViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var closeXBtn: UIButton!
@IBOutlet weak var infoIcon: UIImageView!
@IBOutlet weak var alertContainer: UIView!
@IBOutlet weak var alertTitle: UILabel!
@IBOutlet weak var alertMessage: UILabel!
@IBOutlet weak var alertBtn: UIButton!
@IBOutlet weak var titleView: UIView!
@IBOutlet weak var btnView: UIView!
@IBOutlet weak var backgroundView: UIView!
var aTitle: String?
var aMessage: String?
var type: AlertType?
var saveTitle: String?
var iconColor: String?
weak var alertDelegate: AlertListenerProtocol?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
alertTitle.text = aTitle?.uppercased()
alertMessage.text = aMessage
backgroundView.alpha = 0.7
switch type {
case .ERROR:
setErrorAlertStyle()
case .INFO:
setInfoAlertStyle()
case .ONOFF_INFOCOUPON:
setonOffInfoCoupon()
case.NO_NETWORK:
setNoNetworkStyle()
case .LOGOUT:
setLogoutStyle()
default:
break
}
}
override func viewDidLayoutSubviews() {
setUIStyles()
}
// MARK: - Internal functions
func setErrorAlertStyle() {
//
titleHeightConstraint.constant = 30
btnViewHeightConstraint.constant = 40
alertContainerWidthConstraint.constant = 300
}
func setInfoAlertStyle() {
titleHeightConstraint.constant = 0
btnViewHeightConstraint.constant = 0
titleView.isHidden = true
btnView.isHidden = true
alertContainerWidthConstraint.constant = 280
if iconColor == "red"{
infoIcon.image = UIImage(named: "ic_info_dialog_red")
} else {
infoIcon.image = UIImage(named: "ic_info_dialog_gray")
}
}
func setNoNetworkStyle() {
closeXBtn.isHidden = true
titleHeightConstraint.constant = 30
btnViewHeightConstraint.constant = 0
btnView.isHidden = true
cancelBtnViewHeight.constant = 40
cancelBtn.setTitle("Rendben", for: .normal)
alertContainerWidthConstraint.constant = 300
if iconColor == "red"{
infoIcon.image = UIImage(named: "ic_info_dialog_red")
} else {
infoIcon.image = UIImage(named: "ic_info_dialog_gray")
}
}
func setLogoutStyle() {
closeXBtn.isHidden = true
titleHeightConstraint.constant = 30
btnViewHeightConstraint.constant = 0
btnView.isHidden = true
saveBtnViewHeight.constant = 40
saveBtn.setTitle("Kilépek", for: .normal)
cancelBtnViewHeight.constant = 40
alertContainerWidthConstraint.constant = 300
if iconColor == "red"{
infoIcon.image = UIImage(named: "ic_info_dialog_red")
} else {
infoIcon.image = UIImage(named: "ic_info_dialog_gray")
}
}
func setUIStyles() {
alertContainer.setAlertModalContainerStyle()
alertBtn.setButtonStyle()
saveBtn.setButtonStyle()
cancelBtn.setButtonStyle()
}
func setonOffInfoCoupon() {
saveBtn.setTitle(saveTitle, for: .normal)
titleHeightConstraint.constant = 30
btnViewHeightConstraint.constant = 0
alertContainerWidthConstraint.constant = 300
saveBtnViewHeight.constant = 40
cancelBtnViewHeight.constant = 40
closeXBtn.isHidden = true
}
// MARK: - IB Actions
@IBAction func closeBtnTapped(_ sender: Any) {
self.view.removeFromSuperview()
}
@IBAction func saveBtnTapped(_ sender: Any) {
alertDelegate?.savePressed!()
}
@IBAction func cancelBtnTapped(_ sender: Any) {
alertDelegate?.cancelPressed!()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment