Show recipe info in bottom sheet dialog

This commit is contained in:
Kirill Kamakin
2021-11-25 20:06:53 +03:00
parent 6c41c4fcf6
commit e701b5d7df
5 changed files with 36 additions and 29 deletions

View File

@@ -1,40 +1,29 @@
package gq.kirmanak.mealient.ui.recipes.info package gq.kirmanak.mealient.ui.recipes.info
import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.databinding.FragmentRecipeInfoBinding import gq.kirmanak.mealient.databinding.FragmentRecipeInfoBinding
import gq.kirmanak.mealient.ui.auth.AuthenticationViewModel
import timber.log.Timber import timber.log.Timber
@AndroidEntryPoint @AndroidEntryPoint
class RecipeInfoFragment : Fragment() { class RecipeInfoFragment : BottomSheetDialogFragment() {
private var _binding: FragmentRecipeInfoBinding? = null private var _binding: FragmentRecipeInfoBinding? = null
private val binding: FragmentRecipeInfoBinding private val binding: FragmentRecipeInfoBinding
get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" } get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" }
private val arguments by navArgs<RecipeInfoFragmentArgs>() private val arguments by navArgs<RecipeInfoFragmentArgs>()
private val viewModel by viewModels<RecipeInfoViewModel>() private val viewModel by viewModels<RecipeInfoViewModel>()
private val authViewModel by viewModels<AuthenticationViewModel>()
private val authStatuses by lazy { authViewModel.authenticationStatuses() }
private val authStatusObserver = Observer<Boolean> { onAuthStatusChange(it) }
private fun onAuthStatusChange(isAuthenticated: Boolean) {
Timber.v("onAuthStatusChange() called with: isAuthenticated = $isAuthenticated")
if (!isAuthenticated) {
authStatuses.removeObserver(authStatusObserver)
navigateToAuthFragment()
}
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@@ -48,7 +37,6 @@ class RecipeInfoFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
Timber.v("onViewCreated() called with: view = $view, savedInstanceState = $savedInstanceState") Timber.v("onViewCreated() called with: view = $view, savedInstanceState = $savedInstanceState")
authStatuses.observe(this, authStatusObserver)
viewModel.loadRecipeImage(binding.image, arguments.recipeSlug) viewModel.loadRecipeImage(binding.image, arguments.recipeSlug)
viewModel.loadRecipeInfo(arguments.recipeId, arguments.recipeSlug) viewModel.loadRecipeInfo(arguments.recipeId, arguments.recipeSlug)
@@ -71,10 +59,8 @@ class RecipeInfoFragment : Fragment() {
(requireActivity() as? AppCompatActivity)?.supportActionBar?.title = null (requireActivity() as? AppCompatActivity)?.supportActionBar?.title = null
} }
private fun navigateToAuthFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
Timber.v("navigateToAuthFragment() called") BottomSheetDialog(requireContext(), R.style.NoShapeBottomSheetDialog)
findNavController().navigate(RecipeInfoFragmentDirections.actionRecipeInfoFragmentToAuthenticationFragment())
}
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners
android:topLeftRadius="@dimen/rounded_corner_size_default"
android:topRightRadius="@dimen/rounded_corner_size_default" />
</shape>

View File

@@ -10,10 +10,11 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<ImageView <com.google.android.material.imageview.ShapeableImageView
android:id="@+id/image" android:id="@+id/image"
android:scaleType="centerCrop" android:scaleType="centerCrop"
android:layout_width="0dp" android:layout_width="0dp"
app:shapeAppearance="@style/ShapeAppearance.AllCornersRounded"
android:layout_height="@dimen/fragment_recipe_info_image_height" android:layout_height="@dimen/fragment_recipe_info_image_height"
android:layout_marginBottom="@dimen/margin_small" android:layout_marginBottom="@dimen/margin_small"
android:contentDescription="@string/content_description_fragment_recipe_info_image" android:contentDescription="@string/content_description_fragment_recipe_info_image"

View File

@@ -30,23 +30,18 @@
android:id="@+id/action_recipesFragment_to_recipeInfoFragment" android:id="@+id/action_recipesFragment_to_recipeInfoFragment"
app:destination="@id/recipeInfoFragment" /> app:destination="@id/recipeInfoFragment" />
</fragment> </fragment>
<fragment <dialog
android:id="@+id/recipeInfoFragment" android:id="@+id/recipeInfoFragment"
android:name="gq.kirmanak.mealient.ui.recipes.info.RecipeInfoFragment" android:name="gq.kirmanak.mealient.ui.recipes.info.RecipeInfoFragment"
android:label="RecipeInfoFragment" android:label="RecipeInfoFragment"
tools:layout="@layout/fragment_recipe_info"> tools:layout="@layout/fragment_recipe_info">
<action
android:id="@+id/action_recipeInfoFragment_to_authenticationFragment"
app:destination="@id/authenticationFragment"
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true" />
<argument <argument
android:name="recipe_slug" android:name="recipe_slug"
app:argType="string" /> app:argType="string" />
<argument <argument
android:name="recipe_id" android:name="recipe_id"
app:argType="long" /> app:argType="long" />
</fragment> </dialog>
<fragment <fragment
android:id="@+id/disclaimerFragment" android:id="@+id/disclaimerFragment"
android:name="gq.kirmanak.mealient.ui.disclaimer.DisclaimerFragment" android:name="gq.kirmanak.mealient.ui.disclaimer.DisclaimerFragment"

View File

@@ -12,4 +12,21 @@
<item name="android:layout_height">wrap_content</item> <item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">@dimen/margin_small</item> <item name="android:layout_margin">@dimen/margin_small</item>
</style> </style>
<style name="ShapeAppearance.AllCornersRounded" parent="ShapeAppearance.MaterialComponents">
<item name="cornerSize">@dimen/rounded_corner_size_default</item>
</style>
<!-- This is a workaround to support always round corners of the bottom sheet
See more at https://github.com/material-components/material-components-android/pull/437#issuecomment-852461685 -->
<style name="NoShapeBottomSheetDialog" parent="ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/NoShapeBottomSheet</item>
</style>
<style name="NoShapeBottomSheet" parent="Widget.MaterialComponents.BottomSheet.Modal">
<item name="shapeAppearance">@null</item>
<item name="shapeAppearanceOverlay">@null</item>
<item name="behavior_fitToContents">false</item>
<item name="android:background">@drawable/recipe_info_background</item>
</style>
</resources> </resources>