From ec9b8bc9e3c953d110305859ae254e35b882a2e5 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Fri, 17 Jan 2025 13:14:08 +0300 Subject: [PATCH 01/12] add recipe card --- .../kotlin/ru/otus/cookbook/MainActivity.kt | 2 + app/src/main/res/drawable-nodpi/media.png | Bin 0 -> 1276 bytes .../main/res/drawable-nodpi/round_shape.xml | 5 ++ .../main/res/drawable/baseline_close_24.xml | 5 ++ app/src/main/res/layout/activity_main.xml | 15 ++-- app/src/main/res/layout/fragment_cookbook.xml | 26 +++++++ app/src/main/res/layout/item_recipe.xml | 69 ++++++++++++++++++ app/src/main/res/navigation/main_graph.xml | 20 +++++ app/src/main/res/values/strings.xml | 4 + gradle/libs.versions.toml | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 11 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 app/src/main/res/drawable-nodpi/media.png create mode 100644 app/src/main/res/drawable-nodpi/round_shape.xml create mode 100644 app/src/main/res/drawable/baseline_close_24.xml create mode 100644 app/src/main/res/layout/item_recipe.xml create mode 100644 app/src/main/res/navigation/main_graph.xml diff --git a/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt b/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt index 6e524b6..739635a 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt @@ -2,6 +2,7 @@ package ru.otus.cookbook import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.navigation.Navigation import ru.otus.cookbook.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { @@ -12,5 +13,6 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) + } } \ No newline at end of file diff --git a/app/src/main/res/drawable-nodpi/media.png b/app/src/main/res/drawable-nodpi/media.png new file mode 100644 index 0000000000000000000000000000000000000000..a57e1933470d48e376883e52b6d8caf801455870 GIT binary patch literal 1276 zcmV?=~u{+z^Xlj(bx!c<%`%8>biSd5U z?#|APT7_cy1`M8wfPsV&NW=&vVgwQ~0*M%bM2tWpMj#O*kcbgT#0Vs!tiVRA4gJ9o zR7XjHqp=Hx;wk+6RfOy15>!Sxfj2idP&_Na_4PHllLgC;R0R(d z+o>vbpoA{)f*l;A@p$Fe*BTAz57oJ$geuUTT=D1=iKr#JnP94;N)(1F(5Y1T5*B4R z96_Vm1QiHPU~e#hCObU5Qn{$XY&M6m2}K}AU8!u7@;b~Looe_8N+<#`@&-N}1ocJ} zhNIOFpdgSg5GyP;+xz^8oH>i+u%l3_Kzz{ZMIEkutghfad6gtQD5MI+e9!Oe81ADO zg-F#?GgfkdR8!m`1%3E%J*gy~H*5(y*|SSx`jtk)m#F%XPVqfO_&jM-kOWRX;!Q`E?)zECWfx?6}_kR?A z@g~0)v=!K5;%fCe_yNu&md?*TwOO?kh)cfgbAh(Q8dJ@dcrh1vOK`FEo7>~sQ?XSe+V)Yg61a7bvX)G385lQq;uARWmREifMf+D@O zCG>jj1mg1D-`j&A*k>%Rvg_>eDILDWw&nGEdb#UoqW zYRsEghmd>r4E!L%`X$F7-a#TRzPD;C5PKTOA3s1gy8{$(D>ykm@?4eHx~-XF{gjiB zM=TPv;04?YKA(Jo=lMK%MW`oo#Y#wgpxejW0H!2+yE(p$yn+>)Mzh7|5OGTNsX8G> zU7nrf@87=mv~2HUt<%2%#Xp7Z4+hZf_V|1Jh2P3duuj}2OOkhXGO(M=v4)cOM$%o@ zO5iOh4SOb79L4H&1m^~=2w$?4NPmd6oJ_!E8}H2VSn!W$W@CsLfkcczB1Rw)Bany@ mNW=&vVgwQ~0*M%bM1BJ + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/baseline_close_24.xml b/app/src/main/res/drawable/baseline_close_24.xml new file mode 100644 index 0000000..f8ca0c6 --- /dev/null +++ b/app/src/main/res/drawable/baseline_close_24.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 86a5d97..d74dd47 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,13 +7,12 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_cookbook.xml b/app/src/main/res/layout/fragment_cookbook.xml index 77d9ef6..1ff9696 100644 --- a/app/src/main/res/layout/fragment_cookbook.xml +++ b/app/src/main/res/layout/fragment_cookbook.xml @@ -1,6 +1,32 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_recipe.xml b/app/src/main/res/layout/item_recipe.xml new file mode 100644 index 0000000..68e2b1a --- /dev/null +++ b/app/src/main/res/layout/item_recipe.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/navigation/main_graph.xml b/app/src/main/res/navigation/main_graph.xml new file mode 100644 index 0000000..b5b879e --- /dev/null +++ b/app/src/main/res/navigation/main_graph.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8fce1d1..0ed6acf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,7 @@ Cookbook + media + description + Title + C \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 515d8d1..125c195 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -agp = "8.7.3" +agp = "8.8.0" kotlin = "2.1.0" coreKtx = "1.15.0" fragmentKtx = "1.8.5" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c4701d6..aac126a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Sat Nov 30 19:52:55 CET 2024 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From b27a733283bca72c29b054c78c88680b2a4116da Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Fri, 17 Jan 2025 18:35:55 +0300 Subject: [PATCH 02/12] add rcV Recipe --- app/build.gradle.kts | 2 + app/src/main/AndroidManifest.xml | 1 + .../kotlin/ru/otus/cookbook/MainActivity.kt | 8 +- .../cook_recycler_view/CategoryViewHolder.kt | 15 ++++ .../cook_recycler_view/RecipeAdapter.kt | 67 ++++++++++++++++ .../cook_recycler_view/RecipeDiffutils.kt | 15 ++++ .../cook_recycler_view/RecipeViewHolder.kt | 26 +++++++ .../kotlin/ru/otus/cookbook/data/Recipe.kt | 24 +++--- .../ru/otus/cookbook/data/RecipeListItem.kt | 2 +- .../ru/otus/cookbook/ui/CookbookFragment.kt | 14 +++- .../main/res/drawable-nodpi/border_card.xml | 7 ++ app/src/main/res/drawable/divider_rv.xml | 4 + app/src/main/res/layout/activity_main.xml | 15 ++-- .../{item_recipe.xml => category_recipe.xml} | 10 ++- app/src/main/res/layout/fragment_cookbook.xml | 4 +- .../main/res/layout/vh_recipe_category.xml | 17 ++++- app/src/main/res/layout/vh_recipe_item.xml | 76 ++++++++++++++++++- app/src/main/res/values/colors.xml | 2 + app/src/main/res/values/strings.xml | 1 + 19 files changed, 274 insertions(+), 36 deletions(-) create mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/CategoryViewHolder.kt create mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeAdapter.kt create mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeDiffutils.kt create mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt create mode 100644 app/src/main/res/drawable-nodpi/border_card.xml create mode 100644 app/src/main/res/drawable/divider_rv.xml rename app/src/main/res/layout/{item_recipe.xml => category_recipe.xml} (90%) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f3207a6..6933fb3 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -50,4 +50,6 @@ dependencies { implementation(libs.androidx.constraintlayout) testImplementation(libs.junit) testImplementation(libs.kotlin.coroutines.test) + implementation ("com.github.bumptech.glide:glide:4.11.0") + annotationProcessor ("com.github.bumptech.glide:compiler:4.11.0") } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 063f4d1..1d21545 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + (RecipeDiffutils()) { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + + return when (viewType) { + ViewType.CATEGORY.id -> { + CategoryViewHolder( + LayoutInflater.from(parent.context) + .inflate(R.layout.vh_recipe_category, parent, false) + ) + } + + ViewType.RECIPE.id -> { + RecipeViewHolder( + LayoutInflater.from(parent.context) + .inflate(R.layout.vh_recipe_item, parent, false) + ) + } + + else -> throw IllegalArgumentException("wrong type of view") + } + } + + override fun getItemViewType(position: Int): Int { + return when (currentList[position]) { + is RecipeListItem.CategoryItem -> ViewType.CATEGORY.id + is RecipeListItem.RecipeItem -> ViewType.RECIPE.id + else -> -1 + } + } + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + + when (val item = currentList.getOrNull(position)) { + is RecipeListItem.CategoryItem -> { + (holder as CategoryViewHolder).bind(item) + } + + is RecipeListItem.RecipeItem -> { + (holder as RecipeViewHolder).bind(item) + } + + else -> + throw IllegalArgumentException("wrong type of holder") + } + } + + fun changeRecipeItems(newChatList: List) { + this.submitList(newChatList) + currentList + println() + } + + enum class ViewType(val id: Int) { + CATEGORY(0), + RECIPE(1) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeDiffutils.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeDiffutils.kt new file mode 100644 index 0000000..25e2a78 --- /dev/null +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeDiffutils.kt @@ -0,0 +1,15 @@ +package ru.otus.cookbook.cook_recycler_view + +import androidx.recyclerview.widget.DiffUtil +import ru.otus.cookbook.data.RecipeListItem + +class RecipeDiffutils : DiffUtil.ItemCallback() { + + override fun areItemsTheSame(oldItem: RecipeListItem, newItem: RecipeListItem): Boolean { + return oldItem== newItem + } + + override fun areContentsTheSame(oldItem: RecipeListItem, newItem: RecipeListItem): Boolean { + return oldItem == newItem + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt new file mode 100644 index 0000000..1e3547a --- /dev/null +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt @@ -0,0 +1,26 @@ +package ru.otus.cookbook.cook_recycler_view + +import android.view.View +import android.widget.ImageView +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.bumptech.glide.Glide + +import ru.otus.cookbook.R +import ru.otus.cookbook.data.RecipeListItem + +class RecipeViewHolder(private val recipeView: View) : RecyclerView.ViewHolder(recipeView) { + + private val title: TextView by lazy { recipeView.findViewById(R.id.titleRecipe) } + private val description: TextView by lazy { recipeView.findViewById(R.id.titleDescription) } + private val imageRecipe: ImageView by lazy { recipeView.findViewById(R.id.imageRecipe) } + + fun bind(recipeData: RecipeListItem.RecipeItem) { + title.text = recipeData.title + description.text = recipeData.description + + Glide.with(recipeView.context) + .load(recipeData.imageUrl) + .into(imageRecipe) + } +} diff --git a/app/src/main/kotlin/ru/otus/cookbook/data/Recipe.kt b/app/src/main/kotlin/ru/otus/cookbook/data/Recipe.kt index 33a05a8..3be9a62 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/data/Recipe.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/data/Recipe.kt @@ -21,7 +21,7 @@ val cookbook = listOf( title = "Pasta Carbonara", description = "Pasta Carbonara is a classic pasta dish from Rome with eggs, cheese and bacon.", category = RecipeCategory("Pasta"), - imageUrl = "https://en.wikipedia.org/wiki/Carbonara#/media/File:Espaguetis_carbonara.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Espaguetis_carbonara.jpg/1920px-Espaguetis_carbonara.jpg", steps = listOf( "Cook the pasta in a large pot of boiling salted water until al dente.", "Meanwhile, whisk the eggs and Parmesan together in a bowl.", @@ -40,7 +40,7 @@ val cookbook = listOf( title = "Fettuccine_Alfredo", description = "Fettuccine Alfredo is a classic pasta dish from Rome with butter, cream and Parmesan.", category = RecipeCategory("Pasta"), - imageUrl = "https://en.wikipedia.org/wiki/Fettuccine_Alfredo#/media/File:The_Only_Original_Alfredo_Sauce_with_Butter_and_Parmesano-Reggiano_Cheese.png", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/d/dc/The_Only_Original_Alfredo_Sauce_with_Butter_and_Parmesano-Reggiano_Cheese.png", steps = listOf( "Cook the fettuccine in a large pot of boiling salted water until al dente.", "Meanwhile, melt the butter in a large pan over medium heat.", @@ -57,7 +57,7 @@ val cookbook = listOf( title = "Cod in breadcrumbs", description = "Cod in breadcrumbs is a simple and delicious fish dish that is perfect for a quick and easy dinner.", category = RecipeCategory("Fish"), - imageUrl = "https://en.wikipedia.org/wiki/Fish_finger#/media/File:Fishfinger_classic_fried_2.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Fishfinger_classic_fried_2.jpg/1920px-Fishfinger_classic_fried_2.jpg", steps = listOf( "Preheat the oven to 200C/180C Fan/Gas 6.", "Place the cod fillets on a baking tray.", @@ -72,7 +72,7 @@ val cookbook = listOf( title = "Beef Stroganoff", description = "Beef Stroganoff is a classic Russian dish made with beef, mushrooms and sour cream.", category = RecipeCategory("Beef"), - imageUrl = "https://en.wikipedia.org/wiki/Beef_Stroganoff#/media/File:Moscow_(8351271825).jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/95/Moscow_%288351271825%29.jpg/1920px-Moscow_%288351271825%29.jpg", steps = listOf( "Heat the oil in a large pan over medium heat.", "Add the beef and cook until browned.", @@ -89,7 +89,7 @@ val cookbook = listOf( title = "Chicken Tikka Masala", description = "Chicken Tikka Masala is a classic Indian dish made with marinated chicken cooked in a spicy tomato sauce.", category = RecipeCategory("Chicken"), - imageUrl = "https://en.wikipedia.org/wiki/Chicken_tikka_masala#/media/File:Chicken_tikka_masala.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Chicken_tikka_masala.jpg/800px-Chicken_tikka_masala.jpg", steps = listOf( "Marinate the chicken in the yogurt, lemon juice, garlic, ginger, garam masala, turmeric, cumin, coriander, paprika, salt and pepper for at least 1 hour.", "Heat the oil in a large pan over medium heat.", @@ -106,7 +106,7 @@ val cookbook = listOf( title = "Solyanka", description = "Solyanka is a traditional Russian soup made with meat, pickles, olives and capers.", category = RecipeCategory("Soup"), - imageUrl = "https://en.wikipedia.org/wiki/Solyanka#/media/File:Soljanka_food_05.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Soljanka_food_05.jpg/1280px-Soljanka_food_05.jpg", steps = listOf( "Heat the oil in a large pan over medium heat.", "Add the onion, garlic, carrots and celery and cook until softened.", @@ -122,7 +122,7 @@ val cookbook = listOf( title = "Surströmming", description = "Surströmming is a traditional Swedish dish made with fermented herring.", category = RecipeCategory("Fish"), - imageUrl = "https://en.wikipedia.org/wiki/Surstr%C3%B6mming#/media/File:Surstr%C3%B6mming.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/Surströmming.jpg/1280px-Surströmming.jpg", steps = listOf( "Open the can of surströmming outdoors, as the smell is very strong", "Remove the herring from the can and rinse it under cold water", @@ -135,7 +135,7 @@ val cookbook = listOf( title = "Sichuang Hotpot", description = "Sichuang Hotpot is a traditional Chinese dish made with spicy broth, meat, vegetables and noodles.", category = RecipeCategory("Chinese"), - imageUrl = "https://en.wikipedia.org/wiki/Chongqing_hot_pot#/media/File:Chongqing_hotpot_2.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Chongqing_hotpot_2.jpg/1280px-Chongqing_hotpot_2.jpg", steps = listOf( "Prepare the broth by boiling water with Sichuang peppercorns, dried chilies, ginger, garlic, star anise, cinnamon and bay leaves", "Add the meat, vegetables and noodles to the broth and cook until tender", @@ -147,7 +147,7 @@ val cookbook = listOf( title = "Tiramisu", description = "Tiramisu is a classic Italian dessert made with coffee, mascarpone cheese, eggs, sugar and ladyfingers.", category = RecipeCategory("Dessert"), - imageUrl = "https://en.wikipedia.org/wiki/Tiramisu#/media/File:Tiramisu_-_Raffaele_Diomede.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/Tiramisu_-_Raffaele_Diomede.jpg/1280px-Tiramisu_-_Raffaele_Diomede.jpg", steps = listOf( "Brew the coffee and let it cool", "Whisk the egg yolks and sugar together until pale and creamy", @@ -165,7 +165,7 @@ val cookbook = listOf( title = "Borscht", description = "Borscht is a traditional Russian soup made with beets, cabbage, potatoes and beef.", category = RecipeCategory("Soup"), - imageUrl = "https://en.wikipedia.org/wiki/Borscht#/media/File:Borscht_served.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Borscht_served.jpg/1920px-Borscht_served.jpg", steps = listOf( "Heat the oil in a large pan over medium heat.", "Add the onion, garlic, carrots and celery and cook until softened.", @@ -181,7 +181,7 @@ val cookbook = listOf( title = "Peking Duck", description = "Peking Duck is a traditional Chinese dish made with roasted duck, pancakes, hoisin sauce and cucumber.", category = RecipeCategory("Chinese"), - imageUrl = "https://en.wikipedia.org/wiki/Peking_duck#/media/File:Peking_Duck,_2014_(02).jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Peking_Duck%2C_2014_%2802%29.jpg/1280px-Peking_Duck%2C_2014_%2802%29.jpg", steps = listOf( "Prepare the duck by removing the giblets and excess fat", "Rub the duck with a mixture of honey, soy sauce, five-spice powder and salt", @@ -195,7 +195,7 @@ val cookbook = listOf( title = "Sushi", description = "Sushi is a traditional Japanese dish made with vinegared rice, fish, vegetables and seaweed.", category = RecipeCategory("Japanese"), - imageUrl = "https://en.wikipedia.org/wiki/Sushi#/media/File:Sushi_platter.jpg", + imageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Sushi_platter.jpg/1024px-Sushi_platter.jpg", steps = listOf( "Cook the rice and season with rice vinegar, sugar and salt", "Prepare the fish and vegetables by slicing them into thin strips", diff --git a/app/src/main/kotlin/ru/otus/cookbook/data/RecipeListItem.kt b/app/src/main/kotlin/ru/otus/cookbook/data/RecipeListItem.kt index ba9bbe1..8a2ba4c 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/data/RecipeListItem.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/data/RecipeListItem.kt @@ -28,7 +28,7 @@ sealed class RecipeListItem : WithLayoutId { data class CategoryItem(val category: RecipeCategory) : RecipeListItem(), WithLayoutId by CategoryItem { companion object : WithLayoutId { @get:LayoutRes - override val layoutId: Int = R.layout.vh_recipe_category + override val layoutId: Int = R.layout.vh_recipe_item } val name: String get() = category.name diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt index efe6939..011495d 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt @@ -4,18 +4,23 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope +import androidx.recyclerview.widget.DividerItemDecoration import kotlinx.coroutines.launch +import ru.otus.cookbook.cook_recycler_view.RecipeAdapter import ru.otus.cookbook.data.RecipeListItem import ru.otus.cookbook.databinding.FragmentCookbookBinding +import ru.otus.cookbook.R class CookbookFragment : Fragment() { private val binding = FragmentBindingDelegate(this) private val model: CookbookFragmentViewModel by viewModels { CookbookFragmentViewModel.Factory } + private val recipeAdapter by lazy { RecipeAdapter() } override fun onCreateView( inflater: LayoutInflater, @@ -37,10 +42,15 @@ class CookbookFragment : Fragment() { } private fun setupRecyclerView() = binding.withBinding { - // Setup RecyclerView + cardRecipesRcView.adapter = recipeAdapter + + val divider = DividerItemDecoration(this@CookbookFragment.context,DividerItemDecoration.VERTICAL) + divider.setDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.divider_rv)!!) + + cardRecipesRcView.addItemDecoration(divider) } private fun onRecipeListUpdated(recipeList: List) { - // Handle recipe list + recipeAdapter.changeRecipeItems(recipeList) } } \ No newline at end of file diff --git a/app/src/main/res/drawable-nodpi/border_card.xml b/app/src/main/res/drawable-nodpi/border_card.xml new file mode 100644 index 0000000..7d85c96 --- /dev/null +++ b/app/src/main/res/drawable-nodpi/border_card.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/divider_rv.xml b/app/src/main/res/drawable/divider_rv.xml new file mode 100644 index 0000000..03cf7d5 --- /dev/null +++ b/app/src/main/res/drawable/divider_rv.xml @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d74dd47..783430f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,12 +7,13 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_recipe.xml b/app/src/main/res/layout/category_recipe.xml similarity index 90% rename from app/src/main/res/layout/item_recipe.xml rename to app/src/main/res/layout/category_recipe.xml index 68e2b1a..0427c29 100644 --- a/app/src/main/res/layout/item_recipe.xml +++ b/app/src/main/res/layout/category_recipe.xml @@ -5,7 +5,9 @@ android:id="@+id/cardRecipe" android:layout_width="match_parent" android:layout_height="wrap_content" - card_view:cardCornerRadius="15dp"> + card_view:cardCornerRadius="15dp" + android:foreground="@drawable/border_card" + > diff --git a/app/src/main/res/layout/fragment_cookbook.xml b/app/src/main/res/layout/fragment_cookbook.xml index 1ff9696..4cf4bd0 100644 --- a/app/src/main/res/layout/fragment_cookbook.xml +++ b/app/src/main/res/layout/fragment_cookbook.xml @@ -1,6 +1,7 @@ @@ -21,12 +22,13 @@ \ No newline at end of file diff --git a/app/src/main/res/layout/vh_recipe_category.xml b/app/src/main/res/layout/vh_recipe_category.xml index 006fd49..219ccf0 100644 --- a/app/src/main/res/layout/vh_recipe_category.xml +++ b/app/src/main/res/layout/vh_recipe_category.xml @@ -1,6 +1,15 @@ - + - \ No newline at end of file + + + \ No newline at end of file diff --git a/app/src/main/res/layout/vh_recipe_item.xml b/app/src/main/res/layout/vh_recipe_item.xml index 006fd49..621978b 100644 --- a/app/src/main/res/layout/vh_recipe_item.xml +++ b/app/src/main/res/layout/vh_recipe_item.xml @@ -1,6 +1,74 @@ - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index c8524cd..c22dc8c 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -2,4 +2,6 @@ #FF000000 #FFFFFFFF + #00FFFFFF + #525252 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0ed6acf..05c741c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,4 +4,5 @@ description Title C + Fish \ No newline at end of file From 65326bfba71754e683a7c1e0397782626db025c1 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Fri, 17 Jan 2025 18:41:05 +0300 Subject: [PATCH 03/12] fix gradle --- app/build.gradle.kts | 4 ++-- gradle/libs.versions.toml | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6933fb3..0efa4cd 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -50,6 +50,6 @@ dependencies { implementation(libs.androidx.constraintlayout) testImplementation(libs.junit) testImplementation(libs.kotlin.coroutines.test) - implementation ("com.github.bumptech.glide:glide:4.11.0") - annotationProcessor ("com.github.bumptech.glide:compiler:4.11.0") + implementation (libs.glide) + annotationProcessor (libs.compiler) } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 125c195..e121150 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,6 @@ [versions] agp = "8.8.0" +glide = "4.11.0" kotlin = "2.1.0" coreKtx = "1.15.0" fragmentKtx = "1.8.5" @@ -18,6 +19,8 @@ datastore = "1.1.1" [libraries] +compiler = { module = "com.github.bumptech.glide:compiler", version.ref = "glide" } +glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" } kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } kotlin-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" } kotlin-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } From 0866781f897647b7866685b2b963ff6cff9515e1 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Fri, 17 Jan 2025 20:14:29 +0300 Subject: [PATCH 04/12] add onRcViewItemClick --- .../cookbook/cook_recycler_view/RecipeAdapter.kt | 14 ++++++-------- .../cook_recycler_view/RecipeTouchHelper.kt | 11 +++++++++++ .../cook_recycler_view/RecipeViewHolder.kt | 3 +++ .../kotlin/ru/otus/cookbook/ui/CookbookFragment.kt | 2 ++ 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeAdapter.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeAdapter.kt index 88e3fb9..ffab6ef 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeAdapter.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeAdapter.kt @@ -13,17 +13,15 @@ class RecipeAdapter : ListAdapter(Recip return when (viewType) { ViewType.CATEGORY.id -> { - CategoryViewHolder( - LayoutInflater.from(parent.context) - .inflate(R.layout.vh_recipe_category, parent, false) - ) + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.vh_recipe_category, parent, false) + CategoryViewHolder(view) } ViewType.RECIPE.id -> { - RecipeViewHolder( - LayoutInflater.from(parent.context) - .inflate(R.layout.vh_recipe_item, parent, false) - ) + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.vh_recipe_item, parent, false) + RecipeViewHolder(view) } else -> throw IllegalArgumentException("wrong type of view") diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt new file mode 100644 index 0000000..1948a0c --- /dev/null +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt @@ -0,0 +1,11 @@ +package ru.otus.cookbook.cook_recycler_view + + +class RecipeTouchHelper { + + fun onRcViewItemClick(id: Int) { + + println() + } +} + diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt index 1e3547a..6cdfd86 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt @@ -15,7 +15,10 @@ class RecipeViewHolder(private val recipeView: View) : RecyclerView.ViewHolder(r private val description: TextView by lazy { recipeView.findViewById(R.id.titleDescription) } private val imageRecipe: ImageView by lazy { recipeView.findViewById(R.id.imageRecipe) } + fun bind(recipeData: RecipeListItem.RecipeItem) { + recipeView.setOnClickListener { RecipeTouchHelper().onRcViewItemClick(recipeData.id) } + title.text = recipeData.title description.text = recipeData.description diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt index 011495d..278c563 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt @@ -10,11 +10,13 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.ItemTouchHelper import kotlinx.coroutines.launch import ru.otus.cookbook.cook_recycler_view.RecipeAdapter import ru.otus.cookbook.data.RecipeListItem import ru.otus.cookbook.databinding.FragmentCookbookBinding import ru.otus.cookbook.R +import ru.otus.cookbook.cook_recycler_view.RecipeTouchHelper class CookbookFragment : Fragment() { From 59ba02394e3167f82d2dfb084274f24f98a20157 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 13:06:15 +0300 Subject: [PATCH 05/12] fill RecipeFragment from data --- .../kotlin/ru/otus/cookbook/MainActivity.kt | 8 --- .../cook_recycler_view/RecipeTouchHelper.kt | 16 ++++- .../cook_recycler_view/RecipeViewHolder.kt | 2 +- .../ru/otus/cookbook/ui/CookbookFragment.kt | 10 ++- .../ru/otus/cookbook/ui/RecipeFragment.kt | 23 ++++++- .../res/drawable/baseline_arrow_back_24.xml | 5 ++ app/src/main/res/layout/activity_main.xml | 15 ++-- app/src/main/res/layout/fragment_cookbook.xml | 1 + app/src/main/res/layout/fragment_recipe.xml | 69 +++++++++++++++++++ app/src/main/res/navigation/main_graph.xml | 17 ++++- app/src/main/res/values/strings.xml | 3 + 11 files changed, 144 insertions(+), 25 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_arrow_back_24.xml diff --git a/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt b/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt index 78027e7..6e524b6 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt @@ -1,15 +1,7 @@ package ru.otus.cookbook import android.os.Bundle -import android.view.View import androidx.appcompat.app.AppCompatActivity -import androidx.fragment.app.Fragment -import androidx.fragment.app.FragmentManager -import androidx.navigation.Navigation -import androidx.recyclerview.widget.DividerItemDecoration -import androidx.recyclerview.widget.ItemTouchHelper -import androidx.recyclerview.widget.RecyclerView -import ru.otus.cookbook.cook_recycler_view.RecipeAdapter import ru.otus.cookbook.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt index 1948a0c..cd5731d 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt @@ -1,11 +1,23 @@ package ru.otus.cookbook.cook_recycler_view +import android.annotation.SuppressLint +import androidx.navigation.NavController +import ru.otus.cookbook.ui.CookbookFragmentDirections -class RecipeTouchHelper { + +object RecipeTouchHelper { + + @SuppressLint("StaticFieldLeak") + private var _navController: NavController? = null + + fun setNavController(navController: NavController) { + _navController = navController + } fun onRcViewItemClick(id: Int) { + val action = CookbookFragmentDirections.Companion.actionCookbookFragmentToRecipeFragment(id) + _navController?.navigate(action) - println() } } diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt index 6cdfd86..79ed62a 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt @@ -17,7 +17,7 @@ class RecipeViewHolder(private val recipeView: View) : RecyclerView.ViewHolder(r fun bind(recipeData: RecipeListItem.RecipeItem) { - recipeView.setOnClickListener { RecipeTouchHelper().onRcViewItemClick(recipeData.id) } + recipeView.setOnClickListener { RecipeTouchHelper.onRcViewItemClick(recipeData.id) } title.text = recipeData.title description.text = recipeData.description diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt index 278c563..6625673 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt @@ -9,14 +9,14 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope +import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.DividerItemDecoration -import androidx.recyclerview.widget.ItemTouchHelper import kotlinx.coroutines.launch +import ru.otus.cookbook.R import ru.otus.cookbook.cook_recycler_view.RecipeAdapter +import ru.otus.cookbook.cook_recycler_view.RecipeTouchHelper import ru.otus.cookbook.data.RecipeListItem import ru.otus.cookbook.databinding.FragmentCookbookBinding -import ru.otus.cookbook.R -import ru.otus.cookbook.cook_recycler_view.RecipeTouchHelper class CookbookFragment : Fragment() { @@ -44,6 +44,10 @@ class CookbookFragment : Fragment() { } private fun setupRecyclerView() = binding.withBinding { + + val navController = findNavController() + RecipeTouchHelper.setNavController(navController) + cardRecipesRcView.adapter = recipeAdapter val divider = DividerItemDecoration(this@CookbookFragment.context,DividerItemDecoration.VERTICAL) diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt index e4460c1..61a5c4b 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt @@ -4,18 +4,24 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewmodel.MutableCreationExtras +import androidx.navigation.fragment.navArgs +import com.bumptech.glide.Glide import kotlinx.coroutines.launch +import ru.otus.cookbook.R import ru.otus.cookbook.data.Recipe import ru.otus.cookbook.databinding.FragmentRecipeBinding class RecipeFragment : Fragment() { - private val recipeId: Int get() = TODO("Use Safe Args to get the recipe ID: https://developer.android.com/guide/navigation/use-graph/pass-data#Safe-args") + private val args: RecipeFragmentArgs by navArgs() + private val recipeId: Int get() = args.recipeId private val binding = FragmentBindingDelegate(this) private val model: RecipeFragmentViewModel by viewModels( @@ -53,7 +59,20 @@ class RecipeFragment : Fragment() { } private fun displayRecipe(recipe: Recipe) { - // Display the recipe + view?.let { + val imageRecipe = it.findViewById(R.id.imageRecipeOnAbout) + Glide.with(this) + .load(recipe.imageUrl) + .into(imageRecipe) + + it.findViewById(R.id.nameRecipe).text = recipe.title + it.findViewById(R.id.typeRecipe).text = recipe.category.name + it.findViewById(R.id.descriptionRecipe).text = recipe.description + + Glide.with(this) + .load(recipe.imageUrl) + .into(imageRecipe) + } } private fun deleteRecipe() { diff --git a/app/src/main/res/drawable/baseline_arrow_back_24.xml b/app/src/main/res/drawable/baseline_arrow_back_24.xml new file mode 100644 index 0000000..075e95d --- /dev/null +++ b/app/src/main/res/drawable/baseline_arrow_back_24.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 783430f..a4a9938 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,13 +7,12 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - - + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_cookbook.xml b/app/src/main/res/layout/fragment_cookbook.xml index 4cf4bd0..b9a9f74 100644 --- a/app/src/main/res/layout/fragment_cookbook.xml +++ b/app/src/main/res/layout/fragment_cookbook.xml @@ -1,6 +1,7 @@ diff --git a/app/src/main/res/layout/fragment_recipe.xml b/app/src/main/res/layout/fragment_recipe.xml index 77d9ef6..d67191a 100644 --- a/app/src/main/res/layout/fragment_recipe.xml +++ b/app/src/main/res/layout/fragment_recipe.xml @@ -1,6 +1,75 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/main_graph.xml b/app/src/main/res/navigation/main_graph.xml index b5b879e..da38a3f 100644 --- a/app/src/main/res/navigation/main_graph.xml +++ b/app/src/main/res/navigation/main_graph.xml @@ -4,17 +4,32 @@ xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_graph" app:startDestination="@id/cookbookFragment"> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 05c741c..10ea1db 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,4 +5,7 @@ Title C Fish + Roasted in breadcrumbs + Lorem ipsum Quisque blandit dolor vel ullamcorper fringilla. Etiam ut ultricies nibh. Maecenas sit amet ipsum at felis convallis luctus. Praesent at urna ac massa eleifend iaculis. Donec at venenatis metus. Donec malesuada sapien eget neque finibus, vel aliquam mi lacinia. Mauris a sem elementum, pretium diam nec, luctus neque. Praesent lacinia imperdiet erat, eget feugiat mi feugiat ut. Proin iaculis, massa eu lacinia tempor, dui lacus pellentesque dui, ut dapibus tellus purus at augue. Nam sagittis lobortis justo vel porta. Proin vel vulputate quam. Vivamus at vestibulum est. Phasellus a arcu lacus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam elit mauris, pharetra vitae libero sit amet, efficitur rhoncus mauris. + Cod \ No newline at end of file From 5095bb416c6ce1c20d9c1aa67fff6ae963615c00 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 13:11:26 +0300 Subject: [PATCH 06/12] add animation fade_in --- app/src/main/res/navigation/main_graph.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/res/navigation/main_graph.xml b/app/src/main/res/navigation/main_graph.xml index da38a3f..22d3ad4 100644 --- a/app/src/main/res/navigation/main_graph.xml +++ b/app/src/main/res/navigation/main_graph.xml @@ -14,7 +14,11 @@ app:destination="@id/mainActivity" /> + app:destination="@id/recipeFragment" + app:enterAnim="@android:animator/fade_in" + app:exitAnim="@anim/nav_default_exit_anim" + app:popEnterAnim="@anim/nav_default_pop_enter_anim" + app:popExitAnim="@anim/nav_default_pop_exit_anim" /> Date: Sat, 18 Jan 2025 15:03:44 +0300 Subject: [PATCH 07/12] add showAlertDialogOnRecipeFragment --- .../main/kotlin/ru/otus/cookbook/MainActivity.kt | 1 + .../cook_recycler_view/RecipeViewHolder.kt | 3 ++- .../NavigateHelper.kt} | 11 +++++++---- .../ru/otus/cookbook/ui/CookbookFragment.kt | 4 ++-- .../kotlin/ru/otus/cookbook/ui/RecipeFragment.kt | 15 +++++++++++++++ app/src/main/res/drawable/delete.xml | 10 ++++++++++ app/src/main/res/layout/fragment_recipe.xml | 1 + app/src/main/res/menu/main_menu.xml | 9 +++++++++ app/src/main/res/navigation/main_graph.xml | 2 ++ app/src/main/res/values/strings.xml | 3 +++ 10 files changed, 52 insertions(+), 7 deletions(-) rename app/src/main/kotlin/ru/otus/cookbook/{cook_recycler_view/RecipeTouchHelper.kt => helpers/NavigateHelper.kt} (70%) create mode 100644 app/src/main/res/drawable/delete.xml create mode 100644 app/src/main/res/menu/main_menu.xml diff --git a/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt b/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt index 6e524b6..bf002c8 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/MainActivity.kt @@ -1,6 +1,7 @@ package ru.otus.cookbook import android.os.Bundle +import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import ru.otus.cookbook.databinding.ActivityMainBinding diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt index 79ed62a..9fd3c48 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeViewHolder.kt @@ -5,6 +5,7 @@ import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide +import ru.otus.cookbook.helpers.NavigateHelper import ru.otus.cookbook.R import ru.otus.cookbook.data.RecipeListItem @@ -17,7 +18,7 @@ class RecipeViewHolder(private val recipeView: View) : RecyclerView.ViewHolder(r fun bind(recipeData: RecipeListItem.RecipeItem) { - recipeView.setOnClickListener { RecipeTouchHelper.onRcViewItemClick(recipeData.id) } + recipeView.setOnClickListener { NavigateHelper.showMoreAboutRecipe(recipeData.id) } title.text = recipeData.title description.text = recipeData.description diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt similarity index 70% rename from app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt rename to app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt index cd5731d..5d8c6c8 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/RecipeTouchHelper.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt @@ -1,11 +1,10 @@ -package ru.otus.cookbook.cook_recycler_view +package ru.otus.cookbook.helpers import android.annotation.SuppressLint import androidx.navigation.NavController import ru.otus.cookbook.ui.CookbookFragmentDirections - -object RecipeTouchHelper { +object NavigateHelper { @SuppressLint("StaticFieldLeak") private var _navController: NavController? = null @@ -14,10 +13,14 @@ object RecipeTouchHelper { _navController = navController } - fun onRcViewItemClick(id: Int) { + fun showMoreAboutRecipe(id: Int) { val action = CookbookFragmentDirections.Companion.actionCookbookFragmentToRecipeFragment(id) _navController?.navigate(action) + } + + fun showAlertDialogOnRecipeFragment(id: Int) { + _navController?.navigate("alertDialogFragment") } } diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt index 6625673..4d48ef6 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt @@ -14,7 +14,7 @@ import androidx.recyclerview.widget.DividerItemDecoration import kotlinx.coroutines.launch import ru.otus.cookbook.R import ru.otus.cookbook.cook_recycler_view.RecipeAdapter -import ru.otus.cookbook.cook_recycler_view.RecipeTouchHelper +import ru.otus.cookbook.helpers.NavigateHelper import ru.otus.cookbook.data.RecipeListItem import ru.otus.cookbook.databinding.FragmentCookbookBinding @@ -46,7 +46,7 @@ class CookbookFragment : Fragment() { private fun setupRecyclerView() = binding.withBinding { val navController = findNavController() - RecipeTouchHelper.setNavController(navController) + NavigateHelper.setNavController(navController) cardRecipesRcView.adapter = recipeAdapter diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt index 61a5c4b..f7fd70c 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt @@ -11,12 +11,15 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewmodel.MutableCreationExtras +import androidx.navigation.Navigation import androidx.navigation.fragment.navArgs import com.bumptech.glide.Glide +import com.google.android.material.appbar.MaterialToolbar import kotlinx.coroutines.launch import ru.otus.cookbook.R import ru.otus.cookbook.data.Recipe import ru.otus.cookbook.databinding.FragmentRecipeBinding +import ru.otus.cookbook.helpers.NavigateHelper class RecipeFragment : Fragment() { @@ -43,12 +46,20 @@ class RecipeFragment : Fragment() { ) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) viewLifecycleOwner.lifecycleScope.launch { model.recipe .flowWithLifecycle(viewLifecycleOwner.lifecycle) .collect(::displayRecipe) } + + val materialToolbar = view.findViewById(R.id.titleToolbar) + materialToolbar.setOnMenuItemClickListener { + NavigateHelper.showAlertDialogOnRecipeFragment(this.recipeId) + openDialog() + true + } } /** @@ -58,6 +69,10 @@ class RecipeFragment : Fragment() { return model.recipe.value.title } + private fun openDialog() { + println() + } + private fun displayRecipe(recipe: Recipe) { view?.let { val imageRecipe = it.findViewById(R.id.imageRecipeOnAbout) diff --git a/app/src/main/res/drawable/delete.xml b/app/src/main/res/drawable/delete.xml new file mode 100644 index 0000000..2bab095 --- /dev/null +++ b/app/src/main/res/drawable/delete.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_recipe.xml b/app/src/main/res/layout/fragment_recipe.xml index d67191a..79693aa 100644 --- a/app/src/main/res/layout/fragment_recipe.xml +++ b/app/src/main/res/layout/fragment_recipe.xml @@ -17,6 +17,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" app:navigationIcon="@drawable/baseline_arrow_back_24" + app:menu="@menu/main_menu" app:title="Cod" /> diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml new file mode 100644 index 0000000..ae891e1 --- /dev/null +++ b/app/src/main/res/menu/main_menu.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/navigation/main_graph.xml b/app/src/main/res/navigation/main_graph.xml index 22d3ad4..41b6aa0 100644 --- a/app/src/main/res/navigation/main_graph.xml +++ b/app/src/main/res/navigation/main_graph.xml @@ -36,4 +36,6 @@ app:argType="integer" android:defaultValue="1" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 10ea1db..0b6b10e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,4 +8,7 @@ Roasted in breadcrumbs Lorem ipsum Quisque blandit dolor vel ullamcorper fringilla. Etiam ut ultricies nibh. Maecenas sit amet ipsum at felis convallis luctus. Praesent at urna ac massa eleifend iaculis. Donec at venenatis metus. Donec malesuada sapien eget neque finibus, vel aliquam mi lacinia. Mauris a sem elementum, pretium diam nec, luctus neque. Praesent lacinia imperdiet erat, eget feugiat mi feugiat ut. Proin iaculis, massa eu lacinia tempor, dui lacus pellentesque dui, ut dapibus tellus purus at augue. Nam sagittis lobortis justo vel porta. Proin vel vulputate quam. Vivamus at vestibulum est. Phasellus a arcu lacus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam elit mauris, pharetra vitae libero sit amet, efficitur rhoncus mauris. Cod + delete + + Hello blank fragment \ No newline at end of file From 3cf3df5564d457dd1f49bc0ee0977b66d94ed1ac Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 15:06:31 +0300 Subject: [PATCH 08/12] add AlertDialogDialogFragment --- .../AlertDialogDialogFragment.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt new file mode 100644 index 0000000..54ce6af --- /dev/null +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt @@ -0,0 +1,16 @@ +package ru.otus.cookbook.cook_recycler_view + +import android.app.Dialog +import android.os.Bundle +import androidx.appcompat.app.AlertDialog +import androidx.fragment.app.DialogFragment + +class AlertDialogDialogFragment : DialogFragment() { + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val builder = activity?.let { AlertDialog.Builder(it) } + builder?.setTitle("title") + builder?.setMessage("masage") + return builder?.create()!! + } +} \ No newline at end of file From b72b68d0bca429b4100704a97a5aa646a176e633 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 15:06:47 +0300 Subject: [PATCH 09/12] fix setMessage --- .../cookbook/cook_recycler_view/AlertDialogDialogFragment.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt index 54ce6af..7f8d638 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt @@ -10,7 +10,7 @@ class AlertDialogDialogFragment : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val builder = activity?.let { AlertDialog.Builder(it) } builder?.setTitle("title") - builder?.setMessage("masage") + builder?.setMessage("message") return builder?.create()!! } } \ No newline at end of file From 5b25e757540e228072417692437d1407e605df68 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 15:54:21 +0300 Subject: [PATCH 10/12] fix setMessage --- .../AlertDialogDialogFragment.kt | 16 ---------- .../otus/cookbook/helpers/NavigateHelper.kt | 10 ++++--- .../otus/cookbook/ui/AlertDialogFragment.kt | 29 +++++++++++++++++++ .../ru/otus/cookbook/ui/RecipeFragment.kt | 2 +- app/src/main/res/navigation/main_graph.xml | 11 +++++++ 5 files changed, 47 insertions(+), 21 deletions(-) delete mode 100644 app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt create mode 100644 app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt diff --git a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt deleted file mode 100644 index 7f8d638..0000000 --- a/app/src/main/kotlin/ru/otus/cookbook/cook_recycler_view/AlertDialogDialogFragment.kt +++ /dev/null @@ -1,16 +0,0 @@ -package ru.otus.cookbook.cook_recycler_view - -import android.app.Dialog -import android.os.Bundle -import androidx.appcompat.app.AlertDialog -import androidx.fragment.app.DialogFragment - -class AlertDialogDialogFragment : DialogFragment() { - - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - val builder = activity?.let { AlertDialog.Builder(it) } - builder?.setTitle("title") - builder?.setMessage("message") - return builder?.create()!! - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt index 5d8c6c8..68e161c 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt @@ -3,6 +3,8 @@ package ru.otus.cookbook.helpers import android.annotation.SuppressLint import androidx.navigation.NavController import ru.otus.cookbook.ui.CookbookFragmentDirections +import ru.otus.cookbook.ui.RecipeFragmentDirections + object NavigateHelper { @@ -14,13 +16,13 @@ object NavigateHelper { } fun showMoreAboutRecipe(id: Int) { - val action = CookbookFragmentDirections.Companion.actionCookbookFragmentToRecipeFragment(id) + val action = CookbookFragmentDirections.actionCookbookFragmentToRecipeFragment(id) _navController?.navigate(action) } - fun showAlertDialogOnRecipeFragment(id: Int) { - - _navController?.navigate("alertDialogFragment") + fun showAlertDialogOnRecipeFragment(recipeName: String) { + val action = RecipeFragmentDirections.actionRecipeFragmentToAlertDialogDialogFragment(recipeName) + _navController?.navigate(action) } } diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt new file mode 100644 index 0000000..1004a87 --- /dev/null +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt @@ -0,0 +1,29 @@ +package ru.otus.cookbook.ui + +import android.app.Dialog +import android.os.Bundle +import androidx.fragment.app.DialogFragment +import androidx.navigation.fragment.navArgs +import com.google.android.material.dialog.MaterialAlertDialogBuilder + +class AlertDialogFragment : DialogFragment() { + private val args: AlertDialogFragmentArgs by navArgs() + private val recipeName: String get() = args.recipeName + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + + val builder = activity?.let { MaterialAlertDialogBuilder(it) } + + builder?.apply { + setTitle("Delete") + setMessage("Are you sure you want to delete $recipeName ?") + setNegativeButton("Cancel") { dialog, which -> + return@setNegativeButton + } + setPositiveButton("OK") { dialog, which -> + // Respond to positive button press + } + } + return builder?.create()!! + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt index f7fd70c..921a929 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt @@ -56,7 +56,7 @@ class RecipeFragment : Fragment() { val materialToolbar = view.findViewById(R.id.titleToolbar) materialToolbar.setOnMenuItemClickListener { - NavigateHelper.showAlertDialogOnRecipeFragment(this.recipeId) + NavigateHelper.showAlertDialogOnRecipeFragment(getTitle()) openDialog() true } diff --git a/app/src/main/res/navigation/main_graph.xml b/app/src/main/res/navigation/main_graph.xml index 41b6aa0..0805c14 100644 --- a/app/src/main/res/navigation/main_graph.xml +++ b/app/src/main/res/navigation/main_graph.xml @@ -35,7 +35,18 @@ android:name="recipeId" app:argType="integer" android:defaultValue="1" /> + + + + \ No newline at end of file From c6d8757155d62d8686c06ff2ddddf1cbda002bc9 Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 17:12:38 +0300 Subject: [PATCH 11/12] continue --- .../main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt | 7 ++++--- .../main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt | 7 ++++--- app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt | 6 ------ app/src/main/res/navigation/main_graph.xml | 6 +++--- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt index 68e161c..3745caa 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt @@ -5,7 +5,6 @@ import androidx.navigation.NavController import ru.otus.cookbook.ui.CookbookFragmentDirections import ru.otus.cookbook.ui.RecipeFragmentDirections - object NavigateHelper { @SuppressLint("StaticFieldLeak") @@ -16,12 +15,14 @@ object NavigateHelper { } fun showMoreAboutRecipe(id: Int) { - val action = CookbookFragmentDirections.actionCookbookFragmentToRecipeFragment(id) + val action = + CookbookFragmentDirections.actionCookbookFragmentToRecipeFragment(recipeId = id) _navController?.navigate(action) } fun showAlertDialogOnRecipeFragment(recipeName: String) { - val action = RecipeFragmentDirections.actionRecipeFragmentToAlertDialogDialogFragment(recipeName) + val action = + RecipeFragmentDirections.actionRecipeFragmentToAlertDialogDialogFragment(recipeName) _navController?.navigate(action) } } diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt index 1004a87..ba4deca 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt @@ -5,6 +5,7 @@ import android.os.Bundle import androidx.fragment.app.DialogFragment import androidx.navigation.fragment.navArgs import com.google.android.material.dialog.MaterialAlertDialogBuilder +import ru.otus.cookbook.helpers.NavigateHelper class AlertDialogFragment : DialogFragment() { private val args: AlertDialogFragmentArgs by navArgs() @@ -17,11 +18,11 @@ class AlertDialogFragment : DialogFragment() { builder?.apply { setTitle("Delete") setMessage("Are you sure you want to delete $recipeName ?") - setNegativeButton("Cancel") { dialog, which -> + setNegativeButton("Cancel") { _, _ -> return@setNegativeButton } - setPositiveButton("OK") { dialog, which -> - // Respond to positive button press + setPositiveButton("OK") { _, _ -> + } } return builder?.create()!! diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt index 921a929..830dd88 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt @@ -11,7 +11,6 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewmodel.MutableCreationExtras -import androidx.navigation.Navigation import androidx.navigation.fragment.navArgs import com.bumptech.glide.Glide import com.google.android.material.appbar.MaterialToolbar @@ -57,7 +56,6 @@ class RecipeFragment : Fragment() { val materialToolbar = view.findViewById(R.id.titleToolbar) materialToolbar.setOnMenuItemClickListener { NavigateHelper.showAlertDialogOnRecipeFragment(getTitle()) - openDialog() true } } @@ -69,10 +67,6 @@ class RecipeFragment : Fragment() { return model.recipe.value.title } - private fun openDialog() { - println() - } - private fun displayRecipe(recipe: Recipe) { view?.let { val imageRecipe = it.findViewById(R.id.imageRecipeOnAbout) diff --git a/app/src/main/res/navigation/main_graph.xml b/app/src/main/res/navigation/main_graph.xml index 0805c14..ee488be 100644 --- a/app/src/main/res/navigation/main_graph.xml +++ b/app/src/main/res/navigation/main_graph.xml @@ -30,11 +30,11 @@ + android:label="RecipeFragment"> + android:defaultValue="1" + app:argType="integer" /> From 615197f6709ec9311d4e571606b332fbb30f0dcc Mon Sep 17 00:00:00 2001 From: SpiritualAdviser Date: Sat, 18 Jan 2025 17:48:35 +0300 Subject: [PATCH 12/12] delete wrong vay --- .../ru/otus/cookbook/helpers/NavigateHelper.kt | 12 +++++++++++- .../ru/otus/cookbook/ui/AlertDialogFragment.kt | 4 +++- .../kotlin/ru/otus/cookbook/ui/RecipeFragment.kt | 6 +++++- app/src/main/res/layout/fragment_recipe.xml | 1 + 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt index 3745caa..c22875a 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/helpers/NavigateHelper.kt @@ -3,12 +3,15 @@ package ru.otus.cookbook.helpers import android.annotation.SuppressLint import androidx.navigation.NavController import ru.otus.cookbook.ui.CookbookFragmentDirections +import ru.otus.cookbook.ui.RecipeFragment import ru.otus.cookbook.ui.RecipeFragmentDirections +import ru.otus.cookbook.R object NavigateHelper { @SuppressLint("StaticFieldLeak") private var _navController: NavController? = null + private var _recipeFragment: RecipeFragment? = null fun setNavController(navController: NavController) { _navController = navController @@ -20,10 +23,17 @@ object NavigateHelper { _navController?.navigate(action) } - fun showAlertDialogOnRecipeFragment(recipeName: String) { + fun showAlertDialogOnRecipeFragment(recipeName: String, recipeFragment: RecipeFragment) { + _recipeFragment = recipeFragment val action = RecipeFragmentDirections.actionRecipeFragmentToAlertDialogDialogFragment(recipeName) _navController?.navigate(action) } + + fun onDelete() { + _recipeFragment?.onAlertDialogDelete() + _navController?.popBackStack(R.id.cookbookFragment, false) + _recipeFragment = null + } } diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt index ba4deca..b014310 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/AlertDialogFragment.kt @@ -1,5 +1,6 @@ package ru.otus.cookbook.ui +import ru.otus.cookbook.R import android.app.Dialog import android.os.Bundle import androidx.fragment.app.DialogFragment @@ -7,6 +8,7 @@ import androidx.navigation.fragment.navArgs import com.google.android.material.dialog.MaterialAlertDialogBuilder import ru.otus.cookbook.helpers.NavigateHelper + class AlertDialogFragment : DialogFragment() { private val args: AlertDialogFragmentArgs by navArgs() private val recipeName: String get() = args.recipeName @@ -22,7 +24,7 @@ class AlertDialogFragment : DialogFragment() { return@setNegativeButton } setPositiveButton("OK") { _, _ -> - + NavigateHelper.onDelete() } } return builder?.create()!! diff --git a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt index 830dd88..8072b8a 100644 --- a/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt +++ b/app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt @@ -55,7 +55,7 @@ class RecipeFragment : Fragment() { val materialToolbar = view.findViewById(R.id.titleToolbar) materialToolbar.setOnMenuItemClickListener { - NavigateHelper.showAlertDialogOnRecipeFragment(getTitle()) + NavigateHelper.showAlertDialogOnRecipeFragment(getTitle(), this) true } } @@ -84,6 +84,10 @@ class RecipeFragment : Fragment() { } } + fun onAlertDialogDelete() { + deleteRecipe() + } + private fun deleteRecipe() { model.delete() } diff --git a/app/src/main/res/layout/fragment_recipe.xml b/app/src/main/res/layout/fragment_recipe.xml index 79693aa..36d3400 100644 --- a/app/src/main/res/layout/fragment_recipe.xml +++ b/app/src/main/res/layout/fragment_recipe.xml @@ -1,6 +1,7 @@