Browse Source

Add rudimentary support for audio attachments

Closes #7
master
Shadowfacts 1 month ago
parent
commit
d7953470e3
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5

+ 1
- 1
Tusker/Screens/Gallery/GalleryViewController.swift View File

@@ -52,7 +52,7 @@ class GalleryViewController: UIPageViewController, UIPageViewControllerDataSourc
52 52
             switch $0.kind {
53 53
             case .image:
54 54
                 return AttachmentViewController(attachment: $0)
55
-            case .video:
55
+            case .video, .audio:
56 56
                 let vc = AVPlayerViewController()
57 57
                 vc.player = AVPlayer(url: $0.url)
58 58
                 return vc

+ 37
- 14
Tusker/Views/Attachments/AttachmentView.swift View File

@@ -19,7 +19,7 @@ class AttachmentView: UIImageView, GIFAnimatable {
19 19
     
20 20
     weak var delegate: AttachmentViewDelegate?
21 21
 
22
-    var playImageView: UIImageView!
22
+    var playImageView: UIImageView?
23 23
     
24 24
     var attachment: Attachment!
25 25
     var index: Int!
@@ -54,25 +54,21 @@ class AttachmentView: UIImageView, GIFAnimatable {
54 54
         isUserInteractionEnabled = true
55 55
         addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed)))
56 56
         
57
-        playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))
58
-        playImageView.translatesAutoresizingMaskIntoConstraints = false
59
-        addSubview(playImageView)
60
-        NSLayoutConstraint.activate([
61
-            playImageView.widthAnchor.constraint(equalToConstant: 50),
62
-            playImageView.heightAnchor.constraint(equalToConstant: 50),
63
-            playImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
64
-            playImageView.centerYAnchor.constraint(equalTo: centerYAnchor)
65
-        ])
66 57
     }
67 58
     
68 59
     func loadAttachment() {
60
+        guard AttachmentsContainerView.supportedAttachmentTypes.contains(attachment.kind) else {
61
+            preconditionFailure("invalid attachment type")
62
+        }
69 63
         switch attachment.kind {
70 64
         case .image:
71 65
             loadImage()
72 66
         case .video:
73 67
             loadVideo()
68
+        case .audio:
69
+            loadAudio()
74 70
         default:
75
-            fatalError()
71
+            preconditionFailure("invalid attachment type")
76 72
         }
77 73
     }
78 74
     
@@ -89,8 +85,6 @@ class AttachmentView: UIImageView, GIFAnimatable {
89 85
                 }
90 86
             }
91 87
         }
92
-        
93
-        playImageView.isHidden = true
94 88
     }
95 89
     
96 90
     func loadVideo() {
@@ -106,7 +100,36 @@ class AttachmentView: UIImageView, GIFAnimatable {
106 100
             }
107 101
         }
108 102
         
109
-        playImageView.isHidden = false
103
+        let playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))
104
+        playImageView.translatesAutoresizingMaskIntoConstraints = false
105
+        addSubview(playImageView)
106
+        NSLayoutConstraint.activate([
107
+            playImageView.widthAnchor.constraint(equalToConstant: 50),
108
+            playImageView.heightAnchor.constraint(equalToConstant: 50),
109
+            playImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
110
+            playImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
111
+        ])
112
+    }
113
+    
114
+    func loadAudio() {
115
+        let label = UILabel()
116
+        label.text = "Audio Only"
117
+        let playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))
118
+        let stack = UIStackView(arrangedSubviews: [
119
+            label,
120
+            playImageView
121
+        ])
122
+        stack.translatesAutoresizingMaskIntoConstraints = false
123
+        stack.axis = .vertical
124
+        stack.spacing = 8
125
+        stack.alignment = .center
126
+        addSubview(stack)
127
+        NSLayoutConstraint.activate([
128
+            stack.centerXAnchor.constraint(equalTo: centerXAnchor),
129
+            stack.centerYAnchor.constraint(equalTo: centerYAnchor),
130
+            playImageView.widthAnchor.constraint(equalToConstant: 50),
131
+            playImageView.heightAnchor.constraint(equalToConstant: 50),
132
+        ])
110 133
     }
111 134
     
112 135
     override func display(_ layer: CALayer) {

+ 3
- 1
Tusker/Views/Attachments/AttachmentsContainerView.swift View File

@@ -11,6 +11,8 @@ import Pachyderm
11 11
 
12 12
 class AttachmentsContainerView: UIView {
13 13
 
14
+    static let supportedAttachmentTypes = [Attachment.Kind.image, .video, .audio]
15
+    
14 16
     weak var delegate: AttachmentViewDelegate?
15 17
     
16 18
     var statusID: String!
@@ -47,7 +49,7 @@ class AttachmentsContainerView: UIView {
47 49
     
48 50
     func updateUI(status: Status) {
49 51
         self.statusID = status.id
50
-        attachments = status.attachments.filter { $0.kind == .image || $0.kind == .video }
52
+        attachments = status.attachments.filter { AttachmentsContainerView.supportedAttachmentTypes.contains($0.kind) }
51 53
         
52 54
         attachmentViews.allObjects.forEach { $0.removeFromSuperview() }
53 55
         attachmentViews.removeAllObjects()

Loading…
Cancel
Save