All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.pivotal.arca.dispatcher.ModernRequestDispatcher Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/* 
 * 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.annotation.TargetApi;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Context;
import android.content.Loader;
import android.os.Build;
import android.os.Bundle;

import io.pivotal.arca.dispatcher.RequestDispatcher.AbstractRequestDispatcher;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ModernRequestDispatcher extends AbstractRequestDispatcher {

	private final Context mContext;
	private final LoaderManager mLoaderManager;

	public ModernRequestDispatcher(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 ModernQueryLoader(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 ModernUpdateLoader(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 ModernInsertLoader(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 ModernDeleteLoader(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