-
Notifications
You must be signed in to change notification settings - Fork 22
ДЗ Android Navigation Components #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xeniamlkh
wants to merge
3
commits into
Android-Developer-Basic:master
Choose a base branch
from
xeniamlkh:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ru.otus.cookbook | ||
|
|
||
| import android.app.AlertDialog | ||
| import android.app.Dialog | ||
| import android.os.Bundle | ||
| import androidx.fragment.app.DialogFragment | ||
| import androidx.navigation.fragment.findNavController | ||
| import androidx.navigation.fragment.navArgs | ||
| import ru.otus.cookbook.ui.RESULT_KEY | ||
| import kotlin.getValue | ||
|
|
||
| class ConfirmationDialog : DialogFragment() { | ||
|
|
||
| private val args: ConfirmationDialogArgs by navArgs() | ||
| private val recipeTitle: String get() = args.recipeTitle | ||
|
|
||
| override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = | ||
| AlertDialog.Builder(requireContext()) | ||
| .setMessage("Удалить рецепт $recipeTitle?") | ||
| .setPositiveButton("Да") { _, _ -> | ||
| setResult(true) | ||
| } | ||
| .setNegativeButton("Нет") { _, _ -> | ||
| setResult(false) | ||
| } | ||
| .create() | ||
|
|
||
| private fun setResult(result: Boolean) { | ||
| findNavController().previousBackStackEntry?.savedStateHandle?.set(RESULT_KEY, result) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/main/kotlin/ru/otus/cookbook/ui/CookbookRecyclerViewAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package ru.otus.cookbook.ui | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import ru.otus.cookbook.R | ||
| import ru.otus.cookbook.data.RecipeListItem | ||
|
|
||
| class CookbookRecyclerViewAdapter( | ||
| val recipeListener: RecipeItemListener | ||
| ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | ||
|
|
||
| private var list = emptyList<RecipeListItem>() | ||
|
|
||
| fun setList(recipeList: List<RecipeListItem>) { | ||
| list = recipeList | ||
| notifyDataSetChanged() | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
| return when (viewType) { | ||
| ViewType.CATEGORY.id -> { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.vh_recipe_category, parent, false) | ||
| CategoryItemViewHolder(view) | ||
| } | ||
|
|
||
| ViewType.RECIPE.id -> { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.vh_recipe_item, parent, false) | ||
| RecipeItemViewHolder( | ||
| view = view, | ||
| recipeListener = recipeListener | ||
| ) | ||
| } | ||
|
|
||
| else -> throw IllegalArgumentException("Unknown view type") | ||
| } | ||
| } | ||
|
|
||
| override fun getItemViewType(position: Int): Int { | ||
| return when (list[position]) { | ||
| is RecipeListItem.CategoryItem -> ViewType.CATEGORY.id | ||
| is RecipeListItem.RecipeItem -> ViewType.RECIPE.id | ||
| } | ||
| } | ||
|
|
||
| override fun onBindViewHolder( | ||
| holder: RecyclerView.ViewHolder, | ||
| position: Int | ||
| ) { | ||
| val item = list[position] | ||
|
|
||
| when (getItemViewType(position)) { | ||
| ViewType.CATEGORY.id -> (holder as CategoryItemViewHolder).bind(item as RecipeListItem.CategoryItem) | ||
| ViewType.RECIPE.id -> (holder as RecipeItemViewHolder).bind(item as RecipeListItem.RecipeItem) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| override fun getItemCount(): Int = list.size | ||
|
|
||
| private enum class ViewType(val id: Int) { | ||
| CATEGORY(R.layout.vh_recipe_category), | ||
| RECIPE(R.layout.vh_recipe_item) | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
app/src/main/kotlin/ru/otus/cookbook/ui/CookbookViewHolders.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package ru.otus.cookbook.ui | ||
|
|
||
| import android.view.View | ||
| import android.widget.ImageView | ||
| import android.widget.TextView | ||
| import androidx.cardview.widget.CardView | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import ru.otus.cookbook.R | ||
| import ru.otus.cookbook.data.RecipeListItem | ||
| import coil3.load | ||
| import coil3.request.placeholder | ||
| import coil3.request.error | ||
|
|
||
| interface RecipeItemListener { | ||
| fun onRecipeClick(recipeId: Int) | ||
| } | ||
|
|
||
| class CategoryItemViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||
| private val name: TextView by lazy { view.findViewById<TextView>(R.id.category_item_name) } | ||
|
|
||
| fun bind(categoryItem: RecipeListItem.CategoryItem) { | ||
| name.text = categoryItem.name | ||
| } | ||
| } | ||
|
|
||
| class RecipeItemViewHolder( | ||
| view: View, | ||
| val recipeListener: RecipeItemListener | ||
| ) : RecyclerView.ViewHolder(view) { | ||
| private val letter: TextView by lazy { view.findViewById(R.id.recipe_letter) } | ||
| private val title: TextView by lazy { view.findViewById(R.id.recipe_item_title) } | ||
| private val description: TextView by lazy { view.findViewById(R.id.recipe_item_description) } | ||
| private val image: ImageView by lazy { view.findViewById(R.id.recipe_image) } | ||
|
|
||
| private val recipeCard: CardView by lazy { view.findViewById(R.id.recipe_card) } | ||
|
|
||
| fun bind(recipeItem: RecipeListItem.RecipeItem) { | ||
| letter.text = recipeItem.title[0].toString() | ||
| title.text = recipeItem.title | ||
| description.text = recipeItem.description | ||
| image.load(recipeItem.imageUrl) { | ||
| placeholder(R.drawable.ic_android_placeholder) | ||
| error(R.drawable.ic_android_placeholder) | ||
| } | ||
|
|
||
| recipeCard.setOnClickListener { | ||
| recipeListener.onRecipeClick(recipeItem.id) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp"> | ||
|
|
||
| <path android:fillColor="@android:color/white" android:pathData="M40,720Q49,613 105.5,523Q162,433 256,380L182,252Q176,243 179,233Q182,223 192,218Q200,213 210,216Q220,219 226,228L300,356Q386,320 480,320Q574,320 660,356L734,228Q740,219 750,216Q760,213 768,218Q778,223 781,233Q784,243 778,252L704,380Q798,433 854.5,523Q911,613 920,720L40,720ZM280,610Q301,610 315.5,595.5Q330,581 330,560Q330,539 315.5,524.5Q301,510 280,510Q259,510 244.5,524.5Q230,539 230,560Q230,581 244.5,595.5Q259,610 280,610ZM680,610Q701,610 715.5,595.5Q730,581 730,560Q730,539 715.5,524.5Q701,510 680,510Q659,510 644.5,524.5Q630,539 630,560Q630,581 644.5,595.5Q659,610 680,610Z"/> | ||
|
|
||
| </vector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,24 @@ | ||
| <?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" | ||
| <androidx.appcompat.widget.Toolbar | ||
| android:id="@+id/toolbar" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" /> | ||
|
|
||
| <androidx.fragment.app.FragmentContainerView | ||
| android:id="@+id/cookbook_fragment_container" | ||
| android:name="androidx.navigation.fragment.NavHostFragment" | ||
| android:layout_width="match_parent" | ||
| 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" /> | ||
| app:defaultNavHost="true" | ||
| app:navGraph="@navigation/nav_graph" /> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||
| </LinearLayout> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
После получения результата из диалога ключ RESULT_KEY не удаляется из SavedStateHandle. Это может привести к повторной обработке результата при возврате на экран. Давайтедобавим удаление ключа после обработки