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

Paywalls: override locale with paywall localization #1418

Conversation

NachoSoto
Copy link
Contributor

@NachoSoto NachoSoto commented Oct 30, 2023

This avoids a potential issue where the developer hasn't provided a particular localization, but the SDK could localize parts of the paywall:
image
image

@NachoSoto NachoSoto requested a review from a team October 30, 2023 20:52
@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch from d4184b6 to d833f0f Compare October 31, 2023 01:27
@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch 2 times, most recently from 31529cf to 20a5d55 Compare October 31, 2023 01:45
@NachoSoto NachoSoto changed the title [WIP] Paywalls: override locale Paywalls: override locale Oct 31, 2023
@NachoSoto NachoSoto changed the title Paywalls: override locale Paywalls: override locale with paywall localization Oct 31, 2023
@NachoSoto NachoSoto marked this pull request as ready for review October 31, 2023 01:48
@@ -234,7 +234,7 @@ private fun SelectPackageButton(

var columnModifier = modifier.clip(RoundedCornerShape(UIConstant.defaultCornerRadius))

val applicationContext = LocalContext.current.applicationContext.toAndroidContext()
val applicationContext = LocalContext.current.toAndroidContext()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any reason why we were using Context.applicationContext in several places?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah... With this change, we will be holding a reference to the current activity context instead of the application context, which may be leaked. Since the view model holds a reference to this context, if there is a configuration change, the VM will keep a reference to the old activity, causing a leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for explaining that! I refactored the implementation to remove the reference to Context. Would this approach work?

@@ -111,7 +116,11 @@ private fun LoadedPaywall(state: PaywallState.Loaded, viewModel: PaywallViewMode
.background(backgroundColor)
},
) {
TemplatePaywall(state = state, viewModel = viewModel)
CompositionLocalProvider(
LocalContext provides state.contextWithOverriddenLocale(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the key line.

Base automatically changed from paywalls to main October 31, 2023 17:37
@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch from 7742836 to 132e7a8 Compare October 31, 2023 17:41
@NachoSoto NachoSoto requested a review from tonidero October 31, 2023 21:49
Copy link

codecov bot commented Oct 31, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (16e69d3) 83.95% compared to head (a7e0085) 84.27%.
Report is 1 commits behind head on main.

❗ Current head a7e0085 differs from pull request most recent head 9b384b8. Consider uploading reports for the commit 9b384b8 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1418      +/-   ##
==========================================
+ Coverage   83.95%   84.27%   +0.31%     
==========================================
  Files         203      197       -6     
  Lines        6712     6626      -86     
  Branches      974      960      -14     
==========================================
- Hits         5635     5584      -51     
+ Misses        697      674      -23     
+ Partials      380      368      -12     

see 18 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@NachoSoto NachoSoto added the WIP label Nov 3, 2023
@NachoSoto
Copy link
Contributor Author

TODO: test if changing locale while paywall is displayed updates it.

@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch from a7e0085 to e4751b9 Compare November 8, 2023 23:12
@NachoSoto
Copy link
Contributor Author

@tonidero I just tested this:

  • Open paywall in English
  • Change language in Settings
  • Re-open the app

For some reason when I open it again the paywall is gone and I see the main screen of paywalls tester. When I open the paywall again, it's in Spanish as expected.

What do you think?

@tonidero
Copy link
Contributor

tonidero commented Nov 9, 2023

Yeah, seems like the dialog is not surviving configuration changes for some reason.. That doesn't happen when navigating to the paywall using navigation component.

In any case, I tested it with that, and the locale changes appropriately, so I think this should be good for now!

Copy link
Contributor

@tonidero tonidero left a comment

Choose a reason for hiding this comment

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

Just some rename suggestions, but seems to work fine! 👍

@@ -13,20 +14,31 @@ internal interface ApplicationContext {
fun getLocale(): Locale
}

internal class AndroidApplicationContext(private val applicationContext: Context) : ApplicationContext {
internal class AndroidApplicationContext(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not an application context anymore... How about renaming to ResourceProvider or something like that? We do have the application name as well, but I think that's ok.

}

override fun getLocale(): Locale {
return applicationContext.resources.configuration.locales.get(0)
return resources.configuration.locales.get(0)
}
}

internal fun Context.toAndroidContext(): ApplicationContext {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we rename the above, we will also need to rename a few things like this extension function.

@NachoSoto
Copy link
Contributor Author

@tonidero renamed it to ResourceProvider and PaywallResourceProvider.

@NachoSoto NachoSoto removed the WIP label Nov 9, 2023
@NachoSoto NachoSoto enabled auto-merge (squash) November 9, 2023 17:53
Copy link
Contributor

@tonidero tonidero left a comment

Choose a reason for hiding this comment

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

Looks like we need to update the screenshots? Other than that, looks good!

@NachoSoto
Copy link
Contributor Author

@tonidero thanks! I think they failed to generate, I believe this should fix it: #1455

@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch from 9b384b8 to 8d99e37 Compare November 9, 2023 21:34
@NachoSoto
Copy link
Contributor Author

@NachoSoto
Copy link
Contributor Author

Generated it manually: RevenueCat/purchases-android-snapshots#5

@tonidero
Copy link
Contributor

We're setting the git credentials in the purchases-android repo but not on the purchases-android-snapshots repo. Will try to fix that today.

@tonidero
Copy link
Contributor

I believe it should be fixed with: #1456

@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch 2 times, most recently from 77fcab9 to da34036 Compare November 10, 2023 17:48
@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch from da34036 to 9bd03d2 Compare November 10, 2023 18:08
@NachoSoto NachoSoto force-pushed the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch from 9bd03d2 to a959c01 Compare November 10, 2023 18:16
@NachoSoto NachoSoto merged commit 7c04213 into main Nov 10, 2023
@NachoSoto NachoSoto deleted the nacho/pwl-336-prevent-automatically-translating-strings-if-paywall-isnt branch November 10, 2023 18:41
tonidero added a commit that referenced this pull request Nov 14, 2023
### Description
With #1418, we are overriding the context in order to provide our own
locale to make sure the screen has the same locale in the whole screen.
However, with the current implementation, the context becomes a
`ContextImpl` which can't be converted to the original activity. This
was causing a crash when starting the purchase process

This PR fixes the issue by providing a new `LocalActivity` local
composition that is performed before the overriden context takes place
so we can access the activity in our composable tree.
NachoSoto pushed a commit that referenced this pull request Nov 14, 2023
**This is an automatic release.**

### RevenueCatUI
* Paywalls: fix template 5 header aspect ratio (#1465) via NachoSoto
(@NachoSoto)
* Paywalls: Fix template 1 header aspect ratio (#1466) via Toni Rico
(@tonidero)
* Paywalls: Support condensed footer in template 4 (#1469) via Toni Rico
(@tonidero)
* `Paywalls`: improve image loading (#1464) via NachoSoto (@NachoSoto)
* `Paywalls`: override locale with paywall localization (#1418) via
NachoSoto (@NachoSoto)
### Other Changes
* Paywalls: Fix purchasing regression by providing real activity (#1467)
via Toni Rico (@tonidero)
* Bump compile/target version to 34 (#1462) via Toni Rico (@tonidero)
* Update circleci orb to latest version (#1456) via Toni Rico
(@tonidero)
* `Snapshots`: fix Fastlane job (#1461) via NachoSoto (@NachoSoto)
* Update gradle plugin to version 8.1.1 (#1458) via Toni Rico
(@tonidero)

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants