added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / DownloadedList / DownloadedListViewController.swift
1 //
2 //  DownloadedListViewController.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 12/09/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11 class DownloadedListViewController: MainViewController {
12
13     @IBOutlet weak var tableView: UITableView!
14     @IBOutlet weak var noDataLabel: UILabel!
15     
16     override func viewDidLoad() {
17         super.viewDidLoad()
18         
19         title = "nav_downloaded".localized
20         
21         noDataLabel.text = "downloaded_empty_list".localized
22         noDataLabel.isHidden = true
23         setupTableView()
24     }
25     
26     override func viewWillAppear(_ animated: Bool) {
27         super.viewWillAppear(animated)
28         reloaData()
29     }
30     func setupTableView(){
31         tableView.registerNib(name: "BookTableViewCell")
32         tableView.separatorStyle = .none
33         tableView.delegate = self
34         tableView.dataSource = self
35         tableView.rowHeight = 137
36         var insets = tableView.contentInset
37         insets.top = 11
38         tableView.contentInset = insets
39
40     }
41     
42     func reloaData() {
43         DatabaseManager.shared.refresh()
44         tableView.reloadData()
45         noDataLabel.isHidden = (DatabaseManager.shared.rlmApplication?.downloadedBooks.count ?? 0) > 0
46     }
47 }
48
49 extension DownloadedListViewController: UITableViewDataSource{
50     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
51         return DatabaseManager.shared.rlmApplication?.downloadedBooks.count ?? 0
52     }
53     
54     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
55         
56         let cell = tableView.dequeueReusableCell(withIdentifier: "BookTableViewCell", for: indexPath) as! BookTableViewCell
57         
58         if let downloadedBooks = DatabaseManager.shared.rlmApplication?.downloadedBooks, downloadedBooks.count > indexPath.row {
59             cell.setup(bookDetailsModel: downloadedBooks[indexPath.row], delegate: self, indexPath: indexPath)
60         } 
61         
62         return cell
63     }
64     
65     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
66
67     }
68 }
69
70 extension DownloadedListViewController: UITableViewDelegate{
71     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
72         tableView.deselectRow(at: indexPath, animated: true)
73         
74         guard let dataSource = DatabaseManager.shared.rlmApplication?.downloadedBooks else {
75             return
76         }
77
78         if dataSource.count > indexPath.row{
79             navigationController?.pushViewController(BookDetailsViewController.instance(bookSlug: dataSource[indexPath.row].slug) , animated: true)
80         }
81     }
82 }
83
84 extension DownloadedListViewController: BookTableViewCellDelegate{
85     func bookTableViewCellDelegateDidTapTrashButton(bookSlug: String, indexPath: IndexPath?) {
86         
87         if let slug = PlayerController.shared.currentBookDetails?.slug, slug == bookSlug {
88             PlayerController.shared.stopAndClear()
89         }
90         if let index = DatabaseManager.shared.rlmApplication?.downloadedBooks.index(where: {$0.slug == bookSlug}), DatabaseManager.shared.removeBookFromDownloaded(bookSlug: bookSlug){
91             tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
92             presentToast(message: "book_deleted_message".localized)
93         }
94         else{
95             reloaData()
96         }        
97     }
98 }
99