<commit_before>package com.marverenic.music.activity.instance;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.crashlytics.android.Crashlytics;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.activity.BaseActivity;
import com.marverenic.music.instances.Album;
import com.marverenic.music.instances.Artist;
import com.marverenic.music.instances.Library;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.section.AlbumSection;
import com.marverenic.music.instances.section.ArtistBioSingleton;
import com.marverenic.music.instances.section.HeaderSection;
import com.marverenic.music.instances.section.LibraryEmptyState;
import com.marverenic.music.instances.section.LoadingSingleton;
import com.marverenic.music.instances.section.RelatedArtistSection;
import com.marverenic.music.instances.section.SongSection;
import com.marverenic.music.lastfm2.data.store.LastFmStore;
import com.marverenic.music.lastfm2.model.LfmArtist;
import com.marverenic.music.utils.Themes;
import com.marverenic.music.view.BackgroundDecoration;
import com.marverenic.music.view.DividerDecoration;
import com.marverenic.music.view.EnhancedAdapters.HeterogeneousAdapter;
import com.marverenic.music.view.GridSpacingDecoration;
import com.marverenic.music.view.ViewUtils;
import com.trello.rxlifecycle.RxLifecycle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;

public class ArtistActivity extends BaseActivity {

    public static final String ARTIST_EXTRA = "artist";

    @Inject LastFmStore mLfmStore;

    private RecyclerView mRecyclerView;
    private HeterogeneousAdapter mAdapter;
    private LoadingSingleton mLoadingSection;
    private ArtistBioSingleton mBioSection;
    private RelatedArtistSection mRelatedArtistSection;
    private AlbumSection mAlbumSection;
    private SongSection mSongSection;

