Skip to content
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

Search feature implemented #4

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions app/src/main/java/com/example/foodies/SearchFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.foodies

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import coil.load
import com.example.foodies.activities.MealActivity
import com.example.foodies.databinding.FragmentSearchBinding
import com.example.foodies.fragments.home.HomeFragment
import com.example.foodies.viewmodel.SearchViewModel


class SearchFragment : Fragment() {
private lateinit var binding: FragmentSearchBinding
private val viewModel: SearchViewModel by viewModels()
private var mealId = ""
private var mealStr = ""
private var mealThumb = ""


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentSearchBinding.inflate(inflater, container, false)


observeSearched()
setOnMealCardClick()
return binding.root
}

private fun setOnMealCardClick() {
binding.searchedMealCard.setOnClickListener {
val intent = Intent(activity, MealActivity::class.java)
intent.putExtra(HomeFragment.MEAL_ID, mealId)
intent.putExtra(HomeFragment.MEAL_NAME, mealStr)
intent.putExtra(HomeFragment.MEAL_THUMB, mealThumb)

startActivity(intent)
}
}

private fun observeSearched() {
binding.icSearch.setOnClickListener {
onSearchClick()
viewModel.searchedMeal.observe(viewLifecycleOwner, Observer {
binding.apply {
imgSearchedMeal.load(it.strMealThumb)
tvSearchedMeal.text = it.strMeal
searchedMealCard.visibility = View.VISIBLE
}
mealId = it.idMeal
mealStr = it.strMeal!!
mealThumb = it.strMealThumb!!
})
}
}

private fun onSearchClick() {
viewModel.getSearchedMeal(binding.edSearch.text.toString(), requireContext())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import androidx.core.content.ContextCompat.startActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import com.example.foodies.R
import com.example.foodies.activities.CategoriesActivity
import com.example.foodies.activities.MealActivity
import com.example.foodies.adapters.CategoriesAdapter
Expand Down Expand Up @@ -74,6 +76,14 @@ class HomeFragment : Fragment() {

onPopularItemLongClick()

onSearchIconClicked()

}

private fun onSearchIconClicked() {
binding.iconSearch.setOnClickListener {
findNavController().navigate(R.id.action_homeFragment_to_searchFragment)
}
}

private fun onPopularItemLongClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ interface MealsApi {
@GET("categories.php")
fun getCategories(): Call<CategoryList>

@GET("filter.php")
fun getMealsByCategory(@Query("c") category: String): Call<MostPopularMealList>
@GET("search.php?")
fun getMealBySearch(@Query("s") s:String):Call<RandomMeal>

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CategoriesViewModel: ViewModel() {
val meals: LiveData<List<MostPopularMeal>> = _meals

fun getMealsByCategory(category: String) {
MealApiService.retrofitInstance.getMealsByCategory(category).enqueue(object : Callback<MostPopularMealList> {
MealApiService.retrofitInstance.getPopularItems(category).enqueue(object : Callback<MostPopularMealList> {
override fun onResponse(
call: Call<MostPopularMealList>,
response: Response<MostPopularMealList>
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/example/foodies/viewmodel/SearchViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.foodies.viewmodel

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.foodies.module.randommeal.Meal
import com.example.foodies.module.randommeal.RandomMeal
import com.example.foodies.network.MealApiService
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SearchViewModel: ViewModel() {

private var _searchedMeal = MutableLiveData<Meal>()
val searchedMeal: LiveData<Meal> = _searchedMeal


fun getSearchedMeal(search: String, context: Context) {
MealApiService.retrofitInstance.getMealBySearch(search).enqueue(object : Callback<RandomMeal> {
override fun onResponse(call: Call<RandomMeal>, response: Response<RandomMeal>) {
if (response.body()?.meals == null) {
Toast.makeText(context, "No Such Such Dish Found", Toast.LENGTH_SHORT)
} else {
_searchedMeal.value = response.body()!!.meals.first()
}
}

override fun onFailure(call: Call<RandomMeal>, t: Throwable) {
Log.e("TAG", t.message.toString())
}

})
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/search_box_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E3E3E3"/>
<corners android:radius="20dp"/>

</shape>
82 changes: 82 additions & 0 deletions app/src/main/res/layout/fragment_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SearchFragment">

<EditText
android:id="@+id/edSearch"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@drawable/search_box_background"
android:paddingLeft="15dp"
android:hint="What dish you thinking off ..."
android:textSize="16sp"
android:inputType="text"
android:maxLines="1"/>

<ImageView
android:id="@+id/icSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_search"
app:layout_constraintStart_toEndOf="@id/edSearch"
app:layout_constraintBottom_toBottomOf="@id/edSearch"
app:layout_constraintTop_toTopOf="@id/edSearch"/>

<androidx.cardview.widget.CardView
android:id="@+id/searchedMealCard"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="220dp"
app:layout_constraintTop_toBottomOf="@id/edSearch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
app:cardCornerRadius="10dp"
app:cardUseCompatPadding="true">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imgSearchedMeal"
android:layout_width="match_parent"
android:layout_height="170dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:scaleType="centerCrop"/>

<TextView
android:id="@+id/tv_searched_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/imgSearchedMeal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>



</androidx.constraintlayout.widget.ConstraintLayout>
11 changes: 10 additions & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
android:id="@+id/homeFragment"
android:name="com.example.foodies.fragments.home.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_searchFragment"
app:destination="@id/searchFragment" />
</fragment>
<fragment
android:id="@+id/favoritesFragment"
android:name="com.example.foodies.fragments.favorites.FavoritesFragment"
Expand All @@ -20,4 +24,9 @@
android:name="com.example.foodies.fragments.categories.CategoriesFragment"
android:label="Categories"
tools:layout="@layout/fragment_categories" />
<fragment
android:id="@+id/searchFragment"
android:name="com.example.foodies.SearchFragment"
android:label="fragment_search"
tools:layout="@layout/fragment_search" />
</navigation>