Skip to content

Commit

Permalink
Added sugar over multibindings that helps with Kotlin wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
aitorvs committed Dec 7, 2021
1 parent dccdef9 commit 4fac7d1
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.duckduckgo.app.statistics.api.StatisticsService
import com.duckduckgo.app.statistics.api.StatisticsUpdater
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.statistics.store.StatisticsDataStore
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
Expand Down Expand Up @@ -95,7 +96,7 @@ class StubStatisticsModule {
@AppCoroutineScope appCoroutineScope: CoroutineScope,
statisticsDataStore: StatisticsDataStore,
statisticsUpdater: StatisticsUpdater,
listeners: Set<@JvmSuppressWildcards AtbInitializerListener>
listeners: DaggerSet<AtbInitializerListener>
): LifecycleObserver {
return AtbInitializer(appCoroutineScope, statisticsDataStore, statisticsUpdater, listeners)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.duckduckgo.app.flipper

import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.facebook.flipper.core.FlipperPlugin
import com.squareup.anvil.annotations.ContributesTo
Expand All @@ -26,7 +27,7 @@ import dagger.multibindings.Multibinds
import dagger.SingleInstanceIn

private class FlipperPluginPluginPoint(
private val plugins: Set<@JvmSuppressWildcards FlipperPlugin>
private val plugins: DaggerSet<FlipperPlugin>
) : PluginPoint<FlipperPlugin> {
override fun getPlugins(): Collection<FlipperPlugin> {
return plugins
Expand All @@ -37,15 +38,15 @@ private class FlipperPluginPluginPoint(
@ContributesTo(AppScope::class)
abstract class FlipperPluginModule {
@Multibinds
abstract fun bindEmptySettingInternalFeaturePlugins(): Set<@JvmSuppressWildcards FlipperPlugin>
abstract fun bindEmptySettingInternalFeaturePlugins(): DaggerSet<FlipperPlugin>

@Module
@ContributesTo(AppScope::class)
class SettingInternalFeaturePluginModuleExt {
@Provides
@SingleInstanceIn(AppScope::class)
fun provideSettingInternalFeaturePlugins(
plugins: Set<@JvmSuppressWildcards FlipperPlugin>
plugins: DaggerSet<FlipperPlugin>
): PluginPoint<FlipperPlugin> {
return FlipperPluginPluginPoint(plugins)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Bundle
import com.duckduckgo.app.browser.BrowserActivity
import com.duckduckgo.app.global.ActivityLifecycleCallbacks
import com.duckduckgo.browser.api.BrowserLifecycleObserver
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
Expand All @@ -29,7 +30,7 @@ import javax.inject.Inject
@ContributesMultibinding(AppScope::class)
@SingleInstanceIn(AppScope::class)
class BrowserApplicationStateInfo @Inject constructor(
private val observers: Set<@JvmSuppressWildcards BrowserLifecycleObserver>
private val observers: DaggerSet<BrowserLifecycleObserver>
) : ActivityLifecycleCallbacks {
private var created = 0
private var started = 0
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/duckduckgo/app/di/MigrationsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.duckduckgo.app.di
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.app.global.plugins.migrations.MigrationPlugin
import com.duckduckgo.app.global.plugins.migrations.MigrationPluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Binds
Expand All @@ -31,7 +32,7 @@ import dagger.SingleInstanceIn
abstract class MigrationsPluginProviderModule {
// we use multibinds as the list of plugins can be empty
@Multibinds
abstract fun provideMigrationsPlugins(): Set<@JvmSuppressWildcards MigrationPlugin>
abstract fun provideMigrationsPlugins(): DaggerSet<MigrationPlugin>

@Binds
@SingleInstanceIn(AppScope::class)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/duckduckgo/app/di/StatisticsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.statistics.store.OfflinePixelCountDataStore
import com.duckduckgo.app.statistics.store.PendingPixelDao
import com.duckduckgo.app.statistics.store.StatisticsDataStore
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
Expand All @@ -54,7 +55,7 @@ class StatisticsModule {
statisticsDataStore: StatisticsDataStore,
statisticsService: StatisticsService,
variantManager: VariantManager,
plugins: Set<@JvmSuppressWildcards RefreshRetentionAtbPlugin>,
plugins: DaggerSet<RefreshRetentionAtbPlugin>,
): StatisticsUpdater {
return StatisticsRequester(
statisticsDataStore, statisticsService, variantManager, RefreshRetentionAtbPluginPoint(plugins)
Expand Down Expand Up @@ -107,7 +108,7 @@ class StatisticsModule {
@AppCoroutineScope appCoroutineScope: CoroutineScope,
statisticsDataStore: StatisticsDataStore,
statisticsUpdater: StatisticsUpdater,
listeners: Set<@JvmSuppressWildcards AtbInitializerListener>
listeners: DaggerSet<AtbInitializerListener>
): LifecycleObserver {
return AtbInitializer(appCoroutineScope, statisticsDataStore, statisticsUpdater, listeners)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
Expand All @@ -43,7 +44,7 @@ interface ActivityLifecycleCallbacks : Application.ActivityLifecycleCallbacks {
}

private class ActivityLifecycleCallbacksPluginPoint(
private val plugins: Set<@JvmSuppressWildcards ActivityLifecycleCallbacks>
private val plugins: DaggerSet<ActivityLifecycleCallbacks>
) : PluginPoint<ActivityLifecycleCallbacks> {
override fun getPlugins(): Collection<ActivityLifecycleCallbacks> {
return plugins.sortedBy { it.javaClass.simpleName }
Expand All @@ -56,6 +57,6 @@ class ActivityLifecycleCallbacksModule {
@Provides
@SingleInstanceIn(AppScope::class)
fun provideActivityLifecycleCallbacksPluginPoint(
plugins: Set<@JvmSuppressWildcards ActivityLifecycleCallbacks>
plugins: DaggerSet<ActivityLifecycleCallbacks>
): PluginPoint<ActivityLifecycleCallbacks> = ActivityLifecycleCallbacksPluginPoint(plugins)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.duckduckgo.app.process.ProcessDetector.DuckDuckGoProcess
import com.duckduckgo.app.process.ProcessDetector.DuckDuckGoProcess.VpnProcess
import com.duckduckgo.app.referral.AppInstallationReferrerStateListener
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.di.DaggerMap
import com.duckduckgo.mobile.android.vpn.service.VpnUncaughtExceptionHandler
import com.jakewharton.threetenabp.AndroidThreeTen
import dagger.android.AndroidInjector
Expand Down Expand Up @@ -76,7 +77,7 @@ open class DuckDuckGoApplication : HasDaggerInjector, Application(), LifecycleOb
lateinit var appCoroutineScope: CoroutineScope

@Inject
lateinit var injectorFactoryMap: Map<@JvmSuppressWildcards Class<*>, @JvmSuppressWildcards AndroidInjector.Factory<*>>
lateinit var injectorFactoryMap: DaggerMap<Class<*>, AndroidInjector.Factory<*>>

private val processDetector = ProcessDetector()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.duckduckgo.app.global.api

import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
Expand All @@ -30,7 +31,7 @@ interface ApiInterceptorPlugin {
}

private class ApiInterceptorPluginPoint(
private val plugins: Set<@JvmSuppressWildcards ApiInterceptorPlugin>
private val plugins: DaggerSet<ApiInterceptorPlugin>
) : PluginPoint<ApiInterceptorPlugin> {
override fun getPlugins(): Collection<ApiInterceptorPlugin> {
return plugins
Expand All @@ -41,15 +42,15 @@ private class ApiInterceptorPluginPoint(
@ContributesTo(AppScope::class)
abstract class ApiInterceptorPluginModule {
@Multibinds
abstract fun bindEmptyApiInterceptorPlugins(): Set<@JvmSuppressWildcards ApiInterceptorPlugin>
abstract fun bindEmptyApiInterceptorPlugins(): DaggerSet<ApiInterceptorPlugin>

@Module
@ContributesTo(AppScope::class)
class ApiInterceptorPluginModuleExt {
@Provides
@SingleInstanceIn(AppScope::class)
fun provideApiInterceptorPlugins(
plugins: Set<@JvmSuppressWildcards ApiInterceptorPlugin>
plugins: DaggerSet<ApiInterceptorPlugin>
): PluginPoint<ApiInterceptorPlugin> {
return ApiInterceptorPluginPoint(plugins)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.app.global.api

import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.app.global.plugins.pixel.PixelInterceptorPlugin
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
Expand All @@ -26,7 +27,7 @@ import dagger.multibindings.Multibinds
import dagger.SingleInstanceIn

private class PixelInterceptorPluginPoint(
private val plugins: Set<@JvmSuppressWildcards PixelInterceptorPlugin>
private val plugins: DaggerSet<PixelInterceptorPlugin>
) : PluginPoint<PixelInterceptorPlugin> {
override fun getPlugins(): Collection<PixelInterceptorPlugin> {
return plugins.sortedBy { it.javaClass.simpleName }
Expand All @@ -37,15 +38,15 @@ private class PixelInterceptorPluginPoint(
@ContributesTo(AppScope::class)
abstract class PixelInterceptorPluginModule {
@Multibinds
abstract fun bindEmptyPixelInterceptorPlugins(): Set<@JvmSuppressWildcards PixelInterceptorPlugin>
abstract fun bindEmptyPixelInterceptorPlugins(): DaggerSet<PixelInterceptorPlugin>

@Module
@ContributesTo(AppScope::class)
class PixelInterceptorPluginModuleExt {
@Provides
@SingleInstanceIn(AppScope::class)
fun providePixelInterceptorPlugins(
plugins: Set<@JvmSuppressWildcards PixelInterceptorPlugin>
plugins: DaggerSet<PixelInterceptorPlugin>
): PluginPoint<PixelInterceptorPlugin> {
return PixelInterceptorPluginPoint(plugins)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.app.global.plugin

import androidx.lifecycle.LifecycleObserver
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Binds
Expand All @@ -31,7 +32,7 @@ import dagger.SingleInstanceIn
abstract class LifecycleObserverPluginProviderModule {
// we use multibinds as the list of plugins can be empty
@Multibinds
abstract fun provideLifecycleObserverPlugins(): Set<@JvmSuppressWildcards LifecycleObserver>
abstract fun provideLifecycleObserverPlugins(): DaggerSet<LifecycleObserver>

@Binds
@SingleInstanceIn(AppScope::class)
Expand All @@ -42,7 +43,7 @@ abstract class LifecycleObserverPluginProviderModule {

@SingleInstanceIn(AppScope::class)
class LifecycleObserverPluginPoint @Inject constructor(
private val plugins: Set<@JvmSuppressWildcards LifecycleObserver>
private val plugins: DaggerSet<LifecycleObserver>
) : PluginPoint<LifecycleObserver> {
override fun getPlugins(): Set<LifecycleObserver> {
return plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.app.settings.extension

import android.content.Context
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
Expand All @@ -41,7 +42,7 @@ interface InternalFeaturePlugin {
}

private class SettingsInternalFeaturePluginPoint(
private val plugins: Set<@JvmSuppressWildcards InternalFeaturePlugin>
private val plugins: DaggerSet<InternalFeaturePlugin>
) : PluginPoint<InternalFeaturePlugin> {
override fun getPlugins(): Collection<InternalFeaturePlugin> {
return plugins.sortedBy { it.internalFeatureTitle() }
Expand All @@ -52,15 +53,15 @@ private class SettingsInternalFeaturePluginPoint(
@ContributesTo(AppScope::class)
abstract class SettingInternalFeaturePluginModule {
@Multibinds
abstract fun bindEmptySettingInternalFeaturePlugins(): Set<@JvmSuppressWildcards InternalFeaturePlugin>
abstract fun bindEmptySettingInternalFeaturePlugins(): DaggerSet<InternalFeaturePlugin>

@Module
@ContributesTo(AppScope::class)
class SettingInternalFeaturePluginModuleExt {
@Provides
@SingleInstanceIn(AppScope::class)
fun provideSettingInternalFeaturePlugins(
plugins: Set<@JvmSuppressWildcards InternalFeaturePlugin>
plugins: DaggerSet<InternalFeaturePlugin>
): PluginPoint<InternalFeaturePlugin> {
return SettingsInternalFeaturePluginPoint(plugins)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.duckduckgo.app.global.plugins.migrations

import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import javax.inject.Inject
import dagger.SingleInstanceIn
Expand All @@ -28,7 +29,7 @@ interface MigrationPlugin {

@SingleInstanceIn(AppScope::class)
class MigrationPluginPoint @Inject constructor(
private val injectorPlugins: Set<@JvmSuppressWildcards MigrationPlugin>
private val injectorPlugins: DaggerSet<MigrationPlugin>
) : PluginPoint<MigrationPlugin> {
override fun getPlugins(): Collection<MigrationPlugin> {
return injectorPlugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.app.global.plugins.view_model

import androidx.lifecycle.ViewModel
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import javax.inject.Inject
import dagger.SingleInstanceIn
Expand All @@ -28,7 +29,7 @@ interface ViewModelFactoryPlugin {

@SingleInstanceIn(AppScope::class)
class ViewModelFactoryPluginPoint @Inject constructor(
private val injectorPlugins: Set<@JvmSuppressWildcards ViewModelFactoryPlugin>
private val injectorPlugins: DaggerSet<ViewModelFactoryPlugin>
) : PluginPoint<ViewModelFactoryPlugin> {
override fun getPlugins(): List<ViewModelFactoryPlugin> {
return injectorPlugins.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.app.global.plugins.worker

import androidx.work.ListenableWorker
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import javax.inject.Inject
import dagger.SingleInstanceIn
Expand All @@ -31,7 +32,7 @@ interface WorkerInjectorPlugin {

@SingleInstanceIn(AppScope::class)
class WorkerInjectorPluginPoint @Inject constructor(
private val injectorPlugins: Set<@JvmSuppressWildcards WorkerInjectorPlugin>
private val injectorPlugins: DaggerSet<WorkerInjectorPlugin>
) : PluginPoint<WorkerInjectorPlugin> {
override fun getPlugins(): List<WorkerInjectorPlugin> {
return injectorPlugins.toList()
Expand Down
21 changes: 21 additions & 0 deletions di/src/main/java/com/duckduckgo/di/DaggerTypes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2021 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.di

/** Sugar over multibindings that helps with Kotlin wildcards. */
typealias DaggerSet<T> = Set<@JvmSuppressWildcards T>
typealias DaggerMap<K, V> = Map<@JvmSuppressWildcards K, @JvmSuppressWildcards V>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.duckduckgo.feature.toggles.impl

import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.di.DaggerSet
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.feature.toggles.api.FeatureToggle
import com.duckduckgo.feature.toggles.api.FeatureTogglesPlugin
Expand All @@ -39,7 +40,7 @@ class RealFeatureToggleImpl @Inject constructor(private val featureTogglesPlugin
}

class FeatureCustomConfigPluginPoint(
private val toggles: Set<@JvmSuppressWildcards FeatureTogglesPlugin>
private val toggles: DaggerSet<FeatureTogglesPlugin>
) : PluginPoint<FeatureTogglesPlugin> {
override fun getPlugins(): Collection<FeatureTogglesPlugin> {
return toggles
Expand Down
Loading

0 comments on commit 4fac7d1

Please sign in to comment.