77//
88
99import AVKit
10+ import Combine
11+ import Foundation
1012import MediaCore
1113import MediaSwiftUI
1214import Photos
@@ -24,6 +26,10 @@ extension PHLivePhoto: Identifiable {
2426 public var id : PHLivePhoto { self }
2527}
2628
29+ struct Garbage {
30+ static var cancellables : [ AnyCancellable ] = [ ]
31+ }
32+
2733struct BrowserSection : View {
2834 @State private var isLivePhotoBrowserViewVisible = false
2935 @State private var isMediaBrowserViewVisible = false
@@ -62,9 +68,7 @@ struct BrowserSection: View {
6268 . fullScreenCover ( isPresented: $isMediaBrowserViewVisible, onDismiss: {
6369 isMediaBrowserViewVisible = false
6470 } ) {
65- Media . browser ( isPresented: $isMediaBrowserViewVisible, selectionLimit: 0 ) { result in
66- debugPrint ( result)
67- }
71+ Media . browser ( isPresented: $isMediaBrowserViewVisible, selectionLimit: 0 , handleMediaBrowserResult)
6872 }
6973
7074 Button ( action: {
@@ -146,4 +150,37 @@ private extension BrowserSection {
146150 default : ( )
147151 }
148152 }
153+
154+ func handleMediaBrowserResult( _ result: Result < [ BrowserResult < PHAsset , NSItemProvider > ] , Swift . Error > ) {
155+ switch result {
156+ case let . success( browserResult) :
157+ switch browserResult. first {
158+ case let . data( itemProvider) :
159+ if itemProvider. canLoadObject ( ofClass: PHLivePhoto . self) {
160+ itemProvider. loadLivePhoto ( )
161+ . receive ( on: DispatchQueue . main)
162+ . sink ( receiveCompletion: { _ in } ) { phLivePhoto in
163+ livePhoto = phLivePhoto
164+ }
165+ . store ( in: & Garbage. cancellables)
166+ } else if itemProvider. canLoadObject ( ofClass: UIImage . self) {
167+ itemProvider. loadImage ( )
168+ . receive ( on: DispatchQueue . main)
169+ . sink ( receiveCompletion: { _ in } ) { uiImage in
170+ image = uiImage
171+ }
172+ . store ( in: & Garbage. cancellables)
173+ } else if itemProvider. hasItemConformingToTypeIdentifier ( UTType . movie. identifier) {
174+ itemProvider. loadVideo ( )
175+ . receive ( on: DispatchQueue . main)
176+ . sink ( receiveCompletion: { _ in } ) { url in
177+ playerURL = url
178+ }
179+ . store ( in: & Garbage. cancellables)
180+ }
181+ default : ( )
182+ }
183+ default : ( )
184+ }
185+ }
149186}
0 commit comments