Skip to content

Commit

Permalink
Merge pull request #2
Browse files Browse the repository at this point in the history
Favorites feature merged
  • Loading branch information
MathRoda committed Jun 14, 2022
2 parents 2e02482 + 7ba2807 commit de9fb2b
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -39,6 +40,8 @@ dependencies {

def nav_version = "2.4.2"
def lifecycle_version = "2.5.0-rc01"
def room_version = "2.4.2"


implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
Expand Down Expand Up @@ -70,4 +73,10 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

//Room
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"

}
28 changes: 2 additions & 26 deletions app/src/main/java/com/example/foodies/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.example.foodies.activities
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.example.foodies.R
import com.example.foodies.databinding.ActivityMainBinding
import com.example.foodies.viewmodel.HomeViewModel

class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
Expand All @@ -21,34 +23,8 @@ class MainActivity : AppCompatActivity() {

val navHost = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHost.navController

setupNav()

binding.bottomNav.setupWithNavController(navController)
}

private fun setupNav() {
val navHost = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHost.navController
navController.setGraph(R.navigation.nav_graph)

navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.homeFragment -> showBottomNav()
R.id.favoritesFragment -> showBottomNav()
R.id.categoriesFragment -> showBottomNav()
else -> hideBottomNav()
}
}
}

private fun showBottomNav() {
binding.bottomNav.visibility = View.VISIBLE

}

private fun hideBottomNav() {
binding.bottomNav.visibility = View.GONE

}
}
19 changes: 16 additions & 3 deletions app/src/main/java/com/example/foodies/activities/MealActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package com.example.foodies.activities

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.LifecycleOwner
import androidx.appcompat.app.AppCompatActivity
import coil.load
import com.example.foodies.R
import com.example.foodies.databinding.ActivityMealBinding
import com.example.foodies.fragments.home.HomeFragment
import com.example.foodies.module.randommeal.Meal
import com.example.foodies.viewmodel.MealViewModel

class MealActivity : AppCompatActivity() {
Expand All @@ -35,22 +36,34 @@ class MealActivity : AppCompatActivity() {
viewModel.getMealDetail(mealId)
observeMealDetails()
onYoutubeIconClicked()
onFavoriteButtonClick()

}

private fun onFavoriteButtonClick() {
binding.btnFavorite.setOnClickListener {
meal?.let { viewModel.insertUpdate(it) }
Toast
.makeText(this, "You've favored this dish",Toast.LENGTH_LONG)
.show()
}
}

private fun onYoutubeIconClicked() {
binding.youtubeIcon.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(youtubeLink))
startActivity(intent)
}
}

private var meal: Meal? = null
private fun observeMealDetails() {
viewModel.mealDetail.observe(this){
binding.tvCategory.text = it!!.strCategory
binding.tvLocation.text = it.strArea
binding.tvInstructionsSteps.text = it.strInstructions
youtubeLink = it.strYoutube
youtubeLink = it.strYoutube!!
meal = it
onResponseCase()
}
}
Expand Down
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 app/src/main/java/com/example/foodies/database/Converters.kt
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
}


}
20 changes: 20 additions & 0 deletions app/src/main/java/com/example/foodies/database/Dao.kt
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 app/src/main/java/com/example/foodies/database/MealDatabase.kt
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
}
}
}
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
}
}
}
Loading

0 comments on commit de9fb2b

Please sign in to comment.