Skip to content

Домашнее задание по DI#3

Open
BestPricklyCactus wants to merge 3 commits intoOtus-Android:masterfrom
BestPricklyCactus:master
Open

Домашнее задание по DI#3
BestPricklyCactus wants to merge 3 commits intoOtus-Android:masterfrom
BestPricklyCactus:master

Conversation

@BestPricklyCactus
Copy link
Copy Markdown

No description provided.

m_kolobanova added 2 commits March 23, 2026 20:31
…ьзуя Dagger.

Сделала AppComponent как @singleton и подключила его к Application.
Создала Dagger-модули в AppModule.kt и SubcomponentsModule.kt
Добавила базовые DI-интерфейсы в Dependencies.kt и ProductsDependencies.kt
Обновила ServiceLocator для получения зависимости из AppComponent
… избранного.

Упростила выдачу фабрики в FavoritesViewModelFactory.kt.
Добавила @Inject constructor()
Удалила лишний модуль FavoritesFeatureModule
(context.applicationContext as ProductsApplication)
.appComponent
.favoritesComponent()
.create()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

создание компонента и в целом вьюмодели лучше вынести наружу, например в composable навигации

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вынесла в MainNavigation в composable

import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ServiceLocator {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

этот файл вообще больше не нужен, мы его заменяем через Dagger

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удалила


@Singleton
@Component(modules = [AppModule::class])
interface AppComponent : ProductsDependencies {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

из компонента наружу торчит почти все подряд, это лишнее, по заданию нужно реализовать ProductsComponent

@FeatureScope
@Component(dependencies = [ProductsDependencies::class])
interface ProductsComponent {
    fun viewModelFactory(): ProductsViewModelFactory

    @Component.Factory
    interface Factory {
        fun create(dependencies: ProductsDependencies): ProductsComponent
    }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удалила все ненужное. Оставила:

  1. наследование от ProductsDependencies (для передачи в ProductsComponent)
  2. Фабрику сабкомпонента FavoritesComponent

fun productApiService(): ProductApiService
fun productRepository(): ProductRepository
fun favoritesRepository(): FavoritesRepository
override fun consumeProductsUseCase(): ConsumeProductsUseCase
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вот эти все оверрайды тут не нужны, можно удалить

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Удалила

return retrofit.create(ProductApiService::class.java)
}

@Provides
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

указание реализаций интерфейса делается через @Binds, а не @Provides:

@Binds
fun bindProductRepository(impl: ProductRepositoryImpl): ProductRepository

все ниже аналогично

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ой. Переделала

}

@Provides
fun providePriceFormatter(): PriceFormatter {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если у класса нет интерфейса, то его нужно в граф добавлять через @Inject constructor

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Слелала.

…ех классов добавлены @Inject constructor, Исправлен AppComponent.kt Создание appComponent и view model вынесена в composable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants