DateRow.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // DateRow.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. open class _DateRow: _DateFieldRow {
  26. required public init(tag: String?) {
  27. super.init(tag: tag)
  28. dateFormatter = DateFormatter()
  29. dateFormatter?.timeStyle = .none
  30. dateFormatter?.dateStyle = .medium
  31. dateFormatter?.locale = Locale.current
  32. }
  33. }
  34. open class _TimeRow: _DateFieldRow {
  35. required public init(tag: String?) {
  36. super.init(tag: tag)
  37. dateFormatter = DateFormatter()
  38. dateFormatter?.timeStyle = .short
  39. dateFormatter?.dateStyle = .none
  40. dateFormatter?.locale = Locale.current
  41. }
  42. }
  43. open class _DateTimeRow: _DateFieldRow {
  44. required public init(tag: String?) {
  45. super.init(tag: tag)
  46. dateFormatter = DateFormatter()
  47. dateFormatter?.timeStyle = .short
  48. dateFormatter?.dateStyle = .short
  49. dateFormatter?.locale = Locale.current
  50. }
  51. }
  52. open class _CountDownRow: _DateFieldRow {
  53. required public init(tag: String?) {
  54. super.init(tag: tag)
  55. displayValueFor = { [unowned self] value in
  56. guard let val = value else {
  57. return nil
  58. }
  59. if let formatter = self.dateFormatter {
  60. return formatter.string(from: val)
  61. }
  62. let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: val)
  63. return DateComponentsFormatter.localizedString(from: dateComponents, unitsStyle: .full)?.replacingOccurrences(of: ",", with: "")
  64. }
  65. }
  66. }
  67. /// A row with an Date as value where the user can select a date from a picker view.
  68. public final class DateRow: _DateRow, RowType {
  69. required public init(tag: String?) {
  70. super.init(tag: tag)
  71. }
  72. }
  73. /// A row with an Date as value where the user can select a time from a picker view.
  74. public final class TimeRow: _TimeRow, RowType {
  75. required public init(tag: String?) {
  76. super.init(tag: tag)
  77. }
  78. }
  79. /// A row with an Date as value where the user can select date and time from a picker view.
  80. public final class DateTimeRow: _DateTimeRow, RowType {
  81. required public init(tag: String?) {
  82. super.init(tag: tag)
  83. }
  84. }
  85. /// A row with an Date as value where the user can select hour and minute as a countdown timer in a picker view.
  86. public final class CountDownRow: _CountDownRow, RowType {
  87. required public init(tag: String?) {
  88. super.init(tag: tag)
  89. }
  90. }