JobsNavigator.swift 4.08 KB
Newer Older
domenicw's avatar
domenicw committed
1
2
3
4
5
6
7
8
9
10
//
//  JobsNavigator.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 18.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit
domenicw's avatar
domenicw committed
11
import QuickLook
domenicw's avatar
domenicw committed
12
13
14
15
16

public class JobsNavigator: Navigator {
    
    // MARK: - Variables
    
17
18
    let manager = NetworkManager<AMIVApiJobs>()
    
domenicw's avatar
domenicw committed
19
20
    private var quickLookDataSource: QuickLookDataSource?
    
domenicw's avatar
domenicw committed
21
22
23
24
25
26
27
28
29
    public var rootViewController: UIViewController {
        return self.navigationController
    }
    
    private var navigationController: UINavigationController
    
    // MARK: - Initializers
    
    public init() {
domenicw's avatar
domenicw committed
30
        let jobs = JobsViewController(model: JobsViewModel.empty())
31
        self.navigationController = UINavigationController(rootViewController: jobs)
domenicw's avatar
domenicw committed
32
33
        self.navigationController.navigationBar.tintColor = .amivRed
        jobs.delegate = self
34
35
        
        manager.getJobOffers { (offers, error) in
domenicw's avatar
domenicw committed
36
37
            guard error == nil, let offers = offers else {
                #warning("handle error")
38
39
40
                return
            }
            
domenicw's avatar
domenicw committed
41
42
            let model = JobsViewModel(jobOffers: offers)
            jobs.model = model
43
        }
domenicw's avatar
domenicw committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    }
    
    // MARK: - Navigation
    
    private func goToJobsDetailView(model: GenericInfoViewControllerModel) {
        let info = GenericInfoViewController(model: model)
        info.delegate = self
        self.navigationController.pushViewController(info, animated: true)
    }
    
}

extension JobsNavigator: JobsViewControllerDelegate {
    
domenicw's avatar
domenicw committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    public func didSelectJob(_ viewController: JobsViewController, job: JobOffer) {
        self.manager.getImage(for: job.logo.filePath) { (data, error) in
            DispatchQueue.main.async {
                guard error == nil, let data = data else {
                    let model = GenericInfoViewControllerModel(jobOffer: job, image: nil)
                    self.goToJobsDetailView(model: model)
                    return
                }
                
                let image = UIImage(data: data)
                let model = GenericInfoViewControllerModel(jobOffer: job, image: image)
                self.goToJobsDetailView(model: model)
            }
        }
domenicw's avatar
domenicw committed
72
73
    }
    
domenicw's avatar
domenicw committed
74
    public func refreshData(_ viewController: JobsViewController) {
domenicw's avatar
domenicw committed
75
76
77
78
79
80
81
82
83
84
85
        manager.getJobOffers { (offers, error) in
            guard error == nil, let offers = offers else {
                DispatchQueue.main.async {
                    viewController.tableView.refreshControl?.endRefreshing()
                }
                return
            }
            
            let model = JobsViewModel(jobOffers: offers)
            viewController.model = model
        }
domenicw's avatar
domenicw committed
86
87
88
89
90
91
    }
    
}

extension JobsNavigator: GenericInfoViewControllerDelegate {
    
domenicw's avatar
domenicw committed
92
    public func buttonTapped(_ viewController: GenericInfoViewController, action: GenericInfoViewControllerAction) {
domenicw's avatar
domenicw committed
93
94
95
96
97
        debugPrint("Info View button tapped")
        
        // TODO: - Show job description pdf using QLPreviewController
        // https://www.hackingwithswift.com/example-code/libraries/how-to-preview-files-using-quick-look-and-qlpreviewcontroller
        
domenicw's avatar
domenicw committed
98
99
100
        guard case .openPDF(let path) = action else {
            return
        }
domenicw's avatar
domenicw committed
101
        
domenicw's avatar
domenicw committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        self.manager.getMedia(for: path) { (url, error) in
            guard error == nil, let url = url else {
                return
            }
            
            let quickLook = QLPreviewController()
            let dataSource = QuickLookDataSource(urls: [url])
            self.quickLookDataSource = dataSource
            quickLook.dataSource = dataSource
            DispatchQueue.main.async {
                self.navigationController.present(quickLook, animated: true, completion: nil)
            }
        }
        /*
domenicw's avatar
domenicw committed
116
117
118
119
120
121
        let url = Bundle.main.url(forResource: "Dienstverschiebungsgesuch", withExtension: "pdf")!
        let quickLook = QLPreviewController()
        let dataSource = QuickLookDataSource(urls: [url])
        self.quickLookDataSource = dataSource
        quickLook.dataSource = dataSource
        self.navigationController.present(quickLook, animated: true, completion: nil)
domenicw's avatar
domenicw committed
122
        */
domenicw's avatar
domenicw committed
123
124
125
    }
    
}