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

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

import com.marverenic.heterogeneousadapter.HeterogeneousAdapter;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.data.store.MusicStore;
import com.marverenic.music.data.store.PlaylistStore;
import com.marverenic.music.instances.Album;
import com.marverenic.music.instances.Artist;
import com.marverenic.music.instances.Genre;
import com.marverenic.music.instances.Playlist;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.section.AlbumSection;
import com.marverenic.music.instances.section.ArtistSection;
import com.marverenic.music.instances.section.BasicEmptyState;
import com.marverenic.music.instances.section.GenreSection;
import com.marverenic.music.instances.section.HeaderSection;
import com.marverenic.music.instances.section.PlaylistSection;
import com.marverenic.music.instances.section.SongSection;
import com.marverenic.music.player.PlayerController;
import com.marverenic.music.view.BackgroundDecoration;
import com.marverenic.music.view.DividerDecoration;
import com.marverenic.music.view.GridSpacingDecoration;
import com.marverenic.music.view.ViewUtils;

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

import javax.inject.Inject;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import timber.log.Timber;

public class SearchActivity extends BaseActivity implements SearchView.OnQueryTextListener {

    public static Intent newIntent(Context context) {
        return new Intent(context, SearchActivity.class);
    }

    @Inject MusicStore mMusicStore;
    @Inject PlaylistStore mPlaylistStore;

    private SearchView searchView;
    private BehaviorSubject<String> mQueryObservable;

    private RecyclerView mRecyclerView;
    private HeterogeneousAdapter mAdapter;

    private PlaylistSection mPlaylistSection;
    private SongSection mSongSection;
    private AlbumSection mAlbumSection;
    private ArtistSection mArtistSection;
    private GenreSection mGenreSection;

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

        JockeyApplication.getComponent(this).inject(this);
        mQueryObservable = BehaviorSubject.create("");

        // Set up the RecyclerView's adapter
        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        initAdapter();

