| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- //
- // CommonHelper.swift
- // Sirani
- //
- // Created by Hendi on 04/01/24.
- // Copyright © 2024 PT. Mitra Sinerji Teknoindo. All rights reserved.
- //
- import Foundation
- import UIKit
- class CommonHelper {
-
- class func showNoConnection(in viewController: UIViewController) {
- let alertNoConnection = UIAlertController(
- title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
- message: Constants.instance.ALERT_NO_CONNECTION_MSG,
- preferredStyle: .alert
- )
- alertNoConnection.addAction(
- UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
- )
- viewController.present(alertNoConnection, animated: true, completion: nil)
- }
-
- class func showNotAllowConnection(in viewController: UIViewController) {
- let alertNoConnection = UIAlertController(
- title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
- message: Constants.instance.ALERT_NOT_ALLOW_MSG,
- preferredStyle: .alert
- )
- alertNoConnection.addAction(
- UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
- )
- viewController.present(alertNoConnection, animated: true, completion: nil)
- }
-
- class func formatDate(from dateString: String) -> String {
- let trimmed = dateString.trimmingCharacters(in: .whitespacesAndNewlines)
- if trimmed.isEmpty || trimmed.lowercased() == "null" || trimmed == "-" ||
- trimmed.hasPrefix("0001") || trimmed.hasPrefix("0000") || trimmed.hasPrefix("1970-01-01") {
- return "-"
- }
-
- let formats = [
- "yyyy-MM-dd HH:mm:ss",
- "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
- "yyyy-MM-dd'T'HH:mm:ssZ",
- "yyyy-MM-dd",
- "dd-MM-yyyy HH:mm:ss",
- "dd/MM/yyyy HH:mm:ss",
- "dd-MM-yyyy",
- "dd/MM/yyyy",
- "yyyy/MM/dd",
- "dd MMM yyyy",
- "dd MMMM yyyy"
- ]
-
- let inputFormatter = DateFormatter()
- inputFormatter.locale = Locale(identifier: "id_ID")
-
- let calendar = Calendar.current
-
- for fmt in formats {
- inputFormatter.dateFormat = fmt
- if let date = inputFormatter.date(from: trimmed) {
- let year = calendar.component(.year, from: date)
- if year < 1970 {
- return "-"
- }
- let outputFormatter = DateFormatter()
- outputFormatter.locale = Locale(identifier: "id_ID")
- outputFormatter.dateFormat = "dd MMM yyyy"
- return outputFormatter.string(from: date)
- }
- }
-
- if trimmed.count >= 10 {
- let prefix10 = String(trimmed.prefix(10))
- for fmt in ["yyyy-MM-dd", "dd-MM-yyyy", "dd/MM/yyyy", "yyyy/MM/dd"] {
- inputFormatter.dateFormat = fmt
- if let date = inputFormatter.date(from: prefix10) {
- let year = calendar.component(.year, from: date)
- if year < 1970 {
- return "-"
- }
- let outputFormatter = DateFormatter()
- outputFormatter.locale = Locale(identifier: "id_ID")
- outputFormatter.dateFormat = "dd MMM yyyy"
- return outputFormatter.string(from: date)
- }
- }
- }
-
- return trimmed
- }
- }
|