import
android.appwidget.AppWidgetManager;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.ImageView;
import
android.widget.TextView;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
import
java.util.ArrayList;
public
class
DetailsActivity
extends
AppCompatActivity {
TextView name, content;
ImageView addToWidget;
boolean
added =
false
;
private
String recipe1
=
"Step1: Take a Lemon and required no of glasses of water"
+
"Step2: Squeeze out the lemon juice into glasses,stir well"
+
"and put iceCubes before serve"
;
private
String recipe2
=
"Step1: Take a bread and apply some butter on it"
+
"Step2:Put it in the toaster and it is ready"
;
ArrayList<String> steps =
new
ArrayList<String>();
public
static
Recipe recipe;
AppWidgetManager appWidgetManager;
int
appWidgetId;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
steps.add(recipe1);
steps.add(recipe2);
addToWidget = findViewById(R.id.addToWidget);
appWidgetManager = AppWidgetManager.getInstance(
DetailsActivity.
this
);
appWidgetId =
1
;
name = findViewById(R.id.name);
content = findViewById(R.id.steps);
final
String heading
= getIntent().getStringExtra(
"name"
);
final
int
pos = getIntent().getIntExtra(
"pos"
, -
1
);
recipe =
new
Recipe(heading, steps.get(pos));
name.setText(heading);
content.setText(steps.get(pos));
addToWidget.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view)
{
added
= !added;
Toast
.makeText(DetailsActivity.
this
,
"Click"
,
Toast.LENGTH_SHORT)
.show();
if
(added) {
RecipeWidget.updateAppWidget(
DetailsActivity.
this
,
appWidgetManager, appWidgetId,
recipe);
Toast
.makeText(DetailsActivity.
this
,
"Added to Widget"
,
Toast.LENGTH_SHORT)
.show();
addToWidget.setImageDrawable(
getResources().getDrawable(
R.drawable.add_widget));
}
else
{
addToWidget.setImageDrawable(
getResources().getDrawable(
R.drawable.not_widget));
RecipeWidget.updateAppWidget(
DetailsActivity.
this
,
appWidgetManager, appWidgetId,
null
);
}
}
});
}
public
static
Recipe getRecipe() {
return
recipe; }
}