package com.andrey7mel.testrx.view.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
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 android.widget.Button;
import android.widget.EditText;

import com.andrey7mel.testrx.R;
import com.andrey7mel.testrx.presenter.BasePresenter;
import com.andrey7mel.testrx.presenter.RepoListPresenter;
import com.andrey7mel.testrx.presenter.vo.Repository;
import com.andrey7mel.testrx.view.ActivityCallback;
import com.andrey7mel.testrx.view.adapters.RepoListAdapter;

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

import butterknife.Bind;
import butterknife.ButterKnife;

public class RepoListFragment extends BaseFragment implements RepoListView {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    @Bind(R.id.edit_text)
    EditText editText;
    @Bind(R.id.button_search)
    Button searchButton;
    private RepoListPresenter presenter = new RepoListPresenter(this);
    private RepoListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repo_list, container, false);
        ButterKnife.bind(this, view);

        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(llm);
        adapter = new RepoListAdapter(new ArrayList<>(), presenter);
        recyclerView.setAdapter(adapter);

        searchButton.setOnClickListener(v -> presenter.onSearchButtonClick());

        presenter.onCreate(savedInstanceState);

        return view;
    }


    private void makeToast(String text) {
        Snackbar.make(recyclerView, text, Snackbar.LENGTH_LONG).show();
    }

    @Override
    protected BasePresenter getPresenter() {
        return presenter;
    }


    @Override
    public void showError(String error) {
        makeToast(error);
    }

    @Override
    public void showRepoList(List<Repository> repoList) {
        adapter.setRepoList(repoList);
    }

    @Override
    public void startRepoInfoFragment(Repository repository) {
        ((ActivityCallback) getActivity()).startRepoInfoFragment(repository);
    }

    @Override
    public void showEmptyList() {
        makeToast(getActivity().getString(R.string.empty_list));
    }

    @Override
    public String getUserName() {
        return editText.getText().toString();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        presenter.onSaveInstanceState(outState);
    }

}

@@ -40,16 +40,45 @@
 ments;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -91,24 +91,24 @@
 .os.Bundle;%0A
-
 import andro
@@ -1053,16 +1053,17 @@
 itText;%0A
+%0A
     @Bin
@@ -1109,16 +1109,17 @@
 Button;%0A
+%0A
     priv
@@ -1181,16 +1181,17 @@
 (this);%0A
+%0A
     priv
@@ -1220,16 +1220,548 @@
 apter;%0A%0A
+    private ActivityCallback activityCallback;%0A%0A    @Override%0A    public void onAttach(Activity activity) %7B%0A        super.onAttach(activity);%0A%0A        // This makes sure that the container activity has implemented%0A        // the callback interface. If not, it throws an exception%0A        try %7B%0A            activityCallback = (ActivityCallback) activity;%0A        %7D catch (ClassCastException e) %7B%0A            throw new ClassCastException(activity.toString()%0A                    + %22 must implement activityCallback%22);%0A        %7D%0A    %7D%0A%0A
     @Nul
@@ -2905,11 +2905,9 @@
     
-((A
+a
 ctiv
@@ -2921,24 +2921,8 @@
 back
-) getActivity())
 .sta

Fix activity callback