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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ dependencies {
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
testImplementation(libs.kotlin.coroutines.test)
implementation(libs.androidx.recyclerview)
implementation(libs.glide)
annotationProcessor(libs.compiler)
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:name=".App"
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/kotlin/ru/otus/cookbook/ui/CookbookFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import kotlinx.coroutines.launch
import ru.otus.cookbook.data.RecipeListItem
import ru.otus.cookbook.databinding.FragmentCookbookBinding
Expand All @@ -16,6 +18,7 @@ class CookbookFragment : Fragment() {

private val binding = FragmentBindingDelegate<FragmentCookbookBinding>(this)
private val model: CookbookFragmentViewModel by viewModels { CookbookFragmentViewModel.Factory }
private val adapter = RecipesAdapter { launchRecipeFragment(it) }

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -28,6 +31,13 @@ class CookbookFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.withBinding {
(activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
(activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener {
findNavController().navigateUp()
}
}
setupRecyclerView()
viewLifecycleOwner.lifecycleScope.launch {
model.recipeList
Expand All @@ -36,11 +46,15 @@ class CookbookFragment : Fragment() {
}
}

private fun launchRecipeFragment(recipeId: Int) {
findNavController().navigate(CookbookFragmentDirections.actionCookbookFragmentToRecipeFragment(recipeId))
}

private fun setupRecyclerView() = binding.withBinding {
// Setup RecyclerView
recyclerViewCategory.adapter = adapter
}

private fun onRecipeListUpdated(recipeList: List<RecipeListItem>) {
// Handle recipe list
adapter.submitList(recipeList)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
import ru.otus.cookbook.R
import androidx.navigation.fragment.findNavController
import kotlin.getValue

class DeleteRecipeDialogFragment: DialogFragment() {

private val args by navArgs<DeleteRecipeDialogFragmentArgs>()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val recipeTitle = args.recipeTitle
return MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.delete))
.setMessage(getString(R.string.are_you_sure_want_to_delete, recipeTitle))
.setPositiveButton(getString(R.string.ok)) { _, _ ->
dismiss()
setResult(true)
}
.setNegativeButton(getString(R.string.cancel)) { _, _ ->
dismiss()
setResult(false)
}
.create()
}

private fun setResult(result: Boolean) {
val navController = findNavController()
navController.previousBackStackEntry?.savedStateHandle?.set(
DIALOG_RESULT_KEY,
result
)
}

companion object {
const val DIALOG_RESULT_KEY = "delete"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.otus.cookbook.ui

import androidx.recyclerview.widget.RecyclerView.ViewHolder
import ru.otus.cookbook.databinding.VhRecipeCategoryBinding

class RecipeCategoryViewHolder(val binding: VhRecipeCategoryBinding): ViewHolder(binding.root) {
}
20 changes: 20 additions & 0 deletions app/src/main/kotlin/ru/otus/cookbook/ui/RecipeDiffCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.otus.cookbook.ui

import androidx.recyclerview.widget.DiffUtil
import ru.otus.cookbook.data.RecipeListItem

class RecipeDiffCallback: DiffUtil.ItemCallback<RecipeListItem>() {
override fun areItemsTheSame(oldItem: RecipeListItem, newItem: RecipeListItem): Boolean {
return when{
oldItem is RecipeListItem.CategoryItem && newItem is RecipeListItem.CategoryItem ->
oldItem.name == newItem.name
oldItem is RecipeListItem.RecipeItem && newItem is RecipeListItem.RecipeItem ->
oldItem.id == newItem.id
else -> false
}
}

override fun areContentsTheSame(oldItem: RecipeListItem, newItem: RecipeListItem): Boolean {
return oldItem == newItem
}
}
60 changes: 57 additions & 3 deletions app/src/main/kotlin/ru/otus/cookbook/ui/RecipeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
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 kotlinx.coroutines.launch
import ru.otus.cookbook.data.Recipe
import ru.otus.cookbook.databinding.FragmentRecipeBinding
import androidx.navigation.fragment.findNavController
import android.os.Handler
import android.os.Looper
import com.bumptech.glide.Glide

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 by navArgs<RecipeFragmentArgs>()
private val recipeId: Int get() = args.recipeId

private val binding = FragmentBindingDelegate<FragmentRecipeBinding>(this)
private val model: RecipeFragmentViewModel by viewModels(
Expand All @@ -38,13 +44,52 @@ class RecipeFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.withBinding {
(activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
(activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
setupToolBar()
observeDeleteResult()

viewLifecycleOwner.lifecycleScope.launch {
model.recipe
.flowWithLifecycle(viewLifecycleOwner.lifecycle)
.collect(::displayRecipe)
}
}

private fun setupToolBar() {
binding.withBinding {
(activity as? AppCompatActivity)?.supportActionBar?.title = getTitle()
toolbar.setNavigationOnClickListener {
findNavController().navigateUp()
}
imageViewDelete.setOnClickListener {
findNavController().navigate(
RecipeFragmentDirections.actionRecipeFragmentToDeleteRecipeDialogFragment(
getTitle()
)
)
}
}
}

private fun observeDeleteResult() {
val navController = findNavController()
navController.currentBackStackEntry
?.savedStateHandle
?.getLiveData<Boolean>(DeleteRecipeDialogFragment.DIALOG_RESULT_KEY)
?.observe(viewLifecycleOwner) { result ->
if (result) {
deleteRecipe()
Handler(Looper.getMainLooper()).post {

Choose a reason for hiding this comment

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

Интересно, что получается использование Handler.post здесь избыточно, так как observer уже выполняется в юай потоке. Можно напрямую вызвать navController.navigateUp() без обертки в handler

navController.navigateUp()
}
}
}
}

/**
* Use to get recipe title and pass to confirmation dialog
*/
Expand All @@ -53,7 +98,16 @@ class RecipeFragment : Fragment() {
}

private fun displayRecipe(recipe: Recipe) {
// Display the recipe
binding.withBinding {
textViewTitle.text = recipe.title
textViewDescription.text = recipe.description
textViewRecipe.text = recipe.steps.mapIndexed { index, step -> "${index + 1}. $step"}
.joinToString("\n")
Glide.with(root)
.load(recipe.imageUrl)
.centerCrop()
.into(imageViewRecipe)
}
}

private fun deleteRecipe() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.otus.cookbook.ui

import androidx.recyclerview.widget.RecyclerView
import ru.otus.cookbook.databinding.VhRecipeItemBinding

class RecipeItemViewHolder(val binding: VhRecipeItemBinding): RecyclerView.ViewHolder(binding.root) {
}
65 changes: 65 additions & 0 deletions app/src/main/kotlin/ru/otus/cookbook/ui/RecipesAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ru.otus.cookbook.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import ru.otus.cookbook.data.RecipeListItem
import ru.otus.cookbook.databinding.VhRecipeCategoryBinding
import ru.otus.cookbook.databinding.VhRecipeItemBinding

class RecipesAdapter(val onRecipeClick: (Int) -> Unit):
ListAdapter<RecipeListItem, RecyclerView.ViewHolder>(RecipeDiffCallback()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)

return when(viewType){
VIEW_TYPE_CATEGORY -> {
val binding = VhRecipeCategoryBinding.inflate(inflater, parent, false)
RecipeCategoryViewHolder(binding)
}
VIEW_TYPE_RECIPE -> {
val binding = VhRecipeItemBinding.inflate(inflater, parent, false)
RecipeItemViewHolder(binding)
}
else -> throw RuntimeException("RuntimeException")
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = getItem(position)
when(holder){
is RecipeCategoryViewHolder -> {
val category = item as RecipeListItem.CategoryItem
holder.binding.textViewCategory.text = category.category.name
}
is RecipeItemViewHolder -> {
val recipeItem = item as RecipeListItem.RecipeItem
with(holder){
binding.textBackground.text = recipeItem.title.first().toString()
binding.title.text = recipeItem.title
binding.description.text = recipeItem.description
Glide.with(binding.root.context)
.load(recipeItem.imageUrl)
.centerCrop()
.into(binding.imageViewRecipe)
binding.root.setOnClickListener { onRecipeClick(item.id) }
}
}
}
}

override fun getItemViewType(position: Int): Int {
return when (getItem(position)) {
is RecipeListItem.CategoryItem -> VIEW_TYPE_CATEGORY
is RecipeListItem.RecipeItem -> VIEW_TYPE_RECIPE
}
}

companion object{
private const val VIEW_TYPE_CATEGORY = 1
private const val VIEW_TYPE_RECIPE = 2
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/arrow_back.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="16dp" android:height="16dp" android:viewportWidth="16" android:viewportHeight="16">
<path android:pathData="M3.825,9L9.425,14.6L8,16L0,8L8,0L9.425,1.4L3.825,7H16V9H3.825Z" android:fillColor="#1D1B20"/>
</vector>
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/close.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="14dp" android:height="14dp" android:viewportWidth="14" android:viewportHeight="14">
<path android:pathData="M1.4,14L0,12.6L5.6,7L0,1.4L1.4,0L7,5.6L12.6,0L14,1.4L8.4,7L14,12.6L12.6,14L7,8.4L1.4,14Z" android:fillColor="#1D1B20"/>
</vector>
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/image_view_recipe.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
</shape>
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/trash_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="16dp" android:height="18dp" android:viewportWidth="16" android:viewportHeight="18">
<path android:pathData="M3,18C2.45,18 1.979,17.804 1.587,17.413C1.196,17.021 1,16.55 1,16V3H0V1H5V0H11V1H16V3H15V16C15,16.55 14.804,17.021 14.413,17.413C14.021,17.804 13.55,18 13,18H3ZM13,3H3V16H13V3ZM5,14H7V5H5V14ZM9,14H11V5H9V14Z" android:fillColor="#49454F"/>
</vector>
22 changes: 10 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>
</LinearLayout>
27 changes: 24 additions & 3 deletions app/src/main/res/layout/fragment_cookbook.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.MaterialToolbar
style="@style/MyToolbarStyle"
android:id="@+id/toolbar"
app:navigationIcon="@drawable/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:id="@+id/recyclerViewCategory"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
tools:listitem="@layout/vh_recipe_category"/>

</LinearLayout>
Loading