refactor: extract AnkiDroidApp.instance to :common:android#21164
refactor: extract AnkiDroidApp.instance to :common:android#21164criticalAY wants to merge 1 commit into
AnkiDroidApp.instance to :common:android#21164Conversation
| fun toBundle(): Bundle = | ||
| bundleOf( | ||
| INTENT_MODEL_FILENAME to saveTempNoteType(AnkiDroidApp.instance.applicationContext, notetype), | ||
| INTENT_MODEL_FILENAME to saveTempNoteType(AnkiDroidApplication.instance.applicationContext, notetype), |
There was a problem hiding this comment.
TODO: cleanup in another commit - applicationContext is not necessary, this is an Applicaton
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // SPDX-FileCopyrightText: 2026 Ashish Yadav <mailtoashish693@gmail.com> | ||
|
|
||
| package com.ichi2.anki.common.application |
There was a problem hiding this comment.
I'd use common.android unless you feel a lot will be in this class
|
|
||
| val isInitialized: Boolean | ||
| get() = ::instance.isInitialized | ||
| } |
There was a problem hiding this comment.
Disagree with me here:
I'd prefer this if it were a bare appContext variable.
- AnkiDroidApplication.instance.contentResolver
+ appContext.contentResolver-
rename current usages of
appContext -
something like this
/** */
lateinit var appContext: Context
private set
object AnkiDroidApplication {
/** Sets the [appContext]. Called once from `AnkiDroidApp.onCreate()`. */
fun setApplicationInstance(app: Application) {
appContext = app
}
}878e3e4 to
bdfbabc
Compare
david-allison
left a comment
There was a problem hiding this comment.
Clean out + rename the existing non-test appContext variables before the commit declaring + using the global accessor.
AnkiDroidUsageAnalytics.appContext for example has special consideration, as instance may not have been set
…idApplication Adds a top-level `appContext: Context` accessor in :common:android, set during AnkiDroidApp.onCreate() via AnkiDroidApplication.setApplicationInstance. Shared and feature-module code can now reach the Application context without depending on :AnkiDroid's AnkiDroidApp subclass.
bdfbabc to
ee10d2c
Compare
david-allison
left a comment
There was a problem hiding this comment.
One issue, then LGTM
The TODO is optional
Leaving this for second approval as it's a common pattern we want to get right (naming etc...) first time
| var templateChanges = ArrayList<TemplateChange>() | ||
| private set | ||
|
|
||
| // TODO: cleanup applicationContext is not necessary, this is an Application |
| } else { | ||
| Timber.w("AnkiDroid instance not set") | ||
| null | ||
| } |
There was a problem hiding this comment.
AnkiDroidApplication.isInitializedI believe- I'd prefer not exposing
isInitalizedand useAnkiDroidApplication.instanceOrNull(or similarly named variables)- as a TODO/separate commit
val instanceOrNull: Context?
get() = if (::appContext.isInitialized) appContext else null
Purpose / Description
Extract application context to :common:android, a step towards clean code and one more step closer to widget extraction
Fixes
Approach
See commit
How Has This Been Tested?
Local build and ran on Emualtor works smooth
Learning (optional, can help others)
NA
Checklist
Please, go through these checks before submitting the PR.