fix updated main status not being applied
This commit is contained in:
@@ -53,24 +53,30 @@ public class ThreadFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateMainStatus() {
|
public void maybeApplyMainStatus() {
|
||||||
ThreadFragment fragment = new ThreadFragment();
|
ThreadFragment fragment = new ThreadFragment();
|
||||||
|
fragment.contextInitiallyRendered = true;
|
||||||
fragment.mainStatus = Status.ofFake("123456", "original text", Instant.EPOCH);
|
fragment.mainStatus = Status.ofFake("123456", "original text", Instant.EPOCH);
|
||||||
|
|
||||||
Status update1 = Status.ofFake("123456", "updated text", Instant.EPOCH);
|
Status update1 = Status.ofFake("123456", "updated text", Instant.EPOCH);
|
||||||
update1.editedAt = Instant.ofEpochSecond(1);
|
update1.editedAt = Instant.ofEpochSecond(1);
|
||||||
fragment.updatedStatus = update1;
|
fragment.updatedStatus = update1;
|
||||||
StatusUpdatedEvent event1 = (StatusUpdatedEvent) fragment.updateMainStatus();
|
StatusUpdatedEvent event1 = (StatusUpdatedEvent) fragment.maybeApplyMainStatus();
|
||||||
assertEquals("fired update event", update1, event1.status);
|
assertEquals("fired update event", update1, event1.status);
|
||||||
assertEquals("updated main status", update1, fragment.mainStatus);
|
assertEquals("updated main status", update1, fragment.mainStatus);
|
||||||
|
|
||||||
Status update2 = Status.ofFake("123456", "updated text", Instant.EPOCH);
|
Status update2 = Status.ofFake("123456", "updated text", Instant.EPOCH);
|
||||||
update2.favouritesCount = 123;
|
update2.favouritesCount = 123;
|
||||||
fragment.updatedStatus = update2;
|
fragment.updatedStatus = update2;
|
||||||
StatusCountersUpdatedEvent event2 = (StatusCountersUpdatedEvent) fragment.updateMainStatus();
|
StatusCountersUpdatedEvent event2 = (StatusCountersUpdatedEvent) fragment.maybeApplyMainStatus();
|
||||||
assertEquals("only fired counter update event", update2.id, event2.id);
|
assertEquals("only fired counter update event", update2.id, event2.id);
|
||||||
assertEquals("updated counter is correct", 123, event2.favorites);
|
assertEquals("updated counter is correct", 123, event2.favorites);
|
||||||
assertEquals("updated main status", update2, fragment.mainStatus);
|
assertEquals("updated main status", update2, fragment.mainStatus);
|
||||||
|
|
||||||
|
Status update3 = Status.ofFake("123456", "whatever", Instant.EPOCH);
|
||||||
|
fragment.contextInitiallyRendered = false;
|
||||||
|
fragment.updatedStatus = update3;
|
||||||
|
assertNull("no update when context hasn't been rendered", fragment.maybeApplyMainStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ import me.grishka.appkit.utils.V;
|
|||||||
public class ThreadFragment extends StatusListFragment implements ProvidesAssistContent {
|
public class ThreadFragment extends StatusListFragment implements ProvidesAssistContent {
|
||||||
protected Status mainStatus, updatedStatus;
|
protected Status mainStatus, updatedStatus;
|
||||||
private final HashMap<String, NeighborAncestryInfo> ancestryMap = new HashMap<>();
|
private final HashMap<String, NeighborAncestryInfo> ancestryMap = new HashMap<>();
|
||||||
private boolean initialAnimationFinished;
|
protected boolean contextInitiallyRendered;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState){
|
public void onCreate(Bundle savedInstanceState){
|
||||||
@@ -111,7 +111,7 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doLoadData(int offset, int count){
|
protected void doLoadData(int offset, int count){
|
||||||
refreshMainStatus();
|
loadMainStatus();
|
||||||
currentRequest=new GetStatusContext(mainStatus.id)
|
currentRequest=new GetStatusContext(mainStatus.id)
|
||||||
.setCallback(new SimpleCallback<>(this){
|
.setCallback(new SimpleCallback<>(this){
|
||||||
@Override
|
@Override
|
||||||
@@ -155,21 +155,28 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
|
|||||||
adapter.notifyDataSetChanged();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
list.scrollToPosition(displayItems.size()-count);
|
list.scrollToPosition(displayItems.size()-count);
|
||||||
|
|
||||||
|
// no animation is going to happen, so proceeding to apply right now
|
||||||
|
if (data.size() == 1) {
|
||||||
|
contextInitiallyRendered = true;
|
||||||
|
// for the case that the main status has already finished loading
|
||||||
|
maybeApplyMainStatus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.exec(accountID);
|
.exec(accountID);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshMainStatus() {
|
private void loadMainStatus() {
|
||||||
new GetStatusByID(mainStatus.id)
|
new GetStatusByID(mainStatus.id)
|
||||||
.setCallback(new Callback<>() {
|
.setCallback(new Callback<>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Status status) {
|
public void onSuccess(Status status) {
|
||||||
if (getContext() == null || status == null) return;
|
if (getContext() == null || status == null) return;
|
||||||
updatedStatus = status;
|
updatedStatus = status;
|
||||||
// only update main status if the initial animation is already finished.
|
// for the case that the context has already loaded (and the animation has
|
||||||
// otherwise, the animator will call it in onAnimationFinished
|
// already finished), falling back to applying it ourselves:
|
||||||
if (initialAnimationFinished || data.size() == 1) updateMainStatus();
|
maybeApplyMainStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -177,7 +184,9 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
|
|||||||
}).exec(accountID);
|
}).exec(accountID);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object updateMainStatus() {
|
protected Object maybeApplyMainStatus() {
|
||||||
|
if (updatedStatus == null || !contextInitiallyRendered) return null;
|
||||||
|
|
||||||
// returning fired event object to facilitate testing
|
// returning fired event object to facilitate testing
|
||||||
Object event;
|
Object event;
|
||||||
if (updatedStatus.editedAt != null &&
|
if (updatedStatus.editedAt != null &&
|
||||||
@@ -288,15 +297,14 @@ public class ThreadFragment extends StatusListFragment implements ProvidesAssist
|
|||||||
showContent();
|
showContent();
|
||||||
if(!loaded)
|
if(!loaded)
|
||||||
footerProgress.setVisibility(View.VISIBLE);
|
footerProgress.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
list.setItemAnimator(new BetterItemAnimator() {
|
list.setItemAnimator(new BetterItemAnimator() {
|
||||||
@Override
|
@Override
|
||||||
public void onAnimationFinished(@NonNull RecyclerView.ViewHolder viewHolder) {
|
public void onAnimationFinished(@NonNull RecyclerView.ViewHolder viewHolder) {
|
||||||
super.onAnimationFinished(viewHolder);
|
super.onAnimationFinished(viewHolder);
|
||||||
// in case someone else is about to call updateMainStatus faster...
|
contextInitiallyRendered = true;
|
||||||
initialAnimationFinished = true;
|
// for the case that both requests are already done (and thus won't apply it)
|
||||||
// ...if not (someone did fetch it but the animation wasn't finished yet),
|
maybeApplyMainStatus();
|
||||||
// call it now
|
|
||||||
if (updatedStatus != null) updateMainStatus();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user