This commit implements profile image cropping and optimization, as well as a major refactor on EditPictureControl. It now employs the following techniques: - Users can now crop their profile pictures to fit a square aspect ratio nicely and avoid issues with automatic resizing/cropping - Profile images are resized to a 400px by 400px image before sending it over the wire for better bandwidth usage - Profile pictures are now tagged as such to the media uploaders, to enable media optimization or special care on their end. Integrating the cropping step was very difficult with the previous structures, so `EditPictureControl` was heavily refactored to have improved state handling and better testability: 1. Enums with associated values are being used to capture all of the state in the picture selection process, as that helps ensure the needed info in each step is there and more clearly delianeate different steps — all at compile-time 2. The view was split into a view-model architecture, with almost all of the view logic ported to the new view-model class, making the view and the logic more clear to read as concerns are separated. This also enables better testabilty Several automated tests were added to cover EditPictureControl logic and looks. Closes: https://github.com/damus-io/damus/issues/2643 Changelog-Added: Profile image cropping tools Changelog-Changed: Improved profile image bandwidth optimization Changelog-Changed: Improved reliability of picture selector Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
83 lines
3.1 KiB
Swift
83 lines
3.1 KiB
Swift
//
|
|
// CameraController.swift
|
|
// damus
|
|
//
|
|
// Created by KernelKind on 2/15/24.
|
|
//
|
|
|
|
import UIKit
|
|
import SwiftUI
|
|
|
|
struct CameraController: UIViewControllerRepresentable {
|
|
|
|
@Environment(\.presentationMode)
|
|
@Binding private var presentationMode
|
|
|
|
let uploader: any MediaUploaderProtocol
|
|
var imagesOnly: Bool = false
|
|
var mode: Mode
|
|
|
|
enum Mode {
|
|
case save_to_library(when_done: () -> Void)
|
|
case handle_image(handler: (UIImage) -> Void)
|
|
}
|
|
|
|
final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
|
let parent: CameraController
|
|
|
|
init(_ parent: CameraController) {
|
|
self.parent = parent
|
|
}
|
|
|
|
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
|
|
switch parent.mode {
|
|
case .save_to_library(when_done: let done):
|
|
if !parent.imagesOnly, let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
|
|
// Handle the selected video
|
|
UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, nil, nil, nil)
|
|
} else if let cameraImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
|
|
let orientedImage = cameraImage.fixOrientation()
|
|
UIImageWriteToSavedPhotosAlbum(orientedImage, nil, nil, nil)
|
|
} else if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
|
|
let orientedImage = editedImage.fixOrientation()
|
|
UIImageWriteToSavedPhotosAlbum(orientedImage, nil, nil, nil)
|
|
}
|
|
done()
|
|
case .handle_image(handler: let handler):
|
|
if let cameraImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
|
|
let orientedImage = cameraImage.fixOrientation()
|
|
handler(orientedImage)
|
|
} else if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
|
|
let orientedImage = editedImage.fixOrientation()
|
|
handler(orientedImage)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
|
parent.presentationMode.dismiss()
|
|
}
|
|
}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
return Coordinator(self)
|
|
}
|
|
|
|
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraController>) -> UIImagePickerController {
|
|
let picker = UIImagePickerController()
|
|
picker.sourceType = .camera
|
|
picker.mediaTypes = ["public.image", "com.compuserve.gif"]
|
|
if uploader.supportsVideo && !imagesOnly {
|
|
picker.mediaTypes.append("public.movie")
|
|
}
|
|
picker.delegate = context.coordinator
|
|
return picker
|
|
}
|
|
|
|
func updateUIViewController(_ uiViewController: UIImagePickerController,
|
|
context: UIViewControllerRepresentableContext<CameraController>) {
|
|
|
|
}
|
|
}
|