123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- //
- // MCareUtil.swift
- // mcare-demo-InHouse
- //
- // Created by SungWon Lee on 27/10/2018.
- // Copyright © 2018 LemonHealthcare. All rights reserved.
- //
- import Foundation
- import MCareFramework
- class MCareUtil {
- static let shared = MCareUtil()
-
- private init() {}
-
- // 커스텀 알림창을 띄워준다.(중복해서 띄워주기 싫은 알림창은 Tag를 지정)
- func showCustomAlert(viewTag: Int = 0, title: String?, message: String, firstBtnTitle: String, secondBtnTitle: String?, titleIcon: String?, firstBtnAction: (() -> Void)?, secondBtnAction: (() -> Void)?) {
- DispatchQueue.main.async {
- // viewTag가 0이 아니고 뷰가 떠있으면 다시 띄워주지 않음
- if viewTag != 0 && self.isViewWithTag(tag: viewTag) {
- return
- }
-
- var alertTitle: String? = title
- if alertTitle == nil {
- alertTitle = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""
- }
- let alertView: MCareCustomAlerview = MCareCustomAlerview(viewTag: viewTag, title: alertTitle, message: message, firstBtnTitle: firstBtnTitle, secondBtnTitle: secondBtnTitle, titleIcon: titleIcon, firstBtnAction: { () -> Void in
- // firstbutton click handler
- if let _firstButtonAction = firstBtnAction {
- _firstButtonAction()
- }
-
- }, secondBtnAction: { () -> Void in
- // handler
- if let _secondButtonAction = secondBtnAction {
- _secondButtonAction()
- }
- })
- alertView.show()
- }
- }
-
- // 뷰 태크로 해당 뷰가 뷰 히어라키에 존재하는지 찾는다.
- func isViewWithTag(tag: Int) -> Bool {
-
- guard let topView = UIApplication.shared.keyWindow?.subviews[0] else {
- return false
- }
-
- var result: Bool = false
- for item in topView.subviews {
- if item.tag == tag {
- result = true
- break
- }
- }
-
- return result
- }
-
- //MARK: =최상위 ViewController 반환
- func topViewController() -> UIViewController? {
-
- let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
-
- guard var vc = appDelegate.window?.rootViewController else { return nil }
-
- while let presentedViewController = vc.presentedViewController {
- vc = presentedViewController
- }
-
- return vc
- }
-
- //MARK: =앱을 종료 처리
- func exitApplication(alertMessage: String?) {
- var alertMsg:String
- if let _alertMessage = alertMessage {
- alertMsg = _alertMessage + "\n" + NSLocalizedString("MCareExitMSG", comment: "앱이 종료됩니다.")
- }
- else {
- alertMsg = NSLocalizedString("MCareExitMSG", comment: "앱이 종료됩니다.")
- }
-
- let alertBtnTitle = NSLocalizedString("AlertOK", comment: "확인")
-
- showCustomAlert(title: nil, message: alertMsg, firstBtnTitle: alertBtnTitle, secondBtnTitle: nil, titleIcon: MCarePlusConstants.MCARE_ALERT_TITLE_ICON, firstBtnAction: { () -> Void in
- DispatchQueue.main.async {
- exit(0)
- }
- }, secondBtnAction: nil)
- }
-
- }
- extension Optional where Wrapped == String {
- var isNilOrEmpty: Bool {
- return self?.trimmingCharacters(in: .whitespaces).isEmpty ?? true
- }
- }
|