Commit 35874081 authored by Thomas's avatar Thomas
Browse files

improve cache

parent 03bf06b6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -843,7 +843,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
                }
                if (mediaCount > 0) {
                    Data inputData = new Data.Builder()
                            .putString(Helper.ARG_STATUS_DRAFT, ComposeWorker.serialize(statusDraft))
                            .putString(Helper.ARG_STATUS_DRAFT_ID, String.valueOf(statusDraft.id))
                            .putString(Helper.ARG_INSTANCE, instance)
                            .putString(Helper.ARG_TOKEN, token)
                            .putString(Helper.ARG_USER_ID, account.user_id)
+6 −6
Original line number Diff line number Diff line
@@ -66,15 +66,15 @@ public interface MastodonTimelinesService {
    Call<List<Status>> getHashTag(
            @Header("Authorization") String token,
            @Path("hashtag") String hashtag,
            @Query("local") boolean local,
            @Query("only_media") boolean only_media,
            @Query("local") Boolean local,
            @Query("only_media") Boolean only_media,
            @Query("all[]") List<String> all,
            @Query("any[]") List<String> any,
            @Query("none[]") List<String> none,
            @Query("max_id") String max_id,
            @Query("since_id") String since_id,
            @Query("min_id") String min_id,
            @Query("limit") int limit
            @Query("limit") Integer limit
    );

    //Home timeline
@@ -84,8 +84,8 @@ public interface MastodonTimelinesService {
            @Query("max_id") String max_id,
            @Query("since_id") String since_id,
            @Query("min_id") String min_id,
            @Query("limit") int limit,
            @Query("local") boolean local
            @Query("limit") Integer limit,
            @Query("local") Boolean local
    );

    //List timeline
@@ -96,7 +96,7 @@ public interface MastodonTimelinesService {
            @Query("max_id") String max_id,
            @Query("since_id") String since_id,
            @Query("min_id") String min_id,
            @Query("limit") int limit
            @Query("limit") Integer limit
    );

    //get conversations
+1 −2
Original line number Diff line number Diff line
@@ -441,14 +441,13 @@ public class StatusCache {
    }

    /**
     * @param slug     String - slug for the timeline (it's a unique string value for a timeline)
     * @param instance String - instance
     * @param user_id  String - us
     * @param search   String search
     * @return - List<Status>
     * @throws DBException exception
     */
    public List<Status> searchStatus(String slug, String instance, String user_id, String search) throws DBException {
    public List<Status> searchStatus(String instance, String user_id, String search) throws DBException {
        if (db == null) {
            throw new DBException("db is null. Wrong initialization.");
        }
+9 −1
Original line number Diff line number Diff line
@@ -363,7 +363,15 @@ public class ComposeWorker extends Worker {
    @Override
    public Result doWork() {
        Data inputData = getInputData();
        StatusDraft statusDraft = restore(inputData.getString(Helper.ARG_STATUS_DRAFT));
        String statusDraftId = inputData.getString(Helper.ARG_STATUS_DRAFT_ID);
        StatusDraft statusDraft = null;
        if (statusDraftId != null) {
            try {
                statusDraft = new StatusDraft(getApplicationContext()).geStatusDraft(statusDraftId);
            } catch (DBException e) {
                e.printStackTrace();
            }
        }
        String token = inputData.getString(Helper.ARG_TOKEN);
        String instance = inputData.getString(Helper.ARG_INSTANCE);
        String userId = inputData.getString(Helper.ARG_USER_ID);
+54 −1
Original line number Diff line number Diff line
@@ -581,7 +581,8 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
    }

    /**
     * Router for timelines
     * Router for common timelines that can have the same treatments
     * - HOME / LOCAL / PUBLIC / LIST / TAG
     *
     * @param direction - DIRECTION null if first call, then is set to TOP or BOTTOM depending of scroll
     */
@@ -619,6 +620,58 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
                timelineParams.hashtagTrim = tagTimeline.name;
                break;
        }
        SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
        boolean useCache = sharedpreferences.getBoolean(getString(R.string.SET_USE_CACHE), true);
        if (useCache) {
            getCachedStatus(direction, fetchingMissing, timelineParams);
        } else {
            getLiveStatus(direction, fetchingMissing, timelineParams);
        }

    }

    private void getCachedStatus(DIRECTION direction, boolean fetchingMissing, TimelinesVM.TimelineParams timelineParams) {
        if (direction == null) {
            timelinesVM.getTimelineCache(timelineParams)
                    .observe(getViewLifecycleOwner(), statusesCached -> {
                        if (statusesCached == null || statusesCached.statuses == null || statusesCached.statuses.size() == 0) {
                            getLiveStatus(null, fetchingMissing, timelineParams);
                        } else {
                            initializeStatusesCommonView(statusesCached);
                        }
                    });
        } else if (direction == DIRECTION.BOTTOM) {
            timelinesVM.getTimelineCache(timelineParams)
                    .observe(getViewLifecycleOwner(), statusesCachedBottom -> {
                        if (statusesCachedBottom == null || statusesCachedBottom.statuses == null || statusesCachedBottom.statuses.size() == 0) {
                            getLiveStatus(DIRECTION.BOTTOM, fetchingMissing, timelineParams);
                        } else {
                            dealWithPagination(statusesCachedBottom, DIRECTION.BOTTOM, fetchingMissing);
                        }
                    });
        } else if (direction == DIRECTION.TOP) {
            timelinesVM.getTimelineCache(timelineParams)
                    .observe(getViewLifecycleOwner(), statusesCachedTop -> {
                        if (statusesCachedTop == null || statusesCachedTop.statuses == null || statusesCachedTop.statuses.size() == 0) {
                            getLiveStatus(DIRECTION.TOP, fetchingMissing, timelineParams);
                        } else {
                            dealWithPagination(statusesCachedTop, DIRECTION.TOP, fetchingMissing);
                        }

                    });
        } else if (direction == DIRECTION.REFRESH || direction == DIRECTION.SCROLL_TOP) {
            timelinesVM.getTimelineCache(timelineParams)
                    .observe(getViewLifecycleOwner(), statusesRefresh -> {
                        if (statusAdapter != null) {
                            dealWithPagination(statusesRefresh, direction, true);
                        } else {
                            initializeStatusesCommonView(statusesRefresh);
                        }
                    });
        }
    }

    private void getLiveStatus(DIRECTION direction, boolean fetchingMissing, TimelinesVM.TimelineParams timelineParams) {
        if (direction == null) {
            timelinesVM.getTimeline(timelineParams)
                    .observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
Loading