From e9d9f8f6ef27af178e7287b4e7e9005a3b196602 Mon Sep 17 00:00:00 2001
From: Thomas <tschneider.ac@gmail.com>
Date: Mon, 6 Jun 2022 18:19:40 +0200
Subject: [PATCH] Fix issue #134 & #68 - Issue with Friendica and button
 actions

---
 .../android/ui/drawer/StatusAdapter.java      | 98 +++++++++++++------
 1 file changed, 70 insertions(+), 28 deletions(-)

diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java
index b8b1c034..a9e51bef 100644
--- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java
+++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java
@@ -398,9 +398,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                             .observe((LifecycleOwner) context, _status -> {
                                                 if (_status != null) {
                                                     statusToDeal.bookmarked = _status.bookmarked;
-                                                    sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                    adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                                } else {
+                                                    statusToDeal.bookmarked = true;
                                                 }
+                                                sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                                adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                             });
                                 } else {
                                     Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show();
@@ -412,9 +414,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                 .observe((LifecycleOwner) context, _status -> {
                                     if (_status != null) {
                                         statusToDeal.bookmarked = _status.bookmarked;
-                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                    } else {
+                                        statusToDeal.bookmarked = false;
                                     }
+                                    sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                    adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                 });
                     } else {
                         ((SparkButton) v).playAnimation();
@@ -422,9 +426,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                 .observe((LifecycleOwner) context, _status -> {
                                     if (_status != null) {
                                         statusToDeal.bookmarked = _status.bookmarked;
-                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                    } else {
+                                        statusToDeal.bookmarked = true;
                                     }
+                                    sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                    adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                 });
                     }
                 }
@@ -518,9 +524,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                                         if (_status != null) {
                                                             statusToDeal.reblogged = _status.reblogged;
                                                             statusToDeal.reblogs_count = _status.reblogs_count;
-                                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                                        } else {
+                                                            statusToDeal.reblogged = true;
+                                                            statusToDeal.reblogs_count += 1;
                                                         }
+                                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                                     });
                                         } else {
                                             Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show();
@@ -533,9 +542,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                             if (_status != null) {
                                                 statusToDeal.reblogged = _status.reblogged;
                                                 statusToDeal.reblogs_count = _status.reblogs_count;
-                                                sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                            } else {
+                                                statusToDeal.reblogged = false;
+                                                statusToDeal.reblogs_count -= 1;
                                             }
+                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                         });
                             } else {
                                 ((SparkButton) v).playAnimation();
@@ -544,9 +556,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                             if (_status != null) {
                                                 statusToDeal.reblogged = _status.reblogged;
                                                 statusToDeal.reblogs_count = _status.reblogs_count;
-                                                sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                            } else {
+                                                statusToDeal.reblogged = true;
+                                                statusToDeal.reblogs_count += 1;
                                             }
+                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                         });
                             }
                         }
@@ -567,9 +582,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                                     if (_status != null) {
                                                         statusToDeal.reblogged = _status.reblogged;
                                                         statusToDeal.reblogs_count = _status.reblogs_count;
-                                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                                    } else {
+                                                        statusToDeal.reblogged = true;
+                                                        statusToDeal.reblogs_count += 1;
                                                     }
+                                                    sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                                    adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                                 });
                                     } else {
                                         Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show();
@@ -582,9 +600,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                         if (_status != null) {
                                             statusToDeal.reblogged = _status.reblogged;
                                             statusToDeal.reblogs_count = _status.reblogs_count;
-                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                        } else {
+                                            statusToDeal.reblogged = false;
+                                            statusToDeal.reblogs_count -= 1;
                                         }
+                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                     });
                         } else {
                             ((SparkButton) v).playAnimation();
@@ -593,9 +614,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                         if (_status != null) {
                                             statusToDeal.reblogged = _status.reblogged;
                                             statusToDeal.reblogs_count = _status.reblogs_count;
-                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                        } else {
+                                            statusToDeal.reblogged = true;
+                                            statusToDeal.reblogs_count += 1;
                                         }
+                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                     });
                         }
                     }
@@ -630,9 +654,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                                         if (_status != null) {
                                                             statusToDeal.favourited = _status.favourited;
                                                             statusToDeal.favourites_count = _status.favourites_count;
-                                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                                        } else {
+                                                            statusToDeal.favourited = true;
+                                                            statusToDeal.favourites_count += 1;
                                                         }
+                                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                                     });
                                         } else {
                                             Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show();
@@ -645,8 +672,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                             if (_status != null) {
                                                 statusToDeal.favourited = _status.favourited;
                                                 statusToDeal.favourites_count = _status.favourites_count;
-                                                adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                            } else {
+                                                statusToDeal.favourited = false;
+                                                statusToDeal.favourites_count -= 1;
                                             }
+                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                         });
                             } else {
                                 ((SparkButton) v).playAnimation();
@@ -655,9 +685,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                             if (_status != null) {
                                                 statusToDeal.favourited = _status.favourited;
                                                 statusToDeal.favourites_count = _status.favourites_count;
-                                                sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                            } else {
+                                                statusToDeal.favourited = true;
+                                                statusToDeal.favourites_count += 1;
                                             }
+                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                         });
                             }
                         }
@@ -678,9 +711,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                                     if (_status != null) {
                                                         statusToDeal.favourited = _status.favourited;
                                                         statusToDeal.favourites_count = _status.favourites_count;
-                                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                                    } else {
+                                                        statusToDeal.favourited = true;
+                                                        statusToDeal.favourites_count += 1;
                                                     }
+                                                    sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                                    adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                                 });
                                     } else {
                                         Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show();
@@ -693,8 +729,11 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                         if (_status != null) {
                                             statusToDeal.favourited = _status.favourited;
                                             statusToDeal.favourites_count = _status.favourites_count;
-                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                        } else {
+                                            statusToDeal.favourited = false;
+                                            statusToDeal.favourites_count -= 1;
                                         }
+                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                     });
                         } else {
                             ((SparkButton) v).playAnimation();
@@ -703,9 +742,12 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
                                         if (_status != null) {
                                             statusToDeal.favourited = _status.favourited;
                                             statusToDeal.favourites_count = _status.favourites_count;
-                                            sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
-                                            adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
+                                        } else {
+                                            statusToDeal.favourited = true;
+                                            statusToDeal.favourites_count += 1;
                                         }
+                                        sendAction(context, Helper.ARG_STATUS_ACTION, statusToDeal, null);
+                                        adapter.notifyItemChanged(getPositionAsync(notificationList, statusList, statusToDeal));
                                     });
                         }
                     }