Skip to content

Commit

Permalink
feat (BookMarkRecipe): create delete function on BookMark
Browse files Browse the repository at this point in the history
  • Loading branch information
faizul14 committed Jan 19, 2024
1 parent 00f44b1 commit 6ee1928
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faezolmp.mealrecipeapp.core.data

import com.faezolmp.mealrecipeapp.core.data.source.local.LocalDataSource
import com.faezolmp.mealrecipeapp.core.data.source.local.entity.EntityMeal
import com.faezolmp.mealrecipeapp.core.data.source.local.room.DaoMeal
import com.faezolmp.mealrecipeapp.core.data.source.remote.RemoteDataSource
Expand All @@ -20,7 +21,7 @@ import javax.inject.Inject

class ImplementRepository @Inject constructor(
private val remoteDataSource: RemoteDataSource,
private val daoMeal: DaoMeal,
private val localDataSource: LocalDataSource,
) : Repository {
override fun sampleinterface() = "retunvaluesample"
override fun getListCategory(): Flow<Resource<List<ModelListCategory>>> {
Expand Down Expand Up @@ -96,7 +97,7 @@ class ImplementRepository @Inject constructor(
return flow {
emit(Resource.Loading())
try {
val dataResponse = daoMeal.getListDataMeal().first()
val dataResponse = localDataSource.getDataListMeal().first()
if (dataResponse.isNotEmpty()){
val data = DataMapper.mapperDataListBookMarkFromDataLayerToDomainLayer(dataResponse)
val dataResult = flowOf(Resource.Success(data))
Expand All @@ -111,10 +112,10 @@ class ImplementRepository @Inject constructor(
}

override suspend fun addDataMealBooMark(dataBookMarkMeal: ModelListMealBookMark) {
daoMeal.addMeal(DataMapper.mapperDataListBookMarkFromDomainLyerToDataLayer(dataBookMarkMeal))
localDataSource.addDataMeal(DataMapper.mapperDataListBookMarkFromDomainLyerToDataLayer(dataBookMarkMeal))
}

override fun deleteDataMealBooMark(dataBookMarkMeal: ModelListMealBookMark) {
daoMeal.deleteMeal(DataMapper.mapperDataListBookMarkFromDomainLyerToDataLayer(dataBookMarkMeal))
localDataSource.deleteDataMeal(DataMapper.mapperDataListBookMarkFromDomainLyerToDataLayer(dataBookMarkMeal))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LocalDataSource @Inject constructor(private val daoMeal: DaoMeal) {

suspend fun addDataMeal(dataMeal: EntityMeal) = daoMeal.addMeal(dataMeal)
fun deleteDataMeal(dataMeal: EntityMeal) = executorService.execute{
daoMeal.deleteMeal(dataMeal)
dataMeal.idMeal?.let { daoMeal.deleteMeal(it) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ interface DaoMeal {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMeal(entityMeal: EntityMeal)

@Delete
fun deleteMeal(entityMeal: EntityMeal)
@Query("DELETE FROM EntityMeal WHERE idMeal=:idMeal")
fun deleteMeal(idMeal: String)

@Query("SELECT * FROM EntityMeal")
fun getListDataMeal(): Flow<List<EntityMeal>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.faezolmp.mealrecipeapp.core.domain.model.ModelListMealBookMark
import com.faezolmp.mealrecipeapp.core.domain.model.ModelListMealByCategory
import com.faezolmp.mealrecipeapp.core.domain.repository.Repository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class ImplementUseCase @Inject constructor(
Expand Down Expand Up @@ -43,4 +45,21 @@ class ImplementUseCase @Inject constructor(
override fun deleteDataMealBooMark(dataBooMarkMeal: ModelListMealBookMark) {
repository.deleteDataMealBooMark(dataBooMarkMeal)
}

override fun isBookMark(idmeal: String): Flow<Boolean> = flow {
repository.getDataMailBookmark().collect { resource ->
when (resource) {
is Resource.Success -> {
val dataList = resource.data
val isBookmarked = dataList?.any { it.idMeal == idmeal }
if (isBookmarked == true) {
emit(true)
}
}
else -> {
emit(false)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface UseCase {

suspend fun addDataMealBooMark(dataBooMarkMeal: ModelListMealBookMark)
fun deleteDataMealBooMark(dataBooMarkMeal: ModelListMealBookMark)

fun isBookMark(idmeal: String): Flow<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DetailReceipeActivity : AppCompatActivity() {
private lateinit var binding: ActivityDetailReceipeBinding
private val viewModel: DetailReceipeViewModel by viewModels()
private lateinit var idmeal: String
private var isBookMark : Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -33,6 +34,14 @@ class DetailReceipeActivity : AppCompatActivity() {
}

private fun observerViewModel() {

viewModel.isBookMark(idmeal).observe(this){dataBookMark ->
if (dataBookMark is Boolean){
isBookMark = dataBookMark
isDataAvailable()
}
}

viewModel.getDetailDataMeal(idmeal).observe(this) { dataMeal ->
when (dataMeal) {
is Resource.Loading -> {
Expand All @@ -54,6 +63,13 @@ class DetailReceipeActivity : AppCompatActivity() {
}
}

private fun isDataAvailable(){
binding.apply {
val imgdraw = if (isBookMark) resources.getDrawable(R.drawable.mark_ic_true) else resources.getDrawable(R.drawable.mark_ic_false)
imgFavorite.setImageDrawable(imgdraw)
}
}

private fun hitView(dataDetail: ModelDetailDataMeal) {
binding.apply {

Expand All @@ -71,8 +87,15 @@ class DetailReceipeActivity : AppCompatActivity() {
strMealThumb = dataDetail.strMealThumb,
idMeal = dataDetail.idMeal,
)
viewModel.addDataBookMarkMeal(data)
imgFavorite.setImageDrawable(resources.getDrawable(R.drawable.mark_ic_true))
if (isBookMark){
viewModel.deleteDataBookMark(data)
isBookMark = false
}else{
viewModel.addDataBookMarkMeal(data)
isBookMark = true
}
isDataAvailable()
// imgFavorite.setImageDrawable(resources.getDrawable(R.drawable.mark_ic_true))
}
Glide
.with(this@DetailReceipeActivity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ class DetailReceipeViewModel @Inject constructor(private val useCase: UseCase):
fun deleteDataBookMark(dataBokmarkMeal: ModelListMealBookMark){
useCase.deleteDataMealBooMark(dataBokmarkMeal)
}

fun isBookMark(idmeal: String) = useCase.isBookMark(idmeal).asLiveData()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.faezolmp.mealrecipeapp.core.data.Resource
import com.faezolmp.mealrecipeapp.core.ui.ListCategoryAdapter
import com.faezolmp.mealrecipeapp.core.ui.ListDataByAdapter
Expand Down Expand Up @@ -100,7 +101,7 @@ class MainActivity : AppCompatActivity(), ListCategoryAdapter.CallBackInterface
viewModel.getListDataMealByCategory(category).observe(this){dataMeal ->
when (dataMeal) {
is Resource.Loading -> {
Toast.makeText(this, "Loading .. .", Toast.LENGTH_SHORT).show()
// Toast.makeText(this, "Loading .. .", Toast.LENGTH_SHORT).show()
}

is Resource.Error -> {
Expand Down Expand Up @@ -131,6 +132,9 @@ class MainActivity : AppCompatActivity(), ListCategoryAdapter.CallBackInterface
loadingListMeal.visibility = View.VISIBLE
}
// Toast.makeText(this@MainActivity, category, Toast.LENGTH_SHORT).show()
binding.apply {
popularText.setText("Meal recipe $category")
}
try {
lifecycleScope.launch {
viewModel.categoryClick.value = category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SearchViewModel @Inject constructor(private val useCase: UseCase): ViewMod
val trackerSearch = MutableStateFlow("")

val querySearch = trackerSearch
.debounce(300)
.debounce(100)
.distinctUntilChanged()
.filter {
it.trim().isNotEmpty()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/category_card_bc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<solid android:color="#00FFFFFF"/>
<stroke
android:width="2dp"
android:color="#3ABC57" />
android:color="@color/colorPrimaryDark" />

<size
android:width="65dp"
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="@font/poppins_semibold"
android:text="Popular recipe"
android:text="List recipe"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="@+id/textView2"
Expand Down Expand Up @@ -318,7 +318,9 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/popular_text"
app:lottie_autoPlay="true"
app:lottie_rawRes="@raw/loading_anim" />
app:lottie_loop="true"
app:lottie_repeatMode="reverse"
app:lottie_rawRes="@raw/garpu_anim" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@
app:lottie_repeatMode="reverse"
app:lottie_loop="true"
app:lottie_speed="0.8"
app:lottie_rawRes="@raw/loading_anim" />
app:lottie_rawRes="@raw/garpu_anim" />


</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/raw/garpu_anim.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"v":"4.8.0","meta":{"g":"LottieFiles AE 3.0.2","a":"","k":"","d":"","tc":""},"fr":30,"ip":0,"op":80,"w":1080,"h":1080,"nm":"Comp 1","ddd":0,"assets":[],"layers":[{"ddd":0,"ind":1,"ty":4,"nm":"Layer 1","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.363],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":0,"s":[0]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":10,"s":[45]},{"i":{"x":[0.376],"y":[0.95]},"o":{"x":[0.333],"y":[0]},"t":20,"s":[45]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":30,"s":[0]},{"i":{"x":[0.376],"y":[0.9]},"o":{"x":[0.333],"y":[0]},"t":40,"s":[0]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":50,"s":[-45]},{"i":{"x":[0.35],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":60,"s":[-45]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":70,"s":[0]},{"t":80,"s":[0]}],"ix":10},"p":{"a":1,"k":[{"i":{"x":0.363,"y":1},"o":{"x":0.333,"y":0},"t":0,"s":[313.167,559,0],"to":[41.805,0.167,0],"ti":[-41.805,-0.167,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":10,"s":[564,560,0],"to":[0,0,0],"ti":[0,0,0]},{"i":{"x":0.376,"y":1},"o":{"x":0.333,"y":0},"t":20,"s":[564,560,0],"to":[36.899,-0.167,0],"ti":[-36.899,0.167,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":30,"s":[785.396,559,0],"to":[0,0,0],"ti":[0,0,0]},{"i":{"x":0.376,"y":1},"o":{"x":0.333,"y":0},"t":40,"s":[785.396,559,0],"to":[-44.566,0.5,0],"ti":[44.566,-0.5,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":50,"s":[518,562,0],"to":[0,0,0],"ti":[0,0,0]},{"i":{"x":0.35,"y":1},"o":{"x":0.333,"y":0},"t":60,"s":[518,562,0],"to":[-34.139,-0.5,0],"ti":[34.139,0.5,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":70,"s":[313.167,559,0],"to":[0,0,0],"ti":[0,0,0]},{"t":80,"s":[313.167,559,0]}],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ind":0,"ty":"sh","ix":1,"ks":{"a":0,"k":{"i":[[42.262,17.184],[0,-17.184],[0,38.663],[0,30.061],[14.735,0],[0,-30.073],[0,-38.652],[0,47.223],[0,64.426],[0,-84.993],[-43.336,0],[0,-90.196],[-35.859,0],[0,90.195],[0,0],[0,88.872]],"o":[[0,64.426],[0,47.223],[0,-38.652],[0,-30.073],[-14.744,0],[0,30.061],[0,38.663],[0,-17.184],[-42.254,17.184],[0,88.872],[0,0],[0,90.195],[35.858,0],[0,-90.196],[43.336,0],[0,-84.993]],"v":[[76.482,-396.503],[76.482,-220.402],[23.224,-216.115],[23.224,-346.044],[0.004,-426.25],[-23.224,-346.044],[-23.224,-216.115],[-76.482,-220.402],[-76.482,-396.503],[-122.452,-171.249],[-29.521,-48.604],[-67.537,301.705],[0.004,426.25],[67.536,301.705],[29.527,-48.604],[122.452,-171.249]],"c":true},"ix":2},"nm":"Path 1","mn":"ADBE Vector Shape - Group","hd":false},{"ty":"fl","c":{"a":0,"k":[0.227358709597,0.266143409879,0.345098039216,1],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false},{"ty":"tr","p":{"a":0,"k":[0,0],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"Group 1","np":2,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false}],"ip":0,"op":80,"st":0,"bm":0},{"ddd":0,"ind":2,"ty":4,"nm":"Layer 3","parent":1,"td":1,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":0,"k":0,"ix":10},"p":{"a":0,"k":[0.069,-30.501,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[111,111,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ind":0,"ty":"sh","ix":1,"ks":{"a":0,"k":{"i":[[-43.336,0],[0,-90.196],[-35.859,0],[0,90.195],[0,0]],"o":[[0,0],[0,90.195],[35.858,0],[0,-90.196],[43.336,0]],"v":[[-29.521,-48.604],[-67.537,301.705],[0.004,426.25],[67.536,301.705],[29.527,-48.604]],"c":true},"ix":2},"nm":"Path 1","mn":"ADBE Vector Shape - Group","hd":false},{"ty":"fl","c":{"a":0,"k":[0.8,0.125490188599,0.665098063151,1],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false},{"ty":"tr","p":{"a":0,"k":[0,0],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"Group 1","np":2,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false}],"ip":0,"op":80,"st":0,"bm":0},{"ddd":0,"ind":3,"ty":4,"nm":"Layer 2","tt":2,"sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.363],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":0,"s":[0]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":10,"s":[-45]},{"i":{"x":[0.376],"y":[1.05]},"o":{"x":[0.333],"y":[0]},"t":20,"s":[-45]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":30,"s":[0]},{"i":{"x":[0.376],"y":[1.1]},"o":{"x":[0.333],"y":[0]},"t":40,"s":[0]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":50,"s":[45]},{"i":{"x":[0.35],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":60,"s":[45]},{"i":{"x":[0.667],"y":[1]},"o":{"x":[0.333],"y":[0]},"t":70,"s":[0]},{"t":80,"s":[0]}],"ix":10},"p":{"a":1,"k":[{"i":{"x":0.363,"y":1},"o":{"x":0.333,"y":0},"t":0,"s":[785.396,559,0],"to":[-44.566,0.5,0],"ti":[44.566,-0.5,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":10,"s":[518,562,0],"to":[0,0,0],"ti":[0,0,0]},{"i":{"x":0.376,"y":1},"o":{"x":0.333,"y":0},"t":20,"s":[518,562,0],"to":[-34.139,-0.5,0],"ti":[34.139,0.5,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":30,"s":[313.167,559,0],"to":[0,0,0],"ti":[0,0,0]},{"i":{"x":0.376,"y":1},"o":{"x":0.333,"y":0},"t":40,"s":[313.167,559,0],"to":[41.805,0.167,0],"ti":[-41.805,-0.167,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":50,"s":[564,560,0],"to":[0,0,0],"ti":[0,0,0]},{"i":{"x":0.35,"y":1},"o":{"x":0.333,"y":0},"t":60,"s":[564,560,0],"to":[36.899,-0.167,0],"ti":[-36.899,0.167,0]},{"i":{"x":0.667,"y":0.667},"o":{"x":0.333,"y":0.333},"t":70,"s":[785.396,559,0],"to":[0,0,0],"ti":[0,0,0]},{"t":80,"s":[785.396,559,0]}],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ind":0,"ty":"sh","ix":1,"ks":{"a":0,"k":{"i":[[2.12,158.409],[59.132,0],[1.317,-98.688],[0,-39.576],[0,-90.196],[-35.851,0],[0,90.195],[0,0]],"o":[[-1.309,-98.688],[-59.131,0],[-2.112,158.409],[0,0],[0,90.195],[35.859,0],[0,-90.196],[0,-39.576]],"v":[[133.048,-234.994],[0.003,-426.25],[-133.048,-234.994],[-29.512,-48.604],[-67.536,301.705],[0.003,426.25],[67.544,301.705],[29.528,-48.604]],"c":true},"ix":2},"nm":"Path 1","mn":"ADBE Vector Shape - Group","hd":false},{"ty":"fl","c":{"a":0,"k":[0.227358709597,0.266143409879,0.345098039216,1],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false},{"ty":"tr","p":{"a":0,"k":[0,0],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"Group 1","np":2,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false}],"ip":0,"op":80,"st":0,"bm":0}],"markers":[]}

0 comments on commit 6ee1928

Please sign in to comment.