OOMeetingInforHeaderView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // OOMeetingInforHeaderView.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2018/1/4.
  6. // Copyright © 2018年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import JTCalendar
  10. protocol OOMeetingInforHeaderViewDelegate {
  11. func selectedTheMonth(_ theMonth:Date?)
  12. func selectedTheDay(_ theDay:Date?)
  13. }
  14. class OOMeetingInforHeaderView: UIView {
  15. @IBOutlet weak var calendarMenuView: JTCalendarMenuView!
  16. @IBOutlet weak var calendarContentView: JTHorizontalCalendarView!
  17. var calendarManager:JTCalendarManager?
  18. @IBOutlet weak var calendarContentViewHeight: NSLayoutConstraint!
  19. var delegate:OOMeetingInforHeaderViewDelegate?
  20. //public
  21. var eventsByDate:[String:[OOMeetingInfo]]?{
  22. didSet {
  23. calendarManager?.reload()
  24. }
  25. }
  26. private var _todayDate:Date?
  27. private var _minDate:Date?
  28. private var _maxDate:Date?
  29. private var _dateSelected:Date?
  30. override init(frame: CGRect) {
  31. super.init(frame: frame)
  32. commonInit()
  33. }
  34. required init?(coder aDecoder: NSCoder) {
  35. super.init(coder: aDecoder)
  36. commonInit()
  37. }
  38. private func commonInit(){
  39. self.bounds = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 172)
  40. }
  41. override func awakeFromNib() {
  42. calendarManager = JTCalendarManager(locale: Locale.init(identifier: "zh_CN"), andTimeZone: TimeZone.current)
  43. calendarManager?.settings.weekModeEnabled = true
  44. calendarManager?.delegate = self
  45. createMinAndMaxDate()
  46. calendarManager?.menuView = calendarMenuView
  47. calendarManager?.contentView = calendarContentView
  48. calendarManager?.setDate(_todayDate!)
  49. }
  50. @IBAction func configToday(_ sender: UIButton) {
  51. print("Today")
  52. calendarManager?.setDate(_todayDate!)
  53. calendarManager?.reload()
  54. }
  55. @IBAction func changeCalendarMode(_ sender: UIButton) {
  56. print("changeCalendarMode Tap")
  57. calendarManager?.settings.weekModeEnabled = !(calendarManager?.settings.weekModeEnabled)!
  58. calendarManager?.reload()
  59. var containerNewHeight:CGFloat = 387.0
  60. var newHeight:CGFloat = 300.0
  61. //172
  62. if (calendarManager?.settings.weekModeEnabled)! {
  63. newHeight = 85.0
  64. containerNewHeight = 172.0
  65. sender.setImage(#imageLiteral(resourceName: "icon_arrow_down"), for: .normal)
  66. }else{
  67. sender.setImage(#imageLiteral(resourceName: "icon_arrow_up"), for: .normal)
  68. }
  69. UIView.transition(with: self, duration: 0.5, options: [], animations: {
  70. self.calendarContentViewHeight.constant = newHeight
  71. self.height = containerNewHeight
  72. self.superview?.layoutIfNeeded()
  73. }, completion:{
  74. result in
  75. if result {
  76. self.delegate?.selectedTheDay(self._dateSelected)
  77. }
  78. })
  79. }
  80. private func createMinAndMaxDate(){
  81. _todayDate = Date()
  82. _minDate = calendarManager?.dateHelper.add(to: _todayDate, months: -12)
  83. _maxDate = calendarManager?.dateHelper?.add(to: _todayDate, months: 12)
  84. }
  85. }
  86. extension OOMeetingInforHeaderView{
  87. // Used only to have a key for _eventsByDate
  88. func dateFormatter() -> DateFormatter {
  89. var dateFormatter: DateFormatter?
  90. if dateFormatter == nil {
  91. dateFormatter = DateFormatter()
  92. dateFormatter?.dateFormat = "yyyy-MM-dd"
  93. }
  94. return dateFormatter!
  95. }
  96. func haveEventForDay(_ date:Date) -> Bool{
  97. guard let dict = eventsByDate else {
  98. return false
  99. }
  100. let key = self.dateFormatter().string(from: date)
  101. if dict[key] != nil && (dict[key]?.count)! > 0 {
  102. return true
  103. }
  104. return false
  105. }
  106. }
  107. extension OOMeetingInforHeaderView: JTCalendarDelegate {
  108. func calendar(_ calendar: JTCalendarManager, prepareMenuItemView menuItemView: UIView, date: Date) {
  109. var text: String? = nil
  110. var dateFormatter: DateFormatter? = nil
  111. if dateFormatter == nil {
  112. dateFormatter = calendar.dateHelper?.createDateFormatter()
  113. }
  114. dateFormatter?.dateFormat = "yyyy年MM月"
  115. text = dateFormatter?.string(from: date)
  116. (menuItemView as? UILabel)?.text = text
  117. }
  118. func calendar(_ calendar: JTCalendarManager!, prepareDayView dayView: (UIView & JTCalendarDay)!) {
  119. if dayView is JTCalendarDayView {
  120. let dView = dayView as! JTCalendarDayView
  121. // Today
  122. if calendar.dateHelper.date(Date(), isTheSameDayThan: dView.date) { // 今天
  123. dView.circleView?.isHidden = false
  124. dView.circleView?.backgroundColor = O2ThemeManager.color(for: "Base.base_color")?.alpha(0.5)
  125. dView.dotView?.backgroundColor = UIColor.white
  126. dView.textLabel?.textColor = UIColor.white
  127. }
  128. else if _dateSelected != nil && calendar.dateHelper.date(_dateSelected, isTheSameDayThan: dView.date) { // 选中的
  129. dView.circleView?.isHidden = false
  130. dView.circleView?.theme_backgroundColor = ThemeColorPicker(keyPath: "Base.base_color")
  131. dView.dotView?.backgroundColor = UIColor.white
  132. dView.textLabel?.textColor = UIColor.white
  133. }
  134. else if calendar.dateHelper.date(dView.date, isTheSameMonthThan: self.calendarContentView.date) { // 同一个月的
  135. dView.circleView?.isHidden = true
  136. dView.dotView?.theme_backgroundColor = ThemeColorPicker(keyPath: "Base.base_color")
  137. dView.textLabel?.textColor = UIColor(hex: "#666666")
  138. }
  139. else {
  140. dView.circleView?.isHidden = true
  141. dView.dotView?.theme_backgroundColor = ThemeColorPicker(keyPath: "Base.base_color")
  142. dView.textLabel?.textColor = UIColor(hex: "#CCCCCC")
  143. }
  144. if self.haveEventForDay(dView.date!){
  145. dView.dotView?.isHidden = false
  146. }else {
  147. dView.dotView?.isHidden = true
  148. }
  149. }
  150. }
  151. func calendar(_ calendar: JTCalendarManager!, didTouchDayView dayView: (UIView & JTCalendarDay)!) {
  152. if dayView is JTCalendarDayView {
  153. let dView = dayView as! JTCalendarDayView
  154. _dateSelected = dView.date
  155. // Animation for the circleView
  156. dView.circleView?.transform = CGAffineTransform.init(scaleX: 0.1, y: 0.1)
  157. UIView.transition(with: dView, duration: 0.3, options: [], animations: {
  158. dView.circleView?.transform = .identity
  159. self.calendarManager?.reload()
  160. }) { (compeleted) in
  161. }
  162. guard let block = delegate else {
  163. return
  164. }
  165. block.selectedTheDay(_dateSelected)
  166. }
  167. }
  168. func calendar(_ calendar: JTCalendarManager, canDisplayPageWith date: Date) -> Bool {
  169. return calendar.dateHelper.date(date, isEqualOrAfter: _minDate, andEqualOrBefore: _maxDate)
  170. }
  171. func calendarDidLoadPreviousPage(_ calendar: JTCalendarManager) {
  172. print("Previous Page loaded \(calendar.date().description)")
  173. guard let block = delegate else {
  174. return
  175. }
  176. block.selectedTheMonth(calendar.date())
  177. }
  178. func calendarDidLoadNextPage(_ calendar: JTCalendarManager) {
  179. print("Next Page loaded \(calendar.date().description)")
  180. guard let block = delegate else {
  181. return
  182. }
  183. block.selectedTheMonth(calendar.date())
  184. }
  185. }