Skip to content

Commit

Permalink
Merge pull request #35 from mohand3del/feature/profile
Browse files Browse the repository at this point in the history
Finished the profile screen with it's logic
  • Loading branch information
eIbrahim67 authored Aug 29, 2024
2 parents d07a2c4 + d8336ca commit 3c67253
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.recipeappiti.core.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -13,6 +14,7 @@ class DataViewModel(
private val userRepository: UserRepository,
private val mealRepository: MealRepository
) : ViewModel() {
private val _favouritesList = MutableLiveData<MutableSet<String>>()

private val _categorySearch = MutableLiveData<String?>()
val categorySearch: LiveData<String?> get() = _categorySearch
Expand All @@ -23,15 +25,12 @@ class DataViewModel(
private val _itemDetails = MutableLiveData<String>()
val itemDetails: LiveData<String> get() = _itemDetails

private val _favouritesList = MutableLiveData<MutableSet<String>>()

private val _isFavourite = MutableLiveData<Boolean>()
val isFavourite: LiveData<Boolean> get() = _isFavourite

private val _recipes = MutableLiveData<MutableList<Meal>>()
val recipes: LiveData<MutableList<Meal>> get() = _recipes


private val _cuisinesData = MutableLiveData<List<String>>()
val cuisinesData: LiveData<List<String>> get() = _cuisinesData

Expand All @@ -43,13 +42,11 @@ class DataViewModel(
}

fun setCuisines(cuisines: List<String>) {

_cuisinesData.value = cuisines
Log.d("cuisines", _cuisinesData.value.toString())

viewModelScope.launch {

userRepository.updateCuisines(cuisines)

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import com.example.recipeappiti.R
import com.example.recipeappiti.core.model.remote.Area
import com.google.android.material.chip.Chip

class AdapterRVMyCuisines(
class AdapterRVProfileCuisines(
private val chipList: List<Area>,
private var lastCuisine: ((String?) -> Unit)?,
private val myChipsList: List<Area>?
private val userCuisines: List<String>?
) :
RecyclerView.Adapter<AdapterRVMyCuisines.ChipViewHolder>() {

RecyclerView.Adapter<AdapterRVProfileCuisines.ChipViewHolder>() {
private val selectedCuisines = mutableSetOf<String>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChipViewHolder {
Expand All @@ -24,15 +23,14 @@ class AdapterRVMyCuisines(
}

override fun onBindViewHolder(holder: ChipViewHolder, position: Int) {


val chip = chipList[position]
val chipText = chipList[position].strArea
val chipText = chip.strArea
holder.chip.text = chipText

holder.chip.isChecked = selectedCuisines.contains(chipText)

holder.chip.isChecked = myChipsList?.contains(chip) == true
if (userCuisines?.contains(chipText) == true) {
selectedCuisines.add(chipText)
holder.chip.isChecked = true
}

holder.chip.setOnClickListener {
if (selectedCuisines.contains(chipText)) {
Expand All @@ -47,7 +45,6 @@ class AdapterRVMyCuisines(
lastCuisine?.invoke(null)
else
lastCuisine?.invoke(selectedCuisines.firstOrNull())

}
}

Expand All @@ -58,5 +55,4 @@ class AdapterRVMyCuisines(
}

fun getSelectedCuisines() = selectedCuisines.toList()

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import com.example.recipeappiti.core.viewmodel.DataViewModel
import com.example.recipeappiti.core.viewmodel.DataViewModelFactory
import com.example.recipeappiti.home.viewModel.BottomSheetCuisinesViewModel
import com.example.recipeappiti.home.viewModel.BottomSheetCuisinesViewModelFactory
import com.example.recipeappiti.profile.view.adapters.AdapterRVMyCuisines
import com.example.recipeappiti.profile.view.adapters.AdapterRVProfileCuisines
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class BottomSheetMyCuisines : BottomSheetDialogFragment() {

private val viewModel: BottomSheetCuisinesViewModel by viewModels {
class ProfileCuisinesSheet : BottomSheetDialogFragment() {
private val sheetViewModel: BottomSheetCuisinesViewModel by viewModels {
val remoteGsonDataSource = RemoteGsonDataImpl()
val database = UserDatabase.getDatabaseInstance(requireContext())
val userDao = database.userDao()
Expand Down Expand Up @@ -60,23 +59,24 @@ class BottomSheetMyCuisines : BottomSheetDialogFragment() {

recyclerviewCuisines.layoutManager = gridLayoutManager

dataViewModel.cuisinesData.observe(viewLifecycleOwner) { dismiss() }

viewModel.getAllCuisines()
sheetViewModel.getAllCuisines()

viewModel.allCuisines.observe(viewLifecycleOwner) { response ->
sheetViewModel.allCuisines.observe(viewLifecycleOwner) { response ->
when (response) {
is Response.Loading -> {
progressBar.visibility = View.VISIBLE
}

is Response.Success -> {
val adapter = AdapterRVMyCuisines(response.data.meals, { lastCuisine ->
val adapter = AdapterRVProfileCuisines(response.data.meals, { lastCuisine ->
dataViewModel.updateMainCuisine(lastCuisine)
}, null)
}, dataViewModel.cuisinesData.value)
progressBar.visibility = View.GONE
recyclerviewCuisines.adapter = adapter
btnDone.setOnClickListener { dataViewModel.setCuisines(adapter.getSelectedCuisines()) }
btnDone.setOnClickListener {
dataViewModel.setCuisines(adapter.getSelectedCuisines())
dismiss()
}
}

is Response.Failure -> {
Expand All @@ -87,7 +87,5 @@ class BottomSheetMyCuisines : BottomSheetDialogFragment() {
return view
}

companion object {
const val TAG = "BottomSheetCuisines"
}
companion object { const val TAG = "BottomSheetCuisines" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,37 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import com.example.recipeappiti.R
import com.example.recipeappiti.core.model.local.repository.UserRepositoryImpl
import com.example.recipeappiti.core.model.local.source.LocalDataSourceImpl
import com.example.recipeappiti.core.model.local.source.UserDatabase
import com.example.recipeappiti.core.model.remote.Response
import com.example.recipeappiti.main.viewModel.RecipeActivityViewModel
import com.example.recipeappiti.main.viewModel.RecipeActivityViewModelFactory
import com.example.recipeappiti.profile.view.bottomSheets.ProfileCuisinesSheet
import com.google.android.material.bottomnavigation.BottomNavigationView

class ProfileFragment : Fragment() {
private val recipeViewModel: RecipeActivityViewModel by activityViewModels {
val database = UserDatabase.getDatabaseInstance(requireContext())
val userDao = database.userDao()
val localDataSource = LocalDataSourceImpl(userDao)
val userRepository = UserRepositoryImpl(localDataSource)
RecipeActivityViewModelFactory(userRepository)
}

private lateinit var bottomNavigationView: BottomNavigationView
private lateinit var navController: NavController
private lateinit var userNameText: TextView
private lateinit var userEmailText: TextView
private lateinit var userEditCuisines: TextView
private lateinit var profileBackBtn: ImageView

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -22,23 +46,44 @@ class ProfileFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initViews()

initViews(view)
initObservers()
}

private fun initViews() {

private fun initViews(view: View) {
bottomNavigationView = requireActivity().findViewById(R.id.bottom_navigation)
bottomNavigationView.visibility = View.GONE
navController = findNavController()

}
userNameText = view.findViewById(R.id.userNameProfile)
userEmailText = view.findViewById(R.id.userEmailProfile)

override fun onDestroyView() {
super.onDestroyView()
userEditCuisines = view.findViewById(R.id.userEditCuisines)

bottomNavigationView.visibility = View.VISIBLE
userEditCuisines.setOnClickListener {
val cuisinesSheet = ProfileCuisinesSheet()
cuisinesSheet.show(requireActivity().supportFragmentManager, ProfileCuisinesSheet.TAG)
}

profileBackBtn = view.findViewById(R.id.profileBackBtn)
profileBackBtn.setOnClickListener {
navController.popBackStack()
}
}

private fun initObservers() {
recipeViewModel.userName.observe(viewLifecycleOwner) { response ->
response as Response.Success
userNameText.text = response.data
}

recipeViewModel.userEmail.observe(viewLifecycleOwner) { response ->
response as Response.Success
userEmailText.text = response.data
}
}
override fun onDestroyView() {
super.onDestroyView()
bottomNavigationView.visibility = View.VISIBLE
}
}
86 changes: 81 additions & 5 deletions app/src/main/res/layout/fragment_profile.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,90 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".profile.view.fragments.ProfileFragment">

<com.google.android.material.card.MaterialCardView
android:id="@+id/profileBackCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="@dimen/cardCornerRadius"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">

<ImageView
android:id="@+id/profileBackBtn"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@color/white"
android:clickable="true"
android:focusable="true"
android:padding="15dp"
android:src="@drawable/back_arrows_icon_black" />
</com.google.android.material.card.MaterialCardView>

<androidx.cardview.widget.CardView
android:id="@+id/profileImageCard"
android:layout_width="150dp"
android:layout_height="150dp"
app:cardCornerRadius="500dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/man" />

</androidx.cardview.widget.CardView>

<TextView
android:id="@+id/userNameProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Name Name Name"
android:textColor="@color/black"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/profileImageCard" />

<TextView
android:id="@+id/userEmailProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Email Email Email"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/userNameProfile" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello_blank_fragment" />
android:id="@+id/userEditCuisines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="true"
android:fontFamily="@font/work_sans"
android:text="Edit Cuisines"
android:textColor="#146EEB"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/userEmailProfile" />

</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_recipe_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

<ImageView
android:id="@+id/btnBack"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_width="55dp"
android:layout_height="55dp"
android:background="@color/white"
android:clickable="true"
android:focusable="true"
Expand Down

0 comments on commit 3c67253

Please sign in to comment.