2 // LibraryViewController.swift
5 // Created by Pawel Dabrowski on 13/06/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
12 class LibraryViewController: MainViewController {
13 enum LibraryCellState {
19 @IBOutlet weak var tableView: UITableView!
21 var cellsArray = [WLTableViewCell]()
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)
28 var earlyAccessCellState: LibraryCellState = .not_loaded
29 var newestCellState: LibraryCellState = .not_loaded
30 var recommendedCellState: LibraryCellState = .not_loaded
31 var readingCellState: LibraryCellState = .not_loaded
34 override func viewDidLoad() {
36 title = "nav_library".localized
37 navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "navbar_search"), style: .plain, target: self, action: #selector(presentSearch))
39 tableView.registerNib(name: "LibraryEarlyAccessTableViewCell")
40 tableView.registerNib(name: "LibraryCollectionTableViewCell")
41 tableView.registerNib(name: "BecomeFriendTableViewCell")
42 tableView.separatorStyle = .none
44 if DatabaseManager.shared.isUserPremium() == false /* && Constants.donateEnabled*/{
45 cellsArray.append(BecomeFriendTableViewCell.instance(delegate: self))
48 if Constants.donateEnabled {
49 earlyAccessCell.delegate = self
50 cellsArray.append(earlyAccessCell)
53 if syncManager.isLoggedIn() {
54 readingNowCell.delegate = self
55 cellsArray.append(readingNowCell)
58 newestCell.delegate = self
59 cellsArray.append(newestCell)
61 recommendedCell.delegate = self
62 cellsArray.append(recommendedCell)
66 @objc func presentSearch() {
67 appDelegate.mainNavigator.presentSearch()
70 func getDataFor(libraryCollectionType: LibraryCollectionType) {
72 var libraryCellState = LibraryCellState.not_loaded
73 switch libraryCollectionType {
75 libraryCellState = readingCellState
77 libraryCellState = newestCellState
79 libraryCellState = recommendedCellState
82 guard libraryCellState == .not_loaded else {
86 switch libraryCollectionType {
88 readingCellState = .loading
89 readingNowCell.setup(state: .loading, dataSource: nil)
91 newestCellState = .loading
92 newestCell.setup(state: .loading, dataSource: nil)
94 recommendedCellState = .loading
95 recommendedCell.setup(state: .loading, dataSource: nil)
98 syncManager.getDataForLibrary(libraryCollectionType: libraryCollectionType) { [weak self] (result) in
100 guard let strongSelf = self else { return }
103 case .success(let model):
105 let array = model as! [BookModel]
107 switch libraryCollectionType {
109 strongSelf.readingCellState = .loaded
110 strongSelf.readingNowCell.setup(state: .hidden, dataSource: array)
112 strongSelf.newestCellState = .loaded
113 strongSelf.newestCell.setup(state: .hidden, dataSource: array)
114 strongSelf.tableView.reloadData()
117 strongSelf.recommendedCellState = .loaded
118 strongSelf.recommendedCell.setup(state: .hidden, dataSource: array)
121 case .failure(let error):
122 switch libraryCollectionType {
124 strongSelf.readingCellState = .not_loaded
125 strongSelf.readingNowCell.setup(state: .button, dataSource: nil)
127 strongSelf.newestCellState = .not_loaded
128 strongSelf.newestCell.setup(state: .button, dataSource: nil)
130 strongSelf.recommendedCellState = .not_loaded
131 strongSelf.recommendedCell.setup(state: .button, dataSource: nil)
138 guard earlyAccessCellState == .not_loaded else {
142 if Constants.donateEnabled {
143 earlyAccessCellState = .loaded
144 earlyAccessCell.setup(state: .hidden, bookModel: nil)
149 earlyAccessCellState = .loading
150 earlyAccessCell.setup(state: .loading, bookModel: nil)
152 syncManager.getPreview { [weak self] (result) in
154 guard let strongSelf = self else { return }
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)
165 strongSelf.tableView.reloadData()
170 extension LibraryViewController: UITableViewDataSource {
171 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
172 return cellsArray.count
175 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
177 return cellsArray[indexPath.row].getHeight()
180 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
181 let cell = cellsArray[indexPath.row]
182 if cell == earlyAccessCell {
185 else if cell == newestCell {
186 getDataFor(libraryCollectionType: .newest)
188 else if cell == recommendedCell {
189 getDataFor(libraryCollectionType: .recommended)
191 else if cell == readingNowCell {
192 getDataFor(libraryCollectionType: .reading_now)
196 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
198 return cellsArray[indexPath.row]
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)
216 extension LibraryViewController: LibraryEarlyAccessTableViewCellDelegate {
217 func libraryEarlyAccessTableViewCellRefreshButtonTapped() {
222 extension LibraryViewController: LibraryCollectionTableViewCellDelegate {
223 func libraryCollectionTableViewCellDelegateRefreshButtonTapped(collectionViewType: LibraryCollectionType) {
224 getDataFor(libraryCollectionType: collectionViewType)
227 func libraryCollectionTableViewCellDelegateShowAllButtonTapped(collectionViewType: LibraryCollectionType) {
229 var bookListViewControllerType: ListViewControllerType?
230 var dataSource: [BookModel]?
231 switch collectionViewType {
233 bookListViewControllerType = .newest
234 dataSource = newestCell.dataSource
236 bookListViewControllerType = .reading_now
237 dataSource = readingNowCell.dataSource
239 bookListViewControllerType = .recommended
240 dataSource = recommendedCell.dataSource
243 if let controllerType = bookListViewControllerType {
244 let controller = BookListViewController.instance(listViewControllerType: controllerType, dataSource: dataSource)
245 controller.leftBarButtonItemShouldOpenMenu = false
246 navigationController?.pushViewController(controller, animated: true)
250 func libraryCollectionTableViewCellDelegateDidSelect(bookModel: BookModel) {
251 let controller = BookDetailsViewController.instance(bookSlug: bookModel.slug)
252 navigationController?.pushViewController(controller, animated: true)
256 extension LibraryViewController: BecomeFriendTableViewCellDelegate {
257 func becomeFriendTableViewCellTapped() {
259 appDelegate.mainNavigator.presentSupportUs()