OOCalendarEventViewController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. //
  2. // OOCalendarEventViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/7/30.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import WebKit
  11. struct Options {
  12. var key: String
  13. var value: String
  14. init(_ key: String, _ value: String) {
  15. self.key = key
  16. self.value = value
  17. }
  18. }
  19. class OOCalendarEventViewController: UITableViewController {
  20. //MARK: - arguments
  21. private let weekOptions = ["MO","TU" ,"WE","TH", "FR" ,"SA","SU"]
  22. private let colorOptions = ["#428ffc", "#5bcc61", "#f9bf24", "#f75f59", "#f180f7", "#9072f1", "#909090", "#1462be"]
  23. private let remindOptions = [Options("NONE", "不提醒"),
  24. Options("0,0,0,5", "开始时"),
  25. Options("0,0,5,0", "提前5分钟"),
  26. Options("0,0,10,0", "提前10分钟"),
  27. Options("0,0,15,0", "提前15分钟"),
  28. Options("0,0,30,0", "提前30分钟"),
  29. Options("0,1,0,0", "提前1小时"),
  30. Options("0,2,0,0", "提前2小时")]
  31. private let repeatOptions = [Options("NONE", "不重复"),
  32. Options("DAILY", "每天"),
  33. Options("WEEKLY", "每周"),
  34. Options("MONTHLY", "每月(当日)"),
  35. Options("YEARLY", "每年(当日)")]
  36. private var calendarList: [OOCalendarInfo] = []
  37. private lazy var viewModel: OOCalendarViewModel = {
  38. return OOCalendarViewModel()
  39. }()
  40. //event 变量
  41. var canUpdate = false
  42. var eventInfo: OOCalendarEventInfo?
  43. private var calendarId = ""
  44. private var colorValue = "#428ffc"
  45. private var weekDayList: [String] = []
  46. private var remindValue = ""
  47. private var repeatValue = ""
  48. //MARK: - IB
  49. @IBAction func tapDeleteBtn(_ sender: UIButton) {
  50. showDefaultConfirm(title: "删除日程", message: "确定要删除当前日程吗?") { (action) in
  51. self.startDelete()
  52. }
  53. }
  54. @IBAction func tapAllDaySwitch(_ sender: UISwitch) {
  55. if eventAllDaySwitch.isOn {
  56. eventStartTime.text = Date().toString("yyyy-MM-dd")
  57. eventEndTime.text = Date().toString("yyyy-MM-dd")
  58. }else {
  59. eventStartTime.text = Date().toString("yyyy-MM-dd HH:mm")
  60. eventEndTime.text = Date().add(component: .hour, value: 1).toString("yyyy-MM-dd HH:mm")
  61. }
  62. }
  63. @IBOutlet weak var calendarLabelView: UILabel!
  64. @IBOutlet weak var calendarStackView: UIStackView!
  65. @IBOutlet weak var remindLabelView: UILabel!
  66. @IBOutlet weak var remindStackView: UIStackView!
  67. @IBOutlet weak var repeatLabelView: UILabel!
  68. @IBOutlet weak var repeatStackView: UIStackView!
  69. @IBOutlet weak var repeatTableViewCell: UITableViewCell!
  70. @IBOutlet weak var eventTitle: UITextField!
  71. @IBOutlet weak var eventAllDaySwitch: UISwitch!
  72. @IBOutlet weak var eventStartTime: UILabel!
  73. @IBOutlet weak var eventEndTime: UILabel!
  74. @IBOutlet weak var eventStartTimeStackView: UIStackView!
  75. @IBOutlet weak var evenEndTimeStackView: UIStackView!
  76. @IBOutlet weak var eventColorStackView: UIStackView!
  77. @IBOutlet weak var weekDaysStackView: UIStackView!
  78. // @IBOutlet weak var eventRemark: UITextField!
  79. var eventRemark:String = ""
  80. @IBOutlet weak var untilDateLabel: UILabel!
  81. @IBOutlet weak var untilDateStackView: UIStackView!
  82. @IBOutlet weak var deleteBtn: UIButton!
  83. @IBOutlet weak var wkWebRemark: WKWebView!
  84. override func viewDidLoad() {
  85. super.viewDidLoad()
  86. if eventInfo != nil && eventInfo?.id != nil {
  87. let account = O2AuthSDK.shared.myInfo()
  88. // 判断是否能修改
  89. if eventInfo?.createPerson != nil && eventInfo?.createPerson == account?.distinguishedName {
  90. canUpdate = true
  91. }else {
  92. canUpdate = false
  93. }
  94. if canUpdate {
  95. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "修改", style: .plain, target: self, action: #selector(tapSave))
  96. self.navigationItem.leftBarButtonItem?.title = ""
  97. self.navigationItem.title = "修改日程"
  98. }else {
  99. self.navigationItem.title = "日程信息"
  100. }
  101. }else {
  102. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(tapSave))
  103. self.navigationItem.leftBarButtonItem?.title = ""
  104. self.navigationItem.title = "新建日程"
  105. }
  106. //隐藏输入法
  107. eventTitle.delegate = self
  108. eventTitle.returnKeyType = .done
  109. eventStartTime.text = Date().toString("yyyy-MM-dd HH:mm")
  110. eventEndTime.text = Date().add(component: .hour, value: 1).toString("yyyy-MM-dd HH:mm")
  111. self.tableView.tableFooterView = UIView(frame: CGRect.zero)
  112. // 修改填充值
  113. if eventInfo != nil && eventInfo?.id != nil {
  114. updateStuffValue()
  115. if "WEEKLY" == repeatValue {
  116. selectRepeatWeekly()
  117. }else if "NONE" == repeatValue || "" == repeatValue {
  118. selectRepeatNONE()
  119. }else {
  120. selectRepeatOther()
  121. }
  122. }else {
  123. self.repeatLabelView.text = self.repeatOptions[0].value
  124. self.repeatValue = self.repeatOptions[0].key
  125. self.remindLabelView.text = self.remindOptions[0].value
  126. self.remindValue = self.remindOptions[0].key
  127. }
  128. // 添加点击事件
  129. calendarStackView.addTapGesture { (tap) in
  130. let calendarNames = self.calendarList.map { (info) -> String in
  131. info.name ?? ""
  132. }
  133. let calendarIds = self.calendarList.map { (info) -> String in
  134. info.id ?? ""
  135. }
  136. var current = calendarIds[0]
  137. if !self.calendarId.isEmpty {
  138. current = self.calendarId
  139. }
  140. let calendarPicker = O2StringPicker.instance(items: calendarNames, completionHandler: { (result) in
  141. if let cal = self.calendarList.first(where: { (info) -> Bool in
  142. return info.id == result
  143. }) {
  144. self.changeCalendarWithColor(cal)
  145. self.calendarLabelView.text = cal.name
  146. }
  147. }, values: calendarIds, pickerTitle: "请选择日历", currentValue: current)
  148. calendarPicker.show()
  149. }
  150. remindStackView.addTapGesture { (tap) in
  151. let remindnames = self.remindOptions.map { (o) -> String in
  152. o.value
  153. }
  154. let remindIds = self.remindOptions.map { (o) -> String in
  155. o.key
  156. }
  157. var current = remindIds[0]
  158. if !self.remindValue.isEmpty {
  159. current = self.remindValue
  160. }
  161. let remindPicker = O2StringPicker.instance(items: remindnames, completionHandler: { (result) in
  162. if let op = self.remindOptions.first(where: { (o) -> Bool in
  163. return o.key == result
  164. }) {
  165. self.remindValue = op.key
  166. self.remindLabelView.text = op.value
  167. }
  168. }, values: remindIds, pickerTitle: "请选择提醒方式", currentValue: current)
  169. remindPicker.show()
  170. }
  171. repeatStackView.addTapGesture { (tap) in
  172. let repeatNames = self.repeatOptions.map { (o) -> String in
  173. o.value
  174. }
  175. let repeatIds = self.repeatOptions.map { (o) -> String in
  176. o.key
  177. }
  178. var current = repeatIds[0]
  179. if !self.repeatValue.isEmpty {
  180. current = self.repeatValue
  181. }
  182. let repeatPicker = O2StringPicker.instance(items: repeatNames, completionHandler: { (result) in
  183. if let op = self.repeatOptions.first(where: { (o) -> Bool in
  184. return o.key == result
  185. }) {
  186. self.repeatValue = op.key
  187. self.repeatLabelView.text = op.value
  188. if "WEEKLY" == self.repeatValue {
  189. self.selectRepeatWeekly()
  190. }else if "NONE" == self.repeatValue || "" == self.repeatValue {
  191. self.selectRepeatNONE()
  192. }else {
  193. self.selectRepeatOther()
  194. }
  195. }
  196. }, values: repeatIds, pickerTitle: "请选择重复方式", currentValue: current)
  197. repeatPicker.show()
  198. }
  199. eventStartTimeStackView.addTapGesture { (tap) in
  200. self.showDatePicker(true)
  201. }
  202. evenEndTimeStackView.addTapGesture { (tap) in
  203. self.showDatePicker(false)
  204. }
  205. untilDateStackView.addTapGesture { (tap) in
  206. let datePicker = DatePickerView.datePicker(style: .yearMonthDay, scrollToDate: Date()) { date in
  207. self.untilDateLabel.text = date?.toString( "yyyy-MM-dd")
  208. }
  209. let nowDate = self.untilDateLabel.text != nil ? Date.date(self.untilDateLabel.text!, formatter: "yyyy-MM-dd"): Date()
  210. datePicker.scrollToDate = nowDate ?? Date()
  211. datePicker.show()
  212. }
  213. eventColorStackView?.subviews.forEach({ (colorView) in
  214. colorView.isUserInteractionEnabled = true
  215. colorView.setCornerRadius(radius: CGFloat(12))
  216. colorView.addGestureRecognizer(UITapGestureRecognizer(target: self, action:#selector(tapColorView)))
  217. })
  218. weekDaysStackView?.subviews.forEach({ (btnView) in
  219. if btnView is UIButton {
  220. btnView.isUserInteractionEnabled = true
  221. btnView.addTapGesture(target: self, action: #selector(tapWeekBtnView))
  222. if !weekDayList.isEmpty {
  223. let weekView = (btnView as! UIButton)
  224. let tag = weekView.tag
  225. let weekDay = weekOptions[tag]
  226. if weekDayList.contains(weekDay) {
  227. weekView.backgroundColor = base_blue_color
  228. weekView.setTitleColor(UIColor.white, for: .normal)
  229. }else {
  230. weekView.backgroundColor = UIColor.white
  231. weekView.setTitleColor(UIColor.darkGray, for: .normal)
  232. }
  233. }
  234. }
  235. })
  236. viewModel.getMyCalendarList().then { (calendars) in
  237. self.calendarList = (calendars.myCalendars ?? []) + (calendars.unitCalendars ?? [])
  238. //self.calendarPickerView.reloadAllComponents()
  239. //初始化颜色
  240. if !self.calendarList.isEmpty {
  241. if self.eventInfo != nil {
  242. if let index = self.calendarList.firstIndex(where: { (info) -> Bool in
  243. return info.id == self.eventInfo?.calendarId
  244. }) {
  245. self.changeCalendarWithColor(self.calendarList[index], false)
  246. self.calendarLabelView.text = self.calendarList[index].name
  247. //self.calendarPickerView.selectRow(index, inComponent: 0, animated: true)
  248. }else {
  249. self.calendarLabelView.text = self.calendarList[0].name
  250. self.changeCalendarWithColor(self.calendarList[0])
  251. }
  252. }else {
  253. self.calendarLabelView.text = self.calendarList[0].name
  254. self.changeCalendarWithColor(self.calendarList[0])
  255. }
  256. }
  257. }.catch { (error) in
  258. DDLogError(error.localizedDescription)
  259. }
  260. NotificationCenter.default.addObserver(self, selector: #selector(registerCompletion), name: Notification.Name(rawValue:"RegisterCompletionNotification"),
  261. object: nil)
  262. }
  263. @objc func registerCompletion(notification:Notification){
  264. let theData = notification.userInfo!
  265. if let contentHtml = theData["contentHtml"] as? String {
  266. print("contentHtml =" , contentHtml)
  267. self.eventRemark = contentHtml
  268. self.wkWebRemark.loadHTMLString(contentHtml, baseURL: nil)
  269. }
  270. }
  271. override func didReceiveMemoryWarning() {
  272. super.didReceiveMemoryWarning()
  273. // Dispose of any resources that can be recreated.
  274. }
  275. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  276. let navVC = segue.destination as! ZLNavigationController
  277. let destVC = navVC.topViewController as! OOCalendarEditRemarkViewController
  278. print(self.eventRemark)
  279. destVC.contentHTML = self.eventRemark
  280. }
  281. //MARK: - private func
  282. //隐藏输入法
  283. private func hideKeyboard() {
  284. self.view.endEditing(true)
  285. }
  286. //点击颜色模块
  287. @objc func tapColorView(_ tap: UITapGestureRecognizer) {
  288. hideKeyboard()
  289. if let tag = tap.view?.tag {
  290. selectColorView(tag: tag)
  291. }
  292. }
  293. //选择颜色模块
  294. private func selectColorView(tag: Int) {
  295. colorValue = colorOptions[tag]
  296. eventColorStackView.subviews.forEach { (colorView) in
  297. if colorView.tag == tag {
  298. colorView.subviews[0].isHidden = false
  299. }else {
  300. colorView.subviews[0].isHidden = true
  301. }
  302. }
  303. }
  304. //切换日历的时候颜色变化
  305. private func changeCalendarWithColor(_ calendar: OOCalendarInfo?, _ isChangeColorValue: Bool = true) {
  306. calendarId = calendar?.id ?? ""
  307. if let color = calendar?.color {
  308. if isChangeColorValue {
  309. colorValue = color
  310. if let index = self.colorOptions.firstIndex(where: { (colorString) -> Bool in
  311. return color == colorString
  312. }) {
  313. self.selectColorView(tag: index)
  314. }
  315. }
  316. }
  317. }
  318. // 点击周按钮
  319. @objc func tapWeekBtnView(_ tap: UITapGestureRecognizer) {
  320. let weekView = tap.view
  321. if weekView is UIButton {
  322. tapRenderWeekBtn(weekView: weekView as! UIButton)
  323. }
  324. }
  325. private func tapRenderWeekBtn(weekView: UIButton) {
  326. let tag = weekView.tag
  327. let weekDay = weekOptions[tag]
  328. if weekDayList.contains(weekDay) {
  329. weekDayList.removeFirst(weekDay)
  330. weekView.backgroundColor = UIColor.white
  331. weekView.setTitleColor(UIColor.darkGray, for: .normal)
  332. }else {
  333. weekDayList.append(weekDay)
  334. weekView.backgroundColor = base_blue_color
  335. weekView.setTitleColor(UIColor.white, for: .normal)
  336. }
  337. }
  338. // 日期选择器
  339. @objc func showDatePicker(_ isStartTime: Bool) {
  340. hideKeyboard()
  341. let isAllDay = eventAllDaySwitch.isOn
  342. var style = DateStyle.yearMonthDayHourMinute
  343. if isAllDay {
  344. style = DateStyle.yearMonthDay
  345. }
  346. let datePicker = DatePickerView.datePicker(style: style, scrollToDate: Date()) { date in
  347. guard let date = date else { return }
  348. var dateString = ""
  349. if isAllDay {
  350. dateString = date.toString("yyyy-MM-dd")
  351. }else {
  352. dateString = date.toString("yyyy-MM-dd HH:mm")
  353. }
  354. if isStartTime {
  355. self.eventStartTime.text = dateString
  356. }else {
  357. self.eventEndTime.text = dateString
  358. }
  359. }
  360. if isStartTime {
  361. let date = isAllDay ? Date.date(self.eventStartTime.text!, formatter: "yyyy-MM-dd") : Date.date(self.eventStartTime.text!, formatter: "yyyy-MM-dd HH:mm")
  362. datePicker.scrollToDate = date == nil ? Date() : date!
  363. }else {
  364. let date = isAllDay ? Date.date(self.eventEndTime.text!, formatter: "yyyy-MM-dd") : Date.date(self.eventEndTime.text!, formatter: "yyyy-MM-dd HH:mm")
  365. datePicker.scrollToDate = date == nil ? Date() : date!
  366. }
  367. datePicker.show()
  368. }
  369. //保存
  370. @objc func tapSave() {
  371. hideKeyboard()
  372. if eventInfo != nil && eventInfo?.id != nil {
  373. //修改 如果不是重复的 直接更新
  374. if eventInfo?.recurrenceRule == nil || eventInfo?.recurrenceRule == "" {
  375. updateEvent(type: 0)
  376. }else {
  377. showSheetAction(title: "修改", message: "请选择重复日程的修改方式", actions: [
  378. UIAlertAction(title: "只修改当前日程", style: .default, handler: { (_) in
  379. self.updateEvent(type: 0)
  380. }),
  381. UIAlertAction(title: "修改当前日程和之后的此重复日程", style: .default, handler: { (_) in
  382. self.updateEvent(type: 1)
  383. }),
  384. UIAlertAction(title: "修改所有此重复日程", style: .default, handler: { (_) in
  385. self.updateEvent(type: 2)
  386. })])
  387. }
  388. }else {
  389. guard let title = eventTitle.text else {
  390. showError(title: "日程标题不能为空!")
  391. return
  392. }
  393. if calendarId == "" {
  394. showError(title: "没有选择日历!")
  395. return
  396. }
  397. let allday = eventAllDaySwitch.isOn
  398. //let remark = eventRemark.text ?? ""
  399. let remark = eventRemark
  400. let startTime = eventStartTime.text ?? ""
  401. let endTime = eventEndTime.text ?? ""
  402. let start = allday ? Date.date(startTime, formatter: "yyyy-MM-dd") : Date.date(startTime, formatter: "yyyy-MM-dd HH:mm")
  403. let end = allday ? Date.date(endTime, formatter: "yyyy-MM-dd") : Date.date(endTime, formatter: "yyyy-MM-dd HH:mm")
  404. if start == nil || end == nil {
  405. showError(title: "开始日期或结束日期错误!")
  406. return
  407. }
  408. if !(allday && startTime == endTime) && start!.compare(end!) != .orderedAscending {
  409. showError(title: "开始日期不能大于结束日期!")
  410. return
  411. }
  412. self.showLoading()
  413. let event = OOCalendarEventInfo.init()
  414. event.title = title
  415. event.calendarId = calendarId
  416. event.color = colorValue
  417. event.isAllDayEvent = allday
  418. event.startTime = start!.toString("yyyy-MM-dd HH:mm:ss")
  419. event.endTime = end!.toString("yyyy-MM-dd HH:mm:ss")
  420. event.recurrenceRule = rruleEncode()
  421. event.valarmTime_config = remindValue == "NONE" ? "" : remindValue
  422. event.comment = remark
  423. viewModel.saveCalendarEvent(event: event).always{
  424. self.hideLoading()
  425. }.then { (result) in
  426. DDLogInfo("保存结果:\(result)")
  427. self.closeWindow()
  428. }.catch { (error) in
  429. DDLogError(error.localizedDescription)
  430. self.showError(title: "保存日程错误!")
  431. }
  432. }
  433. }
  434. private func updateEvent(type: Int) {
  435. guard let title = eventTitle.text else {
  436. showError(title: "日程标题不能为空!")
  437. return
  438. }
  439. if calendarId == "" {
  440. showError(title: "没有选择日历!")
  441. return
  442. }
  443. let allday = eventAllDaySwitch.isOn
  444. // remark = eventRemark.text ?? ""
  445. let remark = eventRemark
  446. let startTime = eventStartTime.text ?? ""
  447. let endTime = eventEndTime.text ?? ""
  448. let start = allday ? Date.date(startTime, formatter: "yyyy-MM-dd") : Date.date(startTime, formatter: "yyyy-MM-dd HH:mm")
  449. let end = allday ? Date.date(endTime, formatter: "yyyy-MM-dd") : Date.date(endTime, formatter: "yyyy-MM-dd HH:mm")
  450. if start == nil || end == nil {
  451. showError(title: "开始日期或结束日期错误!")
  452. return
  453. }
  454. if !(allday && startTime == endTime) && start!.compare(end!) != .orderedAscending {
  455. showError(title: "开始日期不能大于结束日期!")
  456. return
  457. }
  458. self.showLoading()
  459. eventInfo?.title = title
  460. eventInfo?.calendarId = calendarId
  461. eventInfo?.color = colorValue
  462. eventInfo?.isAllDayEvent = allday
  463. eventInfo?.startTime = start!.toString("yyyy-MM-dd HH:mm:ss")
  464. eventInfo?.endTime = end!.toString("yyyy-MM-dd HH:mm:ss")
  465. eventInfo?.recurrenceRule = rruleEncode()
  466. eventInfo?.valarmTime_config = remindValue == "NONE" ? "" : remindValue
  467. eventInfo?.comment = remark
  468. switch type {
  469. case 0:
  470. updateSingle()
  471. case 1:
  472. updateAfter()
  473. case 2:
  474. updateAll()
  475. default:
  476. DDLogError("更新类型不正确。。。。。。。")
  477. self.showError(title: "更新类型不正确!!!")
  478. }
  479. }
  480. private func updateSingle() {
  481. viewModel.updateCalendarEventSingle(id: (eventInfo?.id!)!, event: eventInfo!).always {
  482. self.hideLoading()
  483. }.then { (result) in
  484. DDLogInfo("保存结果:\(result)")
  485. self.closeWindow()
  486. }.catch { (error) in
  487. DDLogError(error.localizedDescription)
  488. self.showError(title: "更新单个日程错误!")
  489. }
  490. }
  491. private func updateAfter() {
  492. viewModel.updateCalendarEventAfter(id: (eventInfo?.id!)!, event: eventInfo!).always {
  493. self.hideLoading()
  494. }.then { (result) in
  495. DDLogInfo("保存结果:\(result)")
  496. self.closeWindow()
  497. }.catch { (error) in
  498. DDLogError(error.localizedDescription)
  499. self.showError(title: "更新after日程错误!")
  500. }
  501. }
  502. private func updateAll() {
  503. viewModel.updateCalendarEventAll(id: (eventInfo?.id!)!, event: eventInfo!).always {
  504. self.hideLoading()
  505. }.then { (result) in
  506. DDLogInfo("保存结果:\(result)")
  507. self.closeWindow()
  508. }.catch { (error) in
  509. DDLogError(error.localizedDescription)
  510. self.showError(title: "更新all日程错误!")
  511. }
  512. }
  513. private func startDelete() {
  514. if eventInfo?.recurrenceRule == nil || eventInfo?.recurrenceRule == "" {
  515. deleteEvent(type: 0)
  516. }else {
  517. showSheetAction(title: "删除", message: "请选择重复日程的删除方式", actions: [
  518. UIAlertAction(title: "只删除当前日程", style: .default, handler: { (_) in
  519. self.deleteEvent(type: 0)
  520. }),
  521. UIAlertAction(title: "删除当前日程和之后的此重复日程", style: .default, handler: { (_) in
  522. self.deleteEvent(type: 1)
  523. }),
  524. UIAlertAction(title: "删除所有此重复日程", style: .default, handler: { (_) in
  525. self.deleteEvent(type: 2)
  526. })])
  527. }
  528. }
  529. private func deleteEvent(type: Int) {
  530. self.showLoading()
  531. switch type {
  532. case 0:
  533. viewModel.deleteCalendarEventSingle(id: (eventInfo?.id!)!).always{
  534. self.hideLoading()
  535. }.then { (result) in
  536. DDLogInfo("删除结果:\(result)")
  537. self.closeWindow()
  538. }.catch { (error) in
  539. DDLogError(error.localizedDescription)
  540. self.showError(title: "删除Single日程错误!")
  541. }
  542. case 1:
  543. viewModel.deleteCalendarEventAfter(id: (eventInfo?.id!)!).always{
  544. self.hideLoading()
  545. }.then { (result) in
  546. DDLogInfo("删除结果:\(result)")
  547. self.closeWindow()
  548. }.catch { (error) in
  549. DDLogError(error.localizedDescription)
  550. self.showError(title: "删除After日程错误!")
  551. }
  552. case 2:
  553. viewModel.deleteCalendarEventAll(id: (eventInfo?.id!)!).always {
  554. self.hideLoading()
  555. }.then { (result) in
  556. DDLogInfo("删除结果:\(result)")
  557. self.closeWindow()
  558. }.catch { (error) in
  559. DDLogError(error.localizedDescription)
  560. self.showError(title: "删除All日程错误!")
  561. }
  562. default:
  563. DDLogError("删除类型不正确。。。。。。。")
  564. self.showError(title: "删除类型不正确!!!")
  565. }
  566. }
  567. private func updateStuffValue() {
  568. eventTitle.text = eventInfo?.title
  569. if eventInfo?.isAllDayEvent == true {
  570. eventAllDaySwitch.setOn(true, animated: false)
  571. eventStartTime.text = eventInfo?.startTime?.subString(from: 0, to: 10)
  572. eventEndTime.text = eventInfo?.endTime?.subString(from: 0, to: 10)
  573. }else {
  574. eventAllDaySwitch.setOn(false, animated: false)
  575. eventStartTime.text = eventInfo?.startTime?.subString(from: 0, to: 16)
  576. eventEndTime.text = eventInfo?.endTime?.subString(from: 0, to: 16)
  577. }
  578. if let color = eventInfo?.color {
  579. colorValue = color
  580. if let index = self.colorOptions.firstIndex(where: { (colorString) -> Bool in
  581. return color == colorString
  582. }) {
  583. self.selectColorView(tag: index)
  584. }
  585. }
  586. if let remind = eventInfo?.valarmTime_config {
  587. if remind != "" {
  588. remindValue = remind
  589. if let index = self.remindOptions.firstIndex(where: { (r) -> Bool in
  590. return remind == r.key
  591. }) {
  592. // self.remindPickerView.selectRow(index, inComponent: 0, animated: true)
  593. self.remindLabelView.text = self.remindOptions[index].value
  594. }else {
  595. self.remindLabelView.text = self.remindOptions[0].value
  596. }
  597. }
  598. }
  599. if let repeatupdate = eventInfo?.recurrenceRule {
  600. rruleDecode(rrule: repeatupdate)
  601. DDLogInfo("freq:\(self.repeatValue)")
  602. if let index = self.repeatOptions.firstIndex(where: { (re) -> Bool in
  603. return self.repeatValue != "" && re.key == self.repeatValue
  604. }){
  605. // self.repeatPickerView.selectRow(index, inComponent: 0, animated: true)
  606. self.repeatLabelView.text = self.repeatOptions[index].value
  607. }else {
  608. self.repeatLabelView.text = self.repeatOptions[0].value
  609. }
  610. }
  611. // eventRemark.text = eventInfo?.comment
  612. eventRemark = eventInfo?.comment ?? ""
  613. self.wkWebRemark.loadHTMLString(eventRemark, baseURL: nil)
  614. }
  615. private func rruleDecode(rrule: String) {
  616. let array = rrule.split(";")
  617. array.forEach { (rruleItem) in
  618. if rruleItem.contains("FREQ") {
  619. DDLogInfo("freq:\(rruleItem)")
  620. self.repeatValue = rruleItem.split("=")[1]
  621. }
  622. if rruleItem.contains("UNTIL") {
  623. DDLogInfo("UNTIL:\(rruleItem)")
  624. let untilDate = rruleItem.split("=")[1]
  625. let date = untilDate.subString(from:0, to: 8)
  626. untilDateLabel.text = Date.date(date, formatter: "yyyyMMdd")?.toString("yyyy-MM-dd")
  627. }
  628. if rruleItem.contains("BYDAY") {
  629. DDLogInfo("BYDAY:\(rruleItem)")
  630. let wd = rruleItem.split("=")[1]
  631. self.weekDayList = wd.split(",")
  632. }
  633. }
  634. }
  635. private func closeWindow() {
  636. self.navigationController?.popViewController(animated: true)
  637. }
  638. //重复规则编码
  639. private func rruleEncode() -> String {
  640. if repeatValue == "" || repeatValue == "NONE" {
  641. return ""
  642. }
  643. var reft = "FREQ=\(repeatValue)"
  644. let untilDate = untilDateLabel.text
  645. if untilDate != nil && untilDate != "" {
  646. if let date = Date.date(untilDate!, formatter: "yyyy-MM-dd") {
  647. reft = reft+";UNTIL="+date.toString("yyyyMMdd")+"T000000Z"
  648. }
  649. }
  650. if repeatValue == "WEEKLY" && !weekDayList.isEmpty {
  651. let weekDays = weekDayList.joined(separator: ",")
  652. reft = reft+";BYDAY="+weekDays
  653. }
  654. return reft
  655. }
  656. // MARK: - Table view data source
  657. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  658. //取消tableview选中状态
  659. tableView.deselectRow(at: indexPath, animated: false)
  660. // 隐藏输入法
  661. self.view.endEditing(true)
  662. }
  663. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  664. switch indexPath.row {
  665. case 1:
  666. return CGFloat(120)
  667. case 2, 4:
  668. return CGFloat(60)
  669. case 5:
  670. if "WEEKLY" == repeatValue {
  671. return CGFloat(120)
  672. }else if "NONE" == repeatValue || "" == repeatValue {
  673. return CGFloat(60)
  674. }else {
  675. return CGFloat(90)
  676. }
  677. case 6:
  678. // if eventInfo != nil && eventInfo?.id != nil {
  679. // if canUpdate {
  680. // deleteBtn.isHidden = false
  681. // return CGFloat(140)
  682. // }else {
  683. // deleteBtn.isHidden = true
  684. // return CGFloat(100)
  685. // }
  686. // }else {
  687. // deleteBtn.isHidden = true
  688. // return CGFloat(100)
  689. // }
  690. return CGFloat(160)
  691. default:
  692. return CGFloat(45)
  693. }
  694. }
  695. private func selectRepeatWeekly() {
  696. self.tableView.beginUpdates()
  697. self.repeatTableViewCell.frame.size.height = CGFloat(120)
  698. weekDaysStackView.isHidden = false
  699. untilDateStackView.isHidden = false
  700. self.tableView.endUpdates()
  701. }
  702. private func selectRepeatNONE() {
  703. self.tableView.beginUpdates()
  704. self.repeatTableViewCell.frame.size.height = CGFloat(60)
  705. weekDaysStackView.isHidden = true
  706. untilDateStackView.isHidden = true
  707. self.tableView.endUpdates()
  708. }
  709. private func selectRepeatOther() {
  710. self.tableView.beginUpdates()
  711. self.repeatTableViewCell.frame.size.height = CGFloat(90)
  712. weekDaysStackView.isHidden = true
  713. untilDateStackView.isHidden = false
  714. self.tableView.endUpdates()
  715. }
  716. }
  717. //MARK: - extension
  718. //extension OOCalendarEventViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  719. // // 几级选项器
  720. // func numberOfComponents(in pickerView: UIPickerView) -> Int {
  721. //
  722. // return 1
  723. // }
  724. // // 多少行数据
  725. // func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  726. // switch pickerView.tag {
  727. // case 110://日历选项
  728. // return calendarList.count
  729. // case 111: // 提醒选项
  730. // return remindOptions.count
  731. // case 112: // 重复选项
  732. // return repeatOptions.count
  733. // default:
  734. // return 0
  735. // }
  736. // }
  737. // // 选项显示名称
  738. //// func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  739. //// switch pickerView.tag {
  740. //// case 110://日历选项
  741. //// return calendarList[row].name
  742. //// case 111: // 提醒选项
  743. //// return remindOptions[row].value
  744. //// case 112: // 重复选项
  745. //// return repeatOptions[row].value
  746. //// default:
  747. //// return ""
  748. //// }
  749. //// }
  750. // // 选项 view
  751. // func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  752. // var title = ""
  753. // switch pickerView.tag {
  754. // case 110://日历选项
  755. // title = calendarList[row].name ?? ""
  756. // case 111: // 提醒选项
  757. // title = remindOptions[row].value
  758. // case 112: // 重复选项
  759. // title = repeatOptions[row].value
  760. // default:
  761. // title = ""
  762. // }
  763. //
  764. // if view == nil {
  765. // let titleLabel = UILabel.init()
  766. // titleLabel.adjustsFontSizeToFitWidth = true
  767. // titleLabel.textAlignment = NSTextAlignment.center
  768. // titleLabel.textColor = UIColor.darkGray
  769. // titleLabel.font = setting_item_textFont
  770. // titleLabel.text = title
  771. // return titleLabel
  772. // }else {
  773. // let titleLabel = view as! UILabel
  774. // titleLabel.text = title
  775. // return titleLabel
  776. // }
  777. // }
  778. // // 选中
  779. // func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  780. // switch pickerView.tag {
  781. // case 110://日历选项
  782. //
  783. // self.changeCalendarWithColor(calendarList[row])
  784. // case 111: // 提醒选项
  785. //
  786. // remindValue = remindOptions[row].key
  787. // case 112: // 重复选项
  788. //
  789. // repeatValue = repeatOptions[row].key
  790. // if "WEEKLY" == repeatValue {
  791. // selectRepeatWeekly()
  792. // }else if "NONE" == repeatValue || "" == repeatValue {
  793. // selectRepeatNONE()
  794. // }else {
  795. // selectRepeatOther()
  796. // }
  797. // default:
  798. // DDLogError("啥pickerView????")
  799. // }
  800. // }
  801. //
  802. //
  803. //}
  804. extension OOCalendarEventViewController: UITextFieldDelegate {
  805. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  806. return textField.resignFirstResponder()
  807. }
  808. }