Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Пример моего кода из последнего проекта
import Foundation
class KCServicesProcessor {
var categories: [KCServicesCategory]
var selectedServices: [KCService]
var specializations: KCSpecializations
init(categories: [KCServicesCategory],
selectedServices: [KCService],
specializations: KCSpecializations) {
for category in categories {
category.contents.insert(KCService(), at: 0)
for contentItem in category.contents {
if contentItem.type == .group {
(contentItem as! KCServicesGroup).contents.insert(KCService(), at: 0)
}
}
}
self.specializations = specializations
self.categories = categories
self.selectedServices = selectedServices
}
convenience init() {
self.init(categories: [KCServicesCategory](),
selectedServices: [KCService](),
specializations: KCSpecializations(specializations: [KCSpecialization]()))
}
var title: String {
return selectedServices.count == 0 ? "Услуги" : "Услуги(\(selectedServices.count))"
}
var totalSelectedTime: Int {
return selectedServices.reduce(0) {$0+$1.duration.durationInMinutes}
}
func getEditableService(service: KCService)-> KCEditableService {
var editableCategories = [KCEditableServiceCategory]()
for category in categories {
editableCategories.append(KCEditableServiceCategory(id: category.id,
title: category.title,
isSelected: category.id == service.categoryID))
}
return KCEditableService(id: service.id,
title: service.title,
price: service.price.value,
isPriceFrom: service.price.isPriceFrom,
durationInMinutes: service.duration.durationInMinutes,
categories: editableCategories,
sex: service.sex,
serviceDescription: service.serviceDescription)
}
func update(service: KCService, withEditable editable: KCEditableService) {
service.update(editable: editable)
}
func selectService(service: KCService) {
if service.isSelected {
selectedServices = selectedServices.filter({$0.id != service.id})
}else {
selectedServices.append(service)
}
service.select()
}
func deleteService(service: KCService) {
if let category = categories.first(where: {$0.id==service.categoryID}) {
if service.groupID > 0 {
if let groupIndex = category.contents.index(where: {$0.type == .group && $0.entityID == service.groupID}) {
if let serviceIndex = category.contents[groupIndex].orderableElements.index(where: {$0.entityID == service.id}) {
category.contents[groupIndex].orderableElements.remove(at: serviceIndex)
}
}
}else {
if let serviceIndex = category.contents.index(where: {$0.type == .service && $0.entityID == service.id}) {
category.contents.remove(at: serviceIndex)
}
}
}
}
}
//MARK:- логика результатов поиска
extension KCServicesProcessor {
func assignSelected(searchResults: [KCService]) {
searchResults.forEach { (searchResult) in
searchResult.isSelected = self.selectedServices.first(where: {$0.id == searchResult.id})?.isSelected ?? false
}
}
func selectSearchResult(service: KCService) {
if service.isSelected {
if let selectedLocal = selectLocalService(service: service, select: false) {
selectedServices = selectedServices.filter({$0.id != selectedLocal.id})
service.isSelected = false
}
}else {
if let selectedLocal = selectLocalService(service: service, select: true) {
selectedServices.append(selectedLocal)
service.isSelected = true
}
}
}
private func selectLocalService(service: KCService, select: Bool)-> KCService? {
if let category = categories.first(where: {$0.id == service.categoryID}) {
if service.groupID != 0 {
if let group = category.contents.first(where: {$0.entityID == service.groupID && $0.type == .group}) as? KCServicesGroup {
if let localService = group.contents.first(where: {$0.entityID == service.id && $0.type == .service}) as? KCService {
localService.isSelected = select
return localService
}
}
}else {
if let localService = category.contents.first(where: {$0.entityID == service.id && $0.type == .service}) as? KCService {
localService.isSelected = select
return localService
}
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment