WordPress-iOS icon indicating copy to clipboard operation
WordPress-iOS copied to clipboard

Improve performance of Image and Gallery block processors to avoid long delay when saving a post

Open fluiddot opened this issue 1 year ago β€’ 9 comments
trafficstars

Fixes https://github.com/wordpress-mobile/gutenberg-mobile/issues/6696.

[!NOTE] Uses the changes from https://github.com/wordpress-mobile/WordPress-iOS/pull/22886.

To test

Delay when saving a post

  1. Create a post.
  2. Add a Gallery block and insert multiple images.
  3. Wait until all uploads finish.
  4. Close the editor and save the post.
  5. Observe the editor closes quickly with no significant delay.
  6. Re-open the post.
  7. Add more images.
  8. Repeat steps 3, 4.
  9. Observe the editor closes quickly with no significant delay.

Block processors

Follow the testing instructions to cover the functionality related to the processors for each block:

Regression Notes

  1. Potential unintended areas of impact The block processors are only used when saving a post. No other area should be impacted.

  2. What I did to test those areas of impact (or what existing automated tests I relied on) Manually tested saving a post with different blocks. Additionally, each processor has its own unit test that has been verified.

  3. What automated tests I added (or what prevented me from doing so) N/A

PR submission checklist:

  • [x] I have completed the Regression Notes.
  • [ ] I have considered adding unit tests for my changes.
  • [ ] I have considered adding accessibility improvements for my changes.
  • [x] I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing checklist:

  • [ ] WordPress.com sites and self-hosted Jetpack sites.
  • [ ] Portrait and landscape orientations.
  • [ ] Light and dark modes.
  • [ ] Fonts: Larger, smaller and bold text.
  • [ ] High contrast.
  • [ ] VoiceOver.
  • [ ] Languages with large words or with letters/accents not frequently used in English.
  • [ ] Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • [ ] iPhone and iPad.
  • [ ] Multi-tasking: Split view and Slide over. (iPad)

fluiddot avatar Mar 25 '24 15:03 fluiddot

WordPress AlphaπŸ“² You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr22896-7694cb2
Version24.9
Bundle IDorg.wordpress.alpha
Commit7694cb27bc4f662a08faa6745d40c2e67f9577a4
App Center BuildWPiOS - One-Offs #9957
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

wpmobilebot avatar Mar 25 '24 16:03 wpmobilebot

Jetpack AlphaπŸ“² You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr22896-7694cb2
Version24.9
Bundle IDcom.jetpack.alpha
Commit7694cb27bc4f662a08faa6745d40c2e67f9577a4
App Center Buildjetpack-installable-builds #9007
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

wpmobilebot avatar Mar 25 '24 16:03 wpmobilebot

I'm moving this to the next milestone since this is not a blocker, and the code freeze will be completed today.

irfano avatar Apr 01 '24 13:04 irfano

πŸ‘‹ Hey @fluiddot, I'm bumping this PR to 24.8 since it's code freeze day. If this PR needs to target 24.7, please target the release branch once it's been cut. Thanks!

momo-ozawa avatar Apr 15 '24 12:04 momo-ozawa

Hi @fluiddot πŸ‘‹ , I'm bumping this PR's milestone to 24.9 since I'm starting code freeze. Feel free to re-target this to the release branch if this is a blocker or intended for 24.8.

dvdchr avatar Apr 29 '24 08:04 dvdchr

Hey @fluiddot, I'm bumping the milestone for this PR to 25.0 as I'm starting the code freeze. If this PR needs to go into 24.9, please go ahead and re-target it to the release branch.

salimbraksa avatar May 13 '24 20:05 salimbraksa

πŸ‘‹ Hey @fluiddot, I'd like to pull in trunk on this PR as some time has passed. I believe this patch resolves the conflict on PostCoordinator.swift but I'd like for you to verify if possible. Thanks!

diff --cc WordPress/Classes/Services/PostCoordinator.swift
index 7098c95cae,2ea04cc472..0000000000
--- a/WordPress/Classes/Services/PostCoordinator.swift
+++ b/WordPress/Classes/Services/PostCoordinator.swift
@@@ -203,15 -741,7 +736,16 @@@ class PostCoordinator: NSObject 
          completion(.success(post))
      }
  
 +    func updateMediaBlocksBeforeSave(in post: AbstractPost, with media: Set<Media>) {
 +        guard let postContent = post.content else {
 +            return
 +        }
 +        let contentParser = GutenbergContentParser(for: postContent)
 +        media.forEach { self.updateReferences(to: $0, in: contentParser.blocks, post: post) }
 +        post.content = contentParser.html()
 +    }
 +
+     // - warning: deprecated (kahu-offline-mode)
      func cancelAnyPendingSaveOf(post: AbstractPost) {
          removeObserver(for: post)
      }
@@@ -347,12 -879,18 +883,19 @@@
              switch state {
              case .ended:
                  let successHandler = {
 -                    self.updateReferences(to: media, in: post)
 +                    self.updateMediaBlocksBeforeSave(in: post, with: [media])
 +
-                     // Let's check if media uploading is still going, if all finished with success then we can upload the post
-                     if !self.mediaCoordinator.isUploadingMedia(for: post) && !post.hasFailedMedia {
-                         self.removeObserver(for: post)
-                         completion(.success(post))
+                     if self.isSyncPublishingEnabled {
+                         if post.media.allSatisfy({ $0.remoteStatus == .sync }) {
+                             self.removeObserver(for: post)
+                             completion(.success(post))
+                         }
+                     } else {
+                         // Let's check if media uploading is still going, if all finished with success then we can upload the post
+                         if !self.mediaCoordinator.isUploadingMedia(for: post) && !post.hasFailedMedia {
+                             self.removeObserver(for: post)
+                             completion(.success(post))
+                         }
                      }
                  }
                  switch media.mediaType {

twstokes avatar May 14 '24 20:05 twstokes

πŸ‘‹ Hey @fluiddot, I'd like to pull in trunk on this PR as some time has passed. I believe this patch resolves the conflict on PostCoordinator.swift but I'd like for you to verify if possible. Thanks!

Yes @twstokes, your patch contains the same conflict resolution I have in mind. I've solved it in https://github.com/wordpress-mobile/WordPress-iOS/pull/22896/commits/ccffe5bdbb1b110f780ca5bcc44bf05d9117a758.

fluiddot avatar May 16 '24 16:05 fluiddot

It looks like a unit test may be failing because the output changed. I'm only spotting a whitespace difference.

@fluiddot do you think we're good to just update the expected output?

twstokes avatar May 16 '24 18:05 twstokes

It looks like a unit test may be failing because the output changed. I'm only spotting a whitespace difference.

@fluiddot do you think we're good to just update the expected output?

@twstokes Yep, the difference is the extra whitespace at the end of the figure tag. The whitespace is only necessary to conform the xhtml specs for self-closing tags, which is not the case here as we are processing HTML. However, as commented in the parser's code (SwiftSoup), seems the same formatting is used to for compatibility. Hence, this extra whitespace is expected in the output. I've pushed a fix in https://github.com/wordpress-mobile/WordPress-iOS/pull/22896/commits/3a22f64bf5dc303879de9d39c990d89bd9ef9320.

fluiddot avatar May 20 '24 09:05 fluiddot

LGTM @fluiddot! Feel free to merge - I only shared some Swift styling thoughts. πŸš€

Thanks @twstokes for reviewing and approving the PR πŸ™‡ ! I've applied the suggestions you shared and enabled auto-merge.

fluiddot avatar May 21 '24 15:05 fluiddot