code refactor

This commit is contained in:
h4h13 2018-07-30 22:51:53 +05:30
parent e98fbb3b41
commit 4910b3fdc6

View file

@ -25,12 +25,6 @@ import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.Locale;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
@ -60,370 +54,372 @@ import code.name.monkey.retromusic.util.DensityUtil;
import code.name.monkey.retromusic.util.MusicUtil; import code.name.monkey.retromusic.util.MusicUtil;
import code.name.monkey.retromusic.util.PreferenceUtil; import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroUtil; import code.name.monkey.retromusic.util.RetroUtil;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.Locale;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implements public class ArtistDetailActivity extends AbsSlidingMusicPanelActivity implements
ArtistDetailContract.ArtistsDetailsView { ArtistDetailContract.ArtistsDetailsView {
public static final String EXTRA_ARTIST_ID = "extra_artist_id"; public static final String EXTRA_ARTIST_ID = "extra_artist_id";
private static final int REQUEST_CODE_SELECT_IMAGE = 9003; private static final int REQUEST_CODE_SELECT_IMAGE = 9003;
@BindView(R.id.image) @BindView(R.id.image)
ImageView image; ImageView image;
@BindView(R.id.biography) @BindView(R.id.biography)
TextView biographyTextView; TextView biographyTextView;
@BindView(R.id.recycler_view) @BindView(R.id.recycler_view)
RecyclerView recyclerView; RecyclerView recyclerView;
@BindView(R.id.album_recycler_view) @BindView(R.id.album_recycler_view)
RecyclerView albumRecyclerView; RecyclerView albumRecyclerView;
@BindView(R.id.album_title) @BindView(R.id.album_title)
AppCompatTextView albumTitle; AppCompatTextView albumTitle;
@BindView(R.id.song_title) @BindView(R.id.song_title)
AppCompatTextView songTitle; AppCompatTextView songTitle;
@BindView(R.id.biography_title) @BindView(R.id.biography_title)
AppCompatTextView biographyTitle; AppCompatTextView biographyTitle;
@BindView(R.id.title) @BindView(R.id.title)
TextView title; TextView title;
@BindView(R.id.text) @BindView(R.id.text)
TextView text; TextView text;
@BindView(R.id.action_shuffle_all) @BindView(R.id.action_shuffle_all)
FloatingActionButton shuffleButton; FloatingActionButton shuffleButton;
@BindView(R.id.collapsing_toolbar) @BindView(R.id.collapsing_toolbar)
@Nullable @Nullable
CollapsingToolbarLayout collapsingToolbarLayout; CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.app_bar) @BindView(R.id.app_bar)
@Nullable @Nullable
AppBarLayout appBarLayout; AppBarLayout appBarLayout;
@BindView(R.id.gradient_background) @BindView(R.id.gradient_background)
@Nullable @Nullable
View background; View background;
@BindView(R.id.image_container) @BindView(R.id.image_container)
@Nullable @Nullable
View imageContainer; View imageContainer;
@BindView(R.id.content) @BindView(R.id.content)
View contentContainer; View contentContainer;
@BindView(R.id.toolbar) @BindView(R.id.toolbar)
Toolbar toolbar; Toolbar toolbar;
@Nullable @Nullable
private Spanned biography; private Spanned biography;
private Artist artist; private Artist artist;
private LastFMRestClient lastFMRestClient; private LastFMRestClient lastFMRestClient;
private ArtistDetailsPresenter artistDetailsPresenter; private ArtistDetailsPresenter artistDetailsPresenter;
private SimpleSongAdapter songAdapter; private SimpleSongAdapter songAdapter;
private AlbumAdapter albumAdapter; private AlbumAdapter albumAdapter;
private boolean forceDownload; private boolean forceDownload;
@Override @Override
protected View createContentView() { protected View createContentView() {
return wrapSlidingMusicPanel(R.layout.activity_artist_details); return wrapSlidingMusicPanel(R.layout.activity_artist_details);
}
@Override
protected void onCreate(Bundle bundle) {
setDrawUnderStatusbar(true);
super.onCreate(bundle);
ButterKnife.bind(this);
supportPostponeEnterTransition();
setBottomBarVisibility(View.GONE);
setNavigationbarColorAuto();
setLightNavigationBar(true);
lastFMRestClient = new LastFMRestClient(this);
setUpViews();
int artistID = getIntent().getIntExtra(EXTRA_ARTIST_ID, -1);
artistDetailsPresenter = new ArtistDetailsPresenter(this, artistID);
}
private void setUpViews() {
setupRecyclerView();
setupToolbar();
setupContainerHeight();
}
private void setupContainerHeight() {
if (imageContainer != null) {
LayoutParams params = imageContainer.getLayoutParams();
params.width = DensityUtil.getScreenHeight(this) / 2;
imageContainer.setLayoutParams(params);
}
}
private void setupToolbar() {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setTitle(null);
if (toolbar != null && !PreferenceUtil.getInstance(this).getFullScreenMode()) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar
.getLayoutParams();
params.topMargin = RetroUtil.getStatusBarHeight(this);
toolbar.setLayoutParams(params);
} }
@Override int primaryColor = ThemeStore.primaryColor(this);
protected void onCreate(Bundle bundle) { TintHelper.setTintAuto(contentContainer, primaryColor, true);
setDrawUnderStatusbar(true);
super.onCreate(bundle);
ButterKnife.bind(this);
supportPostponeEnterTransition(); if (appBarLayout != null) {
setBottomBarVisibility(View.GONE); appBarLayout.setBackgroundColor(primaryColor);
setNavigationbarColorAuto(); appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
setLightNavigationBar(true); @Override
public void onStateChanged(AppBarLayout appBarLayout,
lastFMRestClient = new LastFMRestClient(this); AppBarStateChangeListener.State state) {
int color;
setUpViews(); switch (state) {
case COLLAPSED:
int artistID = getIntent().getIntExtra(EXTRA_ARTIST_ID, -1); setLightStatusbar(!ATHUtil.isWindowBackgroundDark(ArtistDetailActivity.this));
artistDetailsPresenter = new ArtistDetailsPresenter(this, artistID); color = ATHUtil.resolveColor(ArtistDetailActivity.this, R.attr.iconColor);
} break;
private void setUpViews() {
setupRecyclerView();
setupToolbar();
setupContainerHeight();
}
private void setupContainerHeight() {
if (imageContainer != null) {
LayoutParams params = imageContainer.getLayoutParams();
params.width = DensityUtil.getScreenHeight(this) / 2;
imageContainer.setLayoutParams(params);
}
}
private void setupToolbar() {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setTitle(null);
if (toolbar != null && !PreferenceUtil.getInstance(this).getFullScreenMode()) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
params.topMargin = RetroUtil.getStatusBarHeight(this);
toolbar.setLayoutParams(params);
}
int primaryColor = ThemeStore.primaryColor(this);
TintHelper.setTintAuto(contentContainer, primaryColor, true);
if (appBarLayout != null) {
appBarLayout.setBackgroundColor(primaryColor);
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state) {
int color;
switch (state) {
case COLLAPSED:
setLightStatusbar(!ATHUtil.isWindowBackgroundDark(ArtistDetailActivity.this));
color = ATHUtil.resolveColor(ArtistDetailActivity.this, R.attr.iconColor);
break;
default:
case EXPANDED:
case IDLE:
setLightStatusbar(false);
color = ContextCompat.getColor(ArtistDetailActivity.this, R.color.md_white_1000);
break;
}
ToolbarContentTintHelper.colorizeToolbar(toolbar, color, ArtistDetailActivity.this);
}
});
}
if (collapsingToolbarLayout != null) {
collapsingToolbarLayout.setContentScrimColor(primaryColor);
collapsingToolbarLayout.setStatusBarScrimColor(ColorUtil.darkenColor(primaryColor));
}
}
private void setupRecyclerView() {
albumAdapter = new HorizontalAlbumAdapter(this, new ArrayList<>(), false, null);
albumRecyclerView.setItemAnimator(new DefaultItemAnimator());
albumRecyclerView
.setLayoutManager(new GridLayoutManager(this, 1, GridLayoutManager.HORIZONTAL, false));
albumRecyclerView.setAdapter(albumAdapter);
songAdapter = new SimpleSongAdapter(this, new ArrayList<>(), R.layout.item_song);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(songAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_SELECT_IMAGE:
if (resultCode == RESULT_OK) {
CustomArtistImageUtil.getInstance(this).setCustomArtistImage(artist, data.getData());
}
break;
default: default:
if (resultCode == RESULT_OK) { case EXPANDED:
reload(); case IDLE:
} setLightStatusbar(false);
break; color = ContextCompat.getColor(ArtistDetailActivity.this, R.color.md_white_1000);
break;
}
ToolbarContentTintHelper.colorizeToolbar(toolbar, color, ArtistDetailActivity.this);
} }
});
} }
if (collapsingToolbarLayout != null) {
@Override collapsingToolbarLayout.setContentScrimColor(primaryColor);
protected void onResume() { collapsingToolbarLayout.setStatusBarScrimColor(ColorUtil.darkenColor(primaryColor));
super.onResume();
artistDetailsPresenter.subscribe();
} }
}
@Override private void setupRecyclerView() {
protected void onPause() { albumAdapter = new HorizontalAlbumAdapter(this, new ArrayList<>(), false, null);
super.onPause(); albumRecyclerView.setItemAnimator(new DefaultItemAnimator());
artistDetailsPresenter.unsubscribe(); albumRecyclerView
} .setLayoutManager(new GridLayoutManager(this, 1, GridLayoutManager.HORIZONTAL, false));
albumRecyclerView.setAdapter(albumAdapter);
@Override songAdapter = new SimpleSongAdapter(this, new ArrayList<>(), R.layout.item_song);
public void loading() { recyclerView.setItemAnimator(new DefaultItemAnimator());
} recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(songAdapter);
}
@Override
public void showEmptyView() {
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override super.onActivityResult(requestCode, resultCode, data);
public void completed() { switch (requestCode) {
supportStartPostponedEnterTransition(); case REQUEST_CODE_SELECT_IMAGE:
} if (resultCode == RESULT_OK) {
CustomArtistImageUtil.getInstance(this).setCustomArtistImage(artist, data.getData());
@Override
public void showData(Artist artist) {
setArtist(artist);
}
private Artist getArtist() {
if (artist == null) {
artist = new Artist();
} }
return artist; break;
} default:
if (resultCode == RESULT_OK) {
private void setArtist(Artist artist) { reload();
if (artist.getSongCount() <= 0) {
finish();
} }
this.artist = artist; break;
loadArtistImage();
if (RetroUtil.isAllowedToDownloadMetadata(this)) {
loadBiography();
}
title.setText(artist.getName());
text.setText(String.format("%s • %s", MusicUtil.getArtistInfoString(this, artist), MusicUtil
.getReadableDurationString(MusicUtil.getTotalDuration(this, artist.getSongs()))));
songAdapter.swapDataSet(artist.getSongs());
albumAdapter.swapDataSet(artist.albums);
} }
}
private void loadBiography() { @Override
loadBiography(Locale.getDefault().getLanguage()); protected void onResume() {
super.onResume();
artistDetailsPresenter.subscribe();
}
@Override
protected void onPause() {
super.onPause();
artistDetailsPresenter.unsubscribe();
}
@Override
public void loading() {
}
@Override
public void showEmptyView() {
}
@Override
public void completed() {
supportStartPostponedEnterTransition();
}
@Override
public void showData(Artist artist) {
setArtist(artist);
}
private Artist getArtist() {
if (artist == null) {
artist = new Artist();
} }
return artist;
}
private void loadBiography(@Nullable final String lang) { private void setArtist(Artist artist) {
biography = null; if (artist.getSongCount() <= 0) {
finish();
lastFMRestClient.getApiService()
.getArtistInfo(getArtist().getName(), lang, null)
.enqueue(new Callback<LastFmArtist>() {
@Override
public void onResponse(@NonNull Call<LastFmArtist> call,
@NonNull Response<LastFmArtist> response) {
final LastFmArtist lastFmArtist = response.body();
if (lastFmArtist != null && lastFmArtist.getArtist() != null) {
final String bioContent = lastFmArtist.getArtist().getBio().getContent();
if (bioContent != null && !bioContent.trim().isEmpty()) {
//TransitionManager.beginDelayedTransition(titleContainer);
biographyTextView.setVisibility(View.VISIBLE);
biographyTitle.setVisibility(View.VISIBLE);
biography = Html.fromHtml(bioContent);
biographyTextView.setText(biography);
}
}
// If the "lang" parameter is set and no biography is given, retry with default language
if (biography == null && lang != null) {
loadBiography(null);
return;
}
}
@Override
public void onFailure(@NonNull Call<LastFmArtist> call, @NonNull Throwable t) {
t.printStackTrace();
biography = null;
}
});
} }
this.artist = artist;
loadArtistImage();
@OnClick(R.id.biography) if (RetroUtil.isAllowedToDownloadMetadata(this)) {
void toggleArtistBiography() { loadBiography();
if (biographyTextView.getMaxLines() == 4) {
biographyTextView.setMaxLines(Integer.MAX_VALUE);
} else {
biographyTextView.setMaxLines(4);
}
} }
title.setText(artist.getName());
text.setText(String.format("%s • %s", MusicUtil.getArtistInfoString(this, artist), MusicUtil
.getReadableDurationString(MusicUtil.getTotalDuration(this, artist.getSongs()))));
private void loadArtistImage() { songAdapter.swapDataSet(artist.getSongs());
ArtistGlideRequest.Builder.from(Glide.with(this), artist) albumAdapter.swapDataSet(artist.albums);
.forceDownload(forceDownload) }
.generatePalette(this).build()
.dontAnimate() private void loadBiography() {
.into(new RetroMusicColoredTarget(image) { loadBiography(Locale.getDefault().getLanguage());
@Override }
public void onColorReady(int color) {
setColors(color); private void loadBiography(@Nullable final String lang) {
} biography = null;
});
forceDownload = false; lastFMRestClient.getApiService()
.getArtistInfo(getArtist().getName(), lang, null)
.enqueue(new Callback<LastFmArtist>() {
@Override
public void onResponse(@NonNull Call<LastFmArtist> call,
@NonNull Response<LastFmArtist> response) {
final LastFmArtist lastFmArtist = response.body();
if (lastFmArtist != null && lastFmArtist.getArtist() != null) {
final String bioContent = lastFmArtist.getArtist().getBio().getContent();
if (bioContent != null && !bioContent.trim().isEmpty()) {
//TransitionManager.beginDelayedTransition(titleContainer);
biographyTextView.setVisibility(View.VISIBLE);
biographyTitle.setVisibility(View.VISIBLE);
biography = Html.fromHtml(bioContent);
biographyTextView.setText(biography);
}
}
// If the "lang" parameter is set and no biography is given, retry with default language
if (biography == null && lang != null) {
loadBiography(null);
}
}
@Override
public void onFailure(@NonNull Call<LastFmArtist> call, @NonNull Throwable t) {
t.printStackTrace();
biography = null;
}
});
}
@OnClick(R.id.biography)
void toggleArtistBiography() {
if (biographyTextView.getMaxLines() == 4) {
biographyTextView.setMaxLines(Integer.MAX_VALUE);
} else {
biographyTextView.setMaxLines(4);
} }
}
private void setColors(int color) { private void loadArtistImage() {
ArtistGlideRequest.Builder.from(Glide.with(this), artist)
.forceDownload(forceDownload)
.generatePalette(this).build()
.dontAnimate()
.into(new RetroMusicColoredTarget(image) {
@Override
public void onColorReady(int color) {
setColors(color);
}
});
forceDownload = false;
}
int textColor = private void setColors(int color) {
PreferenceUtil.getInstance(this).getAdaptiveColor() ? color : ThemeStore.accentColor(this);
albumTitle.setTextColor(textColor); int textColor =
songTitle.setTextColor(textColor); PreferenceUtil.getInstance(this).getAdaptiveColor() ? color : ThemeStore.accentColor(this);
biographyTitle.setTextColor(textColor);
TintHelper.setTintAuto(shuffleButton, textColor, true); albumTitle.setTextColor(textColor);
songTitle.setTextColor(textColor);
biographyTitle.setTextColor(textColor);
if (background != null) { TintHelper.setTintAuto(shuffleButton, textColor, true);
background.setBackgroundTintList(ColorStateList.valueOf(color));
} if (background != null) {
findViewById(R.id.root).setBackgroundColor(ThemeStore.primaryColor(this)); background.setBackgroundTintList(ColorStateList.valueOf(color));
} }
findViewById(R.id.root).setBackgroundColor(ThemeStore.primaryColor(this));
}
@OnClick({R.id.action_shuffle_all}) @OnClick({R.id.action_shuffle_all})
public void onViewClicked(View view) { public void onViewClicked(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.action_shuffle_all: case R.id.action_shuffle_all:
MusicPlayerRemote.openAndShuffleQueue(getArtist().getSongs(), true); MusicPlayerRemote.openAndShuffleQueue(getArtist().getSongs(), true);
break; break;
}
} }
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
return handleSortOrderMenuItem(item); return handleSortOrderMenuItem(item);
} }
private boolean handleSortOrderMenuItem(@NonNull MenuItem item) { private boolean handleSortOrderMenuItem(@NonNull MenuItem item) {
final ArrayList<Song> songs = getArtist().getSongs(); final ArrayList<Song> songs = getArtist().getSongs();
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
super.onBackPressed(); super.onBackPressed();
return true; return true;
case R.id.action_play_next: case R.id.action_play_next:
MusicPlayerRemote.playNext(songs); MusicPlayerRemote.playNext(songs);
return true; return true;
case R.id.action_add_to_current_playing: case R.id.action_add_to_current_playing:
MusicPlayerRemote.enqueue(songs); MusicPlayerRemote.enqueue(songs);
return true; return true;
case R.id.action_add_to_playlist: case R.id.action_add_to_playlist:
AddToPlaylistDialog.create(songs).show(getSupportFragmentManager(), "ADD_PLAYLIST"); AddToPlaylistDialog.create(songs).show(getSupportFragmentManager(), "ADD_PLAYLIST");
return true; return true;
case R.id.action_set_artist_image: case R.id.action_set_artist_image:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*"); intent.setType("image/*");
startActivityForResult( startActivityForResult(
Intent.createChooser(intent, getString(R.string.pick_from_local_storage)), Intent.createChooser(intent, getString(R.string.pick_from_local_storage)),
REQUEST_CODE_SELECT_IMAGE); REQUEST_CODE_SELECT_IMAGE);
return true; return true;
case R.id.action_reset_artist_image: case R.id.action_reset_artist_image:
Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.updating), Toast.makeText(ArtistDetailActivity.this, getResources().getString(R.string.updating),
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
CustomArtistImageUtil.getInstance(ArtistDetailActivity.this).resetCustomArtistImage(artist); CustomArtistImageUtil.getInstance(ArtistDetailActivity.this).resetCustomArtistImage(artist);
forceDownload = true; forceDownload = true;
return true;
}
return true; return true;
} }
return true;
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_artist_detail, menu); getMenuInflater().inflate(R.menu.menu_artist_detail, menu);
return super.onCreateOptionsMenu(menu); return super.onCreateOptionsMenu(menu);
} }
@Override @Override
public void onMediaStoreChanged() { public void onMediaStoreChanged() {
super.onMediaStoreChanged(); super.onMediaStoreChanged();
reload(); reload();
} }
private void reload() { private void reload() {
artistDetailsPresenter.unsubscribe(); artistDetailsPresenter.unsubscribe();
artistDetailsPresenter.subscribe(); artistDetailsPresenter.subscribe();
} }
} }