From 2ffc8db2006198e59b0601e0e1e001e2c64464fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Wed, 8 May 2024 13:50:00 +0200 Subject: [PATCH] remove posts --- .../fragment/PostRecyclerViewAdapter.java | 27 +++++++++++-- .../com/dowerx/quack/service/PostService.java | 40 +++++++++++++++++++ app/src/main/res/layout/fragment_post.xml | 38 ++++++++++++------ app/src/main/res/values/strings.xml | 1 + 4 files changed, 91 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java b/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java index 44daa2c..27a679e 100644 --- a/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java +++ b/app/src/main/java/com/dowerx/quack/fragment/PostRecyclerViewAdapter.java @@ -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 { @@ -30,7 +35,21 @@ public class PostRecyclerViewAdapter extends RecyclerView.Adapter { + 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 { + + Callable callback; + Context context; + + public RemovePost(Callable 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(); + } + } + } } diff --git a/app/src/main/res/layout/fragment_post.xml b/app/src/main/res/layout/fragment_post.xml index 654e326..d81c759 100644 --- a/app/src/main/res/layout/fragment_post.xml +++ b/app/src/main/res/layout/fragment_post.xml @@ -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"/> - + app:layout_constraintTop_toBottomOf="@+id/content"> + +