Hovhannes
08/20/2021, 1:44 PMFragment
private var instance: LoginFragment? = null
private val viewModel by viewModels<AuthViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentLoginBinding.bind(view)
instance =this
}
fun login(): String {
viewModel.login(clientId, partner.toString(), password)
val clientId = binding.editTextTextEmailAddress.text.toString().trim()
val encrypted = encrypt(clientId, publicKey)
return encrypted
}
RemoteDataSource
private fun getRetrofitClient(): OkHttpClient {
val str= LoginFragment().getInstance()?.login()
....rkeazor
09/04/2021, 11:17 AMabstract class BaseActivity(layouId: Int = 0) // Hilt complains that default value of 0 is not allowed
Colton Idle
09/17/2021, 10:27 AM@Singleton
@InstallIn(Singleton::class)
class MyRepo @Inject constructor(val myManager: MyManager) :
dave08
09/19/2021, 6:39 AMvalue class
with Dagger2? It seems like it's not working... (don't think it makes a difference, but I'm using Anvil with it...)Hovhannes
09/24/2021, 10:44 AM<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
class App : Application(){
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@App)
modules(listOf(appModule, repoModule, viewModelModule))
}
}
}
enighma
09/27/2021, 11:04 PM@JvmSuppressWildcards
Adding that to the constructor injection site did the trick. @ephemient thank you! 🙂
Update 1:
In my use-case below MyType
was a sealed class, which is what caused the issue.
Original message:
I think this question applies here even though I'm using hilt:
I have a provider function that provides a kotlinflow like so:
fun provideX(): Flow<MyType>
However, when I try to compile I get "cannot be provided without an @Provides-annotated method". I'm guessing it wants a provider for MyType?
I'm not sure, because it seems to work if it's:
fun provideX(): Flow<Long>
I guess in general, are there any recommendation around flows/async together with dagger/hilt?Geert
09/28/2021, 1:02 PMjava.lang.RuntimeException: Cannot create an instance of class app.medxpert.pgo.compose.privacy_agreement.PrivacyAgreementViewModel
The Preferences:
class Preferences @Inject constructor(
@ApplicationContext val context: Context
) {...}
My ViewModel:
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class PrivacyAgreementViewModel @Inject constructor(
private val sharedPrefs: Preferences,
) : ViewModel() {...}
How I call it:
@Composable
fun PrivacyAgreementView(
viewModel: PrivacyAgreementViewModel = hiltViewModel()
) {
What could be wrong?Hovhannes
10/01/2021, 5:59 AM@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {
@Provides
fun provideBuildlogger(): HttpLoggingInterceptor {
return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}
@Provides
@Singleton
fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
} else {
OkHttpClient
.Builder()
.build()
}
@Provides
@Singleton
@Named("base")
fun provideRetrofitBuilder(
alwaysonUrl: String, okHttpClient: OkHttpClient
): Retrofit {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create().asLenient())
.baseUrl(alwaysonUrl)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
@Named("vbtop")
fun provideRetrofitBuilderVbttop(
@Named("vbttop") vbttopUrl: String, okHttpClient: OkHttpClient
): Retrofit {
return Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(vbttopUrl)
.client(okHttpClient)
.build()
}
@Provides
@Singleton
fun provideApi(@Named("base") retrofit: Retrofit): UrlApi {
return retrofit.create(UrlApi::class.java)
}
@Provides
@Singleton
fun provideApiVbtop(@Named("vbttop") retrofit: Retrofit): VbtopApi {
return retrofit.create(VbtopApi::class.java)
}
@Provides
fun provideBase() = BuildConfig.BASE_URL
@Provides
@Named("vbttop")
fun vbttopUrl(): String = ""
}
interface UrlApi {
@GET
suspend fun getUrls(@Url path: String): Response<JsonData>
}
interface VbtopApi {
@GET
suspend fun getVbtop(@Url url:String): Response<VbtopUrl>
}
class MainRepository @Inject constructor(private val urlApi: UrlApi, private val vbtopApi: VbtopApi) {
suspend fun getUrls(path:String) = urlApi.getUrls(path)
suspend fun getVbtop(url:String)=vbtopApi.getVbtop(url)
}
Ravin Jain
10/08/2021, 1:12 AMviewModelScope
as a constructor injection through hilt to other classes which are viewmodelscoped
rkeazor
10/09/2021, 1:59 PMSlackbot
10/13/2021, 9:32 AMSlackbot
10/20/2021, 11:59 AMMikołaj Karwowski
10/25/2021, 9:59 AM[kapt] di.HeaterModule: Can't reference type 'Heater' from default package in Java stub.
as a warning and the error is:
error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract interface HeaterModule {
To be clear, this is my module:
@Module
interface HeaterModule {
//Tried both
// @Binds
// fun bindHeater(heater: SmallHeater): Heater
@Provides
fun provideHeater(): Heater {
return SmallHeater()
}
}
The component that doesn't even do anything:
@Component(modules = [HeaterModule::class])
interface TeaComponent{
// fun teaMaker(): TeaMaker
}
And the cherry on the top where I suspect the errors to be, but can't find them i.e. build.gradle.kts
file:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("kapt") version "1.5.31"
kotlin("jvm") version "1.5.10"
application
}
group = "me.mkarwowski"
version = "1.0-SNAPSHOT"
kapt {
generateStubs = true
keepJavacAnnotationProcessors = true
}
repositories {
mavenCentral()
}
dependencies {
implementation( "com.google.dagger:dagger:2.39.1")
annotationProcessor( "com.google.dagger:dagger-compiler:2.39.1")
kapt( "com.google.dagger:dagger-compiler:2.39.1")
testImplementation(kotlin("test"))
}
tasks.test {
useJUnit()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "1.8"
}
application {
mainClass.set("MainKt")
}
Am I doing anything obviously wrong here?
I'll upload it to github in a second so You can reproduce itChris Fillmore
10/27/2021, 12:39 AMContext
, so I want to use the Activity context where possible.
I suppose I could accomplish this binding with annotations, but I was wondering if there was another way.Sanendak
11/01/2021, 4:51 PMursus
11/12/2021, 12:54 AMvalue class Id(value: String)
? it complains that it doesnt see a provider for the underlying string valueColton Idle
11/14/2021, 6:25 PMGeert
11/15/2021, 10:28 AMmattinger
11/18/2021, 1:42 PMK Merle
11/24/2021, 7:46 AM@ViewModelScope
or are installed in @ViewModelComponent
.Colton Idle
11/27/2021, 7:20 PMCrashReporterService
interface, and a real impl called FirebaseCrashReporterService
. Both the interface and impl are on the Dependency graph, and so... nothing prevents someone from Injecting FirebaseCrashReporterService.
Is there anything I can do to hide my real impls so that people only use the interface?Colton Idle
12/01/2021, 6:31 PMinterface CrashReporterService {
fun logFatal(e: Exception)
}
class FirebaseCrashReporterService : CrashReporterService {
override fun logFatal(e: Exception) {
// do something
}
}
Option 1️⃣ :(dont add the concrete to nav graph and just create it at the Provider)
@Module
object FirebaseCrashReporterModule {
@JvmStatic
@Provides
fun firebaseCrashReporter(dependencies..) : CrashReporter {
return FirebaseCrashReporter(...)
}
}
Option 2️⃣: (use a qualifier to make the type "harder" to accidentally use)
@Module
@InstallIn(SingletonComponent::class)
abstract class AppInterfaceModule {
@Singleton
@Binds
abstract fun bindsCrashReporterService(
@Named("placeholder_qualifier") service: FirebaseCrashReporterService
): CrashReporterService
}
@Module
@InstallIn(SingletonComponent::class)
class AppImplModule {
@Singleton
@Provides
@Named("placeholder_qualifier")
fun bindsCrashReporterService(): FirebaseCrashReporterService {
return FirebaseCrashReporterService()
}
}
blakelee
12/06/2021, 8:01 PM@Module
@InstallIn(FragmentComponent::class)
object FragmentModule {
@Provides
@FragmentScoped
fun provideMyObject(fragment: Fragment): MyObject {
return fragment.getMyObject() // I can only get this from a fragment
}
}
• I create my ViewModel without @HiltViewModel
but with @Inject constructor
• In my fragment I inject it with assisted inject
@Inject
lateinit var provider: Provider<MyViewModel>
private val viewModel: MyViewModel by assistedViewModel { provider.get() }
Since I know I'm only going to be using this ViewModel in a fragment, it would be nice to be able to get items from a fragment.jw
12/11/2021, 12:29 PMMiguel
12/22/2021, 10:10 AMdata class Example @Inject constructor(
val x: X,
val string: String? = x.method()
)
In my dagger class I have:
@Provides
@Singleton
fun x() = X()
but whenever I want to instantiate an Example
object. it asks me for x
dependency which should always be injected. Is there any approach of how I can deal with this?jean
01/04/2022, 2:40 PM@ViewModelScoped
for the second view ViewModel which works fine when I navigate the first time to a contact detail view, but on every following navigation, the view shows the details of the previous contact before loading and showing the new details. Should I implement a “reset” mechanism that clears the content of the view model when navigating to a new contact, or should I use a different scope? I considered to not use any scope but then screen orientation changes would make the view model trigger the load again. What do you advice me to do ?yassir kazzoune
01/06/2022, 10:32 AMjean
01/13/2022, 1:06 PMhiltViewModel()
supports @Assisted
constructor attributes now? Or does one still need to use a factory with @AssistedFactory
?wellingtoncosta
01/14/2022, 12:29 PMdave08
01/18/2022, 4:10 PM@WorkComponent
for WorkManager workers? Is there a way to make my own? Or is it intended, and there's something different to do for workers?dave08
01/18/2022, 4:10 PM@WorkComponent
for WorkManager workers? Is there a way to make my own? Or is it intended, and there's something different to do for workers?Colton Idle
01/18/2022, 5:20 PMFunkyMuse
01/18/2022, 5:55 PMtrevjones
01/19/2022, 3:16 AMWorkerFactoryModule
is install to Singleton and the `androidx.work.Configuration`` is used to initialize inside of a double check lock to ensure a single instance of the factory is created.
Which should mean that the only safe scope to depend on is Singleton
or unscoped. Scoping the worker itself seems bad given each invocation is going to get its own instance of WorkerParameters
dave08
01/19/2022, 4:07 AM