OOCalendarViewController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. //
  2. // OOCalendarViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/8/4.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class OOCalendarViewController: UITableViewController {
  11. private let colorOptions = ["#428ffc", "#5bcc61", "#f9bf24", "#f75f59", "#f180f7", "#9072f1", "#909090", "#1462be"]
  12. private lazy var viewModel: OOCalendarViewModel = {
  13. return OOCalendarViewModel()
  14. }()
  15. var calendarInfo: OOCalendarInfo?
  16. private var colorValue = "#428ffc"
  17. private var count = 0
  18. override func numberOfSections(in tableView: UITableView) -> Int {
  19. // #warning Incomplete implementation, return the number of sections
  20. return 3
  21. }
  22. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  23. // #warning Incomplete implementation, return the number of rows
  24. if section == 0 {
  25. return 5
  26. } else if section == 1 {
  27. return count
  28. } else {
  29. return 1
  30. }
  31. }
  32. // MARK: - IB
  33. @IBOutlet weak var calendarNameField: UITextField!
  34. @IBOutlet weak var calendarIsOpenSwitch: UISwitch!
  35. @IBOutlet weak var calendarColorStackView: UIStackView!
  36. @IBOutlet weak var calendarRemarkField: UITextField!
  37. @IBOutlet weak var calendarTypeField: UITextField!
  38. @IBOutlet weak var calendarOrgField: UITextField!
  39. @IBOutlet weak var calendarManagerField: UITextField!
  40. @IBOutlet weak var calendarScopeField: UITextField!
  41. @IBOutlet weak var calendarNewScopeField: UITextField!
  42. @IBOutlet weak var calendarStatusSwitch: UISwitch!
  43. @IBOutlet weak var calendarIsOpenBtn: UIButton!
  44. // @IBAction func editRemarkBtn(_ sender: Any) {
  45. // self.performSegue(withIdentifier: "ShowEditRemark", sender: nil)
  46. // }
  47. //选择是否公开
  48. @IBAction func selectType(_ sender: Any) {
  49. let alertController = UIAlertController(title: "请选择类型", message: "", preferredStyle: .actionSheet)
  50. var selectStyle = UIAlertAction.Style.default
  51. selectStyle = (calendarTypeField.text == "个人日历") ? UIAlertAction.Style.default : UIAlertAction.Style.destructive
  52. let personAction = UIAlertAction(title: "个人日历", style: selectStyle, handler: { action in
  53. self.calendarTypeField.text = action.title
  54. self.count = 0
  55. let section = NSIndexSet(index: 1)
  56. self.tableView.beginUpdates()
  57. self.tableView.reloadSections(section as IndexSet, with: .none)
  58. self.tableView.endUpdates()
  59. })
  60. selectStyle = (calendarTypeField.text == "组织日历") ? UIAlertAction.Style.default : UIAlertAction.Style.destructive
  61. let orgAction = UIAlertAction(title: "组织日历", style: selectStyle, handler: { action in
  62. self.calendarTypeField.text = action.title
  63. self.count = 4
  64. let section = NSIndexSet(index: 1)
  65. self.tableView.beginUpdates()
  66. self.tableView.reloadSections(section as IndexSet, with: .none)
  67. self.tableView.endUpdates()
  68. })
  69. alertController.addAction(personAction)
  70. alertController.addAction(orgAction)
  71. self.present(alertController, animated: true, completion: nil)
  72. }
  73. override func viewDidLoad() {
  74. super.viewDidLoad()
  75. if calendarInfo != nil && calendarInfo?.id != nil {
  76. self.navigationItem.rightBarButtonItems = [
  77. UIBarButtonItem(title: "修改保存", style: .plain, target: self, action: #selector(tapSave)),
  78. UIBarButtonItem(title: "删除", style: .plain, target: self, action: #selector(tapDelete))
  79. ]
  80. self.navigationItem.title = "修改日历"
  81. loadCalendarInfoFromNet()
  82. } else {
  83. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(tapSave))
  84. self.navigationItem.title = "新增日历"
  85. self.calendarInfo = OOCalendarInfo.init()
  86. }
  87. self.tableView.tableFooterView = UIView(frame: CGRect.zero)
  88. //隐藏输入法
  89. calendarNameField.delegate = self
  90. calendarNameField.returnKeyType = .done
  91. calendarTypeField.delegate = self
  92. calendarTypeField.returnKeyType = .done
  93. calendarRemarkField.delegate = self
  94. calendarRemarkField.returnKeyType = .done
  95. calendarColorStackView?.subviews.forEach({ (colorView) in
  96. colorView.isUserInteractionEnabled = true
  97. colorView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapColorView)))
  98. })
  99. }
  100. override func didReceiveMemoryWarning() {
  101. super.didReceiveMemoryWarning()
  102. // Dispose of any resources that can be recreated.
  103. }
  104. // MARK: - private func
  105. //隐藏输入法
  106. private func hideKeyboard() {
  107. self.view.endEditing(true)
  108. }
  109. @objc func tapDelete() {
  110. showDefaultConfirm(title: "删除日历", message: "确定要删除当前日历吗,会同时删除该日历下的日程事件?") { (action) in
  111. self.deleteCalendar()
  112. }
  113. }
  114. @objc func tapSave() {
  115. hideKeyboard()
  116. let name = calendarNameField.text
  117. let isopen = calendarIsOpenSwitch.isOn
  118. var type = "PERSON"
  119. if let typeValue = calendarTypeField.text {
  120. if typeValue == "个人日历" {
  121. type = "PERSON"
  122. } else {
  123. type = "UNIT"
  124. }
  125. }
  126. let remark = calendarRemarkField.text
  127. if name == nil || name == "" {
  128. showError(title: "日历名称不能为空!")
  129. return
  130. }
  131. self.showLoading(title: "正在保存...")
  132. self.calendarInfo!.name = name
  133. self.calendarInfo!.isPublic = isopen
  134. if type == "PERSON" {
  135. if let account = O2AuthSDK.shared.myInfo() {
  136. self.calendarInfo!.target = account.distinguishedName
  137. }
  138. }
  139. self.calendarInfo!.color = colorValue
  140. self.calendarInfo!.type = type
  141. self.calendarInfo!.desc = remark ?? ""
  142. // calendar.status=="OPEN",
  143. if calendarStatusSwitch.isOn {
  144. self.calendarInfo!.status = "OPEN"
  145. } else {
  146. self.calendarInfo!.status = "CLOSE"
  147. }
  148. viewModel.saveCalendar(calendar: self.calendarInfo!)
  149. .always {
  150. self.hideLoading()
  151. }.then { (result) in
  152. DDLogInfo("保存日历成功!!!\(result)")
  153. self.closeWindow()
  154. }.catch { (error) in
  155. DDLogError(error.localizedDescription)
  156. self.showError(title: "保存日历错误!")
  157. }
  158. }
  159. private func deleteCalendar() {
  160. self.showLoading()
  161. viewModel.deleteCalendar(id: (calendarInfo?.id!)!)
  162. .always {
  163. self.hideLoading()
  164. }
  165. .then { (result) in
  166. DDLogInfo("删除结果:\(result)")
  167. self.closeWindow()
  168. }.catch { (error) in
  169. DDLogError(error.localizedDescription)
  170. self.showError(title: "删除日历错误!")
  171. }
  172. }
  173. @objc func tapColorView(_ tap: UITapGestureRecognizer) {
  174. hideKeyboard()
  175. if let tag = tap.view?.tag {
  176. selectColorView(tag: tag)
  177. }
  178. }
  179. //选择颜色模块
  180. private func selectColorView(tag: Int) {
  181. colorValue = colorOptions[tag]
  182. calendarColorStackView.subviews.forEach { (colorView) in
  183. if colorView.tag == tag {
  184. colorView.subviews[0].isHidden = false
  185. } else {
  186. colorView.subviews[0].isHidden = true
  187. }
  188. }
  189. }
  190. //选择所属组织
  191. @IBAction func selectOrg(_ sender: Any) {
  192. let arrModes = [ContactPickerType.unit]
  193. showContactPicker(modes: arrModes, callback: {
  194. (O2BizContactPickerResult) in
  195. if let depts = O2BizContactPickerResult.departments {
  196. var allDept = ""
  197. var target = ""
  198. for dept in depts {
  199. if allDept == "" {
  200. allDept = dept.name ?? ""
  201. } else {
  202. allDept = allDept + "," + (dept.name ?? "")
  203. }
  204. target = dept.distinguishedName ?? ""
  205. }
  206. self.calendarOrgField.text = allDept
  207. self.calendarInfo?.target = target
  208. }
  209. })
  210. }
  211. //选择管理者
  212. @IBAction func selectManager(_ sender: Any) {
  213. let arrModes = [ContactPickerType.person]
  214. showContactPicker(modes: arrModes, callback: {
  215. (O2BizContactPickerResult) in
  216. if let users = O2BizContactPickerResult.users {
  217. var allUser = ""
  218. var manageablePersonList = [String]()
  219. for user in users {
  220. if allUser == "" {
  221. allUser = user.name ?? ""
  222. } else {
  223. allUser = allUser + "," + (user.name ?? "")
  224. }
  225. if let distinguishedName = user.distinguishedName {
  226. manageablePersonList.append(distinguishedName)
  227. }
  228. }
  229. self.calendarManagerField.text = allUser
  230. self.calendarInfo?.manageablePersonList = manageablePersonList
  231. }
  232. })
  233. }
  234. //选择可见范围
  235. @IBAction func selectScope(_ sender: Any) {
  236. var allUser = ""
  237. var allDept = ""
  238. var allGroup = ""
  239. var all = ""
  240. var viewablePersonList = [String]()
  241. var viewableUnitList = [String]()
  242. var viewableGroupList = [String]()
  243. let arrModes = [ContactPickerType.person, ContactPickerType.unit, ContactPickerType.group]
  244. showContactPicker(modes: arrModes, callback: {
  245. (O2BizContactPickerResult) in
  246. if let users = O2BizContactPickerResult.users {
  247. for user in users {
  248. if allUser == "" {
  249. allUser = user.name ?? ""
  250. } else {
  251. allUser = allUser + "," + (user.name ?? "")
  252. }
  253. if let distinguishedName = user.distinguishedName {
  254. viewablePersonList.append(distinguishedName)
  255. }
  256. }
  257. all = allUser
  258. }
  259. if let depts = O2BizContactPickerResult.departments {
  260. for dept in depts {
  261. if allDept == "" {
  262. allDept = dept.name ?? ""
  263. } else {
  264. allDept = allDept + "," + (dept.name ?? "")
  265. }
  266. if let distinguishedName = dept.distinguishedName {
  267. viewableUnitList.append(distinguishedName)
  268. }
  269. }
  270. if "" == all {
  271. all = allDept
  272. } else {
  273. if "" != allDept {
  274. all = all + "," + allDept
  275. }
  276. }
  277. }
  278. if let groups = O2BizContactPickerResult.groups {
  279. for group in groups {
  280. if allGroup == "" {
  281. allGroup = group.name ?? ""
  282. } else {
  283. allGroup = allGroup + "," + (group.name ?? "")
  284. }
  285. if let distinguishedName = group.distinguishedName {
  286. viewableGroupList.append(distinguishedName)
  287. }
  288. }
  289. if "" == all {
  290. all = allGroup
  291. } else {
  292. if "" != allGroup {
  293. all = all + "," + allGroup
  294. }
  295. }
  296. }
  297. self.calendarScopeField.text = all
  298. self.calendarInfo?.viewablePersonList = viewablePersonList
  299. self.calendarInfo?.viewableUnitList = viewableUnitList
  300. self.calendarInfo?.viewableGroupList = viewableGroupList
  301. })
  302. }
  303. //可新建范围
  304. @IBAction func selectNewScope(_ sender: Any) {
  305. var allUser = ""
  306. var allDept = ""
  307. var allGroup = ""
  308. var all = ""
  309. var publishablePersonList = [String]()
  310. var publishableGroupList = [String]()
  311. var publishableUnitList = [String]()
  312. let arrModes = [ContactPickerType.person, ContactPickerType.unit, ContactPickerType.group]
  313. showContactPicker(modes: arrModes, callback: {
  314. (O2BizContactPickerResult) in
  315. if let users = O2BizContactPickerResult.users {
  316. for user in users {
  317. if allUser == "" {
  318. allUser = user.name ?? ""
  319. } else {
  320. allUser = allUser + "," + (user.name ?? "")
  321. }
  322. if let distinguishedName = user.distinguishedName {
  323. publishablePersonList.append(distinguishedName)
  324. }
  325. }
  326. all = allUser
  327. }
  328. if let depts = O2BizContactPickerResult.departments {
  329. for dept in depts {
  330. if allDept == "" {
  331. allDept = dept.name ?? ""
  332. } else {
  333. allDept = allDept + "," + (dept.name ?? "")
  334. }
  335. if let distinguishedName = dept.distinguishedName {
  336. publishableUnitList.append(distinguishedName)
  337. }
  338. }
  339. if "" == all {
  340. all = allDept
  341. } else {
  342. if "" != allDept {
  343. all = all + "," + allDept
  344. }
  345. }
  346. }
  347. if let groups = O2BizContactPickerResult.groups {
  348. for group in groups {
  349. if allGroup == "" {
  350. allGroup = group.name ?? ""
  351. } else {
  352. allGroup = allGroup + "," + (group.name ?? "")
  353. }
  354. if let distinguishedName = group.distinguishedName {
  355. publishableGroupList.append(distinguishedName)
  356. }
  357. }
  358. if "" == all {
  359. all = allGroup
  360. } else {
  361. if "" != allGroup {
  362. all = all + "," + allGroup
  363. }
  364. }
  365. }
  366. self.calendarNewScopeField.text = all
  367. self.calendarInfo?.publishablePersonList = publishablePersonList
  368. self.calendarInfo?.publishableUnitList = publishableUnitList
  369. self.calendarInfo?.publishableGroupList = publishableGroupList
  370. })
  371. }
  372. private func loadCalendarInfoFromNet() {
  373. DDLogDebug("loadCalendarInfoFromNet............")
  374. viewModel.getCalendar(id: (calendarInfo?.id)!)
  375. .then { (calendar) in
  376. self.updateStuffValue(calendar: calendar)
  377. }.catch { (error) in
  378. DDLogError("查询日历信息出错,\(error.localizedDescription)")
  379. }
  380. }
  381. private func updateStuffValue(calendar: OOCalendarInfo) {
  382. DDLogDebug("updateStuffValue............\(calendar.color)")
  383. calendarNameField.text = calendar.name
  384. if calendar.type == "UNIT" {
  385. self.count = 4;
  386. } else {
  387. self.count = 0;
  388. }
  389. let section = NSIndexSet(index: 1)
  390. self.tableView.beginUpdates()
  391. self.tableView.reloadSections(section as IndexSet, with: .automatic)
  392. self.tableView.endUpdates()
  393. //所属组织
  394. calendarOrgField.text = calendar.target?.getChinaName()
  395. //管理员
  396. calendarManagerField.text = calendar.manageablePersonList?.getChinaName().joined(separator: ",")
  397. //可见范围
  398. var viewablePersonList = [String]()
  399. viewablePersonList += calendar.viewablePersonList ?? []
  400. viewablePersonList += calendar.viewableUnitList ?? []
  401. viewablePersonList += calendar.viewableGroupList ?? []
  402. calendarScopeField.text = viewablePersonList.getChinaName().joined(separator: ",")
  403. //可新建范围
  404. var publishableList = [String]()
  405. publishableList += calendar.publishablePersonList ?? []
  406. publishableList += calendar.publishableGroupList ?? []
  407. publishableList += calendar.publishableUnitList ?? []
  408. calendarNewScopeField.text = publishableList.getChinaName().joined(separator: ",")
  409. if calendar.type == "PERSON" {
  410. calendarTypeField.text = "个人日历"
  411. } else {
  412. calendarTypeField.text = "组织日历"
  413. }
  414. calendarStatusSwitch.setOn(calendar.status == "OPEN", animated: true)
  415. calendarIsOpenSwitch.setOn(calendar.isPublic == true, animated: true)
  416. //隐藏类型选择按钮
  417. calendarIsOpenBtn.isHidden = true
  418. if let color = calendar.color {
  419. if let index = colorOptions.index(where: { (colorItem) -> Bool in
  420. return colorItem == color
  421. }) {
  422. self.selectColorView(tag: index)
  423. }
  424. }
  425. calendarRemarkField.text = calendar.desc
  426. }
  427. private func closeWindow() {
  428. self.navigationController?.popViewController(animated: true)
  429. }
  430. // MARK: - Table view data source
  431. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  432. //取消tableview选中状态
  433. tableView.deselectRow(at: indexPath, animated: false)
  434. // 隐藏输入法
  435. hideKeyboard()
  436. }
  437. /*
  438. // MARK: - Navigation
  439. // In a storyboard-based application, you will often want to do a little preparation before navigation
  440. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  441. // Get the new view controller using segue.destinationViewController.
  442. // Pass the selected object to the new view controller.
  443. }
  444. */
  445. }
  446. extension OOCalendarViewController: UITextFieldDelegate {
  447. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  448. if let Identifier = textField.accessibilityIdentifier {
  449. if Identifier == "calendarType" {
  450. return false
  451. } else {
  452. return true
  453. }
  454. } else {
  455. return true
  456. }
  457. }
  458. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  459. //return textField.resignFirstResponder()
  460. let res = textField.resignFirstResponder()
  461. return false
  462. }
  463. }