    private Artist mReference;
    private LfmArtist mLfmReference;
    private List<LfmArtist> mRelatedArtists;
    private List<Song> mSongs;
    private List<Album> mAlbums;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instance_artwork);

        JockeyApplication.getComponent(this).inject(this);
        mReference = getIntent().getParcelableExtra(ARTIST_EXTRA);

        CollapsingToolbarLayout collapsingToolbar =
                (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle(mReference.getArtistName());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        mAlbums = Library.getArtistAlbumEntries(mReference);
        mSongs = Library.getArtistSongEntries(mReference);

        mLfmStore.getArtistInfo(mReference.getArtistName())
                .compose(RxLifecycle.bindActivity(lifecycle()))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        this::setLastFmReference,
                        Crashlytics::logException);

        // Sort the album list chronologically if all albums have years,
        // otherwise sort alphabetically
        if (allEntriesHaveYears()) {
            Collections.sort(mAlbums, (a1, a2) -> a1.getYear() - a2.getYear());
        } else {
            Collections.sort(mAlbums);
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        setupAdapter();
    }

    private boolean allEntriesHaveYears() {
        for (int i = 0; i < mAlbums.size(); i++) {
            if (mAlbums.get(i).getYear() == 0) {
                return false;
            }
        }
        return true;
    }

    private void setLastFmReference(LfmArtist lfmArtist) {
        mLfmReference = lfmArtist;
        mRelatedArtists = new ArrayList<>();

        for (LfmArtist relatedArtist : lfmArtist.getSimilarArtists()) {
            if (Library.findArtistByName(relatedArtist.getName()) != null) {
                mRelatedArtists.add(relatedArtist);
            }
        }
        setupAdapter();
    }

    private void setupAdapter() {
        if (mRecyclerView == null) {
            return;
        }

        if (mAdapter == null) {
            setupRecyclerView();

            mAdapter = new HeterogeneousAdapter();
            mAdapter.setEmptyState(new LibraryEmptyState(this) {
                @Override
                public String getEmptyMessage() {
                    if (mReference == null) {
                        return getString(R.string.empty_error_artist);
                    } else {
                        return super.getEmptyMessage();
                    }<commit_msg>Load artist artwork from Last.fm<commit_after>package com.marverenic.music.activity.instance;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.crashlytics.android.Crashlytics;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.activity.BaseActivity;
import com.marverenic.music.instances.Album;
import com.marverenic.music.instances.Artist;
import com.marverenic.music.instances.Library;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.section.AlbumSection;
import com.marverenic.music.instances.section.ArtistBioSingleton;
import com.marverenic.music.instances.section.HeaderSection;
import com.marverenic.music.instances.section.LibraryEmptyState;
import com.marverenic.music.instances.section.LoadingSingleton;
import com.marverenic.music.instances.section.RelatedArtistSection;
import com.marverenic.music.instances.section.SongSection;
import com.marverenic.music.lastfm2.data.store.LastFmStore;
import com.marverenic.music.lastfm2.model.Image;
import com.marverenic.music.lastfm2.model.LfmArtist;
import com.marverenic.music.utils.Themes;
import com.marverenic.music.view.BackgroundDecoration;
import com.marverenic.music.view.DividerDecoration;
import com.marverenic.music.view.EnhancedAdapters.HeterogeneousAdapter;
import com.marverenic.music.view.GridSpacingDecoration;
import com.marverenic.music.view.ViewUtils;
import com.trello.rxlifecycle.RxLifecycle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;

public class ArtistActivity extends BaseActivity {

    public static final String ARTIST_EXTRA = "artist";

    @Inject LastFmStore mLfmStore;

    private RecyclerView mRecyclerView;
    private HeterogeneousAdapter mAdapter;
    private LoadingSingleton mLoadingSection;
    private ArtistBioSingleton mBioSection;
    private RelatedArtistSection mRelatedArtistSection;
    private AlbumSection mAlbumSection;
    private SongSection mSongSection;

    private Artist mReference;
    private LfmArtist mLfmReference;
    private List<LfmArtist> mRelatedArtists;
    private List<Song> mSongs;
    private List<Album> mAlbums;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instance_artwork);

        JockeyApplication.getComponent(this).inject(this);
        mReference = getIntent().getParcelableExtra(ARTIST_EXTRA);

        CollapsingToolbarLayout collapsingToolbar =
                (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle(mReference.getArtistName());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        mAlbums = Library.getArtistAlbumEntries(mReference);
        mSongs = Library.getArtistSongEntries(mReference);

        mLfmStore.getArtistInfo(mReference.getArtistName())
                .compose(RxLifecycle.bindActivity(lifecycle()))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        this::setLastFmReference,
                        Crashlytics::logException);

        // Sort the album list chronologically if all albums have years,
        // otherwise sort alphabetically
        if (allEntriesHaveYears()) {
            Collections.sort(mAlbums, (a1, a2) -> a1.getYear() - a2.getYear());
        } else {
            Collections.sort(mAlbums);
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        setupAdapter();
    }

    private boolean allEntriesHaveYears() {
        for (int i = 0; i < mAlbums.size(); i++) {
            if (mAlbums.get(i).getYear() == 0) {
                return false;
            }
        }
        return true;
    }

    private void setLastFmReference(LfmArtist lfmArtist) {
        mLfmReference = lfmArtist;
        mRelatedArtists = new ArrayList<>();

        for (LfmArtist relatedArtist : lfmArtist.getSimilarArtists()) {
            if (Library.findArtistByName(relatedArtist.getName()) != null) {
                mRelatedArtists.add(relatedArtist);
            }
        }
        setupAdapter();

        Image hero = mLfmReference.getImageBySize(Image.Size.MEGA);

        if (hero != null) {
            Glide.with(this)
                    .load(hero.getUrl())
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .centerCrop()
                    .animate(android.R.anim.fade_in)
                    .into((ImageView) findViewById(R.id.backdrop));
        }
    }

    private void setupAdapter() {
        if (mRecyclerView == null) {
            return;
        }

        if (mAdapter == null) {
            setupRecyclerView();

            mAdapter = new HeterogeneousAdapter();
            mAdapter.setEmptyState(new LibraryEmptyState(this) {
                @Override
                public String getEmptyMessage() {
                    if (mReference == null) {
                        return getString(R.string.empty_error_artist);
                    } else {
                        return super.getEmptyMessage();
                    }
{'fraction_non_alphanumeric': 0.09199241671932834, 'fraction_numerical': 0.0018958201679155006, 'mean_word_length': 3.013768115942029, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3143031', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3172, 'n_words': 515}