        mQueryObservable
                .flatMap(query -> mPlaylistStore.searchForPlaylists(query))
                .compose(bindToLifecycle())
                .subscribe(playlists -> {
                    mPlaylistSection.setData(playlists);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for playlists");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForSongs(query))
                .compose(bindToLifecycle())
                .subscribe(songs -> {
                    mSongSection.setData(songs);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for songs");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForAlbums(query))
                .compose(bindToLifecycle())
                .subscribe(albums -> {
                    mAlbumSection.setData(albums);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for albums");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForArtists(query))
                .compose(bindToLifecycle())
                .subscribe(artists -> {
                    mArtistSection.setData(artists);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for artists");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForGenres(query))
                .compose(bindToLifecycle())
                .subscribe(genres -> {
                    mGenreSection.setData(genres);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for genres");
                });

        handleIntent(getIntent());
    }

    private void initAdapter() {
        mPlaylistSection = new PlaylistSection(Collections.emptyList());
        mSongSection = new SongSection(this, Collections.emptyList());
        mAlbumSection = new AlbumSection(this, Collections.emptyList());
        mArtistSection = new ArtistSection(this, Collections.emptyList());
        mGenreSection = new GenreSection(this, Collections.emptyList());

        mAdapter = new HeterogeneousAdapter()
                .addSection(new HeaderSection(getString(R.string.header_playlists)))
                .addSection(mPlaylistSection)
                .addSection(new HeaderSection(getString(R.string.header_songs)))
                .addSection(mSongSection)
                .addSection(new HeaderSection(getString(R.string.header_albums)))
                .addSection(mAlbumSection)
                .addSection(new HeaderSection(getString(R.string.header_artists)))
                .addSection(mArtistSection)
                .addSection(new HeaderSection(getString(R.string.header_genres)))
                .addSection(mGenreSection);<commit_msg>Persist previous search query<commit_after>package com.marverenic.music.activity;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

import com.marverenic.heterogeneousadapter.HeterogeneousAdapter;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.data.store.MusicStore;
import com.marverenic.music.data.store.PlaylistStore;
import com.marverenic.music.instances.Album;
import com.marverenic.music.instances.Artist;
import com.marverenic.music.instances.Genre;
import com.marverenic.music.instances.Playlist;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.section.AlbumSection;
import com.marverenic.music.instances.section.ArtistSection;
import com.marverenic.music.instances.section.BasicEmptyState;
import com.marverenic.music.instances.section.GenreSection;
import com.marverenic.music.instances.section.HeaderSection;
import com.marverenic.music.instances.section.PlaylistSection;
import com.marverenic.music.instances.section.SongSection;
import com.marverenic.music.player.PlayerController;
import com.marverenic.music.view.BackgroundDecoration;
import com.marverenic.music.view.DividerDecoration;
import com.marverenic.music.view.GridSpacingDecoration;
import com.marverenic.music.view.ViewUtils;

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

import javax.inject.Inject;

import rx.Observable;
import rx.subjects.BehaviorSubject;
import timber.log.Timber;

public class SearchActivity extends BaseActivity implements SearchView.OnQueryTextListener {

    private static final String KEY_SAVED_QUERY = "SearchActivity.LAST_QUERY";

    public static Intent newIntent(Context context) {
        return new Intent(context, SearchActivity.class);
    }

    @Inject MusicStore mMusicStore;
    @Inject PlaylistStore mPlaylistStore;

    private SearchView searchView;
    private BehaviorSubject<String> mQueryObservable;

    private RecyclerView mRecyclerView;
    private HeterogeneousAdapter mAdapter;

    private PlaylistSection mPlaylistSection;
    private SongSection mSongSection;
    private AlbumSection mAlbumSection;
    private ArtistSection mArtistSection;
    private GenreSection mGenreSection;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instance);
        JockeyApplication.getComponent(this).inject(this);

        String lastQuery;
        if (savedInstanceState != null) {
            lastQuery = savedInstanceState.getString(KEY_SAVED_QUERY);
        } else {
            lastQuery = "";
        }

        mQueryObservable = BehaviorSubject.create(lastQuery);

        // Set up the RecyclerView's adapter
        mRecyclerView = (RecyclerView) findViewById(R.id.list);
        initAdapter();

        mQueryObservable
                .flatMap(query -> mPlaylistStore.searchForPlaylists(query))
                .compose(bindToLifecycle())
                .subscribe(playlists -> {
                    mPlaylistSection.setData(playlists);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for playlists");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForSongs(query))
                .compose(bindToLifecycle())
                .subscribe(songs -> {
                    mSongSection.setData(songs);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for songs");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForAlbums(query))
                .compose(bindToLifecycle())
                .subscribe(albums -> {
                    mAlbumSection.setData(albums);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for albums");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForArtists(query))
                .compose(bindToLifecycle())
                .subscribe(artists -> {
                    mArtistSection.setData(artists);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for artists");
                });

        mQueryObservable
                .flatMap(query -> mMusicStore.searchForGenres(query))
                .compose(bindToLifecycle())
                .subscribe(genres -> {
                    mGenreSection.setData(genres);
                    mAdapter.notifyDataSetChanged();
                }, throwable -> {
                    Timber.e(throwable, "Failed to search for genres");
                });

        handleIntent(getIntent());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_SAVED_QUERY, mQueryObservable.getValue());
    }

    private void initAdapter() {
        mPlaylistSection = new PlaylistSection(Collections.emptyList());
        mSongSection = new SongSection(this, Collections.emptyList());
        mAlbumSection = new AlbumSection(this, Collections.emptyList());
        mArtistSection = new ArtistSection(this, Collections.emptyList());
        mGenreSection = new GenreSection(this, Collections.emptyList());

        mAdapter = new HeterogeneousAdapter()
                .addSection(new HeaderSection(getString(R.string.header_playlists)))
                .addSection(mPlaylistSection)
                .addSection(new HeaderSection(getString(R.string.header_songs)))
                .addSection(mSongSection)
                .addSection(new HeaderSection(getString(R.string.header_albums)))
                .addSection(mAlbumSection)
                .addSection(new HeaderSection(getString(R.string.header_artists)))
                .addSection(mArtistSection)
                .addSection(new HeaderSection(getString(R.string.header_genres)))
                .addSection(mGenreSection);
{'fraction_non_alphanumeric': 0.09246088193456614, 'fraction_numerical': 0.000474158368895211, 'mean_word_length': 2.695969626168224, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '3140503', 'n_tokens_mistral': 3554, 'n_tokens_neox': 3336, 'n_words': 547}