Created
September 2, 2025 18:13
-
-
Save dulimarta/69aee0afb3068f0b2b48583635713d49 to your computer and use it in GitHub Desktop.
Hilt Dependency Injection to instantiate a repository for (Android) Glance App Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WidgetRepository @Inject constructor(@ApplicationContext val appContext: Context) { | |
| private var _currentTitle = MutableStateFlow<String?>("") | |
| val currentTitle = _currentTitle.asStateFlow() | |
| @EntryPoint | |
| @InstallIn(SingletonComponent::class) | |
| interface WidgetRepoEntryPoint { | |
| fun widgetRepo(): WidgetRepository | |
| } | |
| companion object { | |
| var _instance: WidgetRepository? = null | |
| fun get(appContext: Context): WidgetRepository { | |
| if (_instance == null) { | |
| val entryPoint: WidgetRepoEntryPoint = | |
| EntryPoints.get(appContext, WidgetRepoEntryPoint::class.java) | |
| _instance = entryPoint.widgetRepo() | |
| } | |
| return _instance!! | |
| } | |
| } | |
| fun updateEpisodeTitle(s: String) { | |
| _currentTitle.value = s | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment