Commit e1677b82 authored by Thomas's avatar Thomas
Browse files

Apply logic

parent 5d2f7c34
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -14,7 +14,9 @@ package app.fedilab.android.client.entities.api;
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

import java.util.Date;
import java.util.List;
@@ -33,4 +35,38 @@ public class Notification {
    public Status status;

    public transient List<Notification> relatedNotifications;
    public boolean isFetchMore;
    public boolean isFetchMoreHidden = false;

    /**
     * Serialized a list of Notification class
     *
     * @param notifications List of {@link Notification} to serialize
     * @return String serialized emoji list
     */
    public static String notificationsToStringStorage(List<Notification> notifications) {
        Gson gson = new Gson();
        try {
            return gson.toJson(notifications);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Unserialized a notification List
     *
     * @param serializedNotificationList String serialized Status list
     * @return List of {@link Notification}
     */
    public static List<Notification> restoreNotificationsFromString(String serializedNotificationList) {
        Gson gson = new Gson();
        try {
            return gson.fromJson(serializedNotificationList, new TypeToken<List<Notification>>() {
            }.getType());
        } catch (Exception e) {
            return null;
        }
    }

}
+66 −1
Original line number Diff line number Diff line
@@ -24,11 +24,13 @@ import com.google.gson.annotations.SerializedName;

import java.util.List;

import app.fedilab.android.client.entities.api.Notification;
import app.fedilab.android.client.entities.api.Status;
import app.fedilab.android.exception.DBException;
import app.fedilab.android.helper.MastodonHelper;
import app.fedilab.android.helper.SpannableHelper;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonNotification;

public class QuickLoad {

@@ -45,6 +47,8 @@ public class QuickLoad {
    public int position;
    @SerializedName("statuses")
    public List<Status> statuses;
    @SerializedName("notifications")
    public List<Notification> notifications;
    private Context _mContext;

    public QuickLoad() {
@@ -134,6 +138,29 @@ public class QuickLoad {
        }
    }


    /**
     * @param position             - current position in timeline
     * @param notificationTypeEnum - Timeline.NotificationTypeEnum
     * @param notificationList     - List<Notification> to save
     */
    public void storeNotifications(int position, String user_id, String instance, FragmentMastodonNotification.NotificationTypeEnum notificationTypeEnum, List<Notification> notificationList) {

        String key = notificationTypeEnum.getValue();
        QuickLoad quickLoad = new QuickLoad();
        quickLoad.position = position;
        quickLoad.notifications = notificationList;
        quickLoad.slug = key;
        quickLoad.instance = instance;
        quickLoad.user_id = user_id;
        purge(quickLoad);
        try {
            insertOrUpdate(quickLoad);
        } catch (DBException e) {
            e.printStackTrace();
        }
    }

    /**
     * Insert a QuickLoad in db
     *
@@ -149,7 +176,11 @@ public class QuickLoad {
        values.put(Sqlite.COL_INSTANCE, quickLoad.instance);
        values.put(Sqlite.COL_SLUG, quickLoad.slug);
        values.put(Sqlite.COL_POSITION, quickLoad.position);
        if (quickLoad.statuses != null) {
            values.put(Sqlite.COL_STATUSES, StatusDraft.mastodonStatusListToStringStorage(quickLoad.statuses));
        } else if (quickLoad.notifications != null) {
            values.put(Sqlite.COL_STATUSES, Notification.notificationsToStringStorage(quickLoad.notifications));
        }
        //Inserts token
        try {
            db.insertOrThrow(Sqlite.TABLE_QUICK_LOAD, null, values);
@@ -370,6 +401,40 @@ public class QuickLoad {
        return null;
    }


    /**
     * Retrieves saved values
     *
     * @param notificationTypeEnum - FragmentMastodonNotification.NotificationTypeEnum
     * @return SavedValues
     */
    public QuickLoad getSavedValue(String user_id, String instance, FragmentMastodonNotification.NotificationTypeEnum notificationTypeEnum) {
        String key = notificationTypeEnum.getValue();
        try {
            return get(user_id, instance, key);
        } catch (DBException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Retrieves saved values
     *
     * @param notificationTypeEnum - FragmentMastodonNotification.NotificationTypeEnum
     * @return SavedValues
     */
    public QuickLoad getSavedValue(BaseAccount account, FragmentMastodonNotification.NotificationTypeEnum notificationTypeEnum) {
        String key = notificationTypeEnum.getValue();
        try {
            return get(key, account);
        } catch (DBException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Retrieves saved values
     *
+24 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import app.fedilab.android.R;
import app.fedilab.android.activities.ProfileActivity;
import app.fedilab.android.client.entities.api.Notification;
import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.databinding.DrawerFetchMoreBinding;
import app.fedilab.android.databinding.DrawerFollowBinding;
import app.fedilab.android.databinding.DrawerStatusNotificationBinding;
import app.fedilab.android.databinding.NotificationsRelatedAccountsBinding;
@@ -56,7 +57,9 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
    private final int TYPE_FAVOURITE = 4;
    private final int TYPE_POLL = 5;
    private final int TYPE_STATUS = 6;
    private final int NOTIFICATION_FETCH_MORE = 7;
    private Context context;
    public FetchMoreCallBack fetchMoreCallBack;

    public NotificationAdapter(List<Notification> notificationList) {
        this.notificationList = notificationList;
@@ -72,6 +75,9 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH

    @Override
    public int getItemViewType(int position) {
        if (notificationList.get(position).isFetchMore) {
            return NOTIFICATION_FETCH_MORE;
        }
        String type = notificationList.get(position).type;
        switch (type) {
            case "follow":
@@ -99,6 +105,9 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
        if (viewType == TYPE_FOLLOW || viewType == TYPE_FOLLOW_REQUEST) {
            DrawerFollowBinding itemBinding = DrawerFollowBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ViewHolderFollow(itemBinding);
        } else if (viewType == NOTIFICATION_FETCH_MORE) { //Fetch more button
            DrawerFetchMoreBinding itemBinding = DrawerFetchMoreBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new StatusAdapter.StatusViewHolder(itemBinding);
        } else {
            DrawerStatusNotificationBinding itemBinding = DrawerStatusNotificationBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new StatusAdapter.StatusViewHolder(itemBinding);
@@ -132,6 +141,17 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
                // start the new activity
                context.startActivity(intent, options.toBundle());
            });
        } else if (viewHolder.getItemViewType() == NOTIFICATION_FETCH_MORE) {
            StatusAdapter.StatusViewHolder holder = (StatusAdapter.StatusViewHolder) viewHolder;
            holder.bindingFetchMore.fetchMore.setEnabled(!notification.isFetchMoreHidden);
            holder.bindingFetchMore.fetchMore.setOnClickListener(v -> {
                if (position + 1 < notificationList.size()) {
                    //We hide the button
                    notification.isFetchMoreHidden = true;
                    notifyItemChanged(position);
                    fetchMoreCallBack.onClick(notificationList.get(position + 1).id, notification.id);
                }
            });
        } else {
            StatusAdapter.StatusViewHolder holderStatus = (StatusAdapter.StatusViewHolder) viewHolder;
            holderStatus.bindingNotification.status.typeOfNotification.setVisibility(View.VISIBLE);
@@ -234,6 +254,10 @@ public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
        }
    }

    public interface FetchMoreCallBack {
        void onClick(String min_id, String fetchmoreId);
    }


    public long getItemId(int position) {
        return position;
+287 −29

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -566,10 +566,10 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
     */
    private void route(DIRECTION direction, boolean fetchingMissing) {
        new Thread(() -> {
            QuickLoad quickLoad = new QuickLoad(requireActivity()).getSavedValue(MainActivity.currentUserID, MainActivity.currentInstance, timelineType, ident);
            if (binding == null) {
                return;
            }
            QuickLoad quickLoad = new QuickLoad(requireActivity()).getSavedValue(MainActivity.currentUserID, MainActivity.currentInstance, timelineType, ident);
            if (direction != DIRECTION.REFRESH && !fetchingMissing && !binding.swipeContainer.isRefreshing() && direction == null && quickLoad != null && quickLoad.statuses != null && quickLoad.statuses.size() > 0) {
                Statuses statuses = new Statuses();
                statuses.statuses = quickLoad.statuses;