remove posts
This commit is contained in:
parent
a3eb670595
commit
2ffc8db200
@ -3,12 +3,17 @@ package com.dowerx.quack.fragment;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.dowerx.quack.databinding.FragmentPostBinding;
|
||||
import com.dowerx.quack.model.Post;
|
||||
import com.dowerx.quack.service.AuthService;
|
||||
import com.dowerx.quack.service.PostService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PostRecyclerViewAdapter extends RecyclerView.Adapter<PostRecyclerViewAdapter.ViewHolder> {
|
||||
@ -30,7 +35,21 @@ public class PostRecyclerViewAdapter extends RecyclerView.Adapter<PostRecyclerVi
|
||||
holder.mItem = mValues.get(position);
|
||||
holder.username.setText(holder.mItem.getUser().getUsername());
|
||||
holder.content.setText(holder.mItem.getContent());
|
||||
holder.likes.setText("Likes " + holder.mItem.getLikes());
|
||||
holder.like.setText("Likes " + holder.mItem.getLikes());
|
||||
if (!AuthService.getId().equals(holder.mItem.getUser().getId())) {
|
||||
holder.remove.setAlpha(0.5f);
|
||||
holder.remove.setClickable(false);
|
||||
} else {
|
||||
holder.remove.setOnClickListener(v -> {
|
||||
PostService.RemovePost task = new PostService.RemovePost(()-> {
|
||||
this.mValues.remove(position);
|
||||
this.notifyItemRemoved(position);
|
||||
this.notifyItemRangeChanged(position, mValues.size());
|
||||
return null;
|
||||
}, v.getContext());
|
||||
task.execute(holder.mItem.getId());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,14 +60,16 @@ public class PostRecyclerViewAdapter extends RecyclerView.Adapter<PostRecyclerVi
|
||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||
public final TextView username;
|
||||
public final TextView content;
|
||||
public final TextView likes;
|
||||
public final Button like;
|
||||
public final Button remove;
|
||||
public Post mItem;
|
||||
|
||||
public ViewHolder(@NonNull FragmentPostBinding binding) {
|
||||
super(binding.getRoot());
|
||||
username = binding.username;
|
||||
content = binding.content;
|
||||
likes = binding.likes;
|
||||
like = binding.like;
|
||||
remove = binding.remove;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -29,6 +29,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@ -160,4 +161,43 @@ public class PostService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class RemovePost extends AsyncTask<String, Void, Boolean> {
|
||||
|
||||
Callable<Void> callback;
|
||||
Context context;
|
||||
|
||||
public RemovePost(Callable<Void> callback, Context context) {
|
||||
this.callback = callback;
|
||||
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... strings) {
|
||||
FirebaseFirestore db = FirebaseFirestore.getInstance();
|
||||
try {
|
||||
Tasks.await(db.document("/post/"+strings[0]).delete());
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
if (result) {
|
||||
try {
|
||||
this.callback.call();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
Toast toast = new Toast(context);
|
||||
toast.setDuration(Toast.LENGTH_LONG);
|
||||
toast.setText("failed to remove post");
|
||||
toast.show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,10 +10,8 @@
|
||||
android:id="@+id/username"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="@string/placeholder_name"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.067"
|
||||
android:layout_margin="20dp"
|
||||
android:text=""
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
@ -21,19 +19,35 @@
|
||||
android:id="@+id/content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="some random placeeholder post content"
|
||||
android:text=""
|
||||
app:layout_constraintTop_toBottomOf="@+id/username"
|
||||
android:layout_margin="20dp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/likes"
|
||||
android:layout_width="wrap_content"
|
||||
<LinearLayout
|
||||
android:id="@+id/post_buttons"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="20dp"
|
||||
android:layout_marginTop="20dp"
|
||||
android:text="Likes 0"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.0"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/content" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/content">
|
||||
|
||||
<Button
|
||||
android:id="@+id/like"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginRight="20dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Likes 0"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/remove"
|
||||
android:layout_marginLeft="20dp"
|
||||
android:layout_marginRight="20dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/remove_btn"/>
|
||||
</LinearLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -15,4 +15,5 @@
|
||||
<!-- TODO: Remove or change this placeholder text -->
|
||||
<string name="hello_blank_fragment">Hello blank fragment</string>
|
||||
<string name="post_btn">Post</string>
|
||||
<string name="remove_btn">Remove</string>
|
||||
</resources>
|
Loading…
Reference in New Issue
Block a user