Initialize RecipesFragment
This commit is contained in:
@@ -9,6 +9,7 @@ import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealie.databinding.FragmentAuthenticationBinding
|
||||
@@ -46,6 +47,7 @@ class AuthenticationFragment : Fragment() {
|
||||
private fun checkIfAuthenticatedAlready() {
|
||||
Timber.v("checkIfAuthenticatedAlready() called")
|
||||
lifecycleScope.launchWhenCreated {
|
||||
if (viewModel.isAuthenticated()) navigateToRecipes()
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
if (viewModel.isAuthenticated()) "User is authenticated"
|
||||
@@ -55,6 +57,10 @@ class AuthenticationFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun navigateToRecipes() {
|
||||
findNavController().navigate(AuthenticationFragmentDirections.actionAuthenticationFragmentToRecipesFragment())
|
||||
}
|
||||
|
||||
private fun onLoginClicked() {
|
||||
Timber.v("onLoginClicked() called")
|
||||
val email: String
|
||||
@@ -73,6 +79,7 @@ class AuthenticationFragment : Fragment() {
|
||||
}
|
||||
lifecycleScope.launchWhenResumed {
|
||||
val exception = viewModel.authenticate(email, pass, url)
|
||||
if (exception == null) navigateToRecipes()
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
"Exception is ${exception?.message ?: "null"}",
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package gq.kirmanak.mealie.ui.recipes
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.cachedIn
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class RecipeViewModel @Inject constructor(private val recipeRepo: RecipeRepo) : ViewModel() {
|
||||
private val pager: Pager<Int, RecipeEntity> by lazy { recipeRepo.createPager() }
|
||||
val recipeFlow: Flow<PagingData<RecipeEntity>> by lazy { pager.flow.cachedIn(viewModelScope) }
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package gq.kirmanak.mealie.ui.recipes
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealie.databinding.FragmentRecipesBinding
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
|
||||
@AndroidEntryPoint
|
||||
class RecipesFragment : Fragment() {
|
||||
private var _binding: FragmentRecipesBinding? = null
|
||||
private val binding: FragmentRecipesBinding
|
||||
get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" }
|
||||
private val viewModel by viewModels<RecipeViewModel>()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = FragmentRecipesBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
binding.recipes.layoutManager = LinearLayoutManager(requireContext())
|
||||
val recipesPagingAdapter = RecipesPagingAdapter()
|
||||
binding.recipes.adapter = recipesPagingAdapter
|
||||
lifecycleScope.launchWhenResumed {
|
||||
viewModel.recipeFlow.collectLatest {
|
||||
recipesPagingAdapter.submitData(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package gq.kirmanak.mealie.ui.recipes
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.paging.PagingDataAdapter
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.databinding.ViewHolderRecipeBinding
|
||||
import timber.log.Timber
|
||||
|
||||
class RecipesPagingAdapter : PagingDataAdapter<RecipeEntity, RecipeViewHolder>(RecipeDiffCallback) {
|
||||
override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
|
||||
val item = getItem(position)
|
||||
holder.bind(item)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
|
||||
Timber.v("onCreateViewHolder() called with: parent = $parent, viewType = $viewType")
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
val binding = ViewHolderRecipeBinding.inflate(inflater, parent, false)
|
||||
return RecipeViewHolder(binding)
|
||||
}
|
||||
}
|
||||
|
||||
class RecipeViewHolder(private val binding: ViewHolderRecipeBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
fun bind(item: RecipeEntity?) {
|
||||
binding.name.text = item?.name
|
||||
}
|
||||
}
|
||||
|
||||
private object RecipeDiffCallback : DiffUtil.ItemCallback<RecipeEntity>() {
|
||||
override fun areItemsTheSame(oldItem: RecipeEntity, newItem: RecipeEntity): Boolean {
|
||||
return oldItem.localId == newItem.localId
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: RecipeEntity, newItem: RecipeEntity): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user