Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NCViewerMediaDetailView.swift « NCViewerMedia « Viewer « iOSClient - github.com/nextcloud/ios.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5bccecde71b9caf3e389724475dbd08f8e90a725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
//  NCViewerMediaDetailView.swift
//  Nextcloud
//
//  Created by Marino Faggiana on 31/10/2020.
//  Copyright © 2020 Marino Faggiana. All rights reserved.
//
//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import UIKit
import MapKit
import NCCommunication

typealias NCImageMetadata = (latitude: Double, longitude: Double, location: String?, date: Date?, lensModel: String?)

public protocol NCViewerMediaDetailViewDelegate: AnyObject {
    func downloadFullResolution()
}

class NCViewerMediaDetailView: UIView {

    @IBOutlet weak var separator: UIView!
    @IBOutlet weak var sizeLabel: UILabel!
    @IBOutlet weak var sizeValue: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var dateValue: UILabel!
    @IBOutlet weak var dimLabel: UILabel!
    @IBOutlet weak var dimValue: UILabel!
    @IBOutlet weak var lensModelLabel: UILabel!
    @IBOutlet weak var lensModelValue: UILabel!
    @IBOutlet weak var messageButton: UIButton!
    @IBOutlet weak var mapContainer: UIView!
    @IBOutlet weak var locationButton: UIButton!

    var latitude: Double = 0
    var longitude: Double = 0
    var location: String?
    var date: Date?
    var lensModel: String?
    var metadata: tableMetadata?
    var mapView: MKMapView?
    var ncplayer: NCPlayer?
    weak var delegate: NCViewerMediaDetailViewDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()

        separator.backgroundColor = NCBrandColor.shared.separator
        sizeLabel.text = ""
        sizeValue.text = ""
        dateLabel.text = ""
        dateValue.text = ""
        dimLabel.text = ""
        dimValue.text = ""
        lensModelLabel.text = ""
        lensModelValue.text = ""
        messageButton.setTitle("", for: .normal)
        locationButton.setTitle("", for: .normal)
    }

    deinit {
        print("deinit NCViewerMediaDetailView")

        self.mapView?.removeFromSuperview()
        self.mapView = nil
    }

    func show(metadata: tableMetadata,
              image: UIImage?,
              textColor: UIColor?,
              mediaMetadata: NCImageMetadata,
              ncplayer: NCPlayer?,
              delegate: NCViewerMediaDetailViewDelegate?) {

        self.metadata = metadata
        self.latitude = mediaMetadata.latitude
        self.longitude = mediaMetadata.longitude
        self.location = mediaMetadata.location
        self.date = mediaMetadata.date
        self.lensModel = mediaMetadata.lensModel
        self.ncplayer = ncplayer
        self.delegate = delegate

        if mapView == nil && (latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0) {

            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

            let mapView = MKMapView()
            self.mapView = mapView
            self.mapContainer.addSubview(mapView)

            mapView.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                mapView.topAnchor.constraint(equalTo: self.mapContainer.topAnchor),
                mapView.bottomAnchor.constraint(equalTo: self.mapContainer.bottomAnchor),
                mapView.leadingAnchor.constraint(equalTo: self.mapContainer.leadingAnchor),
                mapView.trailingAnchor.constraint(equalTo: self.mapContainer.trailingAnchor)
            ])

            mapView.layer.cornerRadius = 6
            mapView.isZoomEnabled = true
            mapView.isScrollEnabled = false
            mapView.isUserInteractionEnabled = false
            mapView.addAnnotation(annotation)
            mapView.setRegion(MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 500, longitudinalMeters: 500), animated: false)
        }

        // Size
        sizeLabel.text = NSLocalizedString("_size_", comment: "")
        sizeValue.text = CCUtility.transformedSize(metadata.size)
        sizeValue.textColor = textColor

        // Date
        if let date = date {
            let formatter = DateFormatter()
            formatter.dateStyle = .full
            formatter.timeStyle = .medium
            let dateString = formatter.string(from: date as Date)

            dateLabel.text = NSLocalizedString("_date_", comment: "")
            dateValue.text = dateString
        } else {
            dateLabel.text = NSLocalizedString("_date_", comment: "")
            dateValue.text = NSLocalizedString("_not_available_", comment: "")
        }
        dateValue.textColor = textColor

        // Dimension / Duration
        if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
            if let image = image {
                dimLabel.text = NSLocalizedString("_resolution_", comment: "")
                dimValue.text = "\(Int(image.size.width)) x \(Int(image.size.height))"
            }
        } else if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
            if let durationTime = NCManageDatabase.shared.getVideoDurationTime(metadata: metadata) {
                self.dimLabel.text = NSLocalizedString("_duration_", comment: "")
                self.dimValue.text = NCUtility.shared.stringFromTime(durationTime)
            }
        }
        dimValue.textColor = textColor

        // Model
        if let lensModel = lensModel {
            lensModelLabel.text = NSLocalizedString("_model_", comment: "")
            lensModelValue.text = lensModel
            lensModelValue.textColor = textColor
        }

        // Message
        if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata) && metadata.session.isEmpty {
            messageButton.setTitle(NSLocalizedString("_try_download_full_resolution_", comment: ""), for: .normal)
            messageButton.isHidden = false
        } else {
            messageButton.setTitle("", for: .normal)
            messageButton.isHidden = true
        }

        // Location
        if let location = location {
            locationButton.setTitle(location, for: .normal)
            locationButton.isHidden = false
        } else {
            locationButton.setTitle("", for: .normal)
            locationButton.isHidden = true
        }

        self.isHidden = false
    }

    func hide() {
        self.isHidden = true
    }

    func isShow() -> Bool {
        return !self.isHidden
    }

    // MARK: - Action

    @IBAction func touchLocation(_ sender: Any) {

        guard latitude != -1, latitude != 0, longitude != -1, longitude != 0 else { return }

        let latitude: CLLocationDegrees = self.latitude
        let longitude: CLLocationDegrees = self.longitude

        let regionDistance: CLLocationDistance = 10000
        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
        let options = [
            MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
        ]
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = location
        mapItem.openInMaps(launchOptions: options)
    }

    @IBAction func touchFavorite(_ sender: Any) {

    }

    @IBAction func touchMessage(_ sender: Any) {
        delegate?.downloadFullResolution()
    }
}