From 2b546afa451b2a9fb2258266b20e5d7fe1766201 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 31 Oct 2022 18:05:32 +0100 Subject: [PATCH] Add listener --- .../app/fedilab/android/BaseMainActivity.java | 20 ++++++++++++++++ .../app/fedilab/android/helper/Helper.java | 13 ++++++++--- .../fedilab/android/jobs/ComposeWorker.java | 1 + .../android/ui/drawer/StatusAdapter.java | 23 +++++++++++++++---- .../timeline/FragmentMastodonTimeline.java | 7 ++++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 453a0990b..eda886e20 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -19,6 +19,7 @@ import static app.fedilab.android.BaseMainActivity.status.UNKNOWN; import static app.fedilab.android.helper.CacheHelper.deleteDir; import static app.fedilab.android.helper.Helper.PREF_USER_TOKEN; import static app.fedilab.android.helper.Helper.displayReleaseNotesIfNeeded; +import static app.fedilab.android.ui.drawer.StatusAdapter.sendAction; import android.annotation.SuppressLint; import android.content.BroadcastReceiver; @@ -718,6 +719,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt Cyanea.getInstance().edit().apply().recreate(BaseMainActivity.this); } else if (b.getBoolean(Helper.RECEIVE_NEW_MESSAGE, false)) { Status statusSent = (Status) b.getSerializable(Helper.RECEIVE_STATUS_ACTION); + String statusEditId = b.getString(Helper.ARG_EDIT_STATUS_ID, null); Snackbar.make(binding.displaySnackBar, getString(R.string.message_has_been_sent), Snackbar.LENGTH_LONG) .setAction(getString(R.string.display), view -> { Intent intentContext = new Intent(BaseMainActivity.this, ContextActivity.class); @@ -729,6 +731,24 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt .setActionTextColor(ContextCompat.getColor(BaseMainActivity.this, R.color.cyanea_accent_reference)) .setBackgroundTint(ContextCompat.getColor(BaseMainActivity.this, R.color.cyanea_primary_dark_reference)) .show(); + //The message was edited, we need to update the timeline + if (statusEditId != null) { + //Update message in cache + new Thread(() -> { + StatusCache statusCache = new StatusCache(); + statusCache.instance = BaseMainActivity.currentInstance; + statusCache.user_id = BaseMainActivity.currentUserID; + statusCache.status = statusSent; + statusCache.status_id = statusEditId; + try { + new StatusCache(BaseMainActivity.this).updateIfExists(statusCache); + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + //Update timelines + sendAction(context, Helper.ARG_STATUS_UPDATED, statusSent, null); + } } } } diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 725c0bd55..ce576672b 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -213,6 +213,8 @@ public class Helper { public static final String ARG_EXCLUDED_NOTIFICATION_TYPE = "ARG_EXCLUDED_NOTIFICATION_TYPE"; public static final String ARG_STATUS = "ARG_STATUS"; public static final String ARG_STATUS_DELETED = "ARG_STATUS_DELETED"; + public static final String ARG_STATUS_UPDATED = "ARG_STATUS_UPDATED"; + public static final String ARG_STATUS_POSTED = "ARG_STATUS_POSTED"; public static final String ARG_STATUS_ACTION = "ARG_STATUS_ACTION"; public static final String ARG_STATUS_ACCOUNT_ID_DELETED = "ARG_STATUS_ACCOUNT_ID_DELETED"; @@ -1604,10 +1606,15 @@ public class Helper { * @param date Date */ - public static void absoluteDateTimeReveal(final Context context, final TextView tvDate, final Date date) { + public static void absoluteDateTimeReveal(final Context context, final TextView tvDate, final Date date, final Date dateEdit) { tvDate.setOnClickListener(v -> { - tvDate.setText(dateDiffFull(date)); + if (dateEdit == null) { + tvDate.setText(dateDiffFull(date)); + } else { + String dateEditText = context.getString(R.string.full_date_edited, dateDiffFull(date), dateDiffFull(dateEdit)); + tvDate.setText(dateEditText); + } new CountDownTimer((5 * 1000), 1000) { @@ -1615,7 +1622,7 @@ public class Helper { } public void onFinish() { - tvDate.setText(dateDiff(context, date)); + tvDate.setText(String.format(Locale.getDefault(), "%s%s", dateDiff(context, date), (dateEdit != null ? "*" : ""))); } }.start(); }); diff --git a/app/src/main/java/app/fedilab/android/jobs/ComposeWorker.java b/app/src/main/java/app/fedilab/android/jobs/ComposeWorker.java index 970417670..74cb53866 100644 --- a/app/src/main/java/app/fedilab/android/jobs/ComposeWorker.java +++ b/app/src/main/java/app/fedilab/android/jobs/ComposeWorker.java @@ -328,6 +328,7 @@ public class ComposeWorker extends Worker { if (dataPost.scheduledDate == null && dataPost.token != null && firstSendMessage != null) { Bundle b = new Bundle(); b.putBoolean(Helper.RECEIVE_NEW_MESSAGE, true); + b.putString(Helper.ARG_EDIT_STATUS_ID, dataPost.statusEditId); Intent intentBD = new Intent(Helper.BROADCAST_DATA); b.putSerializable(Helper.RECEIVE_STATUS_ACTION, firstSendMessage); intentBD.putExtras(b); 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 3061c3dc5..45bff2ddc 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 @@ -980,7 +980,12 @@ public class StatusAdapter extends RecyclerView.Adapter holder.binding.statusInfo.setVisibility(View.VISIBLE); holder.binding.reblogsCount.setText(String.valueOf(status.reblogs_count)); holder.binding.favoritesCount.setText(String.valueOf(status.favourites_count)); - holder.binding.time.setText(Helper.longDateToString(status.created_at)); + + if (statusToDeal.edited_at != null) { + holder.binding.time.setText(context.getString(R.string.full_date_edited, Helper.longDateToString(status.created_at), Helper.longDateToString(status.edited_at))); + } else { + holder.binding.time.setText(Helper.longDateToString(status.created_at)); + } holder.binding.time.setVisibility(View.VISIBLE); holder.binding.dateShort.setVisibility(View.GONE); holder.binding.visibility.setImageResource(ressource); @@ -994,17 +999,25 @@ public class StatusAdapter extends RecyclerView.Adapter holder.binding.visibilitySmall.setVisibility(View.GONE); holder.binding.reblogsCount.setText(String.valueOf(statusToDeal.reblogs_count)); holder.binding.favoritesCount.setText(String.valueOf(statusToDeal.favourites_count)); - holder.binding.time.setText(Helper.dateDiff(context, statusToDeal.created_at)); - Helper.absoluteDateTimeReveal(context, holder.binding.time, statusToDeal.created_at); + if (statusToDeal.edited_at != null) { + holder.binding.time.setText(String.format(Locale.getDefault(), "%s%s", Helper.dateDiff(context, statusToDeal.created_at), "*")); + } else { + holder.binding.time.setText(Helper.dateDiff(context, statusToDeal.created_at)); + } + Helper.absoluteDateTimeReveal(context, holder.binding.time, statusToDeal.created_at, statusToDeal.edited_at); holder.binding.visibility.setImageResource(ressource); holder.binding.time.setVisibility(View.VISIBLE); } else { holder.binding.statusInfo.setVisibility(View.GONE); holder.binding.dateShort.setVisibility(View.VISIBLE); holder.binding.visibilitySmall.setVisibility(View.VISIBLE); - holder.binding.dateShort.setText(Helper.dateDiff(context, statusToDeal.created_at)); + if (statusToDeal.edited_at != null) { + holder.binding.dateShort.setText(String.format(Locale.getDefault(), "%s%s", Helper.dateDiff(context, statusToDeal.created_at), "*")); + } else { + holder.binding.dateShort.setText(Helper.dateDiff(context, statusToDeal.created_at)); + } holder.binding.time.setVisibility(View.GONE); - Helper.absoluteDateTimeReveal(context, holder.binding.dateShort, statusToDeal.created_at); + Helper.absoluteDateTimeReveal(context, holder.binding.dateShort, statusToDeal.created_at, statusToDeal.edited_at); } } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index b13ccdc14..09558e03b 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -92,6 +92,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. Status receivedStatus = (Status) b.getSerializable(Helper.ARG_STATUS_ACTION); String delete_statuses_for_user = b.getString(Helper.ARG_STATUS_ACCOUNT_ID_DELETED); Status status_to_delete = (Status) b.getSerializable(Helper.ARG_STATUS_DELETED); + Status status_to_update = (Status) b.getSerializable(Helper.ARG_STATUS_UPDATED); Status statusPosted = (Status) b.getSerializable(Helper.ARG_STATUS_DELETED); if (receivedStatus != null && statusAdapter != null) { int position = getPosition(receivedStatus); @@ -124,6 +125,12 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. timelineStatuses.remove(position); statusAdapter.notifyItemRemoved(position); } + } else if (status_to_update != null && statusAdapter != null) { + int position = getPosition(status_to_update); + if (position >= 0) { + timelineStatuses.set(position, status_to_update); + statusAdapter.notifyItemChanged(position); + } } else if (statusPosted != null && statusAdapter != null && timelineType == Timeline.TimeLineEnum.HOME) { timelineStatuses.add(0, statusPosted); statusAdapter.notifyItemInserted(0); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a178f6baa..95fc8d4b1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1556,4 +1556,5 @@ Translate messages Force translation to a specific language. Choose first value to reset to device settings Edit message + %1$s edited %2$s