Skip to content

Commit

Permalink
Try to merge in new statuses, fix bottom loading, fix saving spans.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlag committed Sep 30, 2018
1 parent c171d11 commit 9b4989d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
Expand Up @@ -83,6 +83,7 @@
import at.connyduck.sparkbutton.helpers.Utils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import kotlin.collections.CollectionsKt;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand Down Expand Up @@ -250,33 +251,37 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,

private void sendInitialRequest() {
if (this.kind == Kind.HOME) {
// Request timeline from disk to make it quick, then replace it with timeline from
// the server to update it
this.disposable.add(this.timeilneRepo.getStatuses(null, null, LOAD_AT_ONCE, true)
.observeOn(AndroidSchedulers.mainThread())
.flatMap(statuses -> {
if (statuses.size() > 1) {
actualTopId = statuses.get(0).getId();
this.statuses.addAll(liftStatusList(statuses));
this.updateAdapter();
this.progressBar.setVisibility(View.GONE);
topId = new BigInteger(statuses.get(0).getId())
.add(BigInteger.ONE).add(BigInteger.TEN).toString();
}
return this.timeilneRepo.getStatuses(topId, null, LOAD_AT_ONCE, false);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(statuses -> {
responseTopId = statuses.get(0).getId();
Log.d("TIMELINEF", String.format("actual %s top %s response %s", actualTopId, topId, responseTopId));
this.statuses.clear();
this.onFetchTimelineSuccess(statuses, FetchEnd.TOP, -1);
}));
this.tryCache();
} else {
sendFetchTimelineRequest(null, null, FetchEnd.BOTTOM, -1);
}
}

private void tryCache() {
// Request timeline from disk to make it quick, then replace it with timeline from
// the server to update it
this.disposable.add(this.timeilneRepo.getStatuses(null, null, LOAD_AT_ONCE, true)
.observeOn(AndroidSchedulers.mainThread())
.flatMap(statuses -> {
if (statuses.size() > 1) {
actualTopId = statuses.get(0).getId();
this.statuses.addAll(liftStatusList(statuses));
this.updateAdapter();
this.progressBar.setVisibility(View.GONE);
topId = new BigInteger(statuses.get(0).getId())
.add(BigInteger.ONE).add(BigInteger.TEN).toString();
}
return this.timeilneRepo.getStatuses(topId, null, LOAD_AT_ONCE, false);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(statuses -> {
responseTopId = statuses.get(0).getId();
Log.d("TIMELINEF", String.format("actual %s top %s response %s", actualTopId, topId, responseTopId));
this.onFetchTimelineSuccess(statuses, FetchEnd.TOP, -1);
this.bottomLoading = false;
}));
}

private void setupTimelinePreferences() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
preferences.registerOnSharedPreferenceChangeListener(this);
Expand Down Expand Up @@ -888,7 +893,7 @@ private void onFetchTimelineSuccess(List<Status> statuses,
break;
}
}
fulfillAnyQueuedFetches(fetchEnd);
updateBottomLoadingState(fetchEnd);
progressBar.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
if (this.statuses.size() == 0) {
Expand All @@ -914,12 +919,12 @@ private void onFetchTimelineFailure(Exception exception, FetchEnd fetchEnd, int
}

Log.e(TAG, "Fetch Failure: " + exception.getMessage());
fulfillAnyQueuedFetches(fetchEnd);
updateBottomLoadingState(fetchEnd);
progressBar.setVisibility(View.GONE);
}
}

private void fulfillAnyQueuedFetches(FetchEnd fetchEnd) {
private void updateBottomLoadingState(FetchEnd fetchEnd) {
switch (fetchEnd) {
case BOTTOM: {
bottomLoading = false;
Expand All @@ -946,27 +951,15 @@ private void updateStatuses(List<Status> newStatuses, boolean fullFetch) {
return;
}

List<Either<Placeholder, Status>> liftedNew = liftStatusList(newStatuses);

if (statuses.isEmpty()) {
statuses.addAll(liftedNew);
} else {
Either<Placeholder, Status> lastOfNew = liftedNew.get(newStatuses.size() - 1);
int index = statuses.indexOf(lastOfNew);

for (int i = 0; i < index; i++) {
statuses.remove(0);
}
int newIndex = liftedNew.indexOf(statuses.get(0));
if (newIndex == -1) {
if (index == -1 && fullFetch) {
liftedNew.add(Either.left(newPlaceholder()));
}
statuses.addAll(0, liftedNew);
} else {
statuses.addAll(0, liftedNew.subList(0, newIndex));
final Status lastNew = CollectionsKt.last(newStatuses);
for (int i = 0; i < this.statuses.size(); i++) {
Status right = this.statuses.get(i).getAsRightOrNull();
if (right != null && right.getId().equals(lastNew.getId())) {
ListUtils.removeFirstN(i + 1, this.statuses);
break;
}
}
this.statuses.addAll(this.liftStatusList(newStatuses));
updateAdapter();
}

Expand Down
Expand Up @@ -194,7 +194,7 @@ class TimelineRepostiryImpl(
authorServerId = actionable.account.id,
inReplyToId = actionable.inReplyToId,
inReplyToAccountId = actionable.inReplyToAccountId,
content = actionable.content.toString(),
content = HtmlUtils.toHtml(actionable.content),
createdAt = actionable.createdAt.time,
emojis = actionable.emojis.let(gson::toJson),
reblogsCount = actionable.reblogsCount,
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/com/keylesspalace/tusky/util/ListUtils.java
Expand Up @@ -18,18 +18,31 @@
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

public class ListUtils {
/** @return true if list is null or else return list.isEmpty() */
/**
* @return true if list is null or else return list.isEmpty()
*/
public static boolean isEmpty(@Nullable List list) {
return list == null || list.isEmpty();
}

/** @return a new ArrayList containing the elements without duplicates in the same order */
/**
* @return a new ArrayList containing the elements without duplicates in the same order
*/
public static <T> ArrayList<T> removeDuplicates(List<T> list) {
LinkedHashSet<T> set = new LinkedHashSet<>(list);
return new ArrayList<>(set);
}

public static <T> void removeFirstN(int number, List<T> list) {
for (Iterator<T> iterator = list.listIterator(); iterator.hasNext() && number > 0; ) {
iterator.next();
iterator.remove();
number--;
}
}
}

0 comments on commit 9b4989d

Please sign in to comment.