CommonHelper.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // CommonHelper.swift
  3. // Sirani
  4. //
  5. // Created by Hendi on 04/01/24.
  6. // Copyright © 2024 PT. Mitra Sinerji Teknoindo. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. class CommonHelper {
  11. class func showNoConnection(in viewController: UIViewController) {
  12. let alertNoConnection = UIAlertController(
  13. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  14. message: Constants.instance.ALERT_NO_CONNECTION_MSG,
  15. preferredStyle: .alert
  16. )
  17. alertNoConnection.addAction(
  18. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  19. )
  20. viewController.present(alertNoConnection, animated: true, completion: nil)
  21. }
  22. class func showNotAllowConnection(in viewController: UIViewController) {
  23. let alertNoConnection = UIAlertController(
  24. title: Constants.instance.ALERT_NO_CONNECTION_TITLE,
  25. message: Constants.instance.ALERT_NOT_ALLOW_MSG,
  26. preferredStyle: .alert
  27. )
  28. alertNoConnection.addAction(
  29. UIAlertAction(title: Constants.instance.ALERT_NO_CONNECTION_YESS, style: .default, handler: nil)
  30. )
  31. viewController.present(alertNoConnection, animated: true, completion: nil)
  32. }
  33. class func formatDate(from dateString: String) -> String {
  34. let trimmed = dateString.trimmingCharacters(in: .whitespacesAndNewlines)
  35. if trimmed.isEmpty || trimmed.lowercased() == "null" || trimmed == "-" ||
  36. trimmed.hasPrefix("0001") || trimmed.hasPrefix("0000") || trimmed.hasPrefix("1970-01-01") {
  37. return "-"
  38. }
  39. let formats = [
  40. "yyyy-MM-dd HH:mm:ss",
  41. "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
  42. "yyyy-MM-dd'T'HH:mm:ssZ",
  43. "yyyy-MM-dd",
  44. "dd-MM-yyyy HH:mm:ss",
  45. "dd/MM/yyyy HH:mm:ss",
  46. "dd-MM-yyyy",
  47. "dd/MM/yyyy",
  48. "yyyy/MM/dd",
  49. "dd MMM yyyy",
  50. "dd MMMM yyyy"
  51. ]
  52. let inputFormatter = DateFormatter()
  53. inputFormatter.locale = Locale(identifier: "id_ID")
  54. let calendar = Calendar.current
  55. for fmt in formats {
  56. inputFormatter.dateFormat = fmt
  57. if let date = inputFormatter.date(from: trimmed) {
  58. let year = calendar.component(.year, from: date)
  59. if year < 1970 {
  60. return "-"
  61. }
  62. let outputFormatter = DateFormatter()
  63. outputFormatter.locale = Locale(identifier: "id_ID")
  64. outputFormatter.dateFormat = "dd MMM yyyy"
  65. return outputFormatter.string(from: date)
  66. }
  67. }
  68. if trimmed.count >= 10 {
  69. let prefix10 = String(trimmed.prefix(10))
  70. for fmt in ["yyyy-MM-dd", "dd-MM-yyyy", "dd/MM/yyyy", "yyyy/MM/dd"] {
  71. inputFormatter.dateFormat = fmt
  72. if let date = inputFormatter.date(from: prefix10) {
  73. let year = calendar.component(.year, from: date)
  74. if year < 1970 {
  75. return "-"
  76. }
  77. let outputFormatter = DateFormatter()
  78. outputFormatter.locale = Locale(identifier: "id_ID")
  79. outputFormatter.dateFormat = "dd MMM yyyy"
  80. return outputFormatter.string(from: date)
  81. }
  82. }
  83. }
  84. return trimmed
  85. }
  86. }