Skip to content
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

ProductImageStatusStorage class for managing stored images statuses in User Defaults #15256

Open
wants to merge 5 commits into
base: feat/save-product-image-upload-statuses-in-user-defaults-codable-conformance-unit-tests
Choose a base branch
from

Conversation

pmusolino
Copy link
Member

Part 3 of the changes needed for product image upload in the background.

Before merging this PR, make sure to merge this one.

Description

This PR includes:

  • The introduction of a new class called ProductImageStatusStorage will facilitate the storage of product image upload statuses in User Defaults. This class utilizes the Combine framework to manage notifications and monitor actual data changes. Currently, this class is not yet implemented in the code but will be incorporated in future pull requests.
  • The introduction of a new feature flag called backgroundProductImageUpload. Every functionality for image upload in background will be hidden under this feature flag.
  • Added some computed properties to ProductImageStatus that will be useful to quickly check the statuses of product images.
  • Removal of an unused test file for FeatureFlag that I discovered accidentally. That test file was empty.

All these changes are part of the larger project for uploading product images while the app is in the background state.

In the next PR, expect the unit tests for ProductImageStatusStorage. Otherwise, the PR would have been too big.

Testing information

This code is still not used in the app, so just review the code for the moment.


  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on all devices (phone/tablet) and no regressions are added.

…in User Defaults. This file has been created in another branch, but given the size of the branch/PR, I'm splitting them, and I renamed the class from `ProductImagesUserDefaultsStatuses` to `ProductImageStatusStorage`.
@pmusolino pmusolino added the feature: add/edit products Related to adding or editing products. label Feb 26, 2025
@pmusolino pmusolino added this to the 21.9 milestone Feb 26, 2025
Copy link

coderabbitai bot commented Feb 26, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This pull request introduces a new feature flag for background product image uploads by adding a corresponding case in both the feature flag enum and the default feature flag service. It also enhances the product image status functionality by adding computed properties and implementing a new storage class that manages statuses reactively using Combine and User Defaults. Additionally, unused test file references for the FeatureFlag have been removed from the project configuration and tests.

Changes

File(s) Change Summary
Experiments/.../DefaultFeatureFlagService.swift
Experiments/.../FeatureFlag.swift
Added new case backgroundProductImageUpload to the FeatureFlag enum and implemented corresponding logic in the isFeatureFlagEnabled method to conditionally enable background product image uploads based on the build configuration.
Networking/.../project.pbxproj Added a new file reference for ProductImageStatusStorage.swift in the project’s Sources section and updated the group structure accordingly.
Networking/.../ProductImageInBackground/ProductImageStatus.swift
Networking/.../ProductImageStatusStorage.swift
Added several computed properties (isUploading, isUploadFailure, siteID, productOrVariationID, assetType, error, isLocalID) to the ProductImageStatus enum; introduced a new ProductImageStatusStorage class with methods for managing statuses and reactive publishers for updates.
WooCommerce/.../project.pbxproj
WooCommerce/.../FeatureFlagTests.swift
Removed all references and the file for FeatureFlagTests.swift from the project configuration and test suite.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant DefaultFeatureFlagService
    participant BuildConfig

    Client->>DefaultFeatureFlagService: isFeatureFlagEnabled(.backgroundProductImageUpload)
    DefaultFeatureFlagService->>BuildConfig: Check buildConfig (localDeveloper or alpha)
    BuildConfig-->>DefaultFeatureFlagService: Return boolean result
    DefaultFeatureFlagService-->>Client: Return true/false
Loading
sequenceDiagram
    participant User
    participant ProductImageStatusStorage
    participant UserDefaults
    participant Observer

    User->>ProductImageStatusStorage: addStatus(newStatus)
    ProductImageStatusStorage->>UserDefaults: saveStatuses(updatedStatuses)
    UserDefaults-->>ProductImageStatusStorage: Confirm save
    ProductImageStatusStorage->>Observer: statusesPublisher emits update
Loading

Suggested labels

type: task, category: ui tests

Suggested reviewers

  • tiagomar

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Feb 26, 2025

WooCommerce iOS📲 You can test the changes from this Pull Request in WooCommerce iOS by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS WooCommerce iOS
Build Numberpr15256-f73197a
Version21.8
Bundle IDcom.automattic.alpha.woocommerce
Commitf73197a
App Center BuildWooCommerce - Prototype Builds #13168
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@pmusolino
Copy link
Member Author

@coderabbitai review

Copy link

coderabbitai bot commented Feb 26, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
Networking/Networking/ProductImageInBackground/ProductImageStatusStorage.swift (6)

9-19: Potentially unused private static instance.
private static let internalInstance might be dead code if you never expose or use it elsewhere. Consider removing to keep the codebase clean.


23-28: Main-thread dispatch for status publication might block.
Publishing on DispatchQueue.main is convenient for UI updates, but be mindful of potential performance impact if large data sets are processed frequently.


