app: lazily initialize hiltEntryPoint to make sure that activity is available before we use appContext

Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
This commit is contained in:
Aditya Wasan 2021-05-22 18:23:14 +05:30 committed by Harsh Shandilya
parent 1ae961e51c
commit 1583c4c600
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 9 additions and 6 deletions

View file

@ -42,13 +42,12 @@ import dev.msfjarvis.aps.util.settings.PreferenceKeys
class RepositorySettings(private val activity: FragmentActivity) : SettingsProvider {
private val hiltEntryPoint =
private val hiltEntryPoint by lazy(LazyThreadSafetyMode.NONE) {
EntryPointAccessors.fromApplication(
activity.applicationContext,
RepositorySettingsEntryPoint::class.java,
)
private val encryptedPreferences = hiltEntryPoint.encryptedPreferences()
private val gitSettings = hiltEntryPoint.gitSettings()
}
private fun <T : FragmentActivity> launchActivity(clazz: Class<T>) {
activity.startActivity(Intent(activity, clazz))
@ -66,6 +65,9 @@ class RepositorySettings(private val activity: FragmentActivity) : SettingsProvi
}
override fun provideSettings(builder: PreferenceScreen.Builder) {
val encryptedPreferences = hiltEntryPoint.encryptedPreferences()
val gitSettings = hiltEntryPoint.gitSettings()
builder.apply {
checkBox(PreferenceKeys.REBASE_ON_PULL) {
titleRes = R.string.pref_rebase_on_pull_title

View file

@ -34,14 +34,14 @@ class GitCommandExecutor(
private val operation: GitOperation,
) {
private val hiltEntryPoint =
private val hiltEntryPoint by lazy(LazyThreadSafetyMode.NONE) {
EntryPointAccessors.fromApplication(
activity.applicationContext,
GitCommandExecutorEntryPoint::class.java
)
private val gitSettings = hiltEntryPoint.gitSettings()
}
suspend fun execute(): Result<Unit, Throwable> {
val gitSettings = hiltEntryPoint.gitSettings()
val snackbar =
activity.snackbar(
message = activity.resources.getString(R.string.git_operation_running),
@ -126,6 +126,7 @@ class GitCommandExecutor(
@EntryPoint
@InstallIn(SingletonComponent::class)
interface GitCommandExecutorEntryPoint {
fun gitSettings(): GitSettings
}
}