<commit_before>package com.leavjenn.hews.ui;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.leavjenn.hews.Constants;
import com.leavjenn.hews.R;
import com.leavjenn.hews.Utils;
import com.leavjenn.hews.misc.SharedPrefsManager;
import com.leavjenn.hews.model.Comment;
import com.leavjenn.hews.model.HNItem;
import com.leavjenn.hews.model.Post;

import org.parceler.Parcels;

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

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

//public class SearchFragment extends Fragment implements PostAdapter.OnReachBottomListener,
//    SharedPreferences.OnSharedPreferenceChangeListener {
public class SearchFragment extends BasePostListFragment {
    private static final String KEY_KEYWORD = "key_keyword";
    private static final String KEY_TIME_RANGE = "key_time_range";
    private static final String KEY_SORT_METHOD = "key_sort_method";
    private static final String KEY_POST_ID_LIST = "key_post_id_list";
    private static final String KEY_LOADED_POSTS = "key_loaded_posts";
    private static final String KEY_LOADED_TIME = "key_loaded_time";
    private static final String KEY_LOADING_STATE = "key_loading_state";
    private static final String KEY_LAST_TIME_POSITION = "key_last_time_position";
    private static final String KEY_SEARCH_RESULT_TOTAL_PAGE = "key_search_result_total_page";

    private RelativeLayout layoutRoot;
//    private SwipeRefreshLayout swipeRefreshLayout;
//    private RecyclerView rvPostList;
    private Snackbar snackbarNoConnection;

    private String mKeyword;
    private String mDateRange;
    private boolean mIsSortByDate;
    private int mLoadedTime;
    private int mLoadingState = Constants.LOADING_IDLE;
    private int mLastTimeListPosition;
    private int mSearchResultTotalPages;
    private List<Long> mPostIdList;
    private Boolean mShowPostSummary;
//    private PostAdapter.OnItemClickListener mOnItemClickListener;
//    private OnRecyclerViewCreateListener mOnRecyclerViewCreateListener;
//    private LinearLayoutManager mLinearLayoutManager;
//    private PostAdapter mPostAdapter;
//    private SharedPreferences prefs;
//    private DataManager mDataManager;
//    private CompositeSubscription mCompositeSubscription;

    /**
     * @param keyword   Parameter 1.
     * @param dateRange Parameter 2.
     * @return A new instance of fragment SearchFragment.
     */
    public static SearchFragment newInstance(String keyword, String dateRange, boolean isSortByDate) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putString(KEY_KEYWORD, keyword);
        args.putString(KEY_TIME_RANGE, dateRange);
        args.putBoolean(KEY_SORT_METHOD, isSortByDate);
        fragment.setArguments(args);
        return fragment;
    }

    public SearchFragment() {
    }

//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        try {
//            mOnItemClickListener = (PostAdapter.OnItemClickListener) activity;
//            mOnRecyclerViewCreateListener = (OnRecyclerViewCreateListener) activity;
//        } catch (ClassCastException e) {
//            throw new ClassCastException(activity.toString()
//                + " must implement (PostAdapter.OnItemClickListener)");
//        }
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            mKeyword = getArguments().getString(KEY_KEYWORD);
//            mDateRange = getArguments().getString(KEY_TIME_RANGE);
//            mIsSortByDate = getArguments().getBoolean(KEY_SORT_METHOD);
//        }

//        prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
//        prefs.registerOnSharedPreferenceChangeListener(this);
//        mDataManager = new DataManager();
//        mCompositeSubscription = new CompositeSubscription();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_search, container, false);
        layoutRoot = (RelativeLayout) rootView.findViewById(R.id.layout_search_root);
        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_layout);
        rvPostList = (RecyclerView) rootView.findViewById(R.id.list_search);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mLinearLayoutManager = new LinearLayoutManager(getActivity());
//        rvPostList.setLayoutManager(mLinearLayoutManager);
//        mOnRecyclerViewCreateListener.onRecyclerViewCreate(rvPostList);
//        mPostAdapter = new PostAdapter(this.getActivity(), this, mOnItemClickListener);
//        rvPostList.setAdapter(mPostAdapter);
//        swipeRefreshLayout.setColorSchemeResources(R.color.orange_600,
//            R.color.orange_900, R.color.orange_900, R.color.orange_600);
//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                refresh();
//            }
//        });
        mShowPostSummary = SharedPrefsManager.getShowPostSummary(prefs, getActivity());
        if (savedInstanceState != null) {
            mKeyword = savedInstanceState.getString(KEY_KEYWORD);
            mDateRange = savedInstanceState.getString(KEY_TIME_RANGE);
            mIsSortByDate = savedInstanceState.getBoolean(KEY_SORT_METHOD);
            mPostIdList = Parcels.unwrap(savedInstanceState.getParcelable(KEY_POST_ID_LIST));
            if (mPostAdapter.getItemCount() == 0) {
                mPostAdapter.addFooter(new HNItem.Footer());
                mPostAdapter.addAllPosts((ArrayList<Post>) Parcels.unwrap(savedInstanceState.getParcelable(KEY_LOADED_POSTS)));
            }
            mSearchResultTotalPages = savedInstanceState.getInt(KEY_SEARCH_RESULT_TOTAL_PAGE);
            mLastTimeListPosition = savedInstanceState.getInt(KEY_LAST_TIME_POSITION, 0);
            rvPostList.getLayoutManager().scrollToPosition(mLastTimeListPosition);
            mLoadedTime = savedInstanceState.getInt(KEY_LOADED_TIME);
            mLoadingState = savedInstanceState.getInt(KEY_LOADING_STATE);
            if (mLoadingState == Constants.LOADING_IN_PROGRESS) {
                loadMore();
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_KEYWORD, mKeyword);
        outState.putString(KEY_TIME_RANGE, mDateRange);
        outState.putBoolean(KEY_SORT_METHOD, mIsSortByDate);
        outState.putParcelable(KEY_LOADED_POSTS, Parcels.wrap(mPostAdapter.getPostList()));
        outState.putParcelable(KEY_POST_ID_LIST, Parcels.wrap(mPostIdList));
        outState.putInt(KEY_SEARCH_RESULT_TOTAL_PAGE, mSearchResultTotalPages);
        mLastTimeListPosition = ((LinearLayoutManager) rvPostList.getLayoutManager()).
            findFirstVisibleItemPosition();
        outState.putInt(KEY_LAST_TIME_POSITION, mLastTimeListPosition);
        outState.putInt(KEY_LOADED_TIME, mLoadedTime);
        outState.putInt(KEY_LOADING_STATE, mLoadingState);
    }

//    @Override
//    public void onDestroy() {<commit_msg>fix (getLayoutManager() NPE): do not save and restore list position.<commit_after>package com.leavjenn.hews.ui;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.leavjenn.hews.Constants;
import com.leavjenn.hews.R;
import com.leavjenn.hews.Utils;
import com.leavjenn.hews.misc.SharedPrefsManager;
import com.leavjenn.hews.model.Comment;
import com.leavjenn.hews.model.HNItem;
import com.leavjenn.hews.model.Post;

import org.parceler.Parcels;

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

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

//public class SearchFragment extends Fragment implements PostAdapter.OnReachBottomListener,
//    SharedPreferences.OnSharedPreferenceChangeListener {
public class SearchFragment extends BasePostListFragment {
    private static final String KEY_KEYWORD = "key_keyword";
    private static final String KEY_TIME_RANGE = "key_time_range";
    private static final String KEY_SORT_METHOD = "key_sort_method";
    private static final String KEY_POST_ID_LIST = "key_post_id_list";
    private static final String KEY_LOADED_POSTS = "key_loaded_posts";
    private static final String KEY_LOADED_TIME = "key_loaded_time";
    private static final String KEY_LOADING_STATE = "key_loading_state";
    private static final String KEY_LAST_TIME_POSITION = "key_last_time_position";
    private static final String KEY_SEARCH_RESULT_TOTAL_PAGE = "key_search_result_total_page";

    private RelativeLayout layoutRoot;
//    private SwipeRefreshLayout swipeRefreshLayout;
//    private RecyclerView rvPostList;
    private Snackbar snackbarNoConnection;

    private String mKeyword;
    private String mDateRange;
    private boolean mIsSortByDate;
    private int mLoadedTime;
    private int mLoadingState = Constants.LOADING_IDLE;
//    private int mLastTimeListPosition;
    private int mSearchResultTotalPages;
    private List<Long> mPostIdList;
    private Boolean mShowPostSummary;
//    private PostAdapter.OnItemClickListener mOnItemClickListener;
//    private OnRecyclerViewCreateListener mOnRecyclerViewCreateListener;
//    private LinearLayoutManager mLinearLayoutManager;
//    private PostAdapter mPostAdapter;
//    private SharedPreferences prefs;
//    private DataManager mDataManager;
//    private CompositeSubscription mCompositeSubscription;

