Last active
November 8, 2017 07:35
-
-
Save alobanov/90c513a5c906937f5959084f1c02e6f7 to your computer and use it in GitHub Desktop.
Модель секции
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Сделаем нашей модели read only протокол, только с необходимыми полями | |
protocol SectionCompoundItemProtocol { | |
var header: String? {get} | |
var footer: String? {get} | |
} | |
class SectionCompoundItem: CompoundItemProtocol { | |
private let decoratedComposite: CompoundItemProtocol | |
// основные поля которые мы добавили с помощью декоратора | |
var header: String? | |
var footer: String? | |
var identifier: String { | |
return self.decoratedComposite.identifier | |
} | |
// из нашего декарируемого объекта будем рекурсивно | |
// запрашивать элементы, вернет только листья | |
var children: [CompoundItemProtocol] { | |
return self.decoratedComposite.children | |
} | |
var items: [CompoundItemProtocol] { | |
return self.decoratedComposite.children.flatMap {$0.items} | |
} | |
var level: CompoundItemLevel { | |
return self.decoratedComposite.level | |
} | |
init(identifier: String, header: String?, footer: String?) { | |
// Для упрощения мы не будем передавать декарируемый | |
// объект, а просто созданим его с нужным уровнем вложенности | |
self.decoratedComposite = BaseCompoundItem(identifier: identifier, level: .section) | |
self.header = header | |
self.footer = footer | |
} | |
// В секцию можно добавить все кроме секций и корневых элементов | |
func add(_ model: CompoundItemProtocol...) { | |
for item in model { | |
if item.level != .section && item.level != .root { | |
self.decoratedComposite.add(item) | |
} else { | |
print("You can`t add section in other section") | |
} | |
} | |
} | |
func remove(_ model: CompoundItemProtocol) { | |
self.decoratedComposite.remove(model) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment