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

[CA018] added like functionality [SD,SM] #72

Merged
merged 18 commits into from
May 19, 2021
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
9 changes: 9 additions & 0 deletions Frontend/.idea/Frontend.iml

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

10 changes: 10 additions & 0 deletions Frontend/Android/.idea/runConfigurations.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.team03.cocktailrecipesapp.dialogs.DeleteRecipeDialogFragment
import com.team03.cocktailrecipesapp.error_listener.DeleteRecipeErrorListener
import com.team03.cocktailrecipesapp.error_listener.GetRecipeErrorListener
Expand All @@ -21,7 +23,7 @@ import kotlinx.android.synthetic.main.rating_layout.view.*
import kotlinx.android.synthetic.main.trending_cocktail_list_card.cocktail_difficulty
import kotlinx.android.synthetic.main.trending_cocktail_list_card.cocktail_name
import kotlinx.android.synthetic.main.trending_cocktail_list_card.cocktail_rating_bar

import kotlinx.android.synthetic.main.trending_cocktail_list_card.view.*

public interface RatingInterface {
fun onSelectedData(rating: Int);
Expand Down Expand Up @@ -73,8 +75,10 @@ class RecipeAdapter(private val context: Context,

class CocktailDetailActivity : AppCompatActivity(), RatingInterface {

var recipe_id = -1
var my_rating = 0
var isLiked = false;
var recipe_id = 0;
var my_rating = 0;

// lateinit var imgBtnRate : ImageButton

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -83,8 +87,7 @@ class CocktailDetailActivity : AppCompatActivity(), RatingInterface {
cocktail_creator_username.setOnClickListener { onRecipeCreatorClick() }

val b = intent.extras
// imgBtnRate = findViewById(R.id.imgBtnRate)

recipe_id = -1 // or other values

if (b != null) recipe_id = b.getInt("cocktail_id")

Expand Down Expand Up @@ -138,6 +141,15 @@ class CocktailDetailActivity : AppCompatActivity(), RatingInterface {
cocktail_instruction.text = recipe.instruction
my_rating = recipe.my_rating
cocktail_creator_username.text = recipe.creator_user
if (recipe.liked != 0)
{
isLiked = true
var imgLike = findViewById<ImageButton>(R.id.imageButtonLike);
imgLike.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.heart_filled ))
}




if (recipe.is_mine == 1)
delete_recipe_button.visibility = View.VISIBLE;
Expand All @@ -146,7 +158,6 @@ class CocktailDetailActivity : AppCompatActivity(), RatingInterface {
val adapter = RecipeAdapter(this, recipe.ingredients)
listView.adapter = adapter


setVisibleAfterLoading()

}
Expand Down Expand Up @@ -179,7 +190,9 @@ class CocktailDetailActivity : AppCompatActivity(), RatingInterface {
cocktail_instruction.visibility = View.INVISIBLE
cocktail_creator_username.visibility = View.INVISIBLE
cocktail_creator_text.visibility = View.INVISIBLE

imageButtonLike.visibility = View.INVISIBLE
imgBtnRate.visibility = View.INVISIBLE
tvRating.visibility = View.INVISIBLE
prepTimeText.visibility = View.INVISIBLE
prepTimeText3.visibility = View.INVISIBLE
ingredients.visibility = View.INVISIBLE
Expand All @@ -199,7 +212,9 @@ class CocktailDetailActivity : AppCompatActivity(), RatingInterface {
cocktail_instruction.visibility = View.VISIBLE
cocktail_creator_username.visibility = View.VISIBLE
cocktail_creator_text.visibility = View.VISIBLE

imageButtonLike.visibility = View.VISIBLE
imgBtnRate.visibility = View.VISIBLE
tvRating.visibility = View.VISIBLE
prepTimeText.visibility = View.VISIBLE
prepTimeText3.visibility = View.VISIBLE
ingredients.visibility = View.VISIBLE
Expand All @@ -210,6 +225,61 @@ class CocktailDetailActivity : AppCompatActivity(), RatingInterface {
progressbar.visibility = View.INVISIBLE
}

fun onUnsuccessfullLike(){
//TODO ERROR_MSG
System.out.println("not successful\n")
}

fun onSuccessfullLike(){
var imgLike = findViewById<ImageButton>(R.id.imageButtonLike);
if(!isLiked) {
imgLike.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.heart_filled ));
isLiked = true
} else {
imgLike.setImageDrawable(ContextCompat.getDrawable(applicationContext, R.drawable.hearth_empty ));
isLiked = false
}
}

fun like(user_id: Int, recipe_id: Int): Boolean {
val server = serverAPI(this)
val listener = LikeListener(::onSuccessfullLike)
val error_listener = LikeErrorListener(::onUnsuccessfullLike)
val answer = server.likeRecipe(user_id, recipe_id,
listener, error_listener);
if (answer == 1)
{
return false
}
return true
}

fun unlike(user_id: Int, recipe_id: Int): Boolean {
val server = serverAPI(this)
val listener = LikeListener(::onSuccessfullLike)
val error_listener = LikeErrorListener(::onUnsuccessfullLike)
val answer = server.unlikeRecipe(user_id, recipe_id,
listener, error_listener);
if (answer == 1)
{
return false
}
return true
}

fun likeOnClick(view: View) {
if(isLiked){
run {
unlike(userId, recipe_id);
}

}
else{
run {
like(userId, recipe_id);
}
}
}

fun rateRecipe(view: View)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.team03.cocktailrecipesapp

import com.android.volley.Response
import com.android.volley.VolleyError

class LikeErrorListener : Response.ErrorListener {
private var onErrorLike : (() -> Unit);

constructor(_onErrorRegister: (() -> Unit)){
onErrorLike = _onErrorRegister;
}

override fun onErrorResponse(error: VolleyError) {
onErrorLike.invoke();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.team03.cocktailrecipesapp

import com.android.volley.Response
import com.google.gson.Gson
import org.json.JSONObject

data class AnswerSuccess(
var success: Boolean,
var message: String
)

class LikeListener : Response.Listener<JSONObject> {
private var onSuccessfullLike : (() -> Unit);

constructor(_onSuccessfullLike: (() -> Unit)){
onSuccessfullLike = _onSuccessfullLike;
}

override fun onResponse(response: JSONObject) {
val answer = Gson().fromJson(response.toString(), AnswerSuccess::class.java)
onSuccessfullLike.invoke();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MainActivity : SharedPreferencesActivity() {
lateinit var recipesLayout: LinearLayout

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
super.onStart()
var language = shared.getString("Language", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ class serverAPI(context: Context)
return sendRequest(json, "like-recipe", listener, error_listener)
}


fun unlikeRecipe(user_id: Int, recipe_id: Int, listener: Response.Listener<JSONObject>, error_listener: Response.ErrorListener) : Int
{
val unlike_recipe_json = """
{
"user_id": """" + user_id.toString() + """",
"recipe_id": """" + recipe_id.toString() + """"
}
"""

val json = JSONObject(unlike_recipe_json)
return sendRequest(json, "unlike-recipe", listener, error_listener)
}

private fun sendRequest(jsonObject: JSONObject, service: String, listener: Response.Listener<JSONObject>, error_listener: Response.ErrorListener) : Int
{
val queue = Volley.newRequestQueue(context_)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,16 @@
app:layout_constraintStart_toEndOf="@+id/cocktail_creator_text"
app:layout_constraintTop_toTopOf="@+id/cocktail_creator_text" />

<ImageButton
android:id="@+id/imageButtonLike"
android:layout_width="48dp"
android:layout_height="48dp"
android:backgroundTint="#00FFFFFF"
android:onClick="likeOnClick"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView8"
app:srcCompat="@drawable/hearth_empty" />

</androidx.constraintlayout.widget.ConstraintLayout>