Use view binding property delegate (#22)

This commit is contained in:
Kirill Kamakin
2021-12-27 11:37:10 +03:00
committed by GitHub
parent 7bd9d20e9d
commit a36fec226d
6 changed files with 21 additions and 89 deletions

View File

@@ -1,16 +1,16 @@
package gq.kirmanak.mealient.ui.recipes
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.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.data.recipes.db.entity.RecipeSummaryEntity
import gq.kirmanak.mealient.databinding.FragmentRecipesBinding
import gq.kirmanak.mealient.ui.auth.AuthenticationViewModel
@@ -21,10 +21,8 @@ import timber.log.Timber
@ExperimentalCoroutinesApi
@AndroidEntryPoint
class RecipesFragment : Fragment() {
private var _binding: FragmentRecipesBinding? = null
private val binding: FragmentRecipesBinding
get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" }
class RecipesFragment : Fragment(R.layout.fragment_recipes) {
private val binding by viewBinding(FragmentRecipesBinding::bind)
private val viewModel by viewModels<RecipeViewModel>()
private val authViewModel by viewModels<AuthenticationViewModel>()
@@ -38,16 +36,6 @@ class RecipesFragment : Fragment() {
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
Timber.v("onCreateView() called with: inflater = $inflater, container = $container, savedInstanceState = $savedInstanceState")
_binding = FragmentRecipesBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Timber.v("onViewCreated() called with: view = $view, savedInstanceState = $savedInstanceState")
@@ -89,10 +77,4 @@ class RecipesFragment : Fragment() {
}
}
}
override fun onDestroyView() {
super.onDestroyView()
Timber.v("onDestroyView() called")
_binding = null
}
}

View File

@@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
@@ -17,9 +18,7 @@ import timber.log.Timber
@AndroidEntryPoint
class RecipeInfoFragment : BottomSheetDialogFragment() {
private var _binding: FragmentRecipeInfoBinding? = null
private val binding: FragmentRecipeInfoBinding
get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" }
private val binding by viewBinding(FragmentRecipeInfoBinding::bind)
private val arguments by navArgs<RecipeInfoFragmentArgs>()
private val viewModel by viewModels<RecipeInfoViewModel>()
@@ -29,8 +28,7 @@ class RecipeInfoFragment : BottomSheetDialogFragment() {
savedInstanceState: Bundle?
): View {
Timber.v("onCreateView() called with: inflater = $inflater, container = $container, savedInstanceState = $savedInstanceState")
_binding = FragmentRecipeInfoBinding.inflate(inflater, container, false)
return binding.root
return FragmentRecipeInfoBinding.inflate(inflater, container, false).root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -53,10 +51,4 @@ class RecipeInfoFragment : BottomSheetDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
BottomSheetDialog(requireContext(), R.style.NoShapeBottomSheetDialog)
override fun onDestroyView() {
super.onDestroyView()
Timber.v("onDestroyView() called")
_binding = null
}
}