com.stanfy.enroscar.async.internal.OperatorBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enroscar-content Show documentation
Show all versions of enroscar-content Show documentation
Helper classes and extended abstractions for Android loaders, content resolvers, and DB access.
package com.stanfy.enroscar.async.internal;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import com.stanfy.enroscar.async.OperatorBuilder;
/**
* Base class for generated operators.
* @author Roman Mazur - Stanfy (http://stanfy.com)
*/
public abstract class OperatorBase {
/** Callbacks description. */
private final D description;
/** Operator context. */
private final OperatorContext operatorContext;
protected OperatorBase(final D description,
final OperatorContext operatorContext) {
this.operatorContext = operatorContext;
this.description = description;
}
public final D when() {
return description;
}
protected final W getOperations() {
return operatorContext.getOperations();
}
protected final void initLoader(final int loaderId, final AsyncProvider> provider,
final boolean destroyOnFinish) {
LoaderManager lm = operatorContext.getLoaderManager();
lm.initLoader(loaderId, null, description.makeCallbacks(loaderId, provider, destroyOnFinish));
}
protected final void restartLoader(final int loaderId, final AsyncProvider> provider) {
LoaderManager lm = operatorContext.getLoaderManager();
lm.restartLoader(loaderId, null, description.makeCallbacks(loaderId, provider, false));
}
protected final void destroyLoader(final int loaderId) {
LoaderManager lm = operatorContext.getLoaderManager();
lm.destroyLoader(loaderId);
}
/**
* @param operations object type
*/
public static final class OperatorContext {
/** Operations object. */
T operations;
/** Context instance. */
Context context;
/** Loader manager. */
LoaderManager loaderManager;
OperatorContext() { }
void validate() {
if (context == null) {
throw new IllegalStateException("Context is not defined");
}
if (loaderManager == null) {
throw new IllegalStateException("Loader manager is not defined");
}
if (operations == null) {
throw new IllegalStateException("Operations object is not defined");
}
}
public LoaderManager getLoaderManager() {
return loaderManager;
}
public T getOperations() {
return operations;
}
}
/**
* @author Roman Mazur - Stanfy (http://stanfy.com)
*/
public abstract static class OperatorBuilderBase implements OperatorBuilder {
/** Context for the built object. */
private final OperatorContext operatorContext = new OperatorContext<>();
@Override
public final OperatorBuilder operations(final W operations) {
this.operatorContext.operations = operations;
return this;
}
@Override
public final OperatorBuilder loaderManager(final LoaderManager loaderManager) {
this.operatorContext.loaderManager = loaderManager;
return this;
}
@Override
public final OperatorBuilder context(final Context context) {
this.operatorContext.context = context;
return this;
}
@Override
public final OperatorBuilder withinActivity(final FragmentActivity activity) {
this.operatorContext.context = activity;
this.operatorContext.loaderManager = activity.getSupportLoaderManager();
return this;
}
@Override
public final OperatorBuilder withinFragment(final Fragment fragment) {
this.operatorContext.context = fragment.getActivity();
this.operatorContext.loaderManager = fragment.getLoaderManager();
return this;
}
@Override
public final T get() {
operatorContext.validate();
return create(operatorContext);
}
/**
* @return operator instance
*/
protected abstract T create(final OperatorContext operatorContext);
}
}