123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- //
- // OOCalendarViewController.swift
- // O2Platform
- //
- // Created by FancyLou on 2018/8/4.
- // Copyright © 2018 zoneland. All rights reserved.
- //
- import UIKit
- import CocoaLumberjack
- class OOCalendarViewController: UITableViewController {
- private let colorOptions = ["#428ffc", "#5bcc61", "#f9bf24", "#f75f59", "#f180f7", "#9072f1", "#909090", "#1462be"]
- private lazy var viewModel: OOCalendarViewModel = {
- return OOCalendarViewModel()
- }()
- var calendarInfo: OOCalendarInfo?
- private var colorValue = "#428ffc"
- private var count = 0
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 3
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- if section == 0 {
- return 5
- } else if section == 1 {
- return count
- } else {
- return 1
- }
- }
- // MARK: - IB
- @IBOutlet weak var calendarNameField: UITextField!
- @IBOutlet weak var calendarIsOpenSwitch: UISwitch!
- @IBOutlet weak var calendarColorStackView: UIStackView!
- @IBOutlet weak var calendarRemarkField: UITextField!
- @IBOutlet weak var calendarTypeField: UITextField!
- @IBOutlet weak var calendarOrgField: UITextField!
- @IBOutlet weak var calendarManagerField: UITextField!
- @IBOutlet weak var calendarScopeField: UITextField!
- @IBOutlet weak var calendarNewScopeField: UITextField!
- @IBOutlet weak var calendarStatusSwitch: UISwitch!
- @IBOutlet weak var calendarIsOpenBtn: UIButton!
- // @IBAction func editRemarkBtn(_ sender: Any) {
- // self.performSegue(withIdentifier: "ShowEditRemark", sender: nil)
- // }
- //选择是否公开
- @IBAction func selectType(_ sender: Any) {
- let alertController = UIAlertController(title: "请选择类型", message: "", preferredStyle: .actionSheet)
- var selectStyle = UIAlertAction.Style.default
- selectStyle = (calendarTypeField.text == "个人日历") ? UIAlertAction.Style.default : UIAlertAction.Style.destructive
- let personAction = UIAlertAction(title: "个人日历", style: selectStyle, handler: { action in
- self.calendarTypeField.text = action.title
- self.count = 0
- let section = NSIndexSet(index: 1)
- self.tableView.beginUpdates()
- self.tableView.reloadSections(section as IndexSet, with: .none)
- self.tableView.endUpdates()
- })
- selectStyle = (calendarTypeField.text == "组织日历") ? UIAlertAction.Style.default : UIAlertAction.Style.destructive
- let orgAction = UIAlertAction(title: "组织日历", style: selectStyle, handler: { action in
- self.calendarTypeField.text = action.title
- self.count = 4
- let section = NSIndexSet(index: 1)
- self.tableView.beginUpdates()
- self.tableView.reloadSections(section as IndexSet, with: .none)
- self.tableView.endUpdates()
- })
- alertController.addAction(personAction)
- alertController.addAction(orgAction)
- self.present(alertController, animated: true, completion: nil)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- if calendarInfo != nil && calendarInfo?.id != nil {
- self.navigationItem.rightBarButtonItems = [
- UIBarButtonItem(title: "修改保存", style: .plain, target: self, action: #selector(tapSave)),
- UIBarButtonItem(title: "删除", style: .plain, target: self, action: #selector(tapDelete))
- ]
- self.navigationItem.title = "修改日历"
- loadCalendarInfoFromNet()
- } else {
- self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(tapSave))
- self.navigationItem.title = "新增日历"
- self.calendarInfo = OOCalendarInfo.init()
- }
- self.tableView.tableFooterView = UIView(frame: CGRect.zero)
- //隐藏输入法
- calendarNameField.delegate = self
- calendarNameField.returnKeyType = .done
- calendarTypeField.delegate = self
- calendarTypeField.returnKeyType = .done
- calendarRemarkField.delegate = self
- calendarRemarkField.returnKeyType = .done
- calendarColorStackView?.subviews.forEach({ (colorView) in
- colorView.isUserInteractionEnabled = true
- colorView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapColorView)))
- })
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // MARK: - private func
- //隐藏输入法
- private func hideKeyboard() {
- self.view.endEditing(true)
- }
- @objc func tapDelete() {
- showDefaultConfirm(title: "删除日历", message: "确定要删除当前日历吗,会同时删除该日历下的日程事件?") { (action) in
- self.deleteCalendar()
- }
- }
- @objc func tapSave() {
- hideKeyboard()
- let name = calendarNameField.text
- let isopen = calendarIsOpenSwitch.isOn
- var type = "PERSON"
- if let typeValue = calendarTypeField.text {
- if typeValue == "个人日历" {
- type = "PERSON"
- } else {
- type = "UNIT"
- }
- }
- let remark = calendarRemarkField.text
- if name == nil || name == "" {
- showError(title: "日历名称不能为空!")
- return
- }
- self.showLoading(title: "正在保存...")
- self.calendarInfo!.name = name
- self.calendarInfo!.isPublic = isopen
- if type == "PERSON" {
- if let account = O2AuthSDK.shared.myInfo() {
- self.calendarInfo!.target = account.distinguishedName
- }
- }
- self.calendarInfo!.color = colorValue
- self.calendarInfo!.type = type
- self.calendarInfo!.desc = remark ?? ""
- // calendar.status=="OPEN",
- if calendarStatusSwitch.isOn {
- self.calendarInfo!.status = "OPEN"
- } else {
- self.calendarInfo!.status = "CLOSE"
- }
- viewModel.saveCalendar(calendar: self.calendarInfo!)
- .always {
- self.hideLoading()
- }.then { (result) in
- DDLogInfo("保存日历成功!!!\(result)")
- self.closeWindow()
- }.catch { (error) in
- DDLogError(error.localizedDescription)
- self.showError(title: "保存日历错误!")
- }
- }
- private func deleteCalendar() {
- self.showLoading()
- viewModel.deleteCalendar(id: (calendarInfo?.id!)!)
- .always {
- self.hideLoading()
- }
- .then { (result) in
- DDLogInfo("删除结果:\(result)")
- self.closeWindow()
- }.catch { (error) in
- DDLogError(error.localizedDescription)
- self.showError(title: "删除日历错误!")
- }
- }
- @objc func tapColorView(_ tap: UITapGestureRecognizer) {
- hideKeyboard()
- if let tag = tap.view?.tag {
- selectColorView(tag: tag)
- }
- }
- //选择颜色模块
- private func selectColorView(tag: Int) {
- colorValue = colorOptions[tag]
- calendarColorStackView.subviews.forEach { (colorView) in
- if colorView.tag == tag {
- colorView.subviews[0].isHidden = false
- } else {
- colorView.subviews[0].isHidden = true
- }
- }
- }
- //选择所属组织
- @IBAction func selectOrg(_ sender: Any) {
- let arrModes = [ContactPickerType.unit]
- showContactPicker(modes: arrModes, callback: {
- (O2BizContactPickerResult) in
- if let depts = O2BizContactPickerResult.departments {
- var allDept = ""
- var target = ""
- for dept in depts {
- if allDept == "" {
- allDept = dept.name ?? ""
- } else {
- allDept = allDept + "," + (dept.name ?? "")
- }
- target = dept.distinguishedName ?? ""
- }
- self.calendarOrgField.text = allDept
- self.calendarInfo?.target = target
- }
- })
- }
- //选择管理者
- @IBAction func selectManager(_ sender: Any) {
- let arrModes = [ContactPickerType.person]
- showContactPicker(modes: arrModes, callback: {
- (O2BizContactPickerResult) in
- if let users = O2BizContactPickerResult.users {
- var allUser = ""
- var manageablePersonList = [String]()
- for user in users {
- if allUser == "" {
- allUser = user.name ?? ""
- } else {
- allUser = allUser + "," + (user.name ?? "")
- }
- if let distinguishedName = user.distinguishedName {
- manageablePersonList.append(distinguishedName)
- }
- }
- self.calendarManagerField.text = allUser
- self.calendarInfo?.manageablePersonList = manageablePersonList
- }
- })
- }
- //选择可见范围
- @IBAction func selectScope(_ sender: Any) {
- var allUser = ""
- var allDept = ""
- var allGroup = ""
- var all = ""
- var viewablePersonList = [String]()
- var viewableUnitList = [String]()
- var viewableGroupList = [String]()
- let arrModes = [ContactPickerType.person, ContactPickerType.unit, ContactPickerType.group]
- showContactPicker(modes: arrModes, callback: {
- (O2BizContactPickerResult) in
- if let users = O2BizContactPickerResult.users {
- for user in users {
- if allUser == "" {
- allUser = user.name ?? ""
- } else {
- allUser = allUser + "," + (user.name ?? "")
- }
- if let distinguishedName = user.distinguishedName {
- viewablePersonList.append(distinguishedName)
- }
- }
- all = allUser
- }
- if let depts = O2BizContactPickerResult.departments {
- for dept in depts {
- if allDept == "" {
- allDept = dept.name ?? ""
- } else {
- allDept = allDept + "," + (dept.name ?? "")
- }
- if let distinguishedName = dept.distinguishedName {
- viewableUnitList.append(distinguishedName)
- }
- }
- if "" == all {
- all = allDept
- } else {
- if "" != allDept {
- all = all + "," + allDept
- }
- }
- }
- if let groups = O2BizContactPickerResult.groups {
- for group in groups {
- if allGroup == "" {
- allGroup = group.name ?? ""
- } else {
- allGroup = allGroup + "," + (group.name ?? "")
- }
- if let distinguishedName = group.distinguishedName {
- viewableGroupList.append(distinguishedName)
- }
- }
- if "" == all {
- all = allGroup
- } else {
- if "" != allGroup {
- all = all + "," + allGroup
- }
- }
- }
- self.calendarScopeField.text = all
- self.calendarInfo?.viewablePersonList = viewablePersonList
- self.calendarInfo?.viewableUnitList = viewableUnitList
- self.calendarInfo?.viewableGroupList = viewableGroupList
- })
- }
- //可新建范围
- @IBAction func selectNewScope(_ sender: Any) {
- var allUser = ""
- var allDept = ""
- var allGroup = ""
- var all = ""
- var publishablePersonList = [String]()
- var publishableGroupList = [String]()
- var publishableUnitList = [String]()
- let arrModes = [ContactPickerType.person, ContactPickerType.unit, ContactPickerType.group]
- showContactPicker(modes: arrModes, callback: {
- (O2BizContactPickerResult) in
- if let users = O2BizContactPickerResult.users {
- for user in users {
- if allUser == "" {
- allUser = user.name ?? ""
- } else {
- allUser = allUser + "," + (user.name ?? "")
- }
- if let distinguishedName = user.distinguishedName {
- publishablePersonList.append(distinguishedName)
- }
- }
- all = allUser
- }
- if let depts = O2BizContactPickerResult.departments {
- for dept in depts {
- if allDept == "" {
- allDept = dept.name ?? ""
- } else {
- allDept = allDept + "," + (dept.name ?? "")
- }
- if let distinguishedName = dept.distinguishedName {
- publishableUnitList.append(distinguishedName)
- }
- }
- if "" == all {
- all = allDept
- } else {
- if "" != allDept {
- all = all + "," + allDept
- }
- }
- }
- if let groups = O2BizContactPickerResult.groups {
- for group in groups {
- if allGroup == "" {
- allGroup = group.name ?? ""
- } else {
- allGroup = allGroup + "," + (group.name ?? "")
- }
- if let distinguishedName = group.distinguishedName {
- publishableGroupList.append(distinguishedName)
- }
- }
- if "" == all {
- all = allGroup
- } else {
- if "" != allGroup {
- all = all + "," + allGroup
- }
- }
- }
- self.calendarNewScopeField.text = all
- self.calendarInfo?.publishablePersonList = publishablePersonList
- self.calendarInfo?.publishableUnitList = publishableUnitList
- self.calendarInfo?.publishableGroupList = publishableGroupList
- })
- }
- private func loadCalendarInfoFromNet() {
- DDLogDebug("loadCalendarInfoFromNet............")
- viewModel.getCalendar(id: (calendarInfo?.id)!)
- .then { (calendar) in
- self.updateStuffValue(calendar: calendar)
- }.catch { (error) in
- DDLogError("查询日历信息出错,\(error.localizedDescription)")
- }
- }
- private func updateStuffValue(calendar: OOCalendarInfo) {
- DDLogDebug("updateStuffValue............\(calendar.color)")
- calendarNameField.text = calendar.name
- if calendar.type == "UNIT" {
- self.count = 4;
- } else {
- self.count = 0;
- }
- let section = NSIndexSet(index: 1)
- self.tableView.beginUpdates()
- self.tableView.reloadSections(section as IndexSet, with: .automatic)
- self.tableView.endUpdates()
- //所属组织
- calendarOrgField.text = calendar.target?.getChinaName()
- //管理员
- calendarManagerField.text = calendar.manageablePersonList?.getChinaName().joined(separator: ",")
- //可见范围
- var viewablePersonList = [String]()
- viewablePersonList += calendar.viewablePersonList ?? []
- viewablePersonList += calendar.viewableUnitList ?? []
- viewablePersonList += calendar.viewableGroupList ?? []
- calendarScopeField.text = viewablePersonList.getChinaName().joined(separator: ",")
- //可新建范围
- var publishableList = [String]()
- publishableList += calendar.publishablePersonList ?? []
- publishableList += calendar.publishableGroupList ?? []
- publishableList += calendar.publishableUnitList ?? []
- calendarNewScopeField.text = publishableList.getChinaName().joined(separator: ",")
- if calendar.type == "PERSON" {
- calendarTypeField.text = "个人日历"
- } else {
- calendarTypeField.text = "组织日历"
- }
- calendarStatusSwitch.setOn(calendar.status == "OPEN", animated: true)
- calendarIsOpenSwitch.setOn(calendar.isPublic == true, animated: true)
- //隐藏类型选择按钮
- calendarIsOpenBtn.isHidden = true
- if let color = calendar.color {
- if let index = colorOptions.index(where: { (colorItem) -> Bool in
- return colorItem == color
- }) {
- self.selectColorView(tag: index)
- }
- }
- calendarRemarkField.text = calendar.desc
- }
- private func closeWindow() {
- self.navigationController?.popViewController(animated: true)
- }
- // MARK: - Table view data source
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- //取消tableview选中状态
- tableView.deselectRow(at: indexPath, animated: false)
- // 隐藏输入法
- hideKeyboard()
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
- extension OOCalendarViewController: UITextFieldDelegate {
- func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
- if let Identifier = textField.accessibilityIdentifier {
- if Identifier == "calendarType" {
- return false
- } else {
- return true
- }
- } else {
- return true
- }
- }
- func textFieldShouldReturn(_ textField: UITextField) -> Bool {
- //return textField.resignFirstResponder()
- let res = textField.resignFirstResponder()
- return false
- }
- }
|