mirror of
https://codeberg.org/tom79/Fedilab
synced 2025-02-03 12:47:37 +01:00
improve cache
This commit is contained in:
parent
03bf06b6a0
commit
3587408123
@ -843,7 +843,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana
|
|||||||
}
|
}
|
||||||
if (mediaCount > 0) {
|
if (mediaCount > 0) {
|
||||||
Data inputData = new Data.Builder()
|
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_INSTANCE, instance)
|
||||||
.putString(Helper.ARG_TOKEN, token)
|
.putString(Helper.ARG_TOKEN, token)
|
||||||
.putString(Helper.ARG_USER_ID, account.user_id)
|
.putString(Helper.ARG_USER_ID, account.user_id)
|
||||||
|
@ -66,15 +66,15 @@ public interface MastodonTimelinesService {
|
|||||||
Call<List<Status>> getHashTag(
|
Call<List<Status>> getHashTag(
|
||||||
@Header("Authorization") String token,
|
@Header("Authorization") String token,
|
||||||
@Path("hashtag") String hashtag,
|
@Path("hashtag") String hashtag,
|
||||||
@Query("local") boolean local,
|
@Query("local") Boolean local,
|
||||||
@Query("only_media") boolean only_media,
|
@Query("only_media") Boolean only_media,
|
||||||
@Query("all[]") List<String> all,
|
@Query("all[]") List<String> all,
|
||||||
@Query("any[]") List<String> any,
|
@Query("any[]") List<String> any,
|
||||||
@Query("none[]") List<String> none,
|
@Query("none[]") List<String> none,
|
||||||
@Query("max_id") String max_id,
|
@Query("max_id") String max_id,
|
||||||
@Query("since_id") String since_id,
|
@Query("since_id") String since_id,
|
||||||
@Query("min_id") String min_id,
|
@Query("min_id") String min_id,
|
||||||
@Query("limit") int limit
|
@Query("limit") Integer limit
|
||||||
);
|
);
|
||||||
|
|
||||||
//Home timeline
|
//Home timeline
|
||||||
@ -84,8 +84,8 @@ public interface MastodonTimelinesService {
|
|||||||
@Query("max_id") String max_id,
|
@Query("max_id") String max_id,
|
||||||
@Query("since_id") String since_id,
|
@Query("since_id") String since_id,
|
||||||
@Query("min_id") String min_id,
|
@Query("min_id") String min_id,
|
||||||
@Query("limit") int limit,
|
@Query("limit") Integer limit,
|
||||||
@Query("local") boolean local
|
@Query("local") Boolean local
|
||||||
);
|
);
|
||||||
|
|
||||||
//List timeline
|
//List timeline
|
||||||
@ -96,7 +96,7 @@ public interface MastodonTimelinesService {
|
|||||||
@Query("max_id") String max_id,
|
@Query("max_id") String max_id,
|
||||||
@Query("since_id") String since_id,
|
@Query("since_id") String since_id,
|
||||||
@Query("min_id") String min_id,
|
@Query("min_id") String min_id,
|
||||||
@Query("limit") int limit
|
@Query("limit") Integer limit
|
||||||
);
|
);
|
||||||
|
|
||||||
//get conversations
|
//get conversations
|
||||||
|
@ -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 instance String - instance
|
||||||
* @param user_id String - us
|
* @param user_id String - us
|
||||||
* @param search String search
|
* @param search String search
|
||||||
* @return - List<Status>
|
* @return - List<Status>
|
||||||
* @throws DBException exception
|
* @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) {
|
if (db == null) {
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
throw new DBException("db is null. Wrong initialization.");
|
||||||
}
|
}
|
||||||
|
@ -363,7 +363,15 @@ public class ComposeWorker extends Worker {
|
|||||||
@Override
|
@Override
|
||||||
public Result doWork() {
|
public Result doWork() {
|
||||||
Data inputData = getInputData();
|
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 token = inputData.getString(Helper.ARG_TOKEN);
|
||||||
String instance = inputData.getString(Helper.ARG_INSTANCE);
|
String instance = inputData.getString(Helper.ARG_INSTANCE);
|
||||||
String userId = inputData.getString(Helper.ARG_USER_ID);
|
String userId = inputData.getString(Helper.ARG_USER_ID);
|
||||||
|
@ -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
|
* @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;
|
timelineParams.hashtagTrim = tagTimeline.name;
|
||||||
break;
|
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) {
|
if (direction == null) {
|
||||||
timelinesVM.getTimeline(timelineParams)
|
timelinesVM.getTimeline(timelineParams)
|
||||||
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
|
||||||
|
@ -138,7 +138,7 @@ public class SearchVM extends AndroidViewModel {
|
|||||||
Results results = new Results();
|
Results results = new Results();
|
||||||
try {
|
try {
|
||||||
results.statuses = new ArrayList<>();
|
results.statuses = new ArrayList<>();
|
||||||
List<Status> statuses = new StatusCache(getApplication()).searchStatus(StatusCache.CacheEnum.HOME, instance, userId, q);
|
List<Status> statuses = new StatusCache(getApplication()).searchStatus(instance, userId, q);
|
||||||
results.statuses.addAll(statuses);
|
results.statuses.addAll(statuses);
|
||||||
} catch (DBException e) {
|
} catch (DBException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -466,12 +466,14 @@ public class TimelinesVM extends AndroidViewModel {
|
|||||||
public int limit = 40;
|
public int limit = 40;
|
||||||
public Boolean local;
|
public Boolean local;
|
||||||
|
|
||||||
public TimelineParams(@NonNull Timeline.TimeLineEnum type, @Nullable FragmentMastodonTimeline.DIRECTION direction, @Nullable String ident) {
|
public TimelineParams(@NonNull Timeline.TimeLineEnum timeLineEnum, @Nullable FragmentMastodonTimeline.DIRECTION timelineDirection, @Nullable String ident) {
|
||||||
if (type != Timeline.TimeLineEnum.REMOTE) {
|
if (type != Timeline.TimeLineEnum.REMOTE) {
|
||||||
instance = MainActivity.currentInstance;
|
instance = MainActivity.currentInstance;
|
||||||
token = MainActivity.currentToken;
|
token = MainActivity.currentToken;
|
||||||
userId = MainActivity.currentUserID;
|
userId = MainActivity.currentUserID;
|
||||||
}
|
}
|
||||||
|
type = timeLineEnum;
|
||||||
|
direction = timelineDirection;
|
||||||
String key = type.getValue();
|
String key = type.getValue();
|
||||||
if (ident != null) {
|
if (ident != null) {
|
||||||
key += "|" + ident;
|
key += "|" + ident;
|
||||||
|
@ -932,6 +932,7 @@
|
|||||||
<string name="SET_NITTER" translatable="false">SET_NITTER</string>
|
<string name="SET_NITTER" translatable="false">SET_NITTER</string>
|
||||||
<string name="SET_NITTER_HOST" translatable="false">SET_NITTER_HOST</string>
|
<string name="SET_NITTER_HOST" translatable="false">SET_NITTER_HOST</string>
|
||||||
<string name="DEFAULT_NITTER_HOST" translatable="false">nitter.net</string>
|
<string name="DEFAULT_NITTER_HOST" translatable="false">nitter.net</string>
|
||||||
|
<string name="SET_USE_CACHE" translatable="false">SET_USE_CACHE</string>
|
||||||
|
|
||||||
<string name="SET_BIBLIOGRAM" translatable="false">SET_BIBLIOGRAM</string>
|
<string name="SET_BIBLIOGRAM" translatable="false">SET_BIBLIOGRAM</string>
|
||||||
<string name="SET_BIBLIOGRAM_HOST" translatable="false">SET_BIBLIOGRAM_HOST</string>
|
<string name="SET_BIBLIOGRAM_HOST" translatable="false">SET_BIBLIOGRAM_HOST</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user