Created
September 11, 2018 08:52
-
-
Save xsahil03x/084a5c5ff4279edde63a100df37b0e3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RecipeIngredientWidget extends AppWidgetProvider { | |
@Inject | |
PreferenceRepository prefRepo; | |
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Recipe recipe) { | |
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), | |
R.layout.recipe_ingredient_widget); | |
// launch app when button is clicked | |
Intent appOpenIntent = new Intent(context, RecipeActivity.class); | |
PendingIntent addIngredientPi = PendingIntent.getActivity(context, 0, appOpenIntent, PendingIntent.FLAG_UPDATE_CURRENT); | |
remoteViews.setOnClickPendingIntent(R.id.btnAddIngredient, addIngredientPi); | |
Intent listViewIntent = new Intent(context, IngredientListWidgetService.class); | |
listViewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); | |
listViewIntent.setData(Uri.parse(listViewIntent.toUri(Intent.URI_INTENT_SCHEME))); | |
remoteViews.setRemoteAdapter(R.id.lvRecipeIngredient, listViewIntent); | |
setViews(context, remoteViews, appWidgetId, recipe); | |
// Instruct the widget manager to update the widget | |
appWidgetManager.updateAppWidget(appWidgetId, remoteViews); | |
} | |
private static void setViews(Context context, RemoteViews remoteViews, int appWidgetId, Recipe recipe) { | |
remoteViews.setTextViewText(R.id.tvRecipeName, recipe.getName()); | |
AppWidgetTarget appWidgetTarget = new AppWidgetTarget(context, R.id.ivRecipe, remoteViews, appWidgetId) { | |
@Override | |
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) { | |
super.onResourceReady(resource, transition); | |
} | |
}; | |
GlideApp.with(context.getApplicationContext()) | |
.asBitmap() | |
.load(recipe.getImageId()) | |
.into(appWidgetTarget); | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
AndroidInjection.inject(this, context); | |
super.onReceive(context, intent); | |
} | |
@Override | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { | |
// There may be multiple widgets active, so update all of them | |
for (int appWidgetId : appWidgetIds) { | |
Recipe recipe = prefRepo.getIngredientsWidgetRecipe(appWidgetId); | |
if (recipe == null) continue; | |
updateAppWidget(context, appWidgetManager, appWidgetId, recipe); | |
} | |
super.onUpdate(context, appWidgetManager, appWidgetIds); | |
} | |
@Override | |
public void onDeleted(Context context, int[] appWidgetIds) { | |
for (int appWidgetId : appWidgetIds) | |
prefRepo.deleteIngredientsWidgetRecipe(appWidgetId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment