-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28ba299
commit 8830cb8
Showing
9 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/foodies/viewmodel/SearchViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters