Skip to content

Instantly share code, notes, and snippets.

@nashysolutions
Last active November 14, 2020 05:01
Show Gist options
  • Select an option

  • Save nashysolutions/999ef0b7337f5d99b9ba7bf425be5b71 to your computer and use it in GitHub Desktop.

Select an option

Save nashysolutions/999ef0b7337f5d99b9ba7bf425be5b71 to your computer and use it in GitHub Desktop.
class MenuViewController: UIViewController {
let table = MenuTableHandler()
var categories: [MenuCategory] = []
var selections = Set<MenuItem>()
fileprivate var searchText: String? {
didSet {
if oldValue == searchText { return }
categories = MenuOption.allCases.map {
let category = $0.makeCategory(searchText: searchText)
category.dataSource = self
return category
}
table.update(with: categories, animate: false)
}
}
}
extension MenuViewController: MenuCategoryDataSource {
func menuItemIsSelected(_ item: MenuItem) -> Bool {
return selections.contains(item)
}
}
final class MenuItem: Hashable, Comparable {
unowned let category: MenuCategory
var isSelected: Bool {
return category.dataSource?.menuItemIsSelected(self) ?? false
}
}
protocol MenuCategoryDataSource: AnyObject {
func menuItemIsSelected(_ item: MenuItem) -> Bool
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment