Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
пример кода - контроллер расписания CRM системы
//
// KCMasterScheduleViewController.swift
// Krasa CRM
//
// Created by Krasa on 01.08.2018.
// Copyright © 2018 Krasa. All rights reserved.
//
import UIKit
//MARK:- интерфейс
class KCMasterScheduleViewController: KCBaseViewController {
@IBOutlet weak var topCollectionView: UICollectionView!
@IBOutlet weak var bottomCollectionView: UICollectionView!
@IBOutlet weak var monthNameLabel: UILabel!
@IBOutlet weak var workTimeBoundsLabel: UILabel!
@IBAction func changeWorkTime() {
let hoursArray = KCWorkingHour.build(fromHour: KCSalonSettings.instance.extremumHours.start,
toHour: KCSalonSettings.instance.extremumHours.end)
KCWorkingHoursPickerView.show(inView: view,
dataSourceBegin: hoursArray.from,
dataSourceEnd: hoursArray.to,
selectionClosure: { [weak self] (fromIndex, toIndex) in
var from = hoursArray.from[fromIndex]
var to = hoursArray.to[toIndex]
from.isSelected = true
to.isSelected = true
KCSalonSettings.instance.extremumHours = ScheduleExtremumHours(start: from, end: to)
self?.setWorkingLabelTitle()
self?.setWorkingHours(start: from.fullServer,
end: to.fullServer)
self?.updateWatcher()
},
validationClosure: { (fromIndex, toIndex) in
return hoursArray.to[toIndex].hourNumber - hoursArray.from[fromIndex].hourNumber > 0
})
}
private var saveItem: UIBarButtonItem!
private var calendarAdapter: KCMasterScheduleCalendarAdapter!
private var scheduleAdapter: KCMasterScheduleCollectionViewAdapter!
private var appSchedule: KCSchedule!
var configurator: KCMasterScheduleViewControllerConfigurator!
override func viewDidLoad() {
super.viewDidLoad()
getWorkingHours()
saveItem = UIBarButtonItem(title: "сохранить",
style: .done,
target: self,
action: #selector(save))
saveItem.isEnabled = false
navigationItem.rightBarButtonItem = saveItem
title = "График"
if #available(iOS 11.0, *) {
navigationItem.largeTitleDisplayMode = .never
}
navigationController?.navigationBar.shadowImage = UIImage()
if configurator.presentationType == .modal {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "закрыть",
style: .plain,
target: self,
action: #selector(close))
}
}
override func onLayoutOnce() {
setAdapters()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { (context) in
var statusBarHeight: CGFloat = 0
var leftSafeArea: CGFloat = 0
var rightSafeArea: CGFloat = 0
if #available(iOS 11.0, *) {
statusBarHeight = self.view.safeAreaInsets.top
leftSafeArea = self.view.safeAreaInsets.left
rightSafeArea = self.view.safeAreaInsets.right
} else {
if size.width < size.height{
statusBarHeight = 20
}else{
statusBarHeight = 0
}
}
self.calendarAdapter.relayout(displayState: .week, newCollectionWidth: size.width - 58 - (leftSafeArea + rightSafeArea))
self.scheduleAdapter.relayout(displayState: .week,
newCollectionWidth: size.width - (leftSafeArea + rightSafeArea),
newCollectionHeight: size.height - 11 - self.workTimeBoundsLabel.frame.height - 11 - 60 - statusBarHeight)
}, completion: nil)
}
override var viewControllerTitle: String {
return "master_scheddfdfule_view_controller_mutherfucker_asshole"
}
}
//MARK:- настройка UI
extension KCMasterScheduleViewController {
private func setAdapters() {
showLoader()
KCScheduleFabrique().getMasterSchedule(utils: KCCalendarUtils(),
selectedMondayDate: configurator.mondayServer,
startTimeString: KCSalonSettings.instance.extremumHours.start.fullServer,
endTimeString: KCSalonSettings.instance.extremumHours.end.fullServer) { [unowned self] (schedule) in
self.appSchedule = schedule
self.setMonthName()
self.calendarAdapter = KCMasterScheduleCalendarAdapter(collectionView: self.topCollectionView,
schedule: schedule,
displayState: .week,
selectedDayClosure: { (day, absoluteDayIndex) in
self
.handleAllDaySelection(dayAbsoluteIndex:
absoluteDayIndex)
},
selectedWeekClosure: { (weekDescription, weekIndex) in
self.scrollSchedule(pageNumber: weekIndex)
self.getWeekData()
self.setMonthName()
})
self.scheduleAdapter = KCMasterScheduleCollectionViewAdapter(collectionView: self.bottomCollectionView,
schedule: schedule,
displayState: .week,
selectedDayClosure: { weekChangeDirection in
self.scrollCalendar(direction: weekChangeDirection)
self.getWeekData()
self.setMonthName()
},
selectedWeekClosure: { (weekChangeDirection) in
}, selectedEventClosure: {e in},
selectedDateClosure: {d in},
selected: { [unowned self] (week, itemNumber, sliceNumber) in
self.configurator.scheduleProcessor.selectItem(week: week,
hourNumber: itemNumber,
dayInWeekNumber: sliceNumber)
self.showOrHideSave()
}, deselected: { [unowned self] (week, itemNumber, sliceNumber) in
self.configurator.scheduleProcessor.deselectItem(week: week,
hourNumber: itemNumber,
dayInWeekNumber: sliceNumber)
self.showOrHideSave()
})
self.addCoachViews()
self.getWeekData()
}
}
private func addCoachViews() {
if let topCoachView = calendarAdapter.viewForGuide,
let bottomCoachViews = scheduleAdapter.viewForGuide {
showAppBoardingView(boardingItems: [KCAppBoardingItem(hintText: KCAppConfiguration.UIConst.ManpageTexts.MasterScheduleScreen.kScheduleDateText,
nextText: "ок",
viewToCoach: topCoachView),
KCAppBoardingItem(hintText: KCAppConfiguration.UIConst.ManpageTexts.MasterScheduleScreen.kScheduleHourText,
nextText: "ок",
viewToCoach: bottomCoachViews.hour),
KCAppBoardingItem(hintText: KCAppConfiguration.UIConst.ManpageTexts.MasterScheduleScreen.kScheduleHourElementText,
nextText: "ок",
viewToCoach: bottomCoachViews.element)])
}
}
private func setMonthName(){
appSchedule.getSelectedWeek { [weak self] (index, week) in
if week != nil {
self?.monthNameLabel.text = week!.monthName
}else{
self?.monthNameLabel.text = ""
}
}
}
private func scrollSchedule(pageNumber: Int){
scheduleAdapter.scrollToPage(page: pageNumber)
}
private func scrollCalendar(direction: WeekChangedDirection){
calendarAdapter.changeWeekPage(direction: direction)
}
private func setWorkingLabelTitle() {
workTimeBoundsLabel
.text
= "Рабочее время: \(KCSalonSettings.instance.extremumHours.start.titleString) - \(KCSalonSettings.instance.extremumHours.end.titleString)"
}
}
//MARK:- подгрузка данных
extension KCMasterScheduleViewController {
private func getWeekData() {
appSchedule.getSelectedWeek { (weekNumber, week) in
if week != nil {
if week!.state == .fresh {
(dataProvider as! KCMasterScheduleDataProvider).getSlices(week: week!,
success: { [weak self] dates in
DispatchQueue.main.async {
week?.updateRepresentation()
}
self?.configurator.scheduleProcessor.dates.append(contentsOf: dates)
self?.hideLoader()
},
errorResponse: { [weak self] kcError in
self?.handleKCError(error: kcError)
self?.hideLoader()
},
failure: { [weak self] code in
self?.handleInternetErrorCode(code: code)
self?.hideLoader()
})
}
}
}
}
private func setWorkingHours(start: String,
end: String) {
(dataProvider as! KCMasterScheduleDataProvider).setWorkingHours(start: start,
end: end,
success: { [weak self] (settings) in
DispatchQueue.main.async {
KCSalonSettings.instance = settings
self?.setWorkingLabelTitle()
}
},
errorResponse: { [weak self] (kcError) in
self?.handleKCError(error: kcError)
},
failure: { [weak self] (code) in
self?.handleInternetErrorCode(code: code)
})
}
private func getWorkingHours() {
(dataProvider as! KCMasterScheduleDataProvider).getWorkingHours(success: { [weak self] (settings) in
DispatchQueue.main.async {
KCSalonSettings.instance = settings
self?.setWorkingLabelTitle()
}
},
errorResponse: { [weak self] (kcError) in
self?.handleKCError(error: kcError)
},
failure: { [weak self] (code) in
self?.handleInternetErrorCode(code: code)
})
}
}
//MARK:- навигаця
extension KCMasterScheduleViewController {
private func showOrHideSave() {
saveItem.isEnabled = !configurator.scheduleProcessor.modifiedDatesIndexes.isEmpty
}
@objc func save() {
if !configurator.scheduleProcessor.modifiedDatesIndexes.isEmpty {
showLoader()
(dataProvider as! KCMasterScheduleDataProvider)
.setDates(dates: configurator.scheduleProcessor.modifiedDates(),
success: { [weak self] (success, message) in
self?.hideLoader()
DispatchQueue.main.async {
if success {
self?.showAlert(message: "Изменения успешно сохранены")
self?.updateWatcher()
}else {
self?.showAlert(message: message)
}
}
},
errorResponse: { [weak self] kcError in
self?.hideLoader()
self?.handleKCError(error: kcError)
},
failure: { [weak self] code in
self?.hideLoader()
self?.handleInternetErrorCode(code: code)
})
}
}
@objc func close() {
router.dismiss(viewController: self, animated: true, completion: nil)
}
}
//MARK:- выделение рядов расписания при нажатии на даты
extension KCMasterScheduleViewController {
private func handleAllDaySelection(dayAbsoluteIndex: Int) {
if let day = appSchedule.getDay(absoluteIndex: dayAbsoluteIndex) {
appSchedule.getSelectedWeek { [weak self] (weekIndex, week) in
if week != nil {
self?.configurator.scheduleProcessor.selectMultiple(week: week!, dayServer: day.serverRepresentation)
self?.bottomCollectionView.reloadData()
self?.showOrHideSave()
}
}
}
}
}
//MARK:- обновление данных по вотчеру
extension KCMasterScheduleViewController {
override func dataModelUpdated() {
setAdapters()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment