-
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.
Favorites feature merged
- Loading branch information
Showing
17 changed files
with
446 additions
and
105 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
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
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/example/foodies/adapters/FavoritesMealsAdapter.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,54 @@ | ||
package com.example.foodies.adapters | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.AsyncListDiffer | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.RecyclerView | ||
import coil.load | ||
import com.example.foodies.databinding.MealsItemBinding | ||
import com.example.foodies.module.mostpopular.MostPopularMeal | ||
import com.example.foodies.module.randommeal.Meal | ||
|
||
class FavoritesMealsAdapter: RecyclerView.Adapter<FavoritesMealsAdapter.ViewHolder>() { | ||
|
||
var onMealClick: ((Meal) -> Unit)? = null | ||
|
||
class ViewHolder(val binding: MealsItemBinding): RecyclerView.ViewHolder(binding.root) | ||
|
||
private val diffUtil = object : DiffUtil.ItemCallback<Meal>(){ | ||
override fun areItemsTheSame(oldItem: Meal, newItem: Meal): Boolean { | ||
return oldItem.idMeal == newItem.idMeal | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: Meal, newItem: Meal): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
} | ||
|
||
val differ = AsyncListDiffer(this, diffUtil) | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
return ViewHolder( | ||
MealsItemBinding.inflate(LayoutInflater.from(parent.context), | ||
parent, | ||
false | ||
) | ||
) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val currentItem = differ.currentList[position] | ||
holder.binding.imgMeal.load(currentItem.strMealThumb) | ||
holder.binding.tvMealName.text = currentItem.strMeal | ||
|
||
holder.itemView.setOnClickListener { | ||
with(onMealClick) {this!!.invoke(currentItem)} | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return differ.currentList.size | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/example/foodies/database/Converters.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,23 @@ | ||
package com.example.foodies.database | ||
|
||
import androidx.room.TypeConverter | ||
|
||
|
||
class Converters { | ||
|
||
@TypeConverter | ||
fun fromAny(any: Any?): String { | ||
if (any == null) | ||
return "" | ||
return any as String | ||
} | ||
|
||
@TypeConverter | ||
fun toAny(string: String?): Any { | ||
if (string == null) | ||
return "" | ||
return string | ||
} | ||
|
||
|
||
} |
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,20 @@ | ||
package com.example.foodies.database | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.* | ||
import androidx.room.Dao | ||
import com.example.foodies.module.randommeal.Meal | ||
|
||
@Dao | ||
interface Dao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertUpdate(meal: Meal) | ||
|
||
@Delete | ||
suspend fun delete(meal: Meal) | ||
|
||
@Query("SELECT * FROM meal_table") | ||
fun getAllMeals(): LiveData<List<Meal>> | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/example/foodies/database/MealDatabase.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,37 @@ | ||
package com.example.foodies.database | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import com.example.foodies.module.randommeal.Meal | ||
|
||
@Database(entities = [Meal::class], version = 1, exportSchema = false) | ||
@TypeConverters(Converters::class) | ||
abstract class MealDatabase: RoomDatabase() { | ||
|
||
abstract fun dao(): Dao | ||
|
||
companion object { | ||
|
||
@Volatile | ||
private var INSTANCE: MealDatabase? = null | ||
|
||
@Synchronized | ||
fun getDatabase(context: Context): MealDatabase { | ||
val tempInstance = INSTANCE | ||
tempInstance?.let { | ||
return tempInstance | ||
} | ||
|
||
val instance = Room.databaseBuilder( | ||
context.applicationContext, | ||
MealDatabase::class.java, | ||
"meal_database" | ||
).build() | ||
INSTANCE = instance | ||
return instance | ||
} | ||
} | ||
} |
46 changes: 45 additions & 1 deletion
46
app/src/main/java/com/example/foodies/fragments/categories/CategoriesFragment.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 |
---|---|---|
@@ -1,19 +1,63 @@ | ||
package com.example.foodies.fragments.categories | ||
|
||
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.viewModels | ||
import androidx.lifecycle.Observer | ||
import androidx.recyclerview.widget.GridLayoutManager | ||
import com.example.foodies.R | ||
import com.example.foodies.activities.CategoriesActivity | ||
import com.example.foodies.adapters.CategoriesAdapter | ||
import com.example.foodies.databinding.FragmentCategoriesBinding | ||
import com.example.foodies.fragments.home.HomeFragment | ||
import com.example.foodies.viewmodel.HomeViewModel | ||
|
||
class CategoriesFragment : Fragment() { | ||
private lateinit var binding: FragmentCategoriesBinding | ||
private lateinit var adapterCategories: CategoriesAdapter | ||
private val viewModel: HomeViewModel by viewModels() | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
// Inflate the layout for this fragment | ||
return inflater.inflate(R.layout.fragment_categories, container, false) | ||
binding = FragmentCategoriesBinding.inflate(inflater, container, false) | ||
adapterCategories = CategoriesAdapter() | ||
|
||
|
||
prepareCategoriesRecyclerView() | ||
|
||
viewModel.getCategories() | ||
observeCategories() | ||
onCategoryItemClick() | ||
|
||
return binding.root | ||
} | ||
|
||
private fun onCategoryItemClick() { | ||
adapterCategories.onItemClick = { | ||
val intent = Intent(activity, CategoriesActivity::class.java) | ||
intent.putExtra(HomeFragment.CATEGORY_TITLE, it.strCategory) | ||
startActivity(intent) | ||
} | ||
} | ||
|
||
private fun observeCategories() { | ||
viewModel.categories.observe(viewLifecycleOwner, Observer { | ||
adapterCategories.setData(it) | ||
}) | ||
} | ||
|
||
private fun prepareCategoriesRecyclerView() { | ||
binding.rvCategoriesMeal.apply { | ||
layoutManager = GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false) | ||
adapter = adapterCategories | ||
} | ||
} | ||
} |
Oops, something went wrong.