-
Notifications
You must be signed in to change notification settings - Fork 11
Домашнее задание по DI #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,22 @@ | ||
| package com.otus.dihomework | ||
|
|
||
| import android.app.Application | ||
| import com.otus.dihomework.di.AppComponent | ||
| import com.otus.dihomework.di.DaggerAppComponent | ||
| import com.otus.dihomework.di.Dependencies | ||
| import com.otus.dihomework.di.DependenciesProvider | ||
|
|
||
| class ProductsApplication : Application(), DependenciesProvider { | ||
| lateinit var appComponent: AppComponent | ||
| private set | ||
|
|
||
| class ProductsApplication : Application() { | ||
| override fun onCreate() { | ||
| super.onCreate() | ||
| appComponent = DaggerAppComponent.factory().create(this) | ||
| ServiceLocator.init(this) | ||
| } | ||
|
|
||
| override fun getDependencies(): Dependencies { | ||
| return appComponent | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.otus.dihomework.di | ||
|
|
||
| import android.content.Context | ||
| import com.otus.dihomework.common.data.ProductApiService | ||
| import com.otus.dihomework.common.domain_api.ConsumeFavoritesUseCase | ||
| import com.otus.dihomework.common.domain_api.ConsumeProductsUseCase | ||
| import com.otus.dihomework.common.domain_api.ToggleFavoriteUseCase | ||
| import com.otus.dihomework.common.domain_impl.FavoritesRepository | ||
| import com.otus.dihomework.common.domain_impl.ProductRepository | ||
| import com.otus.dihomework.common.util.PriceFormatter | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import okhttp3.OkHttpClient | ||
| import retrofit2.Retrofit | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| @Component(modules = [AppModule::class]) | ||
| interface AppComponent : ProductsDependencies { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. из компонента наружу торчит почти все подряд, это лишнее, по заданию нужно реализовать ProductsComponent
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удалила все ненужное. Оставила:
|
||
| fun context(): Context | ||
| fun okHttpClient(): OkHttpClient | ||
| fun retrofit(): Retrofit | ||
| fun productApiService(): ProductApiService | ||
| fun productRepository(): ProductRepository | ||
| fun favoritesRepository(): FavoritesRepository | ||
| override fun consumeProductsUseCase(): ConsumeProductsUseCase | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. вот эти все оверрайды тут не нужны, можно удалить
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удалила |
||
| fun consumeFavoritesUseCase(): ConsumeFavoritesUseCase | ||
| override fun toggleFavoriteUseCase(): ToggleFavoriteUseCase | ||
| override fun priceFormatter(): PriceFormatter | ||
| fun favoritesComponent(): FavoritesComponent.Factory | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun create(@BindsInstance context: Context): AppComponent | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package com.otus.dihomework.di | ||
|
|
||
| import android.content.Context | ||
| import com.google.gson.GsonBuilder | ||
| import com.otus.dihomework.common.data.FavoritesRepositoryImpl | ||
| import com.otus.dihomework.common.data.ProductApiService | ||
| import com.otus.dihomework.common.data.ProductRepositoryImpl | ||
| import com.otus.dihomework.common.domain_api.ConsumeFavoritesUseCase | ||
| import com.otus.dihomework.common.domain_api.ConsumeProductsUseCase | ||
| import com.otus.dihomework.common.domain_api.ToggleFavoriteUseCase | ||
| import com.otus.dihomework.common.domain_impl.ConsumeFavoritesUseCaseImpl | ||
| import com.otus.dihomework.common.domain_impl.ConsumeProductsUseCaseImpl | ||
| import com.otus.dihomework.common.domain_impl.FavoritesRepository | ||
| import com.otus.dihomework.common.domain_impl.ProductRepository | ||
| import com.otus.dihomework.common.domain_impl.ToggleFavoriteUseCaseImpl | ||
| import com.otus.dihomework.common.util.PriceFormatter | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.logging.HttpLoggingInterceptor | ||
| import retrofit2.Retrofit | ||
| import retrofit2.converter.gson.GsonConverterFactory | ||
| import java.util.concurrent.TimeUnit | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module(includes = [SubcomponentsModule::class]) | ||
| object AppModule { | ||
| @Provides | ||
| @Singleton | ||
| fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor { | ||
| return HttpLoggingInterceptor().apply { | ||
| level = HttpLoggingInterceptor.Level.BODY | ||
| } | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideOkHttpClient( | ||
| httpLoggingInterceptor: HttpLoggingInterceptor | ||
| ): OkHttpClient { | ||
| return OkHttpClient.Builder() | ||
| .addInterceptor(httpLoggingInterceptor) | ||
| .connectTimeout(30, TimeUnit.SECONDS) | ||
| .readTimeout(30, TimeUnit.SECONDS) | ||
| .build() | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { | ||
| return Retrofit.Builder() | ||
| .baseUrl("https://otus-android.github.io/") | ||
| .client(okHttpClient) | ||
| .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) | ||
| .build() | ||
| } | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideProductApiService(retrofit: Retrofit): ProductApiService { | ||
| return retrofit.create(ProductApiService::class.java) | ||
| } | ||
|
|
||
| @Provides | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. указание реализаций интерфейса делается через все ниже аналогично
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ой. Переделала |
||
| fun provideProductRepository(): ProductRepository { | ||
| return ProductRepositoryImpl() | ||
| } | ||
|
|
||
| @Provides | ||
| fun provideFavoritesRepository(context: Context): FavoritesRepository { | ||
| return FavoritesRepositoryImpl(context) | ||
| } | ||
|
|
||
| @Provides | ||
| fun provideConsumeProductsUseCase(): ConsumeProductsUseCase { | ||
| return ConsumeProductsUseCaseImpl() | ||
| } | ||
|
|
||
| @Provides | ||
| fun provideConsumeFavoritesUseCase(): ConsumeFavoritesUseCase { | ||
| return ConsumeFavoritesUseCaseImpl() | ||
| } | ||
|
|
||
| @Provides | ||
| fun provideToggleFavoriteUseCase(): ToggleFavoriteUseCase { | ||
| return ToggleFavoriteUseCaseImpl() | ||
| } | ||
|
|
||
| @Provides | ||
| fun providePriceFormatter(): PriceFormatter { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. если у класса нет интерфейса, то его нужно в граф добавлять через
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Слелала. |
||
| return PriceFormatter() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.otus.dihomework.di | ||
|
|
||
| import android.content.Context | ||
|
|
||
| interface Dependencies | ||
|
|
||
| interface DependenciesProvider { | ||
| fun getDependencies(): Dependencies | ||
| } | ||
|
|
||
| inline fun <reified T : Dependencies> Context.findDependencies(): T { | ||
| return (applicationContext as DependenciesProvider).getDependencies() as T | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.otus.dihomework.di | ||
|
|
||
| import com.otus.dihomework.common.di.FeatureScope | ||
| import com.otus.dihomework.features.favorites.FavoritesViewModelFactory | ||
| import dagger.Subcomponent | ||
|
|
||
| @FeatureScope | ||
| @Subcomponent | ||
| interface FavoritesComponent { | ||
| fun viewModelFactory(): FavoritesViewModelFactory | ||
|
|
||
| @Subcomponent.Factory | ||
| interface Factory { | ||
| fun create(): FavoritesComponent | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.otus.dihomework.di | ||
|
|
||
| import com.otus.dihomework.common.domain_api.ConsumeProductsUseCase | ||
| import com.otus.dihomework.common.domain_api.ToggleFavoriteUseCase | ||
| import com.otus.dihomework.common.util.PriceFormatter | ||
|
|
||
| interface ProductsDependencies : Dependencies { | ||
| fun consumeProductsUseCase(): ConsumeProductsUseCase | ||
| fun toggleFavoriteUseCase(): ToggleFavoriteUseCase | ||
| fun priceFormatter(): PriceFormatter | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.otus.dihomework.di | ||
|
|
||
| import dagger.Module | ||
|
|
||
| @Module(subcomponents = [FavoritesComponent::class]) | ||
| object SubcomponentsModule |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,22 +12,33 @@ import androidx.compose.material3.Text | |
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| import com.otus.dihomework.ProductsApplication | ||
| import com.otus.dihomework.R | ||
| import com.otus.dihomework.features.favorites.FavoritesScreenState | ||
| import com.otus.dihomework.features.favorites.FavoritesViewModel | ||
| import com.otus.dihomework.features.favorites.FavoritesViewModelFactory | ||
|
|
||
| @Composable | ||
| fun FavoritesScreenContent( | ||
| modifier: Modifier = Modifier | ||
| ) { | ||
| val context = LocalContext.current | ||
| val viewModelFactory = remember(context) { | ||
| (context.applicationContext as ProductsApplication) | ||
| .appComponent | ||
| .favoritesComponent() | ||
| .create() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. создание компонента и в целом вьюмодели лучше вынести наружу, например в composable навигации
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вынесла в MainNavigation в composable |
||
| .viewModelFactory() | ||
| } | ||
|
|
||
| val viewModel: FavoritesViewModel = viewModel( | ||
| factory = FavoritesViewModelFactory() | ||
| factory = viewModelFactory | ||
| ) | ||
|
|
||
| val state by viewModel.state.collectAsState() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
этот файл вообще больше не нужен, мы его заменяем через Dagger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Удалила