mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 05:41:45 +00:00
183 lines
6.8 KiB
Java
183 lines
6.8 KiB
Java
package com.waterbase.foodify;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.appcompat.app.ActionBar;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
import android.content.Context;
|
|
import android.os.Bundle;
|
|
import android.view.LayoutInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import com.firebase.ui.database.FirebaseRecyclerAdapter;
|
|
import com.firebase.ui.database.FirebaseRecyclerOptions;
|
|
import com.google.firebase.database.DatabaseReference;
|
|
import com.google.firebase.database.FirebaseDatabase;
|
|
import com.google.firebase.database.Query;
|
|
import com.waterbase.foodify.Common.Common;
|
|
import com.waterbase.foodify.Model.Rating;
|
|
import com.waterbase.foodify.ViewHolder.ShowCommentViewHolder;
|
|
|
|
import io.github.inflationx.calligraphy3.CalligraphyConfig;
|
|
import io.github.inflationx.calligraphy3.CalligraphyInterceptor;
|
|
import io.github.inflationx.viewpump.ViewPump;
|
|
import io.github.inflationx.viewpump.ViewPumpContextWrapper;
|
|
|
|
public class ShowComment extends AppCompatActivity {
|
|
|
|
RecyclerView recyclerView;
|
|
RecyclerView.LayoutManager layoutManager;
|
|
|
|
FirebaseDatabase database;
|
|
DatabaseReference ratingTbl;
|
|
|
|
SwipeRefreshLayout mSwipeRefreshLayout;
|
|
|
|
FirebaseRecyclerAdapter<Rating, ShowCommentViewHolder> adapter;
|
|
|
|
String foodId = "";
|
|
|
|
|
|
@Override
|
|
protected void attachBaseContext(Context newBase) {
|
|
super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
|
|
}
|
|
|
|
@Override
|
|
protected void onStop() {
|
|
super.onStop();
|
|
if(adapter != null)
|
|
adapter.stopListening();
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
//Set font all activity
|
|
ViewPump.init(ViewPump.builder()
|
|
.addInterceptor(new CalligraphyInterceptor(
|
|
new CalligraphyConfig.Builder()
|
|
.setDefaultFontPath("fonts/font.otf")
|
|
.setFontAttrId(io.github.inflationx.calligraphy3.R.attr.fontPath)
|
|
.build()))
|
|
.build());
|
|
|
|
setContentView(R.layout.activity_show_comment);
|
|
|
|
setTitle("Bình luận");
|
|
|
|
//Firebase
|
|
database = FirebaseDatabase.getInstance();
|
|
ratingTbl = database.getReference("Rating");
|
|
|
|
recyclerView = findViewById(R.id.recyclerComment);
|
|
layoutManager = new LinearLayoutManager(this);
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
//Swipe Layout
|
|
mSwipeRefreshLayout = findViewById(R.id.swipe_layout);
|
|
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
@Override
|
|
public void onRefresh() {
|
|
if(getIntent() != null)
|
|
foodId = getIntent().getStringExtra(Common.INTENT_FOOD_ID);
|
|
if(!foodId.isEmpty() && foodId != null)
|
|
{
|
|
Query query = ratingTbl.orderByChild("foodId").equalTo(foodId);
|
|
|
|
FirebaseRecyclerOptions<Rating> options = new FirebaseRecyclerOptions.Builder<Rating>()
|
|
.setQuery(query, Rating.class)
|
|
.build();
|
|
|
|
adapter = new FirebaseRecyclerAdapter<Rating, ShowCommentViewHolder>(options) {
|
|
@Override
|
|
protected void onBindViewHolder(@NonNull ShowCommentViewHolder holder, int i, @NonNull Rating model) {
|
|
holder.ratingBar.setRating(Float.parseFloat(model.getRateValue()));
|
|
holder.txtComment.setText(model.getComment());
|
|
holder.txtUserPhone.setText(model.getUserPhone());
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public ShowCommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
.inflate(R.layout.show_comment_layout, parent, false);
|
|
return new ShowCommentViewHolder(view);
|
|
}
|
|
};
|
|
|
|
loadComment(foodId);
|
|
}
|
|
}
|
|
});
|
|
|
|
//Thread to load comment on first launch
|
|
mSwipeRefreshLayout.post(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
mSwipeRefreshLayout.setRefreshing(true);
|
|
|
|
if(getIntent() != null)
|
|
foodId = getIntent().getStringExtra(Common.INTENT_FOOD_ID);
|
|
if(!foodId.isEmpty() && foodId != null) {
|
|
Query query = ratingTbl.orderByChild("foodId").equalTo(foodId);
|
|
|
|
FirebaseRecyclerOptions<Rating> options = new FirebaseRecyclerOptions.Builder<Rating>()
|
|
.setQuery(query, Rating.class)
|
|
.build();
|
|
|
|
|
|
adapter = new FirebaseRecyclerAdapter<Rating, ShowCommentViewHolder>(options) {
|
|
@Override
|
|
protected void onBindViewHolder(@NonNull ShowCommentViewHolder holder, int i, @NonNull Rating model) {
|
|
holder.ratingBar.setRating(Float.parseFloat(model.getRateValue()));
|
|
holder.txtComment.setText(model.getComment());
|
|
holder.txtUserPhone.setText(model.getUserPhone());
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public ShowCommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
.inflate(R.layout.show_comment_layout, parent, false);
|
|
|
|
return new ShowCommentViewHolder(view);
|
|
}
|
|
};
|
|
|
|
loadComment(foodId);
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
// calling the action bar
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
// showing the back button in action bar
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
}
|
|
|
|
private void loadComment(String foodId) {
|
|
adapter.startListening();
|
|
|
|
recyclerView.setAdapter(adapter);
|
|
mSwipeRefreshLayout.setRefreshing(false);
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
|
switch (item.getItemId()) {
|
|
case android.R.id.home:
|
|
this.finish();
|
|
return true;
|
|
}
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
} |