    /**
     * @param keyword   Parameter 1.
     * @param dateRange Parameter 2.
     * @return A new instance of fragment SearchFragment.
     */
    public static SearchFragment newInstance(String keyword, String dateRange, boolean isSortByDate) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putString(KEY_KEYWORD, keyword);
        args.putString(KEY_TIME_RANGE, dateRange);
        args.putBoolean(KEY_SORT_METHOD, isSortByDate);
        fragment.setArguments(args);
        return fragment;
    }

    public SearchFragment() {
    }

//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        try {
//            mOnItemClickListener = (PostAdapter.OnItemClickListener) activity;
//            mOnRecyclerViewCreateListener = (OnRecyclerViewCreateListener) activity;
//        } catch (ClassCastException e) {
//            throw new ClassCastException(activity.toString()
//                + " must implement (PostAdapter.OnItemClickListener)");
//        }
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            mKeyword = getArguments().getString(KEY_KEYWORD);
//            mDateRange = getArguments().getString(KEY_TIME_RANGE);
//            mIsSortByDate = getArguments().getBoolean(KEY_SORT_METHOD);
//        }

//        prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
//        prefs.registerOnSharedPreferenceChangeListener(this);
//        mDataManager = new DataManager();
//        mCompositeSubscription = new CompositeSubscription();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_search, container, false);
        layoutRoot = (RelativeLayout) rootView.findViewById(R.id.layout_search_root);
        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_layout);
        rvPostList = (RecyclerView) rootView.findViewById(R.id.list_search);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mLinearLayoutManager = new LinearLayoutManager(getActivity());
//        rvPostList.setLayoutManager(mLinearLayoutManager);
//        mOnRecyclerViewCreateListener.onRecyclerViewCreate(rvPostList);
//        mPostAdapter = new PostAdapter(this.getActivity(), this, mOnItemClickListener);
//        rvPostList.setAdapter(mPostAdapter);
//        swipeRefreshLayout.setColorSchemeResources(R.color.orange_600,
//            R.color.orange_900, R.color.orange_900, R.color.orange_600);
//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                refresh();
//            }
//        });
        mShowPostSummary = SharedPrefsManager.getShowPostSummary(prefs, getActivity());
        if (savedInstanceState != null) {
            mKeyword = savedInstanceState.getString(KEY_KEYWORD);
            mDateRange = savedInstanceState.getString(KEY_TIME_RANGE);
            mIsSortByDate = savedInstanceState.getBoolean(KEY_SORT_METHOD);
            mPostIdList = Parcels.unwrap(savedInstanceState.getParcelable(KEY_POST_ID_LIST));
            if (mPostAdapter.getItemCount() == 0) {
                mPostAdapter.addFooter(new HNItem.Footer());
                mPostAdapter.addAllPosts((ArrayList<Post>) Parcels.unwrap(savedInstanceState.getParcelable(KEY_LOADED_POSTS)));
            }
            mSearchResultTotalPages = savedInstanceState.getInt(KEY_SEARCH_RESULT_TOTAL_PAGE);
//            mLastTimeListPosition = savedInstanceState.getInt(KEY_LAST_TIME_POSITION, 0);
//            rvPostList.getLayoutManager().scrollToPosition(mLastTimeListPosition);
            mLoadedTime = savedInstanceState.getInt(KEY_LOADED_TIME);
            mLoadingState = savedInstanceState.getInt(KEY_LOADING_STATE);
            if (mLoadingState == Constants.LOADING_IN_PROGRESS) {
                loadMore();
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_KEYWORD, mKeyword);
        outState.putString(KEY_TIME_RANGE, mDateRange);
        outState.putBoolean(KEY_SORT_METHOD, mIsSortByDate);
        outState.putParcelable(KEY_LOADED_POSTS, Parcels.wrap(mPostAdapter.getPostList()));
        outState.putParcelable(KEY_POST_ID_LIST, Parcels.wrap(mPostIdList));
        outState.putInt(KEY_SEARCH_RESULT_TOTAL_PAGE, mSearchResultTotalPages);
//        mLastTimeListPosition = ((LinearLayoutManager) rvPostList.getLayoutManager()).
//            findFirstVisibleItemPosition();
//        outState.putInt(KEY_LAST_TIME_POSITION, mLastTimeListPosition);
        outState.putInt(KEY_LOADED_TIME, mLoadedTime);
        outState.putInt(KEY_LOADING_STATE, mLoadingState);
    }

//    @Override
//    public void onDestroy() {
{'fraction_non_alphanumeric': 0.0891989142099615, 'fraction_numerical': 0.0024619657849883213, 'mean_word_length': 3.5588489208633094, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4820992', 'n_tokens_mistral': 4531, 'n_tokens_neox': 4387, 'n_words': 785}