Skip to content

Commit c3ee85e

Browse files
committed
refactor: rename pre-existing 'appContext' members to avoid shadowing
1 parent ecb3887 commit c3ee85e

4 files changed

Lines changed: 27 additions & 29 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/analytics/AnkiDroidUsageAnalytics.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ object AnkiDroidUsageAnalytics {
5353
* be uninitialized in rare Android scenarios (e.g. BackupManager) and
5454
* analytics is a startup concern that must not crash.
5555
*/
56-
private lateinit var appContext: Context
56+
private lateinit var analyticsContext: Context
5757

5858
private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
59-
private val clientId: String by lazy { getOrCreateClientId(appContext) }
59+
private val clientId: String by lazy { getOrCreateClientId(analyticsContext) }
6060

6161
private val sharedPrefsListener =
6262
SharedPreferences.OnSharedPreferenceChangeListener { prefs, key ->
@@ -87,28 +87,28 @@ object AnkiDroidUsageAnalytics {
8787
private set
8888

8989
fun initialize(context: Context) {
90-
appContext = context.applicationContext
90+
analyticsContext = context.applicationContext
9191

9292
Timber.i("AnkiDroidUsageAnalytics:: initialize()")
9393

9494
// Read opt-in before building the client so `enabled` reflects the
9595
// user's choice rather than the default.
96-
handlePreferences(appContext)
96+
handlePreferences(analyticsContext)
9797

9898
if (analytics == null) {
9999
analytics =
100100
GoogleAnalytics.builder {
101-
measurementId = appContext.getString(R.string.ga_trackingId)
101+
measurementId = analyticsContext.getString(R.string.ga_trackingId)
102102
apiSecret = BuildConfig.ANALYTICS_API_KEY
103-
appName = appContext.getString(R.string.app_name)
103+
appName = analyticsContext.getString(R.string.app_name)
104104
appVersion = BuildConfig.VERSION_NAME
105105
enabled = optIn
106-
samplePercentage = getAnalyticsSamplePercentage(appContext)
106+
samplePercentage = getAnalyticsSamplePercentage(analyticsContext)
107107
debug = false
108108
}
109109
}
110110

111-
initializePrefKeys(appContext)
111+
initializePrefKeys(analyticsContext)
112112

113113
AnalyticsExceptionHandler.install(this::sendAnalyticsException)
114114
}
@@ -228,8 +228,8 @@ object AnkiDroidUsageAnalytics {
228228
}
229229
// Rebuild the underlying client so its own `enabled` flag picks
230230
// up the new opt-in state without waiting for the next launch.
231-
if (::appContext.isInitialized) {
232-
reinitialize(appContext)
231+
if (::analyticsContext.isInitialized) {
232+
reinitialize(analyticsContext)
233233
}
234234
}
235235

AnkiDroid/src/main/java/com/ichi2/anki/multiprofile/ProfileManager.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import java.io.File
4343
class ProfileManager private constructor(
4444
context: Context,
4545
) {
46-
private val appContext = context.applicationContext
46+
private val profileContext = context.applicationContext
4747

4848
lateinit var activeProfileContext: Context
4949
private set
@@ -53,7 +53,7 @@ class ProfileManager private constructor(
5353
* ID of the currently active profile.
5454
*/
5555
private val globalProfilePrefs by lazy {
56-
appContext.getSharedPreferences(PROFILE_REGISTRY_FILENAME, Context.MODE_PRIVATE)
56+
profileContext.getSharedPreferences(PROFILE_REGISTRY_FILENAME, Context.MODE_PRIVATE)
5757
}
5858

5959
private val profileRegistry by lazy { ProfileRegistry(globalProfilePrefs) }
@@ -171,7 +171,7 @@ class ProfileManager private constructor(
171171
try {
172172
val wrapper =
173173
ProfileContextWrapper.create(
174-
context = appContext,
174+
context = profileContext,
175175
profileId = profileId,
176176
profileBaseDir = profileBaseDir.file,
177177
)
@@ -208,7 +208,7 @@ class ProfileManager private constructor(
208208
if (prefs.getString(PREF_COLLECTION_PATH, null) != null) return
209209

210210
val profileCollectionDir =
211-
getDefaultAnkiDroidDirectory(appContext, directoryName = profileId.value).apply { mkdirs() }
211+
getDefaultAnkiDroidDirectory(profileContext, directoryName = profileId.value).apply { mkdirs() }
212212

213213
prefs.edit { putString(PREF_COLLECTION_PATH, profileCollectionDir.absolutePath) }
214214
}
@@ -243,8 +243,8 @@ class ProfileManager private constructor(
243243
*/
244244
private fun resolveProfileDirectory(profileId: ProfileId): ProfileRestrictedDirectory {
245245
val appDataRoot =
246-
ContextCompat.getDataDir(appContext)
247-
?: appContext.filesDir.parentFile
246+
ContextCompat.getDataDir(profileContext)
247+
?: profileContext.filesDir.parentFile
248248

249249
if (appDataRoot == null) {
250250
val e = IllegalStateException("Cannot resolve Application Data Directory")
@@ -333,7 +333,7 @@ class ProfileManager private constructor(
333333

334334
Timber.i("deleteProfile: starting deletion of %s", profileId)
335335

336-
val appDataRoot = ContextCompat.getDataDir(appContext)
336+
val appDataRoot = ContextCompat.getDataDir(profileContext)
337337

338338
if (profileId.isDefault()) {
339339
Timber.d("deleteProfile: wiping legacy default-profile data under %s", appDataRoot)
@@ -405,13 +405,13 @@ class ProfileManager private constructor(
405405
* The default-location fallback used when the profile has never written `PREF_COLLECTION_PATH`.
406406
*
407407
* TODO: consolidate with the profile-creation path this should delegate to
408-
* `CollectionHelper.getDefaultAnkiDroidDirectory(appContext, directoryName = ...)`
408+
* `CollectionHelper.getDefaultAnkiDroidDirectory(profileContext, directoryName = ...)`
409409
* that gives us legacy-storage handling and `SystemStorageException`-on-null for free, and keeps
410410
* the "where does a profile collection live" decision in a single place shared with
411411
* `ensureProfileCollectionPath`.
412412
*/
413413
private fun defaultCollectionDirFor(profileId: ProfileId): File? {
414-
val externalFilesDir = appContext.getExternalFilesDir(null) ?: return null
414+
val externalFilesDir = profileContext.getExternalFilesDir(null) ?: return null
415415
return if (profileId.isDefault()) {
416416
File(externalFilesDir, "AnkiDroid")
417417
} else {
@@ -430,10 +430,10 @@ class ProfileManager private constructor(
430430
* TODO: extract a `ProfilePreferences` accessor (e.g. `prefsForProfile(profileId).collectionPath`)
431431
*/
432432
private fun readStoredCollectionPath(profileId: ProfileId): String? {
433-
val defaultPrefsName = "${appContext.packageName}_preferences"
433+
val defaultPrefsName = "${profileContext.packageName}_preferences"
434434
val prefsName =
435435
if (profileId.isDefault()) defaultPrefsName else "profile_${profileId.value}_$defaultPrefsName"
436-
return appContext
436+
return profileContext
437437
.getSharedPreferences(prefsName, Context.MODE_PRIVATE)
438438
.getString(PREF_COLLECTION_PATH, null)
439439
}

AnkiDroid/src/main/java/com/ichi2/anki/navigation/AnkiDroidNavigator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import com.ichi2.anki.common.destinations.Navigator
1212

1313
/** AnkiDroid's [Navigator] implementation. */
1414
object AnkiDroidNavigator : Navigator {
15-
private lateinit var appContext: Context
15+
private lateinit var navContext: Context
1616

1717
fun initialize(application: Application) {
18-
appContext = application
18+
navContext = application
1919
}
2020

2121
override fun toIntent(destination: Destination): Intent =
2222
when (destination) {
23-
is BrowserDestination -> destination.toIntent(appContext)
23+
is BrowserDestination -> destination.toIntent(navContext)
2424
}
2525
}
2626

AnkiDroid/src/main/java/com/ichi2/anki/reviewreminders/ReminderTroubleshootingFragment.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,12 @@ class ReminderTroubleshootingFragment : Fragment(R.layout.fragment_reminder_trou
158158
* [ScheduleRemindersFragment] and [ReminderTroubleshootingFragment] use this with
159159
* `by activityViewModels { … }` so they observe a single VM + repository instance.
160160
*/
161-
internal fun reminderTroubleshootingViewModelFactory(context: Context): ViewModelProvider.Factory {
162-
val appContext = context.applicationContext
163-
return viewModelFactory {
161+
internal fun reminderTroubleshootingViewModelFactory(context: Context): ViewModelProvider.Factory =
162+
viewModelFactory {
164163
initializer {
165-
ReminderTroubleshootingViewModel(ReminderTroubleshootingRepository(appContext))
164+
ReminderTroubleshootingViewModel(ReminderTroubleshootingRepository(context))
166165
}
167166
}
168-
}
169167

170168
private class TroubleshootingChecksAdapter(
171169
private val getResolveAction: (TroubleshootingCheck) -> ResolveCheckAction?,

0 commit comments

Comments
 (0)