This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Use camera of iPhone to take pictures from inside app (#64)
* Fix typos * Make DescriptionFeatureApp and SettingsFeatureApp compile again * Fix typos * On ReportView: Show Date Widget after Photos Widget * Fix typo in unit tests * Feature: Take photo from inside app using iPhone camera * Update WegliKit/Sources/CameraAccessClient/Live.swift Co-authored-by: Malte Bünz <[email protected]> * Fix unit tests * Use asynch instead of Combine in CameraAccessClient * Add unit tests fore camera feature * Add coordinates to PickerImageResult after taking a photo using the camera * Make camera button first option Co-authored-by: Yannick Vornehm <[email protected]> Co-authored-by: Malte Bünz <[email protected]>
- Loading branch information
1 parent
77dfac2
commit c206b6b
Showing
21 changed files
with
296 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ComposableArchitecture | ||
import Photos | ||
|
||
public typealias CameraAuthorizationStatus = AVAuthorizationStatus | ||
|
||
public struct CameraAccessClient { | ||
public init( | ||
requestAuthorization: @escaping () -> Effect<Bool, Never>, | ||
authorizationStatus: @escaping () -> CameraAuthorizationStatus | ||
) { | ||
self.requestAuthorization = requestAuthorization | ||
self.authorizationStatus = authorizationStatus | ||
} | ||
|
||
public var requestAuthorization: () -> Effect<Bool, Never> | ||
public var authorizationStatus: () -> CameraAuthorizationStatus | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ComposableArchitecture | ||
import Photos | ||
|
||
public extension CameraAccessClient { | ||
static func live() -> Self { | ||
Self( | ||
requestAuthorization: { | ||
.task { | ||
await AVCaptureDevice.requestAccess(for: .video) | ||
} | ||
}, | ||
authorizationStatus: { | ||
AVCaptureDevice.authorizationStatus(for: .video) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ComposableArchitecture | ||
import Photos | ||
|
||
public extension CameraAccessClient { | ||
static let mock = Self( | ||
requestAuthorization: { .none }, | ||
authorizationStatus: { .notDetermined } | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import PhotosUI | ||
import SwiftUI | ||
import SharedModels | ||
|
||
public struct CameraView: UIViewControllerRepresentable { | ||
|
||
@Binding var isPresented: Bool | ||
@Binding var pickerResult: [PickerImageResult?] | ||
|
||
public func makeUIViewController(context: UIViewControllerRepresentableContext<CameraView>) -> UIImagePickerController { | ||
let imagePicker = UIImagePickerController() | ||
imagePicker.allowsEditing = false | ||
imagePicker.sourceType = .camera | ||
imagePicker.delegate = context.coordinator | ||
|
||
return imagePicker | ||
} | ||
|
||
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<CameraView>) {} | ||
|
||
public func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
final public class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | ||
|
||
var parent: CameraView | ||
|
||
init(_ parent: CameraView) { | ||
self.parent = parent | ||
} | ||
|
||
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { | ||
defer { | ||
parent.isPresented = false | ||
} | ||
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage, | ||
let imageData = image.jpegData(compressionQuality: 1) else { | ||
debugPrint("originalImage info from ImagePickerController could not be casted to UIImage") | ||
return | ||
} | ||
|
||
let filename = "camera\(Date().description)" | ||
let url = FileManager.default.createDataTempFile(withData: imageData, withFileName: filename) | ||
|
||
var coordinate: CoordinateRegion.Coordinate? | ||
if let asset: PHAsset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset, | ||
let imageCoordinate = asset.location?.coordinate { | ||
coordinate = .init(imageCoordinate) | ||
} | ||
|
||
parent.pickerResult = [PickerImageResult( | ||
id: filename, | ||
imageUrl: url, | ||
coordinate: coordinate, | ||
creationDate: Date() | ||
)] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.