package com.setiawanpaiman.sunnyreader.ui.fragment;

import android.content.Intent;
import android.net.Uri;

import com.setiawanpaiman.sunnyreader.data.model.Story;
import com.setiawanpaiman.sunnyreader.ui.adapter.EndlessListAdapter;
import com.setiawanpaiman.sunnyreader.ui.adapter.StoryAdapter;
import com.setiawanpaiman.sunnyreader.ui.presenter.EndlessListContract;
import com.setiawanpaiman.sunnyreader.ui.presenter.TopStoriesPresenter;

import rx.schedulers.Schedulers;

public class TopStoriesFragment extends EndlessListFragment<Story>
        implements StoryAdapter.OnClickListener {

    public static TopStoriesFragment newInstance() {
        return new TopStoriesFragment();
    }

    public TopStoriesFragment() {
    }

    @Override
    public EndlessListContract.Presenter<Story> createPresenter() {
        return new TopStoriesPresenter(this, Schedulers.io(),
                getApplicationComponent().provideHackerNewsService());
    }

    @Override
    public EndlessListAdapter<Story, ?> createAdapter() {
        StoryAdapter adapter = new StoryAdapter(getContext());
        adapter.setOnClickListener(this);
        return adapter;
    }

    @Override
    public void onStoryClicked(Story story) {
        // TODO: Open detail activity after it has been implemented
    }

    @Override
    public void onOpenInBrowserClicked(Story story) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(story.getUrl()));
        startActivity(intent);
    }
}

@@ -73,40 +73,183 @@
 ent.
-Intent;%0Aimport android.net.Uri;%0A
+Context;%0Aimport android.content.Intent;%0Aimport android.net.Uri;%0Aimport android.os.Bundle;%0Aimport android.support.annotation.Nullable;%0A%0Aimport com.setiawanpaiman.sunnyreader.R;
 %0Aimp
@@ -730,16 +730,75 @@
 ener %7B%0A%0A
+    private OnInteractionListener mOnInteractionListener;%0A%0A
     publ
@@ -923,32 +923,450 @@
 ment() %7B%0A    %7D%0A%0A
+    @Override%0A    public void onAttach(Context context) %7B%0A        super.onAttach(context);%0A        if (context instanceof OnInteractionListener) %7B%0A            mOnInteractionListener = (OnInteractionListener) context;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onActivityCreated(@Nullable Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState);%0A        setTitle(R.string.app_name);%0A    %7D%0A%0A
     @Override%0A  
@@ -1851,67 +1851,113 @@
     
-// TODO: Open detail activity after it has been implemented
+if (mOnInteractionListener != null) %7B%0A            mOnInteractionListener.onStoryClicked(story);%0A        %7D
 %0A   
@@ -2171,11 +2171,106 @@
 );%0A    %7D
+%0A%0A    public interface OnInteractionListener %7B%0A%0A        void onStoryClicked(Story story);%0A    %7D
 %0A%7D%0A

Add OnInteractionListener to TopStoriesFragment for handling story click