Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions app/src/main/java/com/otus/dihomework/MainNavigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import androidx.compose.material.icons.filled.List
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.otus.dihomework.di.DaggerProductsComponent
import com.otus.dihomework.di.ProductsDependencies
import com.otus.dihomework.di.findDependencies
import com.otus.dihomework.features.favorites.ui.FavoritesScreenContent
import com.otus.dihomework.features.products.ui.ProductsScreenContent

Expand All @@ -29,6 +34,7 @@ sealed class Screen(val route: String, val titleRes: Int, val icon: ImageVector)
fun MainNavigation() {
val navController = rememberNavController()
val screens = listOf(Screen.Products, Screen.Favorites)
val context = LocalContext.current

Scaffold(
topBar = {
Expand Down Expand Up @@ -76,10 +82,22 @@ fun MainNavigation() {
modifier = Modifier.padding(innerPadding)
) {
composable(Screen.Products.route) {
ProductsScreenContent()
val viewModelFactory = remember(context) {
DaggerProductsComponent.factory()
.create(context.findDependencies<ProductsDependencies>())
.viewModelFactory()
}
ProductsScreenContent(viewModelFactory = viewModelFactory)
}
composable(Screen.Favorites.route) {
FavoritesScreenContent()
val viewModelFactory = remember(context) {
(context.applicationContext as ProductsApplication)
.appComponent
.favoritesComponent()
.create()
.viewModelFactory()
}
FavoritesScreenContent(viewModelFactory = viewModelFactory)
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/otus/dihomework/ProductsApplication.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
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()
ServiceLocator.init(this)
appComponent = DaggerAppComponent.factory().create(this)
}

override fun getDependencies(): Dependencies {
return appComponent
}
}
106 changes: 0 additions & 106 deletions app/src/main/java/com/otus/dihomework/ServiceLocator.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import androidx.datastore.preferences.preferencesDataStore
import com.otus.dihomework.common.domain_impl.FavoritesRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "favorites")

class FavoritesRepositoryImpl(
class FavoritesRepositoryImpl @Inject constructor(
private val context: Context
) : FavoritesRepository {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.otus.dihomework.common.data

import com.otus.dihomework.common.domain_api.Product
import javax.inject.Inject

class ProductDomainMapper() {
class ProductDomainMapper @Inject constructor() {

fun fromDto(dto: ProductDto): Product {
return Product(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.otus.dihomework.common.data

import com.otus.dihomework.ServiceLocator
import com.otus.dihomework.common.data.ProductApiService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class ProductRemoteDataSource() {
private val apiService = ServiceLocator.getProductApiService()

class ProductRemoteDataSource @Inject constructor(
private val apiService: ProductApiService
) {
fun consumeProducts(): Flow<List<ProductDto>> = flow {
emit(apiService.getProducts())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.otus.dihomework.common.data

import com.otus.dihomework.ServiceLocator
import com.otus.dihomework.common.domain_impl.ProductRepository
import com.otus.dihomework.common.domain_api.Product
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class ProductRepositoryImpl() : ProductRepository {

private val remoteDataSource = ServiceLocator.getProductRemoteDataSource()
private val mapper = ServiceLocator.getProductDomainMapper()
class ProductRepositoryImpl @Inject constructor(
private val remoteDataSource: ProductRemoteDataSource,
private val mapper: ProductDomainMapper
) : ProductRepository {

override fun consumeProducts(): Flow<List<Product>> {
return remoteDataSource.consumeProducts()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.otus.dihomework.common.domain_impl

import com.otus.dihomework.ServiceLocator
import com.otus.dihomework.common.domain_api.ConsumeFavoritesUseCase
import com.otus.dihomework.common.domain_api.ProductWithFavorite
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import javax.inject.Inject

class ConsumeFavoritesUseCaseImpl() : ConsumeFavoritesUseCase {

private val productRepository = ServiceLocator.getProductRepository()
private val favoritesRepository = ServiceLocator.getFavoritesRepository()
class ConsumeFavoritesUseCaseImpl @Inject constructor(
private val productRepository: ProductRepository,
private val favoritesRepository: FavoritesRepository
) : ConsumeFavoritesUseCase {

override fun invoke(): Flow<List<ProductWithFavorite>> {
return combine(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.otus.dihomework.common.domain_impl

import com.otus.dihomework.ServiceLocator
import com.otus.dihomework.common.domain_api.ConsumeProductsUseCase
import com.otus.dihomework.common.domain_api.ProductWithFavorite
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class ConsumeProductsUseCaseImpl() : ConsumeProductsUseCase {

private val productRepository = ServiceLocator.getProductRepository()
private val favoritesRepository = ServiceLocator.getFavoritesRepository()
class ConsumeProductsUseCaseImpl @Inject constructor(
private val productRepository: ProductRepository,
private val favoritesRepository: FavoritesRepository
) : ConsumeProductsUseCase {

override fun invoke(): Flow<List<ProductWithFavorite>> {
return combine(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.otus.dihomework.common.domain_impl

import com.otus.dihomework.ServiceLocator
import com.otus.dihomework.common.domain_api.ToggleFavoriteUseCase
import javax.inject.Inject

class ToggleFavoriteUseCaseImpl() : ToggleFavoriteUseCase {

private val favoritesRepository = ServiceLocator.getFavoritesRepository()
class ToggleFavoriteUseCaseImpl @Inject constructor(
private val favoritesRepository: FavoritesRepository
) : ToggleFavoriteUseCase {

override suspend fun invoke(productId: String, isFavorite: Boolean) {
if (isFavorite) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.otus.dihomework.common.util

class PriceFormatter() {
import javax.inject.Inject

class PriceFormatter @Inject constructor() {

fun format(price: Double): String {
return "${price.toInt()} ₽"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/otus/dihomework/di/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.otus.dihomework.di

import android.content.Context
import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@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 favoritesComponent(): FavoritesComponent.Factory

@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context): AppComponent
}
}
Loading