gts13
01/07/2024, 2:17 PMinterface ManagerProxy {
fun execute()
}
class Manager() : ManagerProxy {
override fun execute() { // do something }
fun otherExecute() { // do something else }
}
class ClientOne @Inject constructor(
private val manager: Manager
) {
mananager.otherExecute()
...
}
class ClientTwo @Inject constructor(
private val managerProxy: ManagerProxy // need to have the same instance of Manager from ClientOne
) {
mananager.execute()
...
}
gts13
01/07/2024, 2:20 PM@Module
@InstallIn(SingletonComponent::class)
object ApplicationModule {
@Provides
@Named("MyManager")
fun provideMyManager(
someRepository: SomeRepository
): Manager {
return Manager(someRepository)
}
}
@Module
@InstallIn(SingletonComponent::class)
interface ManagerModule {
@Binds
fun bindManagerProxy(
@Named("MyManager") manager: Manager
): ManagerProxy
}
the ClientManager becomes
class Manager @Inject constructor() : ManagerProxy {
override fun execute() { // do something }
fun otherExecute() { // do something else }
}
and then in the ClientOne
class ClientOne @Inject constructor(
@Named("MyManager") private val manager: Manager // named parameter
) { ... }
Nicholas Doglio
01/07/2024, 4:27 PM@ActivityScoped
or @ViewModelScoped
to avoid making it a singletongts13
01/07/2024, 5:23 PMNicholas Doglio
01/07/2024, 5:35 PMApplicationModule
at all
You can just update Manager
to look like this
@Singleton // or other scope like @ActivityScoped
class Manager @Inject constructor() : ManagerProxy
gts13
01/07/2024, 5:44 PMManager
as Singleton or ActivityScoped. ViewModelScoped is my goal but still doesn't work if I do something like this:
@Module
@InstallIn(ViewModelComponent::class)
interface ManagerModule {
@Binds
@ViewModelScoped
fun bindManagerProxy(
@Named("MyManager") manager: Manager
): ManagerProxy
}
@ViewModelScoped
class Manager @Inject constructor() : ManagerProxy
then the ClientOne and ClientTwo won't have the same instance of the Manager
class
@ViewModelScoped
class ClientOne @Inject constructor(
private val manager: Manager
) {}
@ViewModelScoped
class ClientTwo @Inject constructor(
private val managerProxy: ManagerProxy
) {}
Nicholas Doglio
01/07/2024, 5:52 PM@ViewModelScoped
only works if they're being accessed by the same ViewModel, if they're being accessed by separate screens or separate parts of your app you'll either need a different broader scope. You could also look into defining a custom scopegts13
01/07/2024, 5:58 PM@Named
with my very first tryNicholas Doglio
01/07/2024, 10:47 PM@Named
is a qualifier and is better when you when separate implementations but don’t care about any of them being retained/singletons
@Module
@InstallIn(SingletonComponent::class)
object ApplicationModule {
@Provides
@Named("loggedIn")
fun provideLoggedInMyManager(
loggedInRepository: Repository
): Manager {
return Manager(loggedInRepository)
}
@Provides
@Named("loggedOut")
fun provideLoggedOutMyManager(
loggedOutRepository: Repository
): Manager {
return Manager(loggedOutRepository)
}
}
With the above you can have separate implementations of Manager
that take different versions of the Repository
that could have changes in behavior based on logged in state.