package com.marverenic.music.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.data.store.MusicStore;
import com.marverenic.music.instances.Library;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.section.LibraryEmptyState;
import com.marverenic.music.instances.section.SongSection;
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 java.util.List;

import javax.inject.Inject;

public class SongFragment extends Fragment implements Library.LibraryRefreshListener {

    @Inject MusicStore mMusicStore;

    private RecyclerView mRecyclerView;
    private HeterogeneousAdapter mAdapter;
    private SongSection mSongSection;
    private List<Song> mSongs;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JockeyApplication.getComponent(this).inject(this);
        mMusicStore.getSongs().subscribe(
                songs -> {
                    mSongs = songs;
                    setupAdapter();
                },
                Throwable::printStackTrace);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.list, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.list);
        setupAdapter();

        int paddingH = (int) getActivity().getResources().getDimension(R.dimen.global_padding);
        view.setPadding(paddingH, 0, paddingH, 0);

        return view;
    }

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

        if (mSongSection != null) {
            mSongSection.setData(mSongs);
            mAdapter.notifyDataSetChanged();
        } else {
            mAdapter = new HeterogeneousAdapter();
            mSongSection = new SongSection(mSongs);
            mAdapter.addSection(new SongSection(mSongs));
            mAdapter.setEmptyState(new LibraryEmptyState(getActivity()));

            mRecyclerView.addItemDecoration(
                    new BackgroundDecoration(Themes.getBackgroundElevated()));
            mRecyclerView.addItemDecoration(new DividerDecoration(getContext(), R.id.empty_layout));
            mRecyclerView.setAdapter(mAdapter);

            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(layoutManager);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Library.addRefreshListener(this);
        // Assume this fragment's data has gone stale since it was last in the foreground
        onLibraryRefreshed();
    }

    @Override
    public void onPause() {
        super.onPause();
        Library.removeRefreshListener(this);
    }

    @Override
    public void onLibraryRefreshed() {
        mAdapter.notifyDataSetChanged();
    }
}

@@ -472,55 +472,8 @@
 re;%0A
-import com.marverenic.music.instances.Library;%0A
 impo
@@ -958,50 +958,8 @@
 ent 
-implements Library.LibraryRefreshListener 
 %7B%0A%0A 
@@ -3037,465 +3037,7 @@
    %7D
-%0A%0A    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A        Library.addRefreshListener(this);%0A        // Assume this fragment's data has gone stale since it was last in the foreground%0A        onLibraryRefreshed();%0A    %7D%0A%0A    @Override%0A    public void onPause() %7B%0A        super.onPause();%0A        Library.removeRefreshListener(this);%0A    %7D%0A%0A    @Override%0A    public void onLibraryRefreshed() %7B%0A        mAdapter.notifyDataSetChanged();%0A    %7D
 %0A%7D%0A

Remove LibraryRefreshListener implementation from SongFragment