Skip to content

Instantly share code, notes, and snippets.

@haerulmuttaqin
Last active October 29, 2021 11:54
Show Gist options
  • Save haerulmuttaqin/5752b52e819782f9ffd7dab4fecb2164 to your computer and use it in GitHub Desktop.
Save haerulmuttaqin/5752b52e819782f9ffd7dab4fecb2164 to your computer and use it in GitHub Desktop.
/*-----------------------------------------------------------------------------
- Developed by Haerul Muttaqin -
- Last modified 3/17/19 3:27 AM -
- Subscribe : https://www.youtube.com/haerulmuttaqin -
- Copyright (c) 2019. All rights reserved -
-----------------------------------------------------------------------------*/
package com.haerul.foodsapp.view.detail;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.haerul.foodsapp.R;
import com.haerul.foodsapp.Utils;
import com.haerul.foodsapp.model.Meals;
import com.squareup.picasso.Picasso;
import butterknife.BindView;
import butterknife.ButterKnife;
import static com.haerul.foodsapp.view.home.HomeActivity.EXTRA_DETAIL;
public class DetailActivity extends AppCompatActivity implements DetailView {
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.appbar)
AppBarLayout appBarLayout;
@BindView(R.id.collapsing_toolbar)
CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.mealThumb)
ImageView mealThumb;
@BindView(R.id.category)
TextView category;
@BindView(R.id.country)
TextView country;
@BindView(R.id.instructions)
TextView instructions;
@BindView(R.id.ingredient)
TextView ingredients;
@BindView(R.id.measure)
TextView measures;
@BindView(R.id.progressBar)
ProgressBar progressBar;
@BindView(R.id.youtube)
TextView youtube;
@BindView(R.id.source)
TextView source;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ButterKnife.bind(this);
setupActionBar();
Intent intent = getIntent();
String strMealName = intent.getStringExtra(EXTRA_DETAIL);
DetailPresenter presenter = new DetailPresenter(this);
presenter.getMealById(strMealName);
}
private void setupActionBar() {
setSupportActionBar(toolbar);
collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.colorWhite));
collapsingToolbarLayout.setCollapsedTitleTextColor(getResources().getColor(R.color.colorPrimary));
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.colorWhite));
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
void setupColorActionBarIcon(Drawable favoriteItemColor) {
appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
if ((collapsingToolbarLayout.getHeight() + verticalOffset) < (2 * ViewCompat.getMinimumHeight(collapsingToolbarLayout))) {
if (toolbar.getNavigationIcon() != null)
toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);
favoriteItemColor.mutate().setColorFilter(getResources().getColor(R.color.colorPrimary),
PorterDuff.Mode.SRC_ATOP);
} else {
if (toolbar.getNavigationIcon() != null)
toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.colorWhite), PorterDuff.Mode.SRC_ATOP);
favoriteItemColor.mutate().setColorFilter(getResources().getColor(R.color.colorWhite),
PorterDuff.Mode.SRC_ATOP);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_detail, menu);
MenuItem favoriteItem = menu.findItem(R.id.favorite);
Drawable favoriteItemColor = favoriteItem.getIcon();
setupColorActionBarIcon(favoriteItemColor);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home :
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void showLoading() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void hideLoading() {
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void setMeal(Meals.Meal meal) {
Picasso.get().load(meal.getStrMealThumb()).into(mealThumb);
collapsingToolbarLayout.setTitle(meal.getStrMeal());
category.setText(meal.getStrCategory());
country.setText(meal.getStrArea());
instructions.setText(meal.getStrInstructions());
setupActionBar();
//===
if (!meal.getStrIngredient1().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient1());
}
if (!meal.getStrIngredient2().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient2());
}
if (!meal.getStrIngredient3().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient3());
}
if (!meal.getStrIngredient4().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient4());
}
if (!meal.getStrIngredient5().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient5());
}
if (!meal.getStrIngredient6().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient6());
}
if (!meal.getStrIngredient7().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient7());
}
if (!meal.getStrIngredient8().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient8());
}
if (!meal.getStrIngredient9().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient9());
}
if (!meal.getStrIngredient10().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient10());
}
if (!meal.getStrIngredient11().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient11());
}
if (!meal.getStrIngredient12().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient12());
}
if (!meal.getStrIngredient13().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient13());
}
if (!meal.getStrIngredient14().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient14());
}
if (!meal.getStrIngredient15().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient15());
}
if (!meal.getStrIngredient16().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient16());
}
if (!meal.getStrIngredient17().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient17());
}
if (!meal.getStrIngredient18().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient18());
}
if (!meal.getStrIngredient19().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient19());
}
if (!meal.getStrIngredient20().isEmpty()) {
ingredients.append("\n \u2022 " + meal.getStrIngredient20());
}
if (!meal.getStrMeasure1().isEmpty() && !Character.isWhitespace(meal.getStrMeasure1().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure1());
}
if (!meal.getStrMeasure2().isEmpty() && !Character.isWhitespace(meal.getStrMeasure2().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure2());
}
if (!meal.getStrMeasure3().isEmpty() && !Character.isWhitespace(meal.getStrMeasure3().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure3());
}
if (!meal.getStrMeasure4().isEmpty() && !Character.isWhitespace(meal.getStrMeasure4().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure4());
}
if (!meal.getStrMeasure5().isEmpty() && !Character.isWhitespace(meal.getStrMeasure5().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure5());
}
if (!meal.getStrMeasure6().isEmpty() && !Character.isWhitespace(meal.getStrMeasure6().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure6());
}
if (!meal.getStrMeasure7().isEmpty() && !Character.isWhitespace(meal.getStrMeasure7().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure7());
}
if (!meal.getStrMeasure8().isEmpty() && !Character.isWhitespace(meal.getStrMeasure8().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure8());
}
if (!meal.getStrMeasure9().isEmpty() && !Character.isWhitespace(meal.getStrMeasure9().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure9());
}
if (!meal.getStrMeasure10().isEmpty() && !Character.isWhitespace(meal.getStrMeasure10().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure10());
}
if (!meal.getStrMeasure11().isEmpty() && !Character.isWhitespace(meal.getStrMeasure11().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure11());
}
if (!meal.getStrMeasure12().isEmpty() && !Character.isWhitespace(meal.getStrMeasure12().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure12());
}
if (!meal.getStrMeasure13().isEmpty() && !Character.isWhitespace(meal.getStrMeasure13().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure13());
}
if (!meal.getStrMeasure14().isEmpty() && !Character.isWhitespace(meal.getStrMeasure14().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure14());
}
if (!meal.getStrMeasure15().isEmpty() && !Character.isWhitespace(meal.getStrMeasure15().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure15());
}
if (!meal.getStrMeasure16().isEmpty() && !Character.isWhitespace(meal.getStrMeasure16().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure16());
}
if (!meal.getStrMeasure17().isEmpty() && !Character.isWhitespace(meal.getStrMeasure17().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure17());
}
if (!meal.getStrMeasure18().isEmpty() && !Character.isWhitespace(meal.getStrMeasure18().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure18());
}
if (!meal.getStrMeasure19().isEmpty() && !Character.isWhitespace(meal.getStrMeasure19().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure19());
}
if (!meal.getStrMeasure20().isEmpty() && !Character.isWhitespace(meal.getStrMeasure20().charAt(0))) {
measures.append("\n : " + meal.getStrMeasure20());
}
youtube.setOnClickListener(v -> {
Intent intentYoutube = new Intent(Intent.ACTION_VIEW);
intentYoutube.setData(Uri.parse(meal.getStrYoutube()));
startActivity(intentYoutube);
});
source.setOnClickListener(v -> {
Intent intentSource = new Intent(Intent.ACTION_VIEW);
intentSource.setData(Uri.parse(meal.getStrSource()));
startActivity(intentSource);
});
}
@Override
public void onErrorLoading(String message) {
Utils.showDialogMessage(this, "Error", message);
}
}
@trungtinh961
Copy link

trungtinh961 commented Oct 7, 2020

I have one bug when strIngredient** return null value:
"Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment