package com.example.dagger
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import dagger.internal.InjectedFieldSignature
import javax.inject.Inject
class MainActivity : AppCompatActivity() {
@Inject
lateinit var smartPhone: SmartPhone
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
DaggerSmartPhoneComponent.builder()
.memoryCardModule(MemoryCardModule(1000))
.build()
.inject(this)
}
}
package com.example.dagger
import android.util.Log
import dagger.Module
import dagger.Provides
@Module
class MemoryCardModule(val memorySize:Int) {
@Provides
fun providesMemoryCard(): MemoryCard {
Log.i("MYTAG","Size of the memory is $memorySize")
return MemoryCard()
}
}package com.example.dagger
import android.util.Log
import javax.inject.Inject
class MemoryCard {
init {
Log.i("MYTAG", "Memory Card Constructed")
}
fun getSpaceAvailablity() {
Log.i("MYTAG", "Memory space available")
}
}package com.example.dagger
import android.util.Log
import javax.inject.Inject
class NickelCadmiumBattery @Inject constructor(): Battery {
override fun getPower() {
Log.i("MYTAG", " Power from NickelCadmiumBattery")
}
}package com.example.dagger
import dagger.Binds
import dagger.Module
import dagger.Provides
@Module
abstract class NCBatteryModule {
@Binds
abstract fun bindsNCBattery(nickelCadmiumBattery: NickelCadmiumBattery):Battery
}package com.example.dagger
import android.util.Log
import javax.inject.Inject
interface Battery{
fun getPower()
}
No comments:
Post a Comment