-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use androidx.startup for startup activities (#762)
Move initialisation of WorkManager and Timber in to new `Initializer` classes, managed by `androidx.startup`. There's a false-positive `BadConfigurationProvider` lint message because the `Configuration.Provider` is in the content provider and not the application class.
- Loading branch information
1 parent
b373ee9
commit bd6bc95
Showing
8 changed files
with
203 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.di | ||
|
||
import android.content.Context | ||
import app.pachli.initializer.TimberInitializer | ||
import app.pachli.initializer.WorkManagerInitializer | ||
import dagger.hilt.EntryPoint | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.EntryPointAccessors | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
/** | ||
* Entry point that allows [androidx.startup.Initializer] implementations | ||
* to inject their dependencies. | ||
*/ | ||
@EntryPoint | ||
@InstallIn(SingletonComponent::class) | ||
interface InitializerEntryPoint { | ||
fun inject(timberInitializer: TimberInitializer) | ||
fun inject(workManagerInitializer: WorkManagerInitializer) | ||
|
||
companion object { | ||
fun resolve(context: Context): InitializerEntryPoint { | ||
val appContext = context.applicationContext ?: throw IllegalStateException() | ||
return EntryPointAccessors.fromApplication( | ||
appContext, | ||
InitializerEntryPoint::class.java, | ||
) | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/app/pachli/initializer/DependencyGraphInitializer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.initializer | ||
|
||
import android.content.Context | ||
import androidx.startup.Initializer | ||
import app.pachli.di.InitializerEntryPoint | ||
|
||
/** Empty root initializer other initialisers can depend on. */ | ||
class DependencyGraphInitializer : Initializer<Unit> { | ||
override fun create(context: Context) { | ||
InitializerEntryPoint.resolve(context) | ||
} | ||
|
||
override fun dependencies(): List<Class<out Initializer<*>>> { | ||
return emptyList() | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/app/pachli/initializer/TimberInitializer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.initializer | ||
|
||
import android.content.Context | ||
import androidx.startup.Initializer | ||
import app.pachli.BuildConfig | ||
import app.pachli.core.activity.LogEntryTree | ||
import app.pachli.core.activity.TreeRing | ||
import app.pachli.di.InitializerEntryPoint | ||
import javax.inject.Inject | ||
import timber.log.Timber | ||
|
||
/** Initialise [Timber]. */ | ||
class TimberInitializer : Initializer<Unit> { | ||
@Inject | ||
lateinit var logEntryTree: LogEntryTree | ||
|
||
override fun create(context: Context) { | ||
InitializerEntryPoint.resolve(context).inject(this) | ||
|
||
when { | ||
BuildConfig.DEBUG -> Timber.plant(Timber.DebugTree()) | ||
BuildConfig.FLAVOR_color == "orange" -> Timber.plant(TreeRing) | ||
} | ||
Timber.plant(logEntryTree) | ||
} | ||
|
||
override fun dependencies(): List<Class<out Initializer<*>>> { | ||
return listOf(DependencyGraphInitializer::class.java) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/app/pachli/initializer/WorkManagerInitializer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2024 Pachli Association | ||
* | ||
* This file is a part of Pachli. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | ||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
* Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with Pachli; if not, | ||
* see <http://www.gnu.org/licenses>. | ||
*/ | ||
|
||
package app.pachli.initializer | ||
|
||
import android.content.Context | ||
import androidx.hilt.work.HiltWorkerFactory | ||
import androidx.startup.Initializer | ||
import androidx.work.Configuration | ||
import androidx.work.WorkManager | ||
import app.pachli.di.InitializerEntryPoint | ||
import javax.inject.Inject | ||
|
||
/** Initialise [WorkManager] with a [HiltWorkerFactory]. */ | ||
class WorkManagerInitializer : Initializer<WorkManager>, Configuration.Provider { | ||
@Inject | ||
lateinit var workerFactory: HiltWorkerFactory | ||
|
||
override val workManagerConfiguration: Configuration | ||
get() = Configuration.Builder().setWorkerFactory(workerFactory).build() | ||
|
||
override fun create(context: Context): WorkManager { | ||
InitializerEntryPoint.resolve(context).inject(this) | ||
|
||
WorkManager.initialize(context, workManagerConfiguration) | ||
return WorkManager.getInstance(context) | ||
} | ||
|
||
override fun dependencies(): List<Class<out Initializer<*>>> { | ||
return listOf( | ||
DependencyGraphInitializer::class.java, | ||
TimberInitializer::class.java, | ||
) | ||
} | ||
} |
This file contains 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