simplify ancestry code

This commit is contained in:
sk 2023-06-03 21:03:47 +02:00
parent 2919e109ca
commit f696fcd412
2 changed files with 23 additions and 47 deletions

View File

@ -20,9 +20,7 @@ public class ThreadFragmentTest {
} }
private ThreadFragment.NeighborAncestryInfo fakeInfo(Status s, Status d, Status a) { private ThreadFragment.NeighborAncestryInfo fakeInfo(Status s, Status d, Status a) {
ThreadFragment.NeighborAncestryInfo info = new ThreadFragment.NeighborAncestryInfo(s); ThreadFragment.NeighborAncestryInfo info = new ThreadFragment.NeighborAncestryInfo(s, d, a);
info.descendantNeighbor = d;
info.ancestoringNeighbor = a;
return info; return info;
} }

View File

@ -2,7 +2,6 @@ package org.joinmastodon.android.fragments;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Pair;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -32,11 +31,9 @@ import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import me.grishka.appkit.api.SimpleCallback; import me.grishka.appkit.api.SimpleCallback;
@ -74,10 +71,10 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
NeighborAncestryInfo ancestryInfo = ancestryMap.get(s.id); NeighborAncestryInfo ancestryInfo = ancestryMap.get(s.id);
if (ancestryInfo != null) { if (ancestryInfo != null) {
item.setAncestryInfo( item.setAncestryInfo(
ancestryInfo.hasDescendantNeighbor(), ancestryInfo.descendantNeighbor != null,
ancestryInfo.hasAncestoringNeighbor(), ancestryInfo.ancestoringNeighbor != null,
s.id.equals(mainStatus.id), s.id.equals(mainStatus.id),
ancestryInfo.getAncestoringNeighbor() Optional.ofNullable(ancestryInfo.ancestoringNeighbor)
.map(ancestor -> ancestor.id.equals(mainStatus.id)) .map(ancestor -> ancestor.id.equals(mainStatus.id))
.orElse(false) .orElse(false)
); );
@ -162,22 +159,21 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
int count = statuses.size(); int count = statuses.size();
for (int index = 0; index < count; index++) { for (int index = 0; index < count; index++) {
Status current = statuses.get(index); Status current = statuses.get(index);
NeighborAncestryInfo item = new NeighborAncestryInfo(current); ancestry.add(new NeighborAncestryInfo(
current,
item.descendantNeighbor = Optional // descendant neighbor
Optional
.ofNullable(count > index + 1 ? statuses.get(index + 1) : null) .ofNullable(count > index + 1 ? statuses.get(index + 1) : null)
.filter(s -> s.inReplyToId.equals(current.id)) .filter(s -> s.inReplyToId.equals(current.id))
.orElse(null); .orElse(null),
// ancestoring neighbor
item.ancestoringNeighbor = Optional.ofNullable(index > 0 ? ancestry.get(index - 1) : null) Optional.ofNullable(index > 0 ? ancestry.get(index - 1) : null)
.filter(ancestor -> ancestor .filter(ancestor -> Optional.ofNullable(ancestor.descendantNeighbor)
.getDescendantNeighbor()
.map(ancestorsDescendant -> ancestorsDescendant.id.equals(current.id)) .map(ancestorsDescendant -> ancestorsDescendant.id.equals(current.id))
.orElse(false)) .orElse(false))
.flatMap(NeighborAncestryInfo::getStatus) .map(a -> a.status)
.orElse(null); .orElse(null)
));
ancestry.add(item);
} }
return ancestry; return ancestry;
@ -281,31 +277,13 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
return Uri.parse(mainStatus.url); return Uri.parse(mainStatus.url);
} }
public static class NeighborAncestryInfo { protected static class NeighborAncestryInfo {
protected Status status, descendantNeighbor, ancestoringNeighbor; protected Status status, descendantNeighbor, ancestoringNeighbor;
public NeighborAncestryInfo(@NonNull Status status) { protected NeighborAncestryInfo(@NonNull Status status, Status descendantNeighbor, Status ancestoringNeighbor) {
this.status = status; this.status = status;
} this.descendantNeighbor = descendantNeighbor;
this.ancestoringNeighbor = ancestoringNeighbor;
public Optional<Status> getStatus() {
return Optional.ofNullable(status);
}
public Optional<Status> getDescendantNeighbor() {
return Optional.ofNullable(descendantNeighbor);
}
public Optional<Status> getAncestoringNeighbor() {
return Optional.ofNullable(ancestoringNeighbor);
}
public boolean hasDescendantNeighbor() {
return getDescendantNeighbor().isPresent();
}
public boolean hasAncestoringNeighbor() {
return getAncestoringNeighbor().isPresent();
} }
@Override @Override