fix crash in TimelineFragment

This commit is contained in:
Conny Duck 2018-07-12 21:21:53 +02:00
parent 145e30870d
commit 6f179fccaa
4 changed files with 66 additions and 92 deletions

View File

@ -16,8 +16,6 @@
package com.keylesspalace.tusky.fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@ -28,21 +26,11 @@ import retrofit2.Call;
public class BaseFragment extends Fragment {
protected List<Call> callList;
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callList = new ArrayList<>();
handler.post(this::onPostCreate);
}
/**
* For actions which should happen only once per lifecycle but after onCreate.
* Example: subscribe for events in {@code onCreate()} but need dependencies to be injected
*/
public void onPostCreate() {
// No-op
}
@Override

View File

@ -214,23 +214,6 @@ public class NotificationsFragment extends SFragment implements
return rootView;
}
@Override
public void onPostCreate() {
super.onPostCreate();
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof FavoriteEvent) {
handleFavEvent((FavoriteEvent) event);
} else if (event instanceof ReblogEvent) {
handleReblogEvent((ReblogEvent) event);
} else if (event instanceof BlockEvent) {
removeAllByAccountId(((BlockEvent) event).getAccountId());
}
});
}
private void setupNothingView() {
Drawable top = AppCompatResources.getDrawable(Objects.requireNonNull(getContext()),
R.drawable.elephant_friend);
@ -320,6 +303,19 @@ public class NotificationsFragment extends SFragment implements
};
recyclerView.addOnScrollListener(scrollListener);
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof FavoriteEvent) {
handleFavEvent((FavoriteEvent) event);
} else if (event instanceof ReblogEvent) {
handleReblogEvent((ReblogEvent) event);
} else if (event instanceof BlockEvent) {
removeAllByAccountId(((BlockEvent) event).getAccountId());
}
});
}
@Override

View File

@ -293,47 +293,6 @@ public class TimelineFragment extends SFragment implements
recyclerView.setAdapter(adapter);
}
@Override
public void onPostCreate() {
super.onPostCreate();
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof FavoriteEvent) {
FavoriteEvent favEvent = ((FavoriteEvent) event);
handleFavEvent(favEvent);
} else if (event instanceof ReblogEvent) {
ReblogEvent reblogEvent = (ReblogEvent) event;
handleReblogEvent(reblogEvent);
} else if (event instanceof UnfollowEvent) {
if (kind == Kind.HOME) {
String id = ((UnfollowEvent) event).getAccountId();
removeAllByAccountId(id);
}
} else if (event instanceof BlockEvent) {
if (kind != Kind.USER && kind != Kind.USER_WITH_REPLIES) {
String id = ((BlockEvent) event).getAccountId();
removeAllByAccountId(id);
}
} else if (event instanceof MuteEvent) {
if (kind != Kind.USER && kind != Kind.USER_WITH_REPLIES) {
String id = ((MuteEvent) event).getAccountId();
removeAllByAccountId(id);
}
} else if (event instanceof StatusDeletedEvent) {
if (kind != Kind.USER && kind != Kind.USER_WITH_REPLIES) {
String id = ((StatusDeletedEvent) event).getStatusId();
deleteStatusById(id);
}
} else if (event instanceof StatusComposedEvent) {
Status status = ((StatusComposedEvent) event).getStatus();
handleStatusComposeEvent(status);
}
});
}
private void deleteStatusById(String id) {
for (int i = 0; i < statuses.size(); i++) {
Either<Placeholder, Status> either = statuses.get(i);
@ -414,6 +373,42 @@ public class TimelineFragment extends SFragment implements
};
}
recyclerView.addOnScrollListener(scrollListener);
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof FavoriteEvent) {
FavoriteEvent favEvent = ((FavoriteEvent) event);
handleFavEvent(favEvent);
} else if (event instanceof ReblogEvent) {
ReblogEvent reblogEvent = (ReblogEvent) event;
handleReblogEvent(reblogEvent);
} else if (event instanceof UnfollowEvent) {
if (kind == Kind.HOME) {
String id = ((UnfollowEvent) event).getAccountId();
removeAllByAccountId(id);
}
} else if (event instanceof BlockEvent) {
if (kind != Kind.USER && kind != Kind.USER_WITH_REPLIES) {
String id = ((BlockEvent) event).getAccountId();
removeAllByAccountId(id);
}
} else if (event instanceof MuteEvent) {
if (kind != Kind.USER && kind != Kind.USER_WITH_REPLIES) {
String id = ((MuteEvent) event).getAccountId();
removeAllByAccountId(id);
}
} else if (event instanceof StatusDeletedEvent) {
if (kind != Kind.USER && kind != Kind.USER_WITH_REPLIES) {
String id = ((StatusDeletedEvent) event).getStatusId();
deleteStatusById(id);
}
} else if (event instanceof StatusComposedEvent) {
Status status = ((StatusComposedEvent) event).getStatus();
handleStatusComposeEvent(status);
}
});
}
@Override

View File

@ -124,28 +124,6 @@ public final class ViewThreadFragment extends SFragment implements
adapter = new ThreadAdapter(this);
}
@Override
public void onPostCreate() {
super.onPostCreate();
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof FavoriteEvent) {
handleFavEvent((FavoriteEvent) event);
} else if (event instanceof ReblogEvent) {
handleReblogEvent((ReblogEvent) event);
} else if (event instanceof BlockEvent) {
removeAllByAccountId(((BlockEvent) event).getAccountId());
} else if (event instanceof StatusComposedEvent) {
handleStatusComposedEvent((StatusComposedEvent) event);
} else if (event instanceof StatusDeletedEvent) {
handleStatusDeletedEvent((StatusDeletedEvent) event);
}
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
@ -192,6 +170,23 @@ public final class ViewThreadFragment extends SFragment implements
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
onRefresh();
eventHub.getEvents()
.observeOn(AndroidSchedulers.mainThread())
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(event -> {
if (event instanceof FavoriteEvent) {
handleFavEvent((FavoriteEvent) event);
} else if (event instanceof ReblogEvent) {
handleReblogEvent((ReblogEvent) event);
} else if (event instanceof BlockEvent) {
removeAllByAccountId(((BlockEvent) event).getAccountId());
} else if (event instanceof StatusComposedEvent) {
handleStatusComposedEvent((StatusComposedEvent) event);
} else if (event instanceof StatusDeletedEvent) {
handleStatusDeletedEvent((StatusDeletedEvent) event);
}
});
}
public void onRevealPressed() {