Fixed changing view model type

pull/156/head
M66B 6 years ago
parent 869ffd3c36
commit 9e301af6ad

@ -71,7 +71,6 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders; import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -2620,78 +2619,82 @@ public class FragmentMessages extends FragmentBase {
ViewModelMessages model = ViewModelProviders.of(getActivity()).get(ViewModelMessages.class); ViewModelMessages model = ViewModelProviders.of(getActivity()).get(ViewModelMessages.class);
LiveData<PagedList<TupleMessageEx>> liveMessages = model.getPagedList( ViewModelMessages.Model vmodel = model.getModel(
getContext(), getViewLifecycleOwner(), getContext(), getViewLifecycleOwner(),
viewType, account, folder, thread, id, query, server, viewType, account, folder, thread, id, query, server);
new BoundaryCallbackMessages.IBoundaryCallbackMessages() {
@Override
public void onLoading() {
loading = true;
pbWait.setVisibility(View.VISIBLE);
}
@Override
public void onLoaded(int fetched) {
loading = false;
pbWait.setVisibility(View.GONE);
Integer submitted = (Integer) rvMessage.getTag();
if (submitted == null)
submitted = 0;
if (submitted + fetched == 0)
tvNoEmail.setVisibility(View.VISIBLE);
}
@Override vmodel.setCallback(callback);
public void onError(Throwable ex) { vmodel.setObserver(getViewLifecycleOwner(), observer);
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) }
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(Helper.formatThrowable(ex))
.setPositiveButton(android.R.string.cancel, null)
.create()
.show();
}
});
liveMessages.observe(getViewLifecycleOwner(), new Observer<PagedList<TupleMessageEx>>() { private BoundaryCallbackMessages.IBoundaryCallbackMessages callback = new BoundaryCallbackMessages.IBoundaryCallbackMessages() {
@Override @Override
public void onChanged(@Nullable PagedList<TupleMessageEx> messages) { public void onLoading() {
if (messages == null) loading = true;
return; pbWait.setVisibility(View.VISIBLE);
}
if (viewType == AdapterMessage.ViewType.THREAD) @Override
if (handleThreadActions(messages)) public void onLoaded(int fetched) {
return; loading = false;
pbWait.setVisibility(View.GONE);
Integer submitted = (Integer) rvMessage.getTag();
if (submitted == null)
submitted = 0;
if (submitted + fetched == 0)
tvNoEmail.setVisibility(View.VISIBLE);
}
Log.i("Submit messages=" + messages.size()); @Override
adapter.submitList(messages); public void onError(Throwable ex) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(Helper.formatThrowable(ex))
.setPositiveButton(android.R.string.cancel, null)
.create()
.show();
}
};
// This is to workaround not drawing when the search is expanded private Observer<PagedList<TupleMessageEx>> observer = new Observer<PagedList<TupleMessageEx>>() {
new Handler().post(new Runnable() { @Override
@Override public void onChanged(@Nullable PagedList<TupleMessageEx> messages) {
public void run() { if (messages == null)
rvMessage.requestLayout(); return;
}
});
rvMessage.setTag(messages.size()); if (viewType == AdapterMessage.ViewType.THREAD)
if (handleThreadActions(messages))
return;
boolean hasBoundary = (viewType == AdapterMessage.ViewType.FOLDER || viewType == AdapterMessage.ViewType.SEARCH); Log.i("Submit messages=" + messages.size());
adapter.submitList(messages);
if (!hasBoundary || !loading) // This is to workaround not drawing when the search is expanded
pbWait.setVisibility(View.GONE); new Handler().post(new Runnable() {
if (!hasBoundary && messages.size() == 0) @Override
tvNoEmail.setVisibility(View.VISIBLE); public void run() {
if (messages.size() > 0) { rvMessage.requestLayout();
tvNoEmail.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
} }
});
rvMessage.setTag(messages.size());
boolean hasBoundary = (viewType == AdapterMessage.ViewType.FOLDER || viewType == AdapterMessage.ViewType.SEARCH);
if (!hasBoundary || !loading)
pbWait.setVisibility(View.GONE);
if (!hasBoundary && messages.size() == 0)
tvNoEmail.setVisibility(View.VISIBLE);
if (messages.size() > 0) {
tvNoEmail.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
} }
}); }
} };
private boolean handleThreadActions(@NonNull PagedList<TupleMessageEx> messages) { private boolean handleThreadActions(@NonNull PagedList<TupleMessageEx> messages) {
// Auto close / next // Auto close / next

@ -52,12 +52,11 @@ public class ViewModelMessages extends ViewModel {
private static final int LOCAL_PAGE_SIZE = 100; private static final int LOCAL_PAGE_SIZE = 100;
private static final int REMOTE_PAGE_SIZE = 10; private static final int REMOTE_PAGE_SIZE = 10;
LiveData<PagedList<TupleMessageEx>> getPagedList( Model getModel(
Context context, LifecycleOwner owner, Context context, final LifecycleOwner owner,
final AdapterMessage.ViewType viewType, final AdapterMessage.ViewType viewType,
long account, long folder, String thread, long id, long account, long folder, String thread, long id,
String query, boolean server, String query, boolean server) {
BoundaryCallbackMessages.IBoundaryCallbackMessages callback) {
Args args = new Args(context, account, folder, thread, id, query, server); Args args = new Args(context, account, folder, thread, id, query, server);
Log.i("Get model " + viewType + " " + args); Log.i("Get model " + viewType + " " + args);
@ -67,8 +66,10 @@ public class ViewModelMessages extends ViewModel {
if (model == null || !model.args.equals(args)) { if (model == null || !model.args.equals(args)) {
Log.i("Creating model"); Log.i("Creating model");
if (model != null) if (model != null) {
model.clear(); model.clear();
model.list.removeObservers(owner);
}
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
@ -168,9 +169,6 @@ public class ViewModelMessages extends ViewModel {
}); });
} }
if (model.boundary != null)
model.boundary.setCallback(callback);
if (viewType == AdapterMessage.ViewType.UNIFIED) { if (viewType == AdapterMessage.ViewType.UNIFIED) {
remove(AdapterMessage.ViewType.FOLDER); remove(AdapterMessage.ViewType.FOLDER);
remove(AdapterMessage.ViewType.SEARCH); remove(AdapterMessage.ViewType.SEARCH);
@ -182,7 +180,7 @@ public class ViewModelMessages extends ViewModel {
Log.i("Returning model=" + viewType); Log.i("Returning model=" + viewType);
dump(); dump();
return model.list; return model;
} }
@Override @Override
@ -193,10 +191,8 @@ public class ViewModelMessages extends ViewModel {
private void remove(AdapterMessage.ViewType viewType) { private void remove(AdapterMessage.ViewType viewType) {
Model model = models.get(viewType); Model model = models.get(viewType);
if (model != null) { if (model != null)
model.clear();
models.remove(viewType); models.remove(viewType);
}
} }
void observePrevNext(LifecycleOwner owner, final long id, final IPrevNext intf) { void observePrevNext(LifecycleOwner owner, final long id, final IPrevNext intf) {
@ -321,7 +317,7 @@ public class ViewModelMessages extends ViewModel {
Log.i("Current models=" + TextUtils.join(", ", models.keySet())); Log.i("Current models=" + TextUtils.join(", ", models.keySet()));
} }
private class Model { class Model {
private Args args; private Args args;
private LiveData<PagedList<TupleMessageEx>> list; private LiveData<PagedList<TupleMessageEx>> list;
private BoundaryCallbackMessages boundary; private BoundaryCallbackMessages boundary;
@ -332,6 +328,15 @@ public class ViewModelMessages extends ViewModel {
this.boundary = boundary; this.boundary = boundary;
} }
void setCallback(BoundaryCallbackMessages.IBoundaryCallbackMessages callback) {
if (boundary != null)
boundary.setCallback(callback);
}
void setObserver(LifecycleOwner owner, @NonNull Observer<PagedList<TupleMessageEx>> observer) {
list.observe(owner, observer);
}
private void clear() { private void clear() {
if (this.boundary != null) if (this.boundary != null)
this.boundary.clear(); this.boundary.clear();

Loading…
Cancel
Save