From 1939396aaac3b208332964d31143ef7628891384 Mon Sep 17 00:00:00 2001 From: Nezumi <123229563+Nez27@users.noreply.github.com> Date: Wed, 29 Mar 2023 10:41:06 +0700 Subject: [PATCH] Improve layout food detail --- .../foodify/Activity/FoodDetailActivity.java | 126 ++++++++++-------- .../main/res/layout/activity_food_detail.xml | 93 +++++-------- app/src/main/res/layout/item_comment.xml | 3 +- 3 files changed, 110 insertions(+), 112 deletions(-) diff --git a/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java b/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java index a1dceba..487d141 100644 --- a/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java +++ b/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java @@ -350,7 +350,7 @@ public class FoodDetailActivity extends AppCompatActivity { } } else if(response.code() == 404){ //Don't have user comment - user_comment_layout.setVisibility(View.GONE); + user_comment_layout.setVisibility(View.INVISIBLE); showUI(); } else { Toast.makeText(FoodDetailActivity.this, "Có lỗi từ hệ thống! Mã lỗi: " + response.code(), Toast.LENGTH_SHORT).show(); @@ -548,14 +548,14 @@ public class FoodDetailActivity extends AppCompatActivity { } EditText comment_content = dialog.findViewById(R.id.edit_text_comment); - RotationRatingBar ratingBar = dialog.findViewById(R.id.rating_bar); + RotationRatingBar ratingBarDialog = dialog.findViewById(R.id.rating_bar); Button confirm = dialog.findViewById(R.id.confirm_button); Button cancel = dialog.findViewById(R.id.cancel_button); if(actionCode == EDIT_COMMENT){ //Init data if(commentUser != null){ - ratingBar.setRating(commentUser.getRating()); + ratingBarDialog.setRating(commentUser.getRating()); comment_content.setText(commentUser.getContent()); } @@ -568,8 +568,6 @@ public class FoodDetailActivity extends AppCompatActivity { } }); - - confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -577,61 +575,18 @@ public class FoodDetailActivity extends AppCompatActivity { if(actionCode == CREATE_COMMENT){ //Create comment - Comment comment = new Comment(comment_content.getText().toString(), ratingBar.getRating(), Common.CURRENT_USER.getId()); - FoodApiToken.apiService.createComment(Integer.parseInt(foodId), comment).enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if(response.code() == 201){ - Toast.makeText(FoodDetailActivity.this, "Đã thêm bình luận thành công!", Toast.LENGTH_SHORT).show(); - //Init data - getCommentUser(); + createComment(comment_content.getText().toString(), ratingBarDialog); - //Show comment layout and hide rating button when comment completed! - user_comment_layout.setVisibility(View.VISIBLE); - rating_button.setVisibility(View.GONE); + dialog.dismiss(); - - dialog.dismiss(); - } else { - Toast.makeText(FoodDetailActivity.this, "Lỗi hệ thống! Mã lỗi: " + response.code(), Toast.LENGTH_SHORT).show(); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - Toast.makeText(FoodDetailActivity.this, "Đã xuất hiện lỗi hệ thống!", Toast.LENGTH_SHORT).show(); - } - }); } else if(actionCode == EDIT_COMMENT){ //Edit comment if(commentUser != null){ - //Create object comment - Comment newComment = new Comment(comment_content.getText().toString(), ratingBar.getRating(), Common.CURRENT_USER.getId()); + editComment(comment_content.getText().toString(), ratingBarDialog); - FoodApiToken.apiService.updateComment(Integer.parseInt(foodId), commentUser.getId(), newComment).enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if(response.code() == 200){ - Toast.makeText(FoodDetailActivity.this, "Đã cập nhật bình luận thành công!", Toast.LENGTH_SHORT).show(); - - ratingBarComment.setRating(ratingBar.getRating()); - contentComment.setText(comment_content.getText().toString()); - - dialog.dismiss(); - - progressLayout.setVisibility(View.GONE); - } else { - Toast.makeText(FoodDetailActivity.this, "Đã có lỗi hệ thống! Mã lỗi: " + response.code(), Toast.LENGTH_SHORT).show(); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - Toast.makeText(FoodDetailActivity.this, "Đã có lỗi kết nối đến hệ thống!", Toast.LENGTH_SHORT).show(); - } - }); + dialog.dismiss(); } } @@ -641,6 +596,67 @@ public class FoodDetailActivity extends AppCompatActivity { dialog.show(); } + + private void editComment(String content, RotationRatingBar ratingBarDialog) { + //Create object comment + Comment newComment = new Comment(content, ratingBarDialog.getRating(), Common.CURRENT_USER.getId()); + + FoodApiToken.apiService.updateComment(Integer.parseInt(foodId), commentUser.getId(), newComment).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.code() == 200){ + Toast.makeText(FoodDetailActivity.this, "Đã cập nhật bình luận thành công!", Toast.LENGTH_SHORT).show(); + + ratingBarComment.setRating(ratingBarDialog.getRating()); + contentComment.setText(content); + + //Update information food + progressLayout.setVisibility(View.VISIBLE); + getFoodById(foodId); + + + } else { + Toast.makeText(FoodDetailActivity.this, "Đã có lỗi hệ thống! Mã lỗi: " + response.code(), Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Toast.makeText(FoodDetailActivity.this, "Đã có lỗi kết nối đến hệ thống!", Toast.LENGTH_SHORT).show(); + } + }); + } + + private void createComment(String content, RotationRatingBar ratingBarDialog){ + Comment comment = new Comment(content, ratingBarDialog.getRating(), Common.CURRENT_USER.getId()); + FoodApiToken.apiService.createComment(Integer.parseInt(foodId), comment).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.code() == 201){ + Toast.makeText(FoodDetailActivity.this, "Đã thêm bình luận thành công!", Toast.LENGTH_SHORT).show(); + + //Init data + getCommentUser(); + + //Show comment layout and hide rating button when comment completed! + user_comment_layout.setVisibility(View.VISIBLE); + rating_button.setVisibility(View.GONE); + + //Update information food + progressLayout.setVisibility(View.VISIBLE); + getFoodById(foodId); + + } else { + Toast.makeText(FoodDetailActivity.this, "Lỗi hệ thống! Mã lỗi: " + response.code(), Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Toast.makeText(FoodDetailActivity.this, "Đã xuất hiện lỗi hệ thống!", Toast.LENGTH_SHORT).show(); + } + }); + } private void deleteComment(){ FoodApiToken.apiService.deleteComment(Integer.parseInt(foodId), commentUser.getId()).enqueue(new Callback() { @Override @@ -654,7 +670,11 @@ public class FoodDetailActivity extends AppCompatActivity { //Remove layout comment rating_button.setVisibility(View.VISIBLE); - user_comment_layout.setVisibility(View.GONE); + user_comment_layout.setVisibility(View.INVISIBLE); + + //Update information food + progressLayout.setVisibility(View.VISIBLE); + getFoodById(foodId); commentUser = null; } diff --git a/app/src/main/res/layout/activity_food_detail.xml b/app/src/main/res/layout/activity_food_detail.xml index 4c86bb4..3bfc345 100644 --- a/app/src/main/res/layout/activity_food_detail.xml +++ b/app/src/main/res/layout/activity_food_detail.xml @@ -129,6 +129,38 @@ app:layout_constraintStart_toStartOf="parent" /> + + + + + + + + @@ -152,54 +184,12 @@ app:layout_constraintStart_toStartOf="parent" /> - - - - - - - - - @@ -401,24 +391,13 @@ - - diff --git a/app/src/main/res/layout/item_comment.xml b/app/src/main/res/layout/item_comment.xml index 7a9d5de..e9ed113 100644 --- a/app/src/main/res/layout/item_comment.xml +++ b/app/src/main/res/layout/item_comment.xml @@ -2,8 +2,7 @@ + xmlns:app="http://schemas.android.com/apk/res-auto">