2 //  DownloadedListViewController.swift
 
   5 //  Created by Pawel Dabrowski on 12/09/2018.
 
   6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
 
  11 class DownloadedListViewController: MainViewController {
 
  13     @IBOutlet weak var tableView: UITableView!
 
  14     @IBOutlet weak var noDataLabel: UILabel!
 
  16     override func viewDidLoad() {
 
  19         title = "nav_downloaded".localized
 
  21         noDataLabel.text = "downloaded_empty_list".localized
 
  22         noDataLabel.isHidden = true
 
  26     override func viewWillAppear(_ animated: Bool) {
 
  27         super.viewWillAppear(animated)
 
  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
 
  38         tableView.contentInset = insets
 
  43         DatabaseManager.shared.refresh()
 
  44         tableView.reloadData()
 
  45         noDataLabel.isHidden = (DatabaseManager.shared.rlmApplication?.downloadedBooks.count ?? 0) > 0
 
  49 extension DownloadedListViewController: UITableViewDataSource{
 
  50     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
 
  51         return DatabaseManager.shared.rlmApplication?.downloadedBooks.count ?? 0
 
  54     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
 
  56         let cell = tableView.dequeueReusableCell(withIdentifier: "BookTableViewCell", for: indexPath) as! BookTableViewCell
 
  58         if let downloadedBooks = DatabaseManager.shared.rlmApplication?.downloadedBooks, downloadedBooks.count > indexPath.row {
 
  59             cell.setup(bookDetailsModel: downloadedBooks[indexPath.row], delegate: self, indexPath: indexPath)
 
  65     func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
 
  70 extension DownloadedListViewController: UITableViewDelegate{
 
  71     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 
  72         tableView.deselectRow(at: indexPath, animated: true)
 
  74         guard let dataSource = DatabaseManager.shared.rlmApplication?.downloadedBooks else {
 
  78         if dataSource.count > indexPath.row{
 
  79             navigationController?.pushViewController(BookDetailsViewController.instance(bookSlug: dataSource[indexPath.row].slug) , animated: true)
 
  84 extension DownloadedListViewController: BookTableViewCellDelegate{
 
  85     func bookTableViewCellDelegateDidTapTrashButton(bookSlug: String, indexPath: IndexPath?) {
 
  87         if let slug = PlayerController.shared.currentBookDetails?.slug, slug == bookSlug {
 
  88             PlayerController.shared.stopAndClear()
 
  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)