Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Controller class used to encapsulate the usage of croutons.
package your_package;
import android.app.Activity;
import android.app.Application;
import android.content.res.Resources;
import android.os.Bundle;
import cyour_package.R;
import javax.inject.Inject;
import javax.inject.Singleton;
import de.keyboardsurfer.android.widget.crouton.Configuration;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import static de.keyboardsurfer.android.widget.crouton.Configuration.DURATION_SHORT;
/**
* Activity scoped controller used to show "croutons" for confirmation, warning and error messages.
*
* @author Sebastian Engel
*/
@Singleton
public class CroutonController {
private final Activity activity;
private Style confirmationStyle;
private Style warningStyle;
private Style errorStyle;
@Inject
public CroutonController(Activity activity) {
this.activity = activity;
initCroutonStyles();
ensureProperCleanup();
}
///////////////////////////////////////////////////////////////////////////
// Public behavior
///////////////////////////////////////////////////////////////////////////
public void showConfirmationMessage(String message) {
Crouton.showText(activity, message, confirmationStyle);
}
public void showWarningMessage(String message) {
Crouton.showText(activity, message, warningStyle);
}
public void showErrorMessage(String message) {
Crouton.showText(activity, message, errorStyle);
}
///////////////////////////////////////////////////////////////////////////
// Internal behavior
///////////////////////////////////////////////////////////////////////////
private void initCroutonStyles() {
Resources resources = activity.getResources();
confirmationStyle = new Style.Builder()
.setConfiguration(new Configuration.Builder().setDuration(DURATION_SHORT).build())
.setHeightDimensionResId(R.dimen.crouton_height)
.setBackgroundColorValue(resources.getColor(R.color.crouton_confirmation_background))
.build();
warningStyle = new Style.Builder()
.setConfiguration(new Configuration.Builder().setDuration(DURATION_SHORT).build())
.setHeightDimensionResId(R.dimen.crouton_height)
.setBackgroundColorValue(resources.getColor(R.color.crouton_warning_background))
.build();
errorStyle = new Style.Builder()
.setConfiguration(new Configuration.Builder().setDuration(DURATION_SHORT).build())
.setHeightDimensionResId(R.dimen.crouton_height)
.setBackgroundColorValue(resources.getColor(R.color.crouton_error_background))
.build();
}
private void ensureProperCleanup() {
// See https://github.com/keyboardsurfer/Crouton#important
// See https://github.com/keyboardsurfer/Crouton/issues/24
Application app = activity.getApplication();
app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
@Override public void onActivityStarted(Activity activity) {}
@Override public void onActivityResumed(Activity activity) {}
@Override public void onActivityPaused(Activity activity) {}
@Override public void onActivityStopped(Activity activity) {}
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
@Override public void onActivityDestroyed(Activity activity) {
Crouton.cancelAllCroutons();
Crouton.clearCroutonsForActivity(activity);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment