io.pivotal.arca.dispatcher.SupportRequestDispatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of arca-dispatcher Show documentation
Show all versions of arca-dispatcher Show documentation
A comprehensive framework for caching and displaying data.
/*
* Copyright (C) 2014 Pivotal Software, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.pivotal.arca.dispatcher;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import io.pivotal.arca.dispatcher.RequestDispatcher.AbstractRequestDispatcher;
public class SupportRequestDispatcher extends AbstractRequestDispatcher {
private final Context mContext;
private final LoaderManager mLoaderManager;
public SupportRequestDispatcher(final RequestExecutor executor, final Context context, final LoaderManager manager) {
super(executor);
mContext = context;
mLoaderManager = manager;
}
@Override
public void execute(final Query request, final QueryListener listener) {
final LoaderCallbacks> callbacks = new QueryLoaderCallbacks(listener);
execute(request, callbacks);
}
@Override
public void execute(final Update request, final UpdateListener listener) {
final LoaderCallbacks> callbacks = new UpdateLoaderCallbacks(listener);
execute(request, callbacks);
}
@Override
public void execute(final Insert request, final InsertListener listener) {
final LoaderCallbacks> callbacks = new InsertLoaderCallbacks(listener);
execute(request, callbacks);
}
@Override
public void execute(final Delete request, final DeleteListener listener) {
final LoaderCallbacks> callbacks = new DeleteLoaderCallbacks(listener);
execute(request, callbacks);
}
private void execute(final Request> request, final LoaderCallbacks> callbacks) {
final int identifier = request.getIdentifier();
final Bundle bundle = createRequestBundle(request);
mLoaderManager.restartLoader(identifier, bundle, callbacks);
}
// ========================================
private class QueryLoaderCallbacks extends NotifierCallbacks {
public QueryLoaderCallbacks(final QueryListener listener) {
super(listener);
}
@Override
public Loader onCreateLoader(final int id, final Bundle args) {
final RequestExecutor executor = getRequestExecutor();
final Query request = args.getParcelable(Extras.REQUEST);
return new SupportQueryLoader(mContext, executor, request);
}
}
private class UpdateLoaderCallbacks extends NotifierCallbacks {
public UpdateLoaderCallbacks(final UpdateListener listener) {
super(listener);
}
@Override
public Loader onCreateLoader(final int id, final Bundle args) {
final RequestExecutor executor = getRequestExecutor();
final Update request = args.getParcelable(Extras.REQUEST);
return new SupportUpdateLoader(mContext, executor, request);
}
}
private class InsertLoaderCallbacks extends NotifierCallbacks {
public InsertLoaderCallbacks(final InsertListener listener) {
super(listener);
}
@Override
public Loader onCreateLoader(final int id, final Bundle args) {
final RequestExecutor executor = getRequestExecutor();
final Insert request = args.getParcelable(Extras.REQUEST);
return new SupportInsertLoader(mContext, executor, request);
}
}
private class DeleteLoaderCallbacks extends NotifierCallbacks {
public DeleteLoaderCallbacks(final DeleteListener listener) {
super(listener);
}
@Override
public Loader onCreateLoader(final int id, final Bundle args) {
final RequestExecutor executor = getRequestExecutor();
final Delete request = args.getParcelable(Extras.REQUEST);
return new SupportDeleteLoader(mContext, executor, request);
}
}
private abstract class NotifierCallbacks> implements LoaderCallbacks {
private final RequestListener mListener;
public NotifierCallbacks(final RequestListener listener) {
mListener = listener;
}
@Override
public void onLoadFinished(final Loader loader, final T result) {
if (mListener != null) {
mListener.onRequestComplete(result);
}
}
@Override
public void onLoaderReset(final Loader loader) {
if (mListener != null) {
mListener.onRequestReset();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy