Skip to content

Issue/13326 fix scan backup menu animation#13976

Merged
zwarm merged 14 commits intofeature/remove-empty-state-on-site-changefrom
issue/13326-fix-scan-backup-menu-animation
Feb 5, 2021
Merged

Issue/13326 fix scan backup menu animation#13976
zwarm merged 14 commits intofeature/remove-empty-state-on-site-changefrom
issue/13326-fix-scan-backup-menu-animation

Conversation

@malinajirka
Copy link
Copy Markdown
Contributor

Parent issue #13326

This PR fixes two issues

  1. List item added animation is played whenever you open MySite screen. This is solved by synchronously loading cached value (even when it's expired) of JetpackCapabilities. So unless the permissions changes or the user switches between sites the animation should not be played.

  1. The app would crash if fetchJetpackCapabilities was requested before the previous request finished. This is fixed in f74b76e. New approach adds support for parallel request for different sites. If a duplicate request is initiated for the same site, the previous request is force finished with an error.

To test:

  • clear app data
  1. Log in with an account with multiple jetpack sites (eg. the one from the doc)
  2. Select "pressable-jetpack-complete" site and notice "Scan" and "Backup" items are shown (with animation)
  3. Open Reader and go back to MySite
  4. Notice "Scan" and "Backup" items are shown (withOUT animation)
  5. Select pressable-jetpack-daily-scan site and notice "Scan" is shown (backup disappears with an animation)
  6. Kill the app
  7. Re-open the app and notice "Scan" is shown (no animation is played)

  • clear app data
  1. Log in with an account with multiple jetpack sites (eg. the one from the doc)
  2. Enable MySiteImprovements feature flag and repeat steps 2-7 from the above section

  1. Select pressable-jetpack-daily-backup
  2. Open Activity Log and notice filters are shown
  3. Select pressable-jetpack-daily-scan
  4. Open Activity Log and notice filters are NOT shown

PR submission checklist:

  • I have considered adding unit tests where possible.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@malinajirka malinajirka added this to the 16.7 milestone Feb 5, 2021
@malinajirka malinajirka requested a review from zwarm February 5, 2021 12:35
@malinajirka malinajirka self-assigned this Feb 5, 2021
@peril-wordpress-mobile
Copy link
Copy Markdown

peril-wordpress-mobile bot commented Feb 5, 2021

You can trigger optional UI/connected tests for these changes by visiting CircleCI here.

suspend fun getJetpackPurchasedProducts(remoteSiteId: Long): JetpackPurchasedProducts {
val capabilities = withContext(bgDispatcher) {
getOrFetchJetpackCapabilities(remoteSiteId)
suspend fun getJetpackPurchasedProducts(remoteSiteId: Long) = flow {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This method now uses flow (it's used only for the new ImprovedMySiteFragment). It always returns cached capabilities and than initiates fetch of the up-to-date capabilities if needed.

fetchJetpackCapabilities(remoteSiteId)
}
}
fun getCachedJetpackPurchasedProducts(remoteSiteId: Long): JetpackPurchasedProducts =
Copy link
Copy Markdown
Contributor Author

@malinajirka malinajirka Feb 5, 2021

Choose a reason for hiding this comment

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

getCachedJetpackPurchasedProducts and fetchJetpackPurchasedProducts are public since they are used directly from the legacy (currently used) My Site fragment. The legacy fragment doesn't use getJetpackPurchasedProducts (flow) -> it needs to synchronously load cached value since the state is refreshed from onResume().

}

private fun forceResumeDuplicateRequests(remoteSiteId: Long) {
continuation[remoteSiteId]?.let {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Instead of throwing an exception, we simply resume any duplicate requests with an exception.

it.resume(event)
}
if (continuation.isEmpty()) {
dispatcher.unregister(this@JetpackCapabilitiesUseCase)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need to make sure to unregister the instance only when no requests are running (previously, multiple parallel requests for different sites were not supported at all)

private val jetpackCapabilitiesUseCase: JetpackCapabilitiesUseCase
) : MySiteSource<JetpackCapabilities> {
override fun buildSource(siteId: Int) = flow {
emit(JetpackCapabilities(scanAvailable = false, backupAvailable = false))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't need to always set visibility to false before the request to fetch capabilities finished, since the use case now always returns the cached value first.

else -> {
viewModelScope.launch {
val purchasedProducts = jetpackCapabilitiesUseCase.getJetpackPurchasedProducts(site.siteId)
val purchasedProducts = jetpackCapabilitiesUseCase.getCachedJetpackPurchasedProducts(site.siteId)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ActivityLogViewModel will always use the cached value now.

mapToJetpackPurchasedProducts(getCachedJetpackCapabilities(remoteSiteId))

private suspend fun hasValidCache(remoteSiteId: Long): Boolean {
return withContext(bgDispatcher) {
Copy link
Copy Markdown
Contributor Author

@malinajirka malinajirka Feb 5, 2021

Choose a reason for hiding this comment

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

I've removed all context switches from this class, since it doesn't perform any long-running operation so it can run even on the Ui thread. I previously considered loading data from sharedPreferences for a long-running operation - but it's causing more troubles than it's worth it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with the decision made here.

suspend fun fetchJetpackPurchasedProducts(remoteSiteId: Long): JetpackPurchasedProducts =
mapToJetpackPurchasedProducts(fetchJetpackCapabilities(remoteSiteId))

private fun mapToJetpackPurchasedProducts(capabilities: List<JetpackCapability>) =
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This has not been changed - just extracted into a standalone method

backup = capabilities.find { BACKUP_CAPABILITIES.contains(it) } != null
)

fun hasValidCache(remoteSiteId: Long): Boolean {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This method was not changed at all - I'm not sure why github marked it as changed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Happens to me sometimes too .. I think it's AS .

updateCache(remoteSiteId, capabilities)
}
return@withContext capabilities
forceResumeDuplicateRequests(remoteSiteId)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've removed the "withContext" and replace throw exception with forceResumeDuplicateRequests(remoteSiteId), the rest is without changes.

@peril-wordpress-mobile
Copy link
Copy Markdown

peril-wordpress-mobile bot commented Feb 5, 2021

You can test the changes on this Pull Request by downloading the APK here.

@zwarm zwarm self-assigned this Feb 5, 2021
Copy link
Copy Markdown
Contributor

@zwarm zwarm left a comment

Choose a reason for hiding this comment

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

@malinajirka - Everything is working as expected. 👏 In addition to the tests you specified, I also attempted to recreate the crash and was unable to (switching site quickly - over & over again, back & forth through the tabs with site switching thrown in).

Thanks for the walkthrough, it was very helpful.

@zwarm zwarm merged commit 297ac3d into feature/remove-empty-state-on-site-change Feb 5, 2021
@zwarm zwarm deleted the issue/13326-fix-scan-backup-menu-animation branch February 5, 2021 14:08
@ashiagr ashiagr mentioned this pull request Feb 9, 2021
67 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants