Created
March 28, 2021 21:44
-
-
Save viniciusalvesmello/18e08ae30d66f4544575f3c46dfa8b83 to your computer and use it in GitHub Desktop.
Exemplo Simples de MVP com Hilt + Dagger assisted injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SampleRepository { | |
fun getSample(): String | |
} | |
class SampleRepositoryImpl: SampleRepository { | |
override fun getSample() = "Sample" | |
} | |
interface SamplePresenter { | |
fun initView() | |
} | |
class SamplePresenterImpl @AssistedInject constructor( | |
private val sampleRepository: SampleRepository, | |
@Assisted private val view: SampleView, | |
): SamplePresenter { | |
override fun initView() { | |
view.updateView(sampleRepository.getSample()) | |
} | |
@AssistedFactory | |
interface SamplePresenterFactory { | |
fun create(view: SampleView): SamplePresenterImpl | |
} | |
} | |
interface SampleView { | |
fun updateView(sample: String) | |
} | |
class SampleActivity : DaggerAppCompatActivity(), SampleView { | |
... | |
@Inject | |
lateinit var restaurantsRepositoryFactory: RestaurantsRepositoryFactory | |
private val presenter: SamplePresenter by lazy { | |
restaurantsRepositoryFactory.create(this) | |
} | |
... | |
override fun onCreate(savedInstanceState: Bundle?) { | |
... | |
presenter.initView() | |
... | |
} | |
... | |
override fun updateView(sample: String) { | |
... | |
} | |
... | |
} | |
@Module | |
@InstallIn(ActivityComponent::class) | |
class SampleModule { | |
@Provides | |
fun provideSampleRepository(): SampleRepository = SampleRepositoryImpl() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment