Skip to content

Commit d7fc67d

Browse files
committed
conditional resource caching and code cleanup
1 parent 8551365 commit d7fc67d

File tree

4 files changed

+176
-77
lines changed

4 files changed

+176
-77
lines changed

core/src/androidMain/kotlin/io/tolgee/TolgeeAndroid.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ data class TolgeeAndroid internal constructor(
105105
} ?: context.getString(id)
106106
}
107107

108+
fun t(resources: Resources, @StringRes id: Int): String {
109+
return getKeyFromResources(resources, id)?.let { key ->
110+
t(key)
111+
} ?: resources.getString(id)
112+
}
113+
108114
/**
109115
* Provides an immediate translation for a given string resource ID with optional format arguments.
110116
* If the translation key is found in the string resource, it retrieves the translation; otherwise,
@@ -121,6 +127,12 @@ data class TolgeeAndroid internal constructor(
121127
} ?: context.getString(id, *formatArgs)
122128
}
123129

130+
fun t(resources: Resources, @StringRes id: Int, vararg formatArgs: Any): String {
131+
return getKeyFromResources(resources, id)?.let { key ->
132+
t(key, TolgeeMessageParams.Indexed(*formatArgs))
133+
} ?: resources.getString(id, *formatArgs)
134+
}
135+
124136
fun tPlural(resources: Resources, @PluralsRes id: Int, quantity: Int): String {
125137
return getKeyFromResources(resources, id)?.let { key ->
126138
t(key, TolgeeMessageParams.Indexed(quantity))
@@ -159,6 +171,20 @@ data class TolgeeAndroid internal constructor(
159171
} ?: context.getText(id)
160172
}
161173

174+
fun tStyled(resources: Resources, @StringRes id: Int): CharSequence {
175+
return getKeyFromResources(resources, id)?.let { key ->
176+
t(key)
177+
} ?: resources.getText(id)
178+
}
179+
180+
fun tStyled(resources: Resources, @StringRes id: Int, def: CharSequence?): CharSequence? {
181+
return id.takeUnless { it == 0 }?.let {
182+
getKeyFromResources(resources, it)?.let { key ->
183+
t(key)
184+
}
185+
} ?: resources.getText(id, def)
186+
}
187+
162188
fun tPluralStyled(resources: Resources, @PluralsRes id: Int, quantity: Int): CharSequence {
163189
return getKeyFromResources(resources, id)?.let { key ->
164190
t(key, TolgeeMessageParams.Indexed(quantity))

core/src/androidMain/kotlin/io/tolgee/TolgeeContextWrapper.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ class TolgeeContextWrapper(
1919
val tolgee: Tolgee
2020
) : ContextWrapper(base) {
2121

22+
private var baseRes by atomic<Resources?>(null)
23+
private var res by atomic<Resources?>(baseRes)
24+
2225
override fun getResources(): Resources? {
23-
return TolgeeResources(base, tolgee)
26+
val base = super.getResources() ?: return res
27+
28+
if (res == null || baseRes != base) {
29+
res = TolgeeResources(base, tolgee)
30+
baseRes = base
31+
}
32+
33+
return res ?: base
2434
}
2535

2636
companion object {
Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.tolgee
22

33
import android.annotation.SuppressLint
4-
import android.content.Context
54
import android.content.res.AssetFileDescriptor
65
import android.content.res.ColorStateList
76
import android.content.res.Configuration
@@ -52,9 +51,9 @@ import java.io.InputStream
5251
*/
5352
@Suppress("DEPRECATION")
5453
internal class TolgeeResources(
55-
val base: Context,
54+
val base: Resources,
5655
val tolgee: Tolgee
57-
) : Resources(base.resources.assets, base.resources.displayMetrics, base.resources.configuration) {
56+
) : Resources(base.assets, base.displayMetrics, base.configuration) {
5857

5958
/**
6059
* Following methods are intercepted by Tolgee.
@@ -69,27 +68,31 @@ internal class TolgeeResources(
6968
}
7069

7170
override fun getQuantityString(@PluralsRes id: Int, quantity: Int): String {
72-
return base.resources.getQuantityStringT(tolgee, id, quantity)
71+
return base.getQuantityStringT(tolgee, id, quantity)
7372
}
7473

7574
override fun getQuantityString(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any?): String {
76-
return base.resources.getQuantityStringT(tolgee, id, quantity, *formatArgs.filterNotNull().toTypedArray())
75+
return base.getQuantityStringT(tolgee, id, quantity, *formatArgs.filterNotNull().toTypedArray())
7776
}
7877

7978
override fun getStringArray(@ArrayRes id: Int): Array<out String?> {
80-
return base.resources.getStringArrayT(tolgee, id)
79+
return base.getStringArrayT(tolgee, id)
8180
}
8281

8382
override fun getText(@StringRes id: Int): CharSequence {
8483
return base.getTextT(tolgee, id)
8584
}
8685

8786
override fun getQuantityText(@PluralsRes id: Int, quantity: Int): CharSequence {
88-
return base.resources.getQuantityTextT(tolgee, id, quantity)
87+
return base.getQuantityTextT(tolgee, id, quantity)
8988
}
9089

9190
override fun getTextArray(id: Int): Array<out CharSequence?> {
92-
return base.resources.getTextArrayT(tolgee, id)
91+
return base.getTextArrayT(tolgee, id)
92+
}
93+
94+
override fun getText(@StringRes id: Int, def: CharSequence?): CharSequence? {
95+
return base.getTextT(tolgee, id, def)
9396
}
9497

9598
/**
@@ -98,117 +101,117 @@ internal class TolgeeResources(
98101

99102
@RequiresApi(Build.VERSION_CODES.O)
100103
override fun getFont(@FontRes id: Int): Typeface {
101-
return base.resources.getFont(id)
104+
return base.getFont(id)
102105
}
103106

104107
override fun getIntArray(@ArrayRes id: Int): IntArray {
105-
return base.resources.getIntArray(id)
108+
return base.getIntArray(id)
106109
}
107110

108111
override fun obtainTypedArray(@ArrayRes id: Int): TypedArray {
109-
return base.resources.obtainTypedArray(id)
112+
return base.obtainTypedArray(id)
110113
}
111114

112115
override fun getDimension(@DimenRes id: Int): Float {
113-
return base.resources.getDimension(id)
116+
return base.getDimension(id)
114117
}
115118

116119
override fun getDimensionPixelOffset(@DimenRes id: Int): Int {
117-
return base.resources.getDimensionPixelOffset(id)
120+
return base.getDimensionPixelOffset(id)
118121
}
119122

120123
override fun getDimensionPixelSize(@DimenRes id: Int): Int {
121-
return base.resources.getDimensionPixelSize(id)
124+
return base.getDimensionPixelSize(id)
122125
}
123126

124127
override fun getFraction(@FractionRes id: Int, base: Int, pbase: Int): Float {
125-
return this.base.resources.getFraction(id, base, pbase)
128+
return this.base.getFraction(id, base, pbase)
126129
}
127130

128131
@Deprecated("Proxied Deprecation")
129132
override fun getDrawable(@DrawableRes id: Int): Drawable? {
130-
return base.resources.getDrawable(id)
133+
return base.getDrawable(id)
131134
}
132135

133136
override fun getDrawable(@DrawableRes id: Int, theme: Theme?): Drawable? {
134-
return base.resources.getDrawable(id, theme)
137+
return base.getDrawable(id, theme)
135138
}
136139

137140
@Deprecated("Proxied Deprecation")
138141
override fun getDrawableForDensity(@DrawableRes id: Int, density: Int): Drawable? {
139-
return base.resources.getDrawableForDensity(id, density)
142+
return base.getDrawableForDensity(id, density)
140143
}
141144

142145
override fun getDrawableForDensity(@DrawableRes id: Int, density: Int, theme: Theme?): Drawable? {
143-
return base.resources.getDrawableForDensity(id, density, theme)
146+
return base.getDrawableForDensity(id, density, theme)
144147
}
145148

146149
@Deprecated("Proxied Deprecation")
147150
override fun getMovie(@RawRes id: Int): Movie? {
148-
return base.resources.getMovie(id)
151+
return base.getMovie(id)
149152
}
150153

151154
@ColorInt
152155
@Deprecated("Proxied Deprecation")
153156
override fun getColor(@ColorRes id: Int): Int {
154-
return base.resources.getColor(id)
157+
return base.getColor(id)
155158
}
156159

157160
@ColorInt
158161
@RequiresApi(Build.VERSION_CODES.M)
159162
override fun getColor(@ColorRes id: Int, theme: Theme?): Int {
160-
return base.resources.getColor(id, theme)
163+
return base.getColor(id, theme)
161164
}
162165

163166
@Deprecated("Proxied Deprecation")
164167
override fun getColorStateList(@ColorRes id: Int): ColorStateList {
165-
return base.resources.getColorStateList(id)
168+
return base.getColorStateList(id)
166169
}
167170

168171
@RequiresApi(Build.VERSION_CODES.M)
169172
override fun getColorStateList(@ColorRes id: Int, theme: Theme?): ColorStateList {
170-
return base.resources.getColorStateList(id, theme)
173+
return base.getColorStateList(id, theme)
171174
}
172175

173176
override fun getBoolean(@BoolRes id: Int): Boolean {
174-
return base.resources.getBoolean(id)
177+
return base.getBoolean(id)
175178
}
176179

177180
override fun getInteger(@IntegerRes id: Int): Int {
178-
return base.resources.getInteger(id)
181+
return base.getInteger(id)
179182
}
180183

181184
@RequiresApi(Build.VERSION_CODES.Q)
182185
override fun getFloat(@DimenRes id: Int): Float {
183-
return base.resources.getFloat(id)
186+
return base.getFloat(id)
184187
}
185188

186189
override fun getLayout(@LayoutRes id: Int): XmlResourceParser {
187-
return base.resources.getLayout(id)
190+
return base.getLayout(id)
188191
}
189192

190193
override fun getAnimation(@AnimatorRes @AnimRes id: Int): XmlResourceParser {
191-
return base.resources.getAnimation(id)
194+
return base.getAnimation(id)
192195
}
193196

194197
override fun getXml(@XmlRes id: Int): XmlResourceParser {
195-
return base.resources.getXml(id)
198+
return base.getXml(id)
196199
}
197200

198201
override fun openRawResource(@RawRes id: Int): InputStream {
199-
return base.resources.openRawResource(id)
202+
return base.openRawResource(id)
200203
}
201204

202205
override fun openRawResource(@RawRes id: Int, value: TypedValue?): InputStream {
203-
return base.resources.openRawResource(id, value)
206+
return base.openRawResource(id, value)
204207
}
205208

206209
override fun openRawResourceFd(@RawRes id: Int): AssetFileDescriptor? {
207-
return base.resources.openRawResourceFd(id)
210+
return base.openRawResourceFd(id)
208211
}
209212

210213
override fun getValue(@AnyRes id: Int, outValue: TypedValue?, resolveRefs: Boolean) {
211-
return base.resources.getValue(id, outValue, resolveRefs)
214+
return base.getValue(id, outValue, resolveRefs)
212215
}
213216

214217
override fun getValueForDensity(
@@ -217,73 +220,69 @@ internal class TolgeeResources(
217220
outValue: TypedValue?,
218221
resolveRefs: Boolean
219222
) {
220-
return base.resources.getValueForDensity(id, density, outValue, resolveRefs)
223+
return base.getValueForDensity(id, density, outValue, resolveRefs)
221224
}
222225

223226
@SuppressLint("DiscouragedApi")
224227
@Discouraged("Proxied Discourage")
225228
override fun getValue(name: String?, outValue: TypedValue?, resolveRefs: Boolean) {
226-
return base.resources.getValue(name, outValue, resolveRefs)
229+
return base.getValue(name, outValue, resolveRefs)
227230
}
228231

229232
override fun obtainAttributes(set: AttributeSet?, @StyleableRes attrs: IntArray?): TypedArray? {
230-
return base.resources.obtainAttributes(set, attrs)
233+
return base.obtainAttributes(set, attrs)
231234
}
232235

233236
@Deprecated("Proxied Deprecation")
234237
override fun updateConfiguration(config: Configuration?, metrics: DisplayMetrics?) {
235-
return base.resources.updateConfiguration(config, metrics)
238+
return base.updateConfiguration(config, metrics)
236239
}
237240

238241
override fun getDisplayMetrics(): DisplayMetrics? {
239-
return base.resources.displayMetrics
242+
return base.displayMetrics
240243
}
241244

242245
override fun getConfiguration(): Configuration? {
243-
return base.resources.configuration
246+
return base.configuration
244247
}
245248

246249
@SuppressLint("DiscouragedApi")
247250
@Discouraged("Proxied Discourage")
248251
override fun getIdentifier(name: String?, defType: String?, defPackage: String?): Int {
249-
return base.resources.getIdentifier(name, defType, defPackage)
252+
return base.getIdentifier(name, defType, defPackage)
250253
}
251254

252255
override fun getResourceName(@AnyRes resid: Int): String? {
253-
return base.resources.getResourceName(resid)
256+
return base.getResourceName(resid)
254257
}
255258

256259
override fun getResourcePackageName(@AnyRes resid: Int): String? {
257-
return base.resources.getResourcePackageName(resid)
260+
return base.getResourcePackageName(resid)
258261
}
259262

260263
override fun getResourceEntryName(@AnyRes resid: Int): String? {
261-
return base.resources.getResourceEntryName(resid)
264+
return base.getResourceEntryName(resid)
262265
}
263266

264267
override fun parseBundleExtras(parser: XmlResourceParser?, outBundle: Bundle?) {
265-
return base.resources.parseBundleExtras(parser, outBundle)
268+
return base.parseBundleExtras(parser, outBundle)
266269
}
267270

268271
override fun parseBundleExtra(tagName: String?, attrs: AttributeSet?, outBundle: Bundle?) {
269-
return base.resources.parseBundleExtra(tagName, attrs, outBundle)
272+
return base.parseBundleExtra(tagName, attrs, outBundle)
270273
}
271274

272275
@RequiresApi(Build.VERSION_CODES.R)
273276
override fun addLoaders(vararg loaders: ResourcesLoader?) {
274-
return base.resources.addLoaders(*loaders)
277+
return base.addLoaders(*loaders)
275278
}
276279

277280
@RequiresApi(Build.VERSION_CODES.R)
278281
override fun removeLoaders(vararg loaders: ResourcesLoader?) {
279-
return base.resources.removeLoaders(*loaders)
282+
return base.removeLoaders(*loaders)
280283
}
281284

282285
override fun getResourceTypeName(@AnyRes resid: Int): String? {
283-
return base.resources.getResourceTypeName(resid)
284-
}
285-
286-
override fun getText(@StringRes id: Int, def: CharSequence?): CharSequence? {
287-
return base.resources.getText(id, def)
286+
return base.getResourceTypeName(resid)
288287
}
289288
}

0 commit comments

Comments
 (0)