-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Product Creation AI v2] Select package image #13193
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
842e465
Add coordinator for selecting package image.
selanthiraiyan 80f6b58
New source for package image selection.
selanthiraiyan 2be7e9b
Property to store package image.
selanthiraiyan 2bda164
Helper methods for package photo button.
selanthiraiyan 6f686f9
View for package image view.
selanthiraiyan d33b459
Show media picker source sheet.
selanthiraiyan 5093bac
Launch image selection coordinator.
selanthiraiyan 39ac968
Merge branch 'trunk' into feat/13104-select-image
selanthiraiyan 224fe8e
Merge branch 'trunk' into feat/13104-select-image
selanthiraiyan 63cdc81
Remove unnecessary self usage.
selanthiraiyan 0a1fecc
Use async image view to have a loading state while loading WP Media l…
selanthiraiyan de7df87
Use async callback to add loading state for image view.
selanthiraiyan d0b2c81
Merge branch 'trunk' into feat/13104-select-image
selanthiraiyan d20df05
Merge branch 'trunk' into feat/13104-select-image
selanthiraiyan aba6505
Update localization comments.
selanthiraiyan a995988
Update photo selection title.
selanthiraiyan a9c8a49
Merge branch 'trunk' into feat/13104-select-image
selanthiraiyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
146 changes: 146 additions & 0 deletions
146
.../Products/Add Product/AddProductWithAI/ImageSelection/SelectPackageImageCoordinator.swift
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,146 @@ | ||
import Photos | ||
import UIKit | ||
import Yosemite | ||
import WooFoundation | ||
|
||
/// Controls navigation for the flow to select a package photo | ||
/// | ||
final class SelectPackageImageCoordinator: Coordinator { | ||
let navigationController: UINavigationController | ||
private var mediaPickingCoordinator: MediaPickingCoordinator? | ||
private let siteID: Int64 | ||
private let mediaSource: MediaPickingSource | ||
private let productImageLoader: ProductUIImageLoader | ||
private let onImageSelected: (MediaPickerImage?) -> Void | ||
|
||
init(siteID: Int64, | ||
mediaSource: MediaPickingSource, | ||
sourceNavigationController: UINavigationController, | ||
productImageLoader: ProductUIImageLoader = DefaultProductUIImageLoader(phAssetImageLoaderProvider: { PHImageManager.default() }), | ||
onImageSelected: @escaping (MediaPickerImage?) -> Void) { | ||
self.siteID = siteID | ||
self.mediaSource = mediaSource | ||
self.navigationController = sourceNavigationController | ||
self.productImageLoader = productImageLoader | ||
self.onImageSelected = onImageSelected | ||
} | ||
|
||
func start() { | ||
let mediaPickingCoordinator = MediaPickingCoordinator(siteID: siteID, | ||
imagesOnly: true, | ||
allowsMultipleSelections: false, | ||
flow: .readTextFromProductPhoto, | ||
onCameraCaptureCompletion: { [weak self] asset, error in | ||
guard let self else { return } | ||
|
||
Task { @MainActor in | ||
let image = await self.onCameraCaptureCompletion(asset: asset, error: error) | ||
self.onImageSelected(image) | ||
} | ||
}, onDeviceMediaLibraryPickerCompletion: { [weak self] assets in | ||
guard let self else { return } | ||
|
||
Task { @MainActor in | ||
let image = await self.onDeviceMediaLibraryPickerCompletion(assets: assets) | ||
self.onImageSelected(image) | ||
} | ||
}, onWPMediaPickerCompletion: { [weak self] mediaItems in | ||
guard let self else { return } | ||
|
||
Task { @MainActor in | ||
let image = await self.onWPMediaPickerCompletion(mediaItems: mediaItems) | ||
self.onImageSelected(image) | ||
} | ||
}) | ||
self.mediaPickingCoordinator = mediaPickingCoordinator | ||
mediaPickingCoordinator.showMediaPicker(source: mediaSource, from: navigationController) | ||
} | ||
} | ||
|
||
// MARK: - Action handling for camera capture | ||
// | ||
private extension SelectPackageImageCoordinator { | ||
@MainActor | ||
func onCameraCaptureCompletion(asset: PHAsset?, error: Error?) async -> MediaPickerImage? { | ||
guard let asset else { | ||
displayErrorAlert(error: error) | ||
return nil | ||
} | ||
return await requestImage(from: asset) | ||
} | ||
} | ||
|
||
// MARK: Action handling for device media library picker | ||
// | ||
private extension SelectPackageImageCoordinator { | ||
@MainActor | ||
func onDeviceMediaLibraryPickerCompletion(assets: [PHAsset]) async -> MediaPickerImage? { | ||
guard let asset = assets.first else { | ||
return nil | ||
} | ||
return await requestImage(from: asset) | ||
} | ||
} | ||
|
||
// MARK: - Action handling for WordPress Media Library | ||
// | ||
private extension SelectPackageImageCoordinator { | ||
@MainActor | ||
func onWPMediaPickerCompletion(mediaItems: [Media]) async -> MediaPickerImage? { | ||
guard let media = mediaItems.first, | ||
let image = try? await productImageLoader.requestImage(productImage: media.toProductImage)else { | ||
return nil | ||
} | ||
return .init(image: image, source: .media(media: media)) | ||
} | ||
} | ||
|
||
// MARK: Error handling | ||
// | ||
private extension SelectPackageImageCoordinator { | ||
@MainActor | ||
func displayErrorAlert(error: Error?) { | ||
let alertController = UIAlertController(title: Localization.ImageErrorAlert.title, | ||
message: error?.localizedDescription, | ||
preferredStyle: .alert) | ||
let cancel = UIAlertAction(title: Localization.ImageErrorAlert.ok, | ||
style: .cancel, | ||
handler: nil) | ||
alertController.addAction(cancel) | ||
navigationController.present(alertController, animated: true) | ||
} | ||
} | ||
|
||
private extension SelectPackageImageCoordinator { | ||
@MainActor | ||
func requestImage(from asset: PHAsset) async -> MediaPickerImage? { | ||
await withCheckedContinuation { continuation in | ||
// PHImageManager.requestImageForAsset can be called more than once. | ||
var hasReceivedImage = false | ||
productImageLoader.requestImage(asset: asset, targetSize: PHImageManagerMaximumSize, skipsDegradedImage: true) { image in | ||
guard hasReceivedImage == false else { | ||
return | ||
} | ||
continuation.resume(returning: .init(image: image, source: .asset(asset: asset))) | ||
hasReceivedImage = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension SelectPackageImageCoordinator { | ||
enum Localization { | ||
enum ImageErrorAlert { | ||
static let title = NSLocalizedString( | ||
"selectPackageImageCoordinator.imageErrorAlert.title", | ||
value: "Unable to select image", | ||
comment: "Title of the alert when there is an error selecting image" | ||
) | ||
static let ok = NSLocalizedString( | ||
"selectPackageImageCoordinator.imageErrorAlert.ok", | ||
value: "OK", | ||
comment: "Dismiss button on the alert when there is an error selecting image" | ||
) | ||
} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓Could you explain why need to use async here? It seems to me that we're mixing the logic of presenting the selection flow with the selected image.
Would it be possible to separate the logic like what we did previously with the old flow? i.e. We keep the presentation of the image picker flow synchronous; and when an image is selected, we trigger a method in
startingInfoViewModel
with the selected image. Would that work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using
async
because we will send the image view into a loading state and wait for the image to be received from this method. Code hereI actually followed the pattern from
AddProductFromImageCoordinator
and made this methodasync
. We use the same pattern inBlazeEditAdHostingController
as well. Please correct me if I am misunderstanding things.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer a more naive implementation for separation of concerns. I'm also worried about the use of
async
because it could potentially come with complications when migrating to Swift 6. Please use your own judgement to decide whether to rewrite this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the input, Huong!
After considering this, I prefer to keep the implementation as is.
My thoughts are,
SelectPackageImageCoordinator
takes care of selecting and downloading the image.SelectPackageImageCoordinator
also encapsulates the different paths of camera capture, device media library and WP Media library image selection. After initial selection, all those paths require async operations to prepare theMediaPickerImage
. i.e. We present the selection flow and wait to get back aMediaPickerImage
.await
inProductCreationAIStartingInfoViewModel
makes it easy to maintain the previous image state and display a loading indicator until the image is ready.SelectPackageImageCoordinator
along withBlazeEditAdHostingController
(We will removeAddProductFromImageCoordinator
while removing package flow code)Please let me know if you still think we need to change this. I will add a subtask and work on this at the end of the project. 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with keeping this as-is for now, thanks for the explanation!