added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / Library / LibraryViewController.swift
1 //
2 //  LibraryViewController.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 13/06/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11
12 class LibraryViewController: MainViewController {
13     enum LibraryCellState {
14         case not_loaded
15         case loading
16         case loaded
17     }
18     
19     @IBOutlet weak var tableView: UITableView!
20
21     var cellsArray = [WLTableViewCell]()
22     
23     let earlyAccessCell = LibraryEarlyAccessTableViewCell.instance()
24     let newestCell = LibraryCollectionTableViewCell.instance(libraryCollectionType: .newest)
25     let recommendedCell = LibraryCollectionTableViewCell.instance(libraryCollectionType: .recommended)
26     let readingNowCell = LibraryCollectionTableViewCell.instance(libraryCollectionType: .reading_now)
27
28     var earlyAccessCellState: LibraryCellState = .not_loaded
29     var newestCellState: LibraryCellState = .not_loaded
30     var recommendedCellState: LibraryCellState = .not_loaded
31     var readingCellState: LibraryCellState = .not_loaded
32     
33     
34     override func viewDidLoad() {
35         super.viewDidLoad()
36         title = "nav_library".localized
37         navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "navbar_search"), style: .plain, target: self, action: #selector(presentSearch))
38         
39         tableView.registerNib(name: "LibraryEarlyAccessTableViewCell")
40         tableView.registerNib(name: "LibraryCollectionTableViewCell")
41         tableView.registerNib(name: "BecomeFriendTableViewCell")
42         tableView.separatorStyle = .none
43         
44         if DatabaseManager.shared.isUserPremium() == false /* && Constants.donateEnabled*/{
45             cellsArray.append(BecomeFriendTableViewCell.instance(delegate: self))
46         }
47         
48         if Constants.donateEnabled {
49             earlyAccessCell.delegate = self
50             cellsArray.append(earlyAccessCell)
51         }
52         
53         if syncManager.isLoggedIn() {
54             readingNowCell.delegate = self
55             cellsArray.append(readingNowCell)
56         }
57         
58         newestCell.delegate = self
59         cellsArray.append(newestCell)
60
61         recommendedCell.delegate = self
62         cellsArray.append(recommendedCell)
63
64     }
65     
66     @objc func presentSearch() {
67         appDelegate.mainNavigator.presentSearch()
68     }
69     
70     func getDataFor(libraryCollectionType: LibraryCollectionType) {
71
72         var libraryCellState = LibraryCellState.not_loaded
73         switch libraryCollectionType {
74         case .reading_now:
75             libraryCellState = readingCellState
76         case .newest:
77             libraryCellState = newestCellState
78         case .recommended:
79             libraryCellState = recommendedCellState
80         }
81         
82         guard libraryCellState == .not_loaded else {
83             return
84         }
85         
86         switch libraryCollectionType {
87         case .reading_now:
88             readingCellState = .loading
89             readingNowCell.setup(state: .loading, dataSource: nil)
90         case .newest:
91             newestCellState = .loading
92             newestCell.setup(state: .loading, dataSource: nil)
93         case .recommended:
94             recommendedCellState = .loading
95             recommendedCell.setup(state: .loading, dataSource: nil)
96         }
97         
98         syncManager.getDataForLibrary(libraryCollectionType: libraryCollectionType) { [weak self] (result) in
99            
100             guard let strongSelf = self else { return }
101             
102             switch result {
103             case .success(let model):
104                 
105                 let array = model as! [BookModel]
106                 
107                 switch libraryCollectionType {
108                 case .reading_now:
109                     strongSelf.readingCellState = .loaded
110                     strongSelf.readingNowCell.setup(state: .hidden, dataSource: array)
111                 case .newest:
112                     strongSelf.newestCellState = .loaded
113                     strongSelf.newestCell.setup(state: .hidden, dataSource: array)
114                     strongSelf.tableView.reloadData()
115                     
116                 case .recommended:
117                     strongSelf.recommendedCellState = .loaded
118                     strongSelf.recommendedCell.setup(state: .hidden, dataSource: array)
119                 }
120                 
121             case .failure(let error):
122                 switch libraryCollectionType {
123                 case .reading_now:
124                     strongSelf.readingCellState = .not_loaded
125                     strongSelf.readingNowCell.setup(state: .button, dataSource: nil)
126                 case .newest:
127                     strongSelf.newestCellState = .not_loaded
128                     strongSelf.newestCell.setup(state: .button, dataSource: nil)
129                 case .recommended:
130                     strongSelf.recommendedCellState = .not_loaded
131                     strongSelf.recommendedCell.setup(state: .button, dataSource: nil)
132                 }
133             }
134         }
135     }
136     
137     func getPreview() {
138         guard earlyAccessCellState == .not_loaded else {
139             return
140         }
141         
142         if Constants.donateEnabled {
143             earlyAccessCellState = .loaded
144             earlyAccessCell.setup(state: .hidden, bookModel: nil)
145         }
146         
147         
148         
149         earlyAccessCellState = .loading
150         earlyAccessCell.setup(state: .loading, bookModel: nil)
151         
152         syncManager.getPreview { [weak self] (result) in
153             
154             guard let strongSelf = self else { return }
155             
156             switch result {
157             case .success(let model):
158                 let array = model as! [BookModel]
159                 strongSelf.earlyAccessCellState = .loaded
160                 strongSelf.earlyAccessCell.setup(state: .hidden, bookModel: array.count > 0 ? array[0] : nil)
161             case .failure(let error):
162                 strongSelf.earlyAccessCellState = .not_loaded
163                 strongSelf.earlyAccessCell.setup(state: .button, bookModel: nil)
164             }
165             strongSelf.tableView.reloadData()
166         }
167     }
168 }
169
170 extension LibraryViewController: UITableViewDataSource {
171     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
172         return cellsArray.count
173     }
174     
175     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
176         
177         return cellsArray[indexPath.row].getHeight()
178     }
179     
180     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
181         let cell = cellsArray[indexPath.row]
182         if cell == earlyAccessCell {
183             getPreview()
184         }
185         else if cell == newestCell {
186             getDataFor(libraryCollectionType: .newest)
187         }
188         else if cell == recommendedCell {
189             getDataFor(libraryCollectionType: .recommended)
190         }
191         else if cell == readingNowCell {
192             getDataFor(libraryCollectionType: .reading_now)
193         }
194     }
195     
196     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
197
198         return cellsArray[indexPath.row]
199     }
200 }
201
202 extension LibraryViewController: UITableViewDelegate {
203     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
204         if let cell = cellsArray[indexPath.row] as? LibraryEarlyAccessTableViewCell, earlyAccessCellState == .loaded {
205             if let book = cell.book {
206                 let controller = BookDetailsViewController.instance(bookSlug: book.slug, isBookPremium: true)
207                 navigationController?.pushViewController(controller, animated: true)
208             }
209             else {
210                 // no action
211             }
212         }
213     }
214 }
215
216 extension LibraryViewController: LibraryEarlyAccessTableViewCellDelegate {
217     func libraryEarlyAccessTableViewCellRefreshButtonTapped() {
218         getPreview()
219     }
220 }
221
222 extension LibraryViewController: LibraryCollectionTableViewCellDelegate {
223     func libraryCollectionTableViewCellDelegateRefreshButtonTapped(collectionViewType: LibraryCollectionType) {
224         getDataFor(libraryCollectionType: collectionViewType)
225     }
226     
227     func libraryCollectionTableViewCellDelegateShowAllButtonTapped(collectionViewType: LibraryCollectionType) {
228         
229         var bookListViewControllerType: ListViewControllerType?
230         var dataSource: [BookModel]?
231         switch collectionViewType {
232         case .newest:
233             bookListViewControllerType = .newest
234             dataSource = newestCell.dataSource
235         case .reading_now:
236             bookListViewControllerType = .reading_now
237             dataSource = readingNowCell.dataSource
238         case .recommended:
239             bookListViewControllerType = .recommended
240             dataSource = recommendedCell.dataSource
241         }
242     
243         if let controllerType = bookListViewControllerType {
244             let controller = BookListViewController.instance(listViewControllerType: controllerType, dataSource: dataSource)
245             controller.leftBarButtonItemShouldOpenMenu = false
246             navigationController?.pushViewController(controller, animated: true)
247         }
248     }
249     
250     func libraryCollectionTableViewCellDelegateDidSelect(bookModel: BookModel) {
251         let controller = BookDetailsViewController.instance(bookSlug: bookModel.slug)
252         navigationController?.pushViewController(controller, animated: true)
253     }
254 }
255
256 extension LibraryViewController: BecomeFriendTableViewCellDelegate {
257     func becomeFriendTableViewCellTapped() {
258         
259         appDelegate.mainNavigator.presentSupportUs()
260     }
261 }
262