-
Notifications
You must be signed in to change notification settings - Fork 53
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
Paywalls
: override locale with paywall localization
#1418
Conversation
d4184b6
to
d833f0f
Compare
ui/revenuecatui/src/main/kotlin/com/revenuecat/purchases/ui/revenuecatui/InternalPaywall.kt
Outdated
Show resolved
Hide resolved
31529cf
to
20a5d55
Compare
Paywalls
: override localePaywalls
: override locale with paywall localization
@@ -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() |
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.
Any reason why we were using Context.applicationContext
in several places?
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.
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.
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 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(), |
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.
This is the key line.
7742836
to
132e7a8
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
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 ☔ View full report in Codecov by Sentry. |
TODO: test if changing locale while paywall is displayed updates it. |
a7e0085
to
e4751b9
Compare
@tonidero I just tested this:
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? |
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! |
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.
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( |
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.
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 { |
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.
If we rename the above, we will also need to rename a few things like this extension function.
@tonidero renamed it to |
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.
Looks like we need to update the screenshots? Other than that, looks good!
9b384b8
to
8d99e37
Compare
@tonidero no idea why it still didn't work :/ https://app.circleci.com/pipelines/github/RevenueCat/purchases-android/8733/workflows/98ffd3bc-7365-4364-b64a-ebfe948f330f/jobs/31537 |
Generated it manually: RevenueCat/purchases-android-snapshots#5 |
We're setting the git credentials in the |
I believe it should be fixed with: #1456 |
77fcab9
to
da34036
Compare
da34036
to
9bd03d2
Compare
9bd03d2
to
a959c01
Compare
### 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.
**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>
This avoids a potential issue where the developer hasn't provided a particular localization, but the SDK could localize parts of the paywall:

