1
0
mirror of https://codeberg.org/tom79/Fedilab synced 2024-12-12 03:16:36 +01:00

Some fixes with cache and notifications

This commit is contained in:
Thomas 2022-09-30 16:47:40 +02:00
parent ea1df98d15
commit bb4851e424
6 changed files with 37 additions and 7 deletions

View File

@ -35,6 +35,9 @@ public class Notification {
public Account account; public Account account;
@SerializedName("status") @SerializedName("status")
public Status status; public Status status;
@SerializedName("cached")
public boolean cached;
public PositionFetchMore positionFetchMore = PositionFetchMore.BOTTOM; public PositionFetchMore positionFetchMore = PositionFetchMore.BOTTOM;
public enum PositionFetchMore { public enum PositionFetchMore {

View File

@ -358,7 +358,7 @@ public class StatusCache {
throw new DBException("db is null. Wrong initialization."); throw new DBException("db is null. Wrong initialization.");
} }
String order = " DESC"; String order = " DESC";
String selection = Sqlite.COL_INSTANCE + "='" + instance + "' AND " + Sqlite.COL_USER_ID + "= '" + user_id + "' AND " + Sqlite.COL_SLUG + "= '" + Timeline.TimeLineEnum.NOTIFICATION.getValue() + "' "; String selection = Sqlite.COL_INSTANCE + "='" + instance + "' AND " + Sqlite.COL_USER_ID + "= '" + user_id + "' AND " + Sqlite.COL_TYPE + "= '" + Timeline.TimeLineEnum.NOTIFICATION.getValue() + "' ";
String limit = String.valueOf(MastodonHelper.statusesPerCall(context)); String limit = String.valueOf(MastodonHelper.statusesPerCall(context));
if (min_id != null) { if (min_id != null) {
selection += "AND " + Sqlite.COL_STATUS_ID + " > '" + min_id + "' "; selection += "AND " + Sqlite.COL_STATUS_ID + " > '" + min_id + "' ";
@ -369,6 +369,7 @@ public class StatusCache {
selection += "AND " + Sqlite.COL_STATUS_ID + " > '" + since_id + "' "; selection += "AND " + Sqlite.COL_STATUS_ID + " > '" + since_id + "' ";
limit = null; limit = null;
} }
if (exclude_type != null && exclude_type.size() > 0) { if (exclude_type != null && exclude_type.size() > 0) {
StringBuilder exclude = new StringBuilder(); StringBuilder exclude = new StringBuilder();
for (String excluded : exclude_type) { for (String excluded : exclude_type) {

View File

@ -101,6 +101,9 @@ public class TimelineHelper {
//If there are filters: //If there are filters:
if (BaseMainActivity.mainFilters != null && BaseMainActivity.mainFilters.size() > 0 && statuses != null && statuses.size() > 0) { if (BaseMainActivity.mainFilters != null && BaseMainActivity.mainFilters.size() > 0 && statuses != null && statuses.size() > 0) {
for (Status status : statuses) {
status.cached = cached;
}
for (Filter filter : BaseMainActivity.mainFilters) { for (Filter filter : BaseMainActivity.mainFilters) {
if (filter.irreversible) { //Dealt by the server if (filter.irreversible) { //Dealt by the server
continue; continue;
@ -111,7 +114,6 @@ public class TimelineHelper {
Pattern p = Pattern.compile("(^" + Pattern.quote(filter.phrase) + "\\b|\\b" + Pattern.quote(filter.phrase) + "$)"); Pattern p = Pattern.compile("(^" + Pattern.quote(filter.phrase) + "\\b|\\b" + Pattern.quote(filter.phrase) + "$)");
for (Status status : statuses) { for (Status status : statuses) {
String content; String content;
status.cached = cached;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(status.content, Html.FROM_HTML_MODE_LEGACY).toString(); content = Html.fromHtml(status.content, Html.FROM_HTML_MODE_LEGACY).toString();
else else
@ -136,7 +138,6 @@ public class TimelineHelper {
} else { } else {
for (Status status : statuses) { for (Status status : statuses) {
String content; String content;
status.cached = cached;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(status.content, Html.FROM_HTML_MODE_LEGACY).toString(); content = Html.fromHtml(status.content, Html.FROM_HTML_MODE_LEGACY).toString();
else else
@ -175,7 +176,7 @@ public class TimelineHelper {
* @param notifications - List of {@link Notification} * @param notifications - List of {@link Notification}
* @return filtered List<Status> * @return filtered List<Status>
*/ */
public static List<Notification> filterNotification(Context context, List<Notification> notifications) { public static List<Notification> filterNotification(Context context, List<Notification> notifications, boolean cached) {
//A security to make sure filters have been fetched before displaying messages //A security to make sure filters have been fetched before displaying messages
List<Notification> notificationToRemove = new ArrayList<>(); List<Notification> notificationToRemove = new ArrayList<>();
if (!BaseMainActivity.filterFetched) { if (!BaseMainActivity.filterFetched) {
@ -200,6 +201,7 @@ public class TimelineHelper {
if (filter.whole_word) { if (filter.whole_word) {
Pattern p = Pattern.compile("(^" + Pattern.quote(filter.phrase) + "\\b|\\b" + Pattern.quote(filter.phrase) + "$)"); Pattern p = Pattern.compile("(^" + Pattern.quote(filter.phrase) + "\\b|\\b" + Pattern.quote(filter.phrase) + "$)");
for (Notification notification : notifications) { for (Notification notification : notifications) {
notification.cached = cached;
if (notification.status != null) { if (notification.status != null) {
String content; String content;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
@ -215,6 +217,7 @@ public class TimelineHelper {
} else { } else {
for (Notification notification : notifications) { for (Notification notification : notifications) {
String content; String content;
notification.cached = cached;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(notification.status.content, Html.FROM_HTML_MODE_LEGACY).toString(); content = Html.fromHtml(notification.status.content, Html.FROM_HTML_MODE_LEGACY).toString();
else else
@ -224,6 +227,10 @@ public class TimelineHelper {
} }
} }
} }
} else {
for (Notification notification : notifications) {
notification.cached = cached;
}
} }
} }
} }

View File

@ -186,6 +186,11 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} else { } else {
holderFollow.binding.layoutFetchMore.fetchMoreContainer.setVisibility(View.GONE); holderFollow.binding.layoutFetchMore.fetchMoreContainer.setVisibility(View.GONE);
} }
if (notification.cached) {
holderFollow.binding.cacheIndicator.setVisibility(View.VISIBLE);
} else {
holderFollow.binding.cacheIndicator.setVisibility(View.GONE);
}
} else { } else {
StatusAdapter.StatusViewHolder holderStatus = (StatusAdapter.StatusViewHolder) viewHolder; StatusAdapter.StatusViewHolder holderStatus = (StatusAdapter.StatusViewHolder) viewHolder;
holderStatus.bindingNotification.status.typeOfNotification.setVisibility(View.VISIBLE); holderStatus.bindingNotification.status.typeOfNotification.setVisibility(View.VISIBLE);
@ -205,6 +210,9 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
StatusesVM statusesVM = new ViewModelProvider((ViewModelStoreOwner) context).get(StatusesVM.class); StatusesVM statusesVM = new ViewModelProvider((ViewModelStoreOwner) context).get(StatusesVM.class);
SearchVM searchVM = new ViewModelProvider((ViewModelStoreOwner) context).get(SearchVM.class); SearchVM searchVM = new ViewModelProvider((ViewModelStoreOwner) context).get(SearchVM.class);
if (notification.status != null) {
notification.status.cached = notification.cached;
}
statusManagement(context, statusesVM, searchVM, holderStatus, this, null, notification.status, Timeline.TimeLineEnum.NOTIFICATION, false, true, null); statusManagement(context, statusesVM, searchVM, holderStatus, this, null, notification.status, Timeline.TimeLineEnum.NOTIFICATION, false, true, null);
holderStatus.bindingNotification.status.dateShort.setText(Helper.dateDiff(context, notification.created_at)); holderStatus.bindingNotification.status.dateShort.setText(Helper.dateDiff(context, notification.created_at));
holderStatus.bindingNotification.containerTransparent.setAlpha(.3f); holderStatus.bindingNotification.containerTransparent.setAlpha(.3f);

View File

@ -109,8 +109,8 @@ public class NotificationsVM extends AndroidViewModel {
try { try {
Response<List<Notification>> notificationsResponse = notificationsCall.execute(); Response<List<Notification>> notificationsResponse = notificationsCall.execute();
if (notificationsResponse.isSuccessful()) { if (notificationsResponse.isSuccessful()) {
notifications.notifications = notificationsResponse.body(); List<Notification> notFiltered = notificationsResponse.body();
TimelineHelper.filterNotification(getApplication().getApplicationContext(), notifications.notifications); notifications.notifications = TimelineHelper.filterNotification(getApplication().getApplicationContext(), notFiltered, false);
addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams); addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams);
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers()); notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
@ -154,7 +154,7 @@ public class NotificationsVM extends AndroidViewModel {
notifications = statusCacheDAO.getNotifications(timelineParams.excludeType, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId); notifications = statusCacheDAO.getNotifications(timelineParams.excludeType, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
if (notifications != null) { if (notifications != null) {
if (notifications.notifications != null && notifications.notifications.size() > 0) { if (notifications.notifications != null && notifications.notifications.size() > 0) {
TimelineHelper.filterNotification(getApplication().getApplicationContext(), notifications.notifications); TimelineHelper.filterNotification(getApplication().getApplicationContext(), notifications.notifications, true);
addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams); addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams);
notifications.pagination = new Pagination(); notifications.pagination = new Pagination();
notifications.pagination.min_id = notifications.notifications.get(0).id; notifications.pagination.min_id = notifications.notifications.get(0).id;

View File

@ -34,6 +34,17 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/cache_indicator"
android:layout_width="16sp"
android:layout_height="16sp"
android:layout_gravity="center"
android:layout_marginEnd="12dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_baseline_cached_24"
android:visibility="gone"
tools:visibility="visible" />
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title" android:id="@+id/title"
android:layout_width="0dp" android:layout_width="0dp"