diff --git a/app/build.gradle b/app/build.gradle index 1cdd96f..3965aba 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,6 +3,7 @@ plugins { alias(libs.plugins.kotlinAndroid) alias(libs.plugins.kotlinxSerialization) alias(libs.plugins.kapt) + alias(libs.plugins.compose.compiler) } android { @@ -34,9 +35,20 @@ android { } buildFeatures { viewBinding true + compose true } } +composeCompiler { + reportsDestination.set( + layout.buildDirectory.dir("compose-reports") + ) + + metricsDestination.set( + layout.buildDirectory.dir("compose-metrics") + ) +} + dependencies { implementation project(":common:di") implementation project(":common:formatters") @@ -53,8 +65,6 @@ dependencies { implementation libs.lifecycle.viewmodel.ktx implementation libs.fragment.ktx implementation libs.lifecycle.runtime.ktx - implementation libs.navigation.fragment.ktx - implementation libs.navigation.ui.ktx implementation libs.coil implementation libs.gson implementation libs.bundles.network @@ -63,6 +73,17 @@ dependencies { implementation libs.androidx.datastore implementation libs.androidx.datastore.preferences + implementation libs.androidx.foundation + implementation libs.androidx.foundation.layout + implementation libs.androidx.material3 + implementation libs.androidx.activity.compose + implementation libs.androidx.lifecycle.viewmodel.compose + implementation libs.androidx.lifecycle.runtime.compose + + implementation libs.compose.coil + + implementation libs.androidx.navigation.compose + implementation libs.dagger kapt libs.daggerCompiler diff --git a/app/src/main/java/ru/otus/marketsample/BottomBar.kt b/app/src/main/java/ru/otus/marketsample/BottomBar.kt new file mode 100644 index 0000000..1278c7b --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/BottomBar.kt @@ -0,0 +1,48 @@ +package ru.otus.marketsample + +import androidx.compose.material3.Icon +import androidx.compose.material3.NavigationBar +import androidx.compose.material3.NavigationBarItem +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.painterResource +import androidx.navigation.NavController +import ru.otus.common.ui.R + +@Composable +fun BottomBar( + navController: NavController +){ + NavigationBar { + NavigationBarItem( + selected = true, + onClick = { + navController.navigate("products") + }, + icon = { + Icon( + painter = painterResource(R.drawable.ic_list), + contentDescription = "Products" + ) + }, + label = { + Text("Products") + } + ) + NavigationBarItem( + selected = false, + onClick = { + navController.navigate("promo") + }, + icon = { + Icon( + painter = painterResource(R.drawable.ic_discount), + contentDescription = "Promo" + ) + }, + label = { + Text("Promo") + } + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/MainActivity.kt b/app/src/main/java/ru/otus/marketsample/MainActivity.kt index 7e34aaf..096fe45 100644 --- a/app/src/main/java/ru/otus/marketsample/MainActivity.kt +++ b/app/src/main/java/ru/otus/marketsample/MainActivity.kt @@ -3,24 +3,42 @@ package ru.otus.marketsample import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat -import ru.otus.marketsample.databinding.ActivityMainBinding +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Scaffold +import androidx.compose.ui.Modifier +import androidx.navigation.compose.currentBackStackEntryAsState +import androidx.navigation.compose.rememberNavController +import ru.otus.marketsample.navigation.CreateGraph class MainActivity : AppCompatActivity() { - private lateinit var binding: ActivityMainBinding - override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) - binding = ActivityMainBinding.inflate(layoutInflater) - setContentView(binding.root) - - ViewCompat.setOnApplyWindowInsetsListener(binding.container) { view, insets -> - val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) - view.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) - insets + + setContent { + val navController = rememberNavController() + + val currentRoute = navController + .currentBackStackEntryAsState() + .value + ?.destination + ?.route + + Scaffold( + bottomBar = { + if (currentRoute != "details/{productId}") { + BottomBar(navController = navController) + } + } + ) { padding -> + CreateGraph( + navController = navController, + appComponent = (application as MarketSampleApp).appComponent, + modifier = Modifier.padding(padding) + ) + } } } } \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/MainFragment.kt b/app/src/main/java/ru/otus/marketsample/MainFragment.kt deleted file mode 100644 index d03161e..0000000 --- a/app/src/main/java/ru/otus/marketsample/MainFragment.kt +++ /dev/null @@ -1,49 +0,0 @@ -package ru.otus.marketsample - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat -import androidx.fragment.app.Fragment -import androidx.navigation.Navigation.findNavController -import com.google.android.material.bottomnavigation.BottomNavigationView -import ru.otus.marketsample.databinding.FragmentMainBinding - -class MainFragment : Fragment() { - - private var _binding: FragmentMainBinding? = null - private val binding get() = _binding!! - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentMainBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - val navView: BottomNavigationView = binding.navView - - navView.setOnItemSelectedListener { - findNavController(binding.navHostFragmentMain).navigate(it.itemId) - true - } - - ViewCompat.setOnApplyWindowInsetsListener(binding.container) { view, insets -> - val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) - view.setPadding(systemBars.left, 0, systemBars.right, 0) - insets - } - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/details/feature/DetailsFragment.kt b/app/src/main/java/ru/otus/marketsample/details/feature/DetailsFragment.kt deleted file mode 100644 index e23c57e..0000000 --- a/app/src/main/java/ru/otus/marketsample/details/feature/DetailsFragment.kt +++ /dev/null @@ -1,125 +0,0 @@ -package ru.otus.marketsample.details.feature - -import android.content.Context -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.Toast -import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle -import coil.load -import kotlinx.coroutines.launch -import ru.otus.common.di.findDependencies -import ru.otus.marketsample.details.feature.di.DaggerDetailsComponent -import ru.otus.marketsample.R -import ru.otus.marketsample.databinding.FragmentDetailsBinding -import javax.inject.Inject - -class DetailsFragment : Fragment() { - - private var _binding: FragmentDetailsBinding? = null - private val binding get() = _binding!! - - @Inject - lateinit var factory: DetailsViewModelFactory - - private val viewModel: DetailsViewModel by viewModels( - factoryProducer = { factory } - ) - - private val productId by lazy { arguments?.getString("productId")!! } - - override fun onAttach(context: Context) { - super.onAttach(context) - - DaggerDetailsComponent.factory() - .create( - dependencies = findDependencies(), - productId = productId, - ) - .inject(this) - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentDetailsBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - subscribeUI() - } - - private fun subscribeUI() { - viewLifecycleOwner.lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.STARTED) { - launch { - viewModel.state.collect { state -> - when { - state.isLoading -> showLoading() - state.hasError -> { - Toast.makeText( - requireContext(), - "Error wile loading data", - Toast.LENGTH_SHORT - ).show() - - viewModel.errorHasShown() - } - - else -> showProduct(detailsState = state.detailsState) - } - } - } - } - } - } - - private fun showLoading() { - hideAll() - binding.progress.visibility = View.VISIBLE - } - - private fun showProduct(detailsState: DetailsState) { - hideAll() - binding.image.load(detailsState.image) - binding.image.visibility = View.VISIBLE - - binding.name.text = detailsState.name - binding.name.visibility = View.VISIBLE - - binding.price.text = getString(R.string.price_with_arg, detailsState.price) - binding.price.visibility = View.VISIBLE - - if (detailsState.hasDiscount) { - binding.promo.visibility = View.VISIBLE - binding.promo.text = detailsState.discount - } else { - binding.promo.visibility = View.GONE - } - - binding.addToCart.visibility = View.VISIBLE - } - - private fun hideAll() { - binding.progress.visibility = View.GONE - binding.image.visibility = View.GONE - binding.name.visibility = View.GONE - binding.price.visibility = View.GONE - binding.progress.visibility = View.GONE - binding.addToCart.visibility = View.GONE - } -} diff --git a/app/src/main/java/ru/otus/marketsample/details/feature/DetailsItem.kt b/app/src/main/java/ru/otus/marketsample/details/feature/DetailsItem.kt new file mode 100644 index 0000000..969ebcb --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/details/feature/DetailsItem.kt @@ -0,0 +1,80 @@ +package ru.otus.marketsample.details.feature + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import coil.compose.AsyncImage +import ru.otus.common.ui.R +import ru.otus.marketsample.products.feature.DiscountBadge + +@Composable +fun DetailsItem( + details: DetailsState, + modifier: Modifier = Modifier +) { + Column( + modifier = modifier + .fillMaxSize() + .background(Color.White) + ) { + AsyncImage( + model = details.image, + contentDescription = details.name, + contentScale = ContentScale.Crop, + modifier = Modifier + .fillMaxWidth() + .height(300.dp) + ) + + Text( + text = details.name, + fontSize = 24.sp, + color = Color.Black, + modifier = Modifier.padding(16.dp) + ) + + if (details.hasDiscount) { + DiscountBadge( + discountText = details.discount, + modifier = Modifier + .align(Alignment.End) + .padding(8.dp) + ) + } + + Text( + text = details.price, + color = colorResource(id = R.color.purple_500), + fontSize = 18.sp, + modifier = Modifier + .padding(14.dp) + .align(Alignment.End) + ) + + Button( + onClick = { }, + modifier = Modifier + .padding(10.dp) + .align(Alignment.End) + ) { + Text( + text = "Add to cart", + fontSize = 18.sp, + modifier = Modifier.padding(14.dp) + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/details/feature/DetailsScreen.kt b/app/src/main/java/ru/otus/marketsample/details/feature/DetailsScreen.kt new file mode 100644 index 0000000..35ddd28 --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/details/feature/DetailsScreen.kt @@ -0,0 +1,55 @@ +package ru.otus.marketsample.details.feature + +import android.widget.Toast +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.pulltorefresh.PullToRefreshBox +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.lifecycle.compose.collectAsStateWithLifecycle + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun DetailsScreen( + viewModel: DetailsViewModel, + modifier: Modifier = Modifier +) { + val context = LocalContext.current + val state by viewModel.state.collectAsStateWithLifecycle() + + with(state) { + LaunchedEffect(key1 = hasError) { + if (hasError) { + Toast.makeText( + context, + "Error while loading data", + Toast.LENGTH_SHORT + ).show() + + viewModel.errorHasShown() + } + } + + PullToRefreshBox( + isRefreshing = isLoading, + onRefresh = {}, + modifier = modifier.fillMaxSize() + ) { + Box( + modifier = Modifier.fillMaxSize() + ) { + if (this@with.isLoading) { + CircularProgressIndicator(modifier = Modifier.align(Alignment.Center)) + } else { + DetailsItem(details = detailsState) + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/details/feature/di/DetailsComponent.kt b/app/src/main/java/ru/otus/marketsample/details/feature/di/DetailsComponent.kt index 8eecd0b..933738b 100644 --- a/app/src/main/java/ru/otus/marketsample/details/feature/di/DetailsComponent.kt +++ b/app/src/main/java/ru/otus/marketsample/details/feature/di/DetailsComponent.kt @@ -3,8 +3,8 @@ package ru.otus.marketsample.details.feature.di import dagger.BindsInstance import dagger.Component import ru.otus.common.data.products.ProductRepository -import ru.otus.marketsample.details.feature.DetailsFragment import ru.otus.common.di.FeatureScope +import ru.otus.marketsample.details.feature.DetailsViewModelFactory import javax.inject.Named @FeatureScope @@ -19,7 +19,7 @@ interface DetailsComponent { ): DetailsComponent } - fun inject(detailsFragment: DetailsFragment) + fun getViewModelFactory(): DetailsViewModelFactory } interface DetailsComponentDependencies { diff --git a/app/src/main/java/ru/otus/marketsample/navigation/NavGraph.kt b/app/src/main/java/ru/otus/marketsample/navigation/NavGraph.kt new file mode 100644 index 0000000..659a741 --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/navigation/NavGraph.kt @@ -0,0 +1,90 @@ +package ru.otus.marketsample.navigation + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavHostController +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import ru.otus.marketsample.details.feature.DetailsScreen +import ru.otus.marketsample.details.feature.DetailsViewModel +import ru.otus.marketsample.details.feature.di.DaggerDetailsComponent +import ru.otus.marketsample.di.AppComponent +import ru.otus.marketsample.products.feature.ProductListScreen +import ru.otus.marketsample.products.feature.ProductListViewModel +import ru.otus.marketsample.products.feature.di.DaggerProductListComponent +import ru.otus.marketsample.promo.feature.PromoListScreen +import ru.otus.marketsample.promo.feature.PromoListViewModel +import ru.otus.marketsample.promo.feature.di.DaggerPromoComponent + +@Composable +fun CreateGraph( + navController: NavHostController, + appComponent: AppComponent, + modifier: Modifier = Modifier +) { + NavHost( + navController = navController, + startDestination = "products" + ) { + composable("products") { + val productListComponent = remember { + DaggerProductListComponent.factory().create(appComponent) + } + + val productListViewModelFactory = remember { + productListComponent.getViewModelFactory() + } + + val productListViewModel: ProductListViewModel = + viewModel(factory = productListViewModelFactory) + + ProductListScreen( + viewModel = productListViewModel, + onClick = { productId -> + navController.navigate("details/$productId") + } + ) + } + + composable("promo") { + val promoListComponent = remember { + DaggerPromoComponent.factory().create(appComponent) + } + + val promoListViewModelFactory = remember { + promoListComponent.getViewModelFactory() + } + + val promoListViewModel: PromoListViewModel = + viewModel(factory = promoListViewModelFactory) + + PromoListScreen( + viewModel = promoListViewModel + ) + } + + composable("details/{productId}") { backStackEntry -> + val productId = backStackEntry.arguments?.getString("productId") ?: "" + + val detailsComponent = remember { + DaggerDetailsComponent.factory().create( + dependencies = appComponent, + productId = productId + ) + } + + val detailsViewModelFactory = remember { + detailsComponent.getViewModelFactory() + } + + val detailsViewModel: DetailsViewModel = + viewModel(factory = detailsViewModelFactory) + + DetailsScreen( + viewModel = detailsViewModel + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/products/feature/ProductItem.kt b/app/src/main/java/ru/otus/marketsample/products/feature/ProductItem.kt new file mode 100644 index 0000000..48db2df --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/products/feature/ProductItem.kt @@ -0,0 +1,190 @@ +package ru.otus.marketsample.products.feature + +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.colorResource +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import coil.compose.AsyncImage + +@Composable +fun ProductItem( + product: ProductState, + onClick: (String) -> Unit, + modifier: Modifier = Modifier +) { + Row( + modifier = modifier + .fillMaxSize() + .clickable { + onClick(product.id) + } + .padding( + horizontal = 16.dp, + vertical = 24.dp + ), + verticalAlignment = Alignment.CenterVertically + ) { + ProductImage( + product = product, + modifier = Modifier.weight(1f) + ) + + Spacer( + modifier = Modifier.width(12.dp) + ) + + ProductInfo( + product = product, + modifier = Modifier.weight(1f) + ) + + } +} + +@Composable +private fun ProductImage( + product: ProductState, + modifier: Modifier = Modifier +) { + Box( + modifier = modifier.height(130.dp) + ) { + AsyncImage( + model = product.image, + contentDescription = product.name, + contentScale = ContentScale.Crop, + modifier = Modifier + .fillMaxSize() + .clip(RoundedCornerShape(12.dp)), + ) + + if (product.hasDiscount) { + DiscountBadge( + discountText = product.discount, + modifier = Modifier + .align(Alignment.TopEnd) + .padding(8.dp) + ) + } + } +} + +@Composable +fun DiscountBadge( + discountText: String, + modifier: Modifier = Modifier +) { + Box( + modifier = modifier + .border( + width = 2.dp, + color = Color.White, + shape = RoundedCornerShape( + topEnd = 10.dp, + topStart = 40.dp, + bottomStart = 40.dp, + bottomEnd = 40.dp + ) + ) + .background( + brush = Brush.linearGradient( + colors = listOf( + colorResource(id = ru.otus.common.ui.R.color.purple_500), + colorResource(id = ru.otus.common.ui.R.color.purple_200) + ) + ), + shape = RoundedCornerShape( + topEnd = 10.dp, + topStart = 40.dp, + bottomStart = 40.dp, + bottomEnd = 40.dp + ) + ) + ) { + Text( + text = discountText, + color = Color.White, + fontWeight = FontWeight.Bold, + fontSize = 14.sp, + modifier = Modifier + .padding( + horizontal = 10.dp, + vertical = 4.dp + ) + ) + } +} + +@Composable +private fun ProductInfo( + product: ProductState, + modifier: Modifier = Modifier +) { + Column( + modifier = modifier.height(130.dp), + verticalArrangement = Arrangement.SpaceBetween + ) { + Text( + text = product.name, + fontSize = 18.sp, + maxLines = 2, + color = Color.Black, + fontFamily = FontFamily.SansSerif, + overflow = TextOverflow.Ellipsis + ) + + PriceChip( + price = product.price, + modifier = Modifier.align(Alignment.End) + ) + } +} + +@Composable +private fun PriceChip( + price: String, + modifier: Modifier = Modifier +) { + Box( + modifier = modifier + .background( + color = colorResource(id = ru.otus.common.ui.R.color.beige), + shape = RoundedCornerShape(8.dp) + ) + ) { + Text( + text = price, + color = colorResource(id = ru.otus.common.ui.R.color.purple_500), + fontWeight = FontWeight.Bold, + fontSize = 16.sp, + modifier = Modifier + .padding( + horizontal = 12.dp, + vertical = 8.dp + ) + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/products/feature/ProductListFragment.kt b/app/src/main/java/ru/otus/marketsample/products/feature/ProductListFragment.kt deleted file mode 100644 index 88f7ec0..0000000 --- a/app/src/main/java/ru/otus/marketsample/products/feature/ProductListFragment.kt +++ /dev/null @@ -1,117 +0,0 @@ -package ru.otus.marketsample.products.feature - -import android.content.Context -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.Toast -import androidx.core.os.bundleOf -import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle -import androidx.navigation.findNavController -import androidx.recyclerview.widget.LinearLayoutManager -import kotlinx.coroutines.launch -import ru.otus.marketsample.MarketSampleApp -import ru.otus.marketsample.R -import ru.otus.marketsample.databinding.FragmentProductListBinding -import ru.otus.marketsample.products.feature.adapter.ProductsAdapter -import ru.otus.marketsample.products.feature.di.DaggerProductListComponent -import javax.inject.Inject - -class ProductListFragment : Fragment() { - - private var _binding: FragmentProductListBinding? = null - private val binding get() = _binding!! - - @Inject - lateinit var factory: ProductListViewModelFactory - - private val viewModel: ProductListViewModel by viewModels { factory } - - override fun onAttach(context: Context) { - super.onAttach(context) - - val appComponent = (activity?.applicationContext as MarketSampleApp).appComponent - - DaggerProductListComponent.factory() - .create(appComponent) - .inject(this) - } - - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentProductListBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - binding.recyclerView.adapter = ProductsAdapter( - onItemClicked = { productId -> - requireActivity().findNavController(R.id.nav_host_activity_main) - .navigate( - resId = R.id.action_main_to_details, - args = bundleOf("productId" to productId), - ) - } - ) - binding.recyclerView.layoutManager = LinearLayoutManager(context) - - binding.swipeRefreshLayout.setOnRefreshListener { - viewModel.refresh() - } - - subscribeUI() - } - - private fun subscribeUI() { - viewLifecycleOwner.lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.STARTED) { - launch { - viewModel.state.collect { state -> - when { - state.isLoading -> showLoading() - state.hasError -> { - Toast.makeText( - requireContext(), - "Error wile loading data", - Toast.LENGTH_SHORT - ).show() - - viewModel.errorHasShown() - } - - else -> showProductList(productListState = state.productListState) - } - } - } - } - } - } - - private fun showProductList(productListState: List) { - binding.progress.visibility = View.GONE - binding.recyclerView.visibility = View.VISIBLE - (binding.recyclerView.adapter as ProductsAdapter).submitList(productListState) - binding.swipeRefreshLayout.isRefreshing = false - } - - private fun showLoading() { - binding.progress.visibility = View.VISIBLE - binding.recyclerView.visibility = View.GONE - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} diff --git a/app/src/main/java/ru/otus/marketsample/products/feature/ProductListScreen.kt b/app/src/main/java/ru/otus/marketsample/products/feature/ProductListScreen.kt new file mode 100644 index 0000000..f145e58 --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/products/feature/ProductListScreen.kt @@ -0,0 +1,66 @@ +package ru.otus.marketsample.products.feature + +import android.widget.Toast +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.pulltorefresh.PullToRefreshBox +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.lifecycle.compose.collectAsStateWithLifecycle + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun ProductListScreen( + viewModel: ProductListViewModel, + onClick: (String) -> Unit, + modifier: Modifier = Modifier +) { + val context = LocalContext.current + + val state by viewModel.state.collectAsStateWithLifecycle() + + with(state) { + LaunchedEffect(key1 = hasError) { + if (hasError) { + Toast.makeText( + context, + "Error while loading data", + Toast.LENGTH_SHORT + ).show() + + viewModel.errorHasShown() + } + } + + PullToRefreshBox( + isRefreshing = isLoading, + onRefresh = {}, + modifier = modifier.fillMaxSize() + ) { + if (this@with.isLoading) { + CircularProgressIndicator(modifier = Modifier.align(Alignment.Center)) + } else { + LazyColumn( + modifier = Modifier.fillMaxSize() + ) { + items( + items = productListState, + key = { product -> product.id } + ) { product -> + ProductItem( + product = product, + onClick = onClick + ) + } + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/products/feature/adapter/ProductHolder.kt b/app/src/main/java/ru/otus/marketsample/products/feature/adapter/ProductHolder.kt deleted file mode 100644 index f216b25..0000000 --- a/app/src/main/java/ru/otus/marketsample/products/feature/adapter/ProductHolder.kt +++ /dev/null @@ -1,32 +0,0 @@ -package ru.otus.marketsample.products.feature.adapter - -import android.view.View.GONE -import android.view.View.VISIBLE -import androidx.recyclerview.widget.RecyclerView -import coil.load -import ru.otus.marketsample.R -import ru.otus.marketsample.databinding.ItemProductBinding -import ru.otus.marketsample.products.feature.ProductState - -class ProductHolder( - private val binding: ItemProductBinding, - private val onItemClicked: (String) -> Unit, -) : RecyclerView.ViewHolder(binding.root) { - - fun bind(productState: ProductState) { - binding.image.load(productState.image) - binding.name.text = productState.name - binding.price.text = - binding.root.resources.getString(R.string.price_with_arg, productState.price) - if (productState.hasDiscount) { - binding.promo.visibility = VISIBLE - binding.promo.text = productState.discount - } else { - binding.promo.visibility = GONE - } - - binding.root.setOnClickListener { - onItemClicked(productState.id) - } - } -} diff --git a/app/src/main/java/ru/otus/marketsample/products/feature/adapter/ProductsAdapter.kt b/app/src/main/java/ru/otus/marketsample/products/feature/adapter/ProductsAdapter.kt deleted file mode 100644 index 18354a2..0000000 --- a/app/src/main/java/ru/otus/marketsample/products/feature/adapter/ProductsAdapter.kt +++ /dev/null @@ -1,44 +0,0 @@ -package ru.otus.marketsample.products.feature.adapter - -import android.view.LayoutInflater -import android.view.ViewGroup -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.ListAdapter -import ru.otus.common.di.FeatureScope -import ru.otus.marketsample.databinding.ItemProductBinding -import ru.otus.marketsample.products.feature.ProductState -import javax.inject.Inject - -@FeatureScope -class ProductsAdapter @Inject constructor( - private val onItemClicked: (String) -> Unit, -) : - ListAdapter(DiffCallback()) { - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductHolder { - return ProductHolder( - binding = ItemProductBinding.inflate( - LayoutInflater.from(parent.context), parent, false - ), - onItemClicked = onItemClicked, - ) - } - - override fun onBindViewHolder(holder: ProductHolder, position: Int) { - val entity = getItem(position) - entity?.let { - holder.bind(entity) - } - } -} - -private class DiffCallback : DiffUtil.ItemCallback() { - - override fun areItemsTheSame(oldItem: ProductState, newItem: ProductState): Boolean { - return oldItem.id == newItem.id - } - - override fun areContentsTheSame(oldItem: ProductState, newItem: ProductState): Boolean { - return oldItem == newItem - } -} diff --git a/app/src/main/java/ru/otus/marketsample/products/feature/di/ProductListComponent.kt b/app/src/main/java/ru/otus/marketsample/products/feature/di/ProductListComponent.kt index 2b4c9fd..20702d8 100644 --- a/app/src/main/java/ru/otus/marketsample/products/feature/di/ProductListComponent.kt +++ b/app/src/main/java/ru/otus/marketsample/products/feature/di/ProductListComponent.kt @@ -4,7 +4,7 @@ import dagger.Component import ru.otus.common.data.products.ProductRepository import ru.otus.common.data.promo.PromoRepository import ru.otus.common.di.FeatureScope -import ru.otus.marketsample.products.feature.ProductListFragment +import ru.otus.marketsample.products.feature.ProductListViewModelFactory @FeatureScope @Component(dependencies = [ProductListComponentDependencies::class]) @@ -17,7 +17,7 @@ interface ProductListComponent { ): ProductListComponent } - fun inject(productListFragment: ProductListFragment) + fun getViewModelFactory(): ProductListViewModelFactory } interface ProductListComponentDependencies { diff --git a/app/src/main/java/ru/otus/marketsample/promo/feature/PromoItem.kt b/app/src/main/java/ru/otus/marketsample/promo/feature/PromoItem.kt new file mode 100644 index 0000000..fbf45d4 --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/promo/feature/PromoItem.kt @@ -0,0 +1,70 @@ +package ru.otus.marketsample.promo.feature + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import coil.compose.AsyncImage + +@Composable +fun PromoItem( + promo: PromoState, + modifier: Modifier = Modifier +) { + Box( + modifier = modifier + .fillMaxWidth() + .padding(10.dp) + ) { + AsyncImage( + model = promo.image, + contentDescription = promo.name, + contentScale = ContentScale.Crop, + modifier = Modifier + .fillMaxWidth() + .height(250.dp), + ) + Box( + modifier = Modifier + .fillMaxWidth() + .height(100.dp) + .align(Alignment.BottomCenter) + .background( + brush = Brush.verticalGradient( + colors = listOf( + Color.Transparent, + Color.Black.copy(alpha = 0.67f) + ) + ) + ) + ) + Column( + modifier = Modifier + .align(Alignment.BottomStart) + .padding(10.dp) + ) { + Text( + text = promo.name, + color = Color.White, + fontSize = 24.sp + ) + + Text( + text = promo.description, + color = Color.White, + fontSize = 14.sp + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/promo/feature/PromoListFragment.kt b/app/src/main/java/ru/otus/marketsample/promo/feature/PromoListFragment.kt deleted file mode 100644 index 2e4f533..0000000 --- a/app/src/main/java/ru/otus/marketsample/promo/feature/PromoListFragment.kt +++ /dev/null @@ -1,106 +0,0 @@ -package ru.otus.marketsample.promo.feature - -import android.content.Context -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.Toast -import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle -import androidx.recyclerview.widget.LinearLayoutManager -import kotlinx.coroutines.launch -import ru.otus.common.di.findDependencies -import ru.otus.marketsample.databinding.FragmentPromoListBinding -import ru.otus.marketsample.promo.feature.adapter.PromoAdapter -import ru.otus.marketsample.promo.feature.di.DaggerPromoComponent -import javax.inject.Inject - -class PromoListFragment : Fragment() { - - private var _binding: FragmentPromoListBinding? = null - private val binding get() = _binding!! - - @Inject - lateinit var adapter: PromoAdapter - - @Inject - lateinit var factory: PromoListViewModelFactory - - private val viewModel: PromoListViewModel by viewModels { factory } - - override fun onAttach(context: Context) { - super.onAttach(context) - - DaggerPromoComponent.factory() - .create(dependencies = findDependencies()) - .inject(this) - } - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentPromoListBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - binding.recyclerView.adapter = adapter - binding.recyclerView.layoutManager = LinearLayoutManager(context) - - binding.swipeRefreshLayout.setOnRefreshListener { - viewModel.refresh() - } - - subscribeUI() - } - - private fun subscribeUI() { - viewLifecycleOwner.lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.STARTED) { - launch { - viewModel.state.collect { state -> - when { - state.isLoading -> showLoading() - state.hasError -> { - Toast.makeText( - requireContext(), - "Error wile loading data", - Toast.LENGTH_SHORT - ).show() - - viewModel.errorHasShown() - } - - else -> showPromoList(promoListState = state.promoListState) - } - } - } - } - } - } - - private fun showPromoList(promoListState: List) { - binding.progress.visibility = View.GONE - binding.recyclerView.visibility = View.VISIBLE - adapter.submitList(promoListState) - binding.swipeRefreshLayout.isRefreshing = false - } - - private fun showLoading() { - binding.progress.visibility = View.VISIBLE - binding.recyclerView.visibility = View.GONE - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} diff --git a/app/src/main/java/ru/otus/marketsample/promo/feature/PromoListScreen.kt b/app/src/main/java/ru/otus/marketsample/promo/feature/PromoListScreen.kt new file mode 100644 index 0000000..5f747ed --- /dev/null +++ b/app/src/main/java/ru/otus/marketsample/promo/feature/PromoListScreen.kt @@ -0,0 +1,64 @@ +package ru.otus.marketsample.promo.feature + +import android.widget.Toast +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.pulltorefresh.PullToRefreshBox +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.lifecycle.compose.collectAsStateWithLifecycle + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun PromoListScreen( + viewModel: PromoListViewModel, + modifier: Modifier = Modifier +) { + val context = LocalContext.current + + val state by viewModel.state.collectAsStateWithLifecycle() + + with(state) { + LaunchedEffect(key1 = hasError) { + if (hasError) { + Toast.makeText( + context, + "Error while loading data", + Toast.LENGTH_SHORT + ).show() + + viewModel.errorHasShown() + } + } + + PullToRefreshBox( + isRefreshing = isLoading, + onRefresh = {}, + modifier = modifier.fillMaxSize() + ) { + if (this@with.isLoading) { + CircularProgressIndicator(modifier = Modifier.align(Alignment.Center)) + } else { + LazyColumn( + modifier = Modifier.fillMaxSize() + ) { + items( + items = promoListState, + key = { product -> product.id } + ) { promo -> + PromoItem( + promo = promo + ) + } + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/otus/marketsample/promo/feature/adapter/PromoAdapter.kt b/app/src/main/java/ru/otus/marketsample/promo/feature/adapter/PromoAdapter.kt deleted file mode 100644 index 0f6b562..0000000 --- a/app/src/main/java/ru/otus/marketsample/promo/feature/adapter/PromoAdapter.kt +++ /dev/null @@ -1,40 +0,0 @@ -package ru.otus.marketsample.promo.feature.adapter - -import android.view.LayoutInflater -import android.view.ViewGroup -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.ListAdapter -import ru.otus.common.di.FeatureScope -import ru.otus.marketsample.databinding.ItemPromoBinding -import ru.otus.marketsample.promo.feature.PromoState -import javax.inject.Inject - -@FeatureScope -class PromoAdapter @Inject constructor() : ListAdapter(DiffCallback()) { - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PromoHolder { - return PromoHolder( - ItemPromoBinding.inflate( - LayoutInflater.from(parent.context), parent, false - ) - ) - } - - override fun onBindViewHolder(holder: PromoHolder, position: Int) { - val entity = getItem(position) - entity?.let { - holder.bind(entity) - } - } -} - -private class DiffCallback : DiffUtil.ItemCallback() { - - override fun areItemsTheSame(oldItem: PromoState, newItem: PromoState): Boolean { - return oldItem.id == newItem.id - } - - override fun areContentsTheSame(oldItem: PromoState, newItem: PromoState): Boolean { - return oldItem == newItem - } -} diff --git a/app/src/main/java/ru/otus/marketsample/promo/feature/adapter/PromoHolder.kt b/app/src/main/java/ru/otus/marketsample/promo/feature/adapter/PromoHolder.kt deleted file mode 100644 index 5d08f5d..0000000 --- a/app/src/main/java/ru/otus/marketsample/promo/feature/adapter/PromoHolder.kt +++ /dev/null @@ -1,17 +0,0 @@ -package ru.otus.marketsample.promo.feature.adapter - -import androidx.recyclerview.widget.RecyclerView -import coil.load -import ru.otus.marketsample.databinding.ItemPromoBinding -import ru.otus.marketsample.promo.feature.PromoState - -class PromoHolder( - private val binding: ItemPromoBinding, -) : RecyclerView.ViewHolder(binding.root) { - - fun bind(promoState: PromoState) { - binding.image.load(promoState.image) - binding.name.text = promoState.name - binding.description.text = promoState.description - } -} diff --git a/app/src/main/java/ru/otus/marketsample/promo/feature/di/PromoComponent.kt b/app/src/main/java/ru/otus/marketsample/promo/feature/di/PromoComponent.kt index b1ad582..3cfd563 100644 --- a/app/src/main/java/ru/otus/marketsample/promo/feature/di/PromoComponent.kt +++ b/app/src/main/java/ru/otus/marketsample/promo/feature/di/PromoComponent.kt @@ -3,7 +3,7 @@ package ru.otus.marketsample.promo.feature.di import dagger.Component import ru.otus.common.data.promo.PromoRepository import ru.otus.common.di.FeatureScope -import ru.otus.marketsample.promo.feature.PromoListFragment +import ru.otus.marketsample.promo.feature.PromoListViewModelFactory @FeatureScope @Component(dependencies = [PromoComponentDependencies::class]) @@ -14,7 +14,7 @@ interface PromoComponent { fun create(dependencies: PromoComponentDependencies): PromoComponent } - fun inject(productFragment: PromoListFragment) + fun getViewModelFactory(): PromoListViewModelFactory } interface PromoComponentDependencies { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml deleted file mode 100644 index 9945e95..0000000 --- a/app/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_details.xml b/app/src/main/res/layout/fragment_details.xml deleted file mode 100644 index c37c1f0..0000000 --- a/app/src/main/res/layout/fragment_details.xml +++ /dev/null @@ -1,78 +0,0 @@ - - - - - - - - - - - - - -