2 // SearchViewFiltersManager.swift
5 // Created by Pawel Dabrowski on 16/06/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
11 protocol SearchFiltersManagerDelegate: class {
12 func searchFiltersManagerFiltersChanged(reloadData: Bool)
15 class SearchFiltersManager: NSObject{
17 init(delegate: SearchFiltersManagerDelegate) {
18 self.delegate = delegate
21 weak var delegate: SearchFiltersManagerDelegate?
31 lazy var onlyLecturesCategory : CategoryModel = {
32 var mdl = CategoryModel()
33 mdl.name = "only_lecture".localized
34 mdl.slug = "only_lectures_slug"
39 lazy var hasAudiobookCategory : CategoryModel = {
40 var mdl = CategoryModel()
41 mdl.name = "has_audiobook".localized
42 mdl.slug = "has_audiobook_slug"
47 var selectedKindsArray = [CategoryModel]()
48 var selectedEpochsArray = [CategoryModel]()
49 var selectedGenresArray = [CategoryModel]()
51 func numberOfFilters() -> Int{
52 return (onlyLecturesCategory.checked ? 1 : 0) + (hasAudiobookCategory.checked ? 1 : 0) + selectedKindsArray.count + selectedGenresArray.count + selectedEpochsArray.count
55 func removeFilter(atIndex: Int) {
57 if let obj = getFilterTypeAndIndex(forIndex: atIndex){
59 print("removeFilter getFilterType \(obj.0) AndIndex \(obj.1)")
62 onlyLecturesCategory.checked = false
64 hasAudiobookCategory.checked = false
66 print("remove selectedKindsArray.count: \(selectedKindsArray.count), index: \(obj.1)")
67 selectedKindsArray.remove(at: obj.1)
69 print("remove selectedEpochsArray.count: \(selectedEpochsArray.count), index: \(obj.1)")
70 selectedEpochsArray.remove(at: obj.1)
72 print("remove selectedGenresArray.count: \(selectedGenresArray.count), index: \(obj.1)")
73 selectedGenresArray.remove(at: obj.1)
78 func getFilterArrayAndIndex(forIndex: Int) -> ([CategoryModel], Int)? {
79 if let obj = getFilterTypeAndIndex(forIndex: forIndex){
80 print("FilterArrayAndIndex getFilterType \(obj.0) AndIndex \(obj.1)")
84 return ([onlyLecturesCategory], 0)
86 return ([hasAudiobookCategory], 0)
88 print("selectedKindsArray.count: \(selectedKindsArray.count), index: \(obj.1)")
89 return (selectedKindsArray, obj.1)
91 print("selectedEpochsArray.count: \(selectedEpochsArray.count), index: \(obj.1)")
92 return (selectedEpochsArray, obj.1)
94 print("selectedGenresArray.count: \(selectedGenresArray.count), index: \(obj.1)")
95 return (selectedGenresArray, obj.1)
101 func getFilterTypeAndIndex(forIndex: Int) -> (FilterType, Int)? {
104 if onlyLecturesCategory.checked{
106 return (.onlyLectures, 0)
113 if hasAudiobookCategory.checked {
114 if forIndex == 0 || (forIndex == 1 && onlyLecturesCategory.checked){
115 return (.hasAudiobook, 0)
122 if index < selectedEpochsArray.count{
123 return (.epochs, index)
125 else if index < (selectedEpochsArray.count + selectedKindsArray.count){
126 return (.kinds, index - selectedEpochsArray.count)
128 else if index < (selectedEpochsArray.count + selectedKindsArray.count + selectedGenresArray.count){
129 return (.genres, index - selectedEpochsArray.count - selectedKindsArray.count )
135 func getFilter(forIndex: Int) -> CategoryModel? {
137 if let obj = getFilterArrayAndIndex(forIndex: forIndex){
143 func getFilterForApi(filterType: FilterType) -> String? {
145 var value: String = ""
148 return onlyLecturesCategory.slug
150 return hasAudiobookCategory.slug
152 value = selectedEpochsArray.map({$0.slug}).joined(separator: ",")
154 value = selectedGenresArray.map({$0.slug}).joined(separator: ",")
156 value = selectedKindsArray.map({$0.slug}).joined(separator: ",")
159 return value.count > 0 ? value : nil
162 func getParametersForApi() -> FilterBooksParameters {
163 let params = FilterBooksParameters()
164 params.epochs = getFilterForApi(filterType: .epochs)
165 params.genres = getFilterForApi(filterType: .genres)
166 params.kinds = getFilterForApi(filterType: .kinds)
167 params.onlyLectures = onlyLecturesCategory.checked
168 params.hasAudiobook = hasAudiobookCategory.checked
173 func clearFilters() {
174 selectedKindsArray = [CategoryModel]()
175 selectedEpochsArray = [CategoryModel]()
176 selectedGenresArray = [CategoryModel]()
177 onlyLecturesCategory.checked = false
178 hasAudiobookCategory.checked = false
179 delegate?.searchFiltersManagerFiltersChanged(reloadData: true)
183 extension SearchFiltersManager: UICollectionViewDataSource{
185 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
186 let numerOfFilters = numberOfFilters()
187 print("numerOfFilters += \(numerOfFilters)")
188 return numerOfFilters
191 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
193 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchFilterCollectionViewCell", for: indexPath) as! SearchFilterCollectionViewCell
194 print("beforeGetFilter")
195 if let filter = getFilter(forIndex: indexPath.row){
196 cell.setup(categoryModel: filter)
198 print("afterGetFilter")
203 extension SearchFiltersManager: UICollectionViewDelegate{
205 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
206 print("didSelectItems.row += \(indexPath.row)")
208 removeFilter(atIndex: indexPath.row)
209 collectionView.deleteItems(at: [indexPath])
211 delegate?.searchFiltersManagerFiltersChanged(reloadData: numberOfFilters() == 0)
215 extension SearchFiltersManager: UICollectionViewDelegateFlowLayout {
216 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
217 var size = CGSize(width: 200, height: 44)
218 if let filter = getFilter(forIndex: indexPath.row){
220 size.width = filter.name.sizeOf(UIFont.systemFont(ofSize: 10, weight: .medium)).width + 14 + 35