|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import SwiftUI |
| 16 | +import Firebase |
| 17 | + |
| 18 | +/// ImagePickerRepresentable wraps a UIImagePickerController, so it is accessible through SwiftUI. |
| 19 | +struct ImagePickerRepresentable { |
| 20 | + enum Source { |
| 21 | + case camera |
| 22 | + case photoLibrary |
| 23 | + } |
| 24 | + |
| 25 | + /// Denotes whether the user is taking a photo or selecting one. |
| 26 | + var source: Source |
| 27 | + |
| 28 | + /// Persistent storage which retains the image. |
| 29 | + @ObservedObject var store: ImageStore |
| 30 | + |
| 31 | + /// Binds to whether the image picker is visible. |
| 32 | + @Binding var visible: Bool |
| 33 | + |
| 34 | + /// Completion handler that is invoked when the image picker dismisses. |
| 35 | + var completion: () -> Void |
| 36 | + |
| 37 | + /// Coordinator is an internal class that acts as a delegate for the image picker. |
| 38 | + class Coordinator: NSObject { |
| 39 | + private var representable: ImagePickerRepresentable |
| 40 | + private var store: ImageStore |
| 41 | + |
| 42 | + init(representable: ImagePickerRepresentable, store: ImageStore) { |
| 43 | + self.representable = representable |
| 44 | + self.store = store |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +extension ImagePickerRepresentable: UIViewControllerRepresentable { |
| 50 | + typealias UIViewControllerType = UIImagePickerController |
| 51 | + |
| 52 | + /// Invoked by the system to setup a coordinator that the UIImagePickerViewController can use. |
| 53 | + /// - Returns: The coordinator. |
| 54 | + func makeCoordinator() -> Coordinator { |
| 55 | + Coordinator(representable: self, store: self.store) |
| 56 | + } |
| 57 | + |
| 58 | + func makeUIViewController(context: Context) -> UIImagePickerController { |
| 59 | + let imagePicker = UIImagePickerController() |
| 60 | + |
| 61 | + switch self.source { |
| 62 | + case .camera: |
| 63 | + imagePicker.sourceType = .camera |
| 64 | + imagePicker.cameraCaptureMode = .photo |
| 65 | + case .photoLibrary: |
| 66 | + imagePicker.sourceType = .photoLibrary |
| 67 | + } |
| 68 | + |
| 69 | + imagePicker.delegate = context.coordinator |
| 70 | + return imagePicker |
| 71 | + } |
| 72 | + |
| 73 | + /// Required to implement, but unnecessary. We do not need to invalidate the SwiftUI canvas. |
| 74 | + func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { } |
| 75 | +} |
| 76 | + |
| 77 | +extension ImagePickerRepresentable.Coordinator: UIImagePickerControllerDelegate { |
| 78 | + func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { |
| 79 | + guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return } |
| 80 | + |
| 81 | + let imagePath = "\(Auth.auth().currentUser!.uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000)).jpg" |
| 82 | + self.store.uploadImage(image, atPath: imagePath) |
| 83 | + // TODO: Move UserDefaults and imagePath logic into store. |
| 84 | + UserDefaults.standard.setValue(imagePath, forKey: self.store.imagePathKey) |
| 85 | + |
| 86 | + self.store.image = image |
| 87 | + self.representable.visible = false |
| 88 | + picker.dismiss(animated: true, completion: self.representable.completion) |
| 89 | + } |
| 90 | + |
| 91 | + func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { |
| 92 | + self.representable.visible = false |
| 93 | + picker.dismiss(animated: true, completion: self.representable.completion) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// The coordinator must implement the UINavigationControllerDelegate protocol in order to be the UIImagePickerController's delegate. |
| 98 | +extension ImagePickerRepresentable.Coordinator: UINavigationControllerDelegate { } |
0 commit comments