49-61: Usage of UserDefaults.synchronize() is discouraged.
Apple generally recommends against calling synchronize() as it’s automatically managed. Consider removing to avoid potential performance overhead.


109-113: Consider consistency in clearing statuses.
You reset to [] but you call removeObject instead of encoding an empty array. Both are valid; consider uniform usage of saveStatuses([]).


133-142: Consider a logging approach instead of print for errors.
Printing can be overlooked in production environments. A logging framework or error-handling flow might be beneficial.


144-151: Timely decoding error messages, but same logging note as above.
Again, consider a centralized or structured logging method.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5dc5a42 and d348d7e.

📒 Files selected for processing (7)
  • Experiments/Experiments/DefaultFeatureFlagService.swift (1 hunks)
  • Experiments/Experiments/FeatureFlag.swift (1 hunks)
  • Networking/Networking.xcodeproj/project.pbxproj (4 hunks)
  • Networking/Networking/ProductImageInBackground/ProductImageStatus.swift (1 hunks)
  • Networking/Networking/ProductImageInBackground/ProductImageStatusStorage.swift (1 hunks)
  • WooCommerce/WooCommerce.xcodeproj/project.pbxproj (0 hunks)
  • WooCommerce/WooCommerceTests/System/FeatureFlagTests.swift (0 hunks)
💤 Files with no reviewable changes (2)
  • WooCommerce/WooCommerce.xcodeproj/project.pbxproj
  • WooCommerce/WooCommerceTests/System/FeatureFlagTests.swift
🔇 Additional comments (23)
Experiments/Experiments/FeatureFlag.swift (1)

199-202: Feature flag addition looks good

The new feature flag for background product image uploads is well-documented and follows the same pattern as other feature flags in the enum. The placement at the end of the enum is appropriate.

Experiments/Experiments/DefaultFeatureFlagService.swift (1)

94-95: Implementation is consistent with other development features

The implementation for the new feature flag follows the same pattern as other development features by enabling it only for local development and alpha builds, which is appropriate for a feature under development.

Networking/Networking.xcodeproj/project.pbxproj (1)

462-462: Project file updated correctly for the new ProductImageStatusStorage.swift

The project file has been properly updated to include the new file:

  • Added to PBXBuildFile section for compilation
  • Added to PBXFileReference section with correct file type
  • Placed in the appropriate ProductImageInBackground group
  • Included in the Sources build phase

The changes are consistent with the PR objectives to introduce a new storage class for managing product image upload statuses.

Also applies to: 1680-1680, 2715-2715, 5507-5507

Networking/Networking/ProductImageInBackground/ProductImageStatus.swift (7)

89-94: Straightforward computed property for checking upload status.
The code cleanly checks if the enum case is .uploading.


96-101: Clear computed property for checking upload failure status.
This mirrors the structure of isUploading and is equally well implemented.


103-110: Site ID extraction appears correct.
All enum cases are handled. This is straightforward and accurate.


112-119: Concise retrieval of productOrVariationID.
Clean pattern matching to retrieve the associated value from all relevant cases.


121-129: Conditional assetType property.
Returns ProductImageAssetType for .uploading and .uploadFailure or nil otherwise. This is logically sound.


131-137: Error property is well-scoped.
It correctly returns the error associated with .uploadFailure, simplifying checks in caller code.


138-143: isLocalID logic is clear.
It appropriately checks for an ID of 0, indicating a local product or variation.

Networking/Networking/ProductImageInBackground/ProductImageStatusStorage.swift (13)

1-3: Imports are straightforward.
No issues here.


4-8: Class documentation is clear and informative.
Helps future maintainers understand the rationale behind Combine usage and the data flow.


20-22: Encoder/decoder property definitions appear correct.
No issues, as explicit control over JSON encoding is often necessary.


29-47: Only first failure is emitted in errorsPublisher.
If multiple statuses experience upload failures simultaneously, this logic captures only the first. Confirm this is the intended behavior.


63-67: Add status method is succinct and functional.
Appends to the current list, then persists.


69-72: Remove status method is straightforward.
No apparent issues.


74-77: removeStatus(where:) provides a flexible filter removal.
This meets typical removal needs elegantly.


79-87: updateStatus method merges add and replace flows.
The approach is neat and aligns with typical “upsert” logic.


89-91: Simple findStatus method for retrieving the first match.
Looks good and is consistent with the rest of the API.


93-95: getAllStatuses method is minimal and clear.
No concerns here.


97-107: Filtering statuses by site and product ID.
Handles the cases for all statuses thoroughly.


115-131: setAllStatuses methods are intuitive.
They replace or merge statuses as expected.


155-184: Reactive user defaults observation.
The approach is valid. However, if writes happen on a background thread, concurrency needs careful handling. Confirm that the main-thread checks suffice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: add/edit products Related to adding or editing products.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants