<commit_before>/*
 * This program is part of the OpenLMIS logistics management information
 * system platform software.
 *
 * Copyright © 2015 ThoughtWorks, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version. This program is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details. You should
 * have received a copy of the GNU Affero General Public License along with
 * this program. If not, see http://www.gnu.org/licenses. For additional
 * information contact <EMAIL>
 */

package org.openlmis.core.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.inject.Inject;

import org.openlmis.core.R;
import org.openlmis.core.presenter.StockMovementHistoryPresenter;
import org.openlmis.core.utils.Constants;
import org.openlmis.core.utils.ToastUtil;
import org.openlmis.core.view.LoadingView;
import org.openlmis.core.view.adapter.StockMovementHistoryAdapter;

public class StockMovementHistoryFragment extends BaseFragment implements StockMovementHistoryPresenter.StockMovementHistoryView, OnRefreshListener {

    @Inject
    StockMovementHistoryPresenter presenter;

    private long startIndex = 0;
    private boolean isLoading;
    private boolean isFirstLoading;
    private boolean isRotated;
    private long stockCardID;

    private SwipeRefreshLayout swipeRefreshLayout;
    private LoadingView loadingView;
    private ListView historyListView;

    private BaseAdapter adapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        stockCardID = activity.getIntent().getLongExtra(Constants.PARAM_STOCK_CARD_ID, 0);
        if (activity instanceof LoadingView) {
            loadingView = (LoadingView) activity;
        } else {
            throw new ClassCastException("Host Activity should implements LoadingView method");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        presenter.attachView(this);
        presenter.setStockCardId(stockCardID);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_stock_movement_history, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initUI(view);
        if (isRotated) {
            addFooterViewIfMoreThanOneScreen();
        } else {
            initData();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        isRotated = true;
        super.onSaveInstanceState(outState);
    }

    private void initUI(View view) {
        historyListView = (ListView) view.findViewById(R.id.list);

        adapter = new StockMovementHistoryAdapter(getActivity(), presenter.getStockMovementModelList());
        historyListView.setAdapter(adapter);

        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.stock_movement_history_swipe_container);
        swipeRefreshLayout.setOnRefreshListener(this);
    }


    public void initData() {
        isFirstLoading = true;
        loadingView.loading();
        swipeRefreshLayout.post(new Runnable() {<commit_msg>MG - #000 - Initialize View by InjectView<commit_after>/*
 * This program is part of the OpenLMIS logistics management information
 * system platform software.
 *
 * Copyright © 2015 ThoughtWorks, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version. This program is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details. You should
 * have received a copy of the GNU Affero General Public License along with
 * this program. If not, see http://www.gnu.org/licenses. For additional
 * information contact <EMAIL>
 */

package org.openlmis.core.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.inject.Inject;

import org.openlmis.core.R;
import org.openlmis.core.presenter.StockMovementHistoryPresenter;
import org.openlmis.core.utils.Constants;
import org.openlmis.core.utils.ToastUtil;
import org.openlmis.core.view.LoadingView;
import org.openlmis.core.view.adapter.StockMovementHistoryAdapter;

import roboguice.inject.InjectView;

public class StockMovementHistoryFragment extends BaseFragment implements StockMovementHistoryPresenter.StockMovementHistoryView, OnRefreshListener {

    @Inject
    StockMovementHistoryPresenter presenter;

    @InjectView(R.id.stock_movement_history_swipe_container)
    SwipeRefreshLayout swipeRefreshLayout;

    @InjectView(R.id.list)
    ListView historyListView;

    private long stockCardID;
    private long startIndex = 0;
    private boolean isRotated;
    private boolean isLoading;
    private boolean isFirstLoading;

    private LoadingView loadingView;
    private BaseAdapter adapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        stockCardID = activity.getIntent().getLongExtra(Constants.PARAM_STOCK_CARD_ID, 0);
        if (activity instanceof LoadingView) {
            loadingView = (LoadingView) activity;
        } else {
            throw new ClassCastException("Host Activity should implements LoadingView method");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        presenter.attachView(this);
        presenter.setStockCardId(stockCardID);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_stock_movement_history, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initUI();
        if (isRotated) {
            addFooterViewIfMoreThanOneScreen();
        } else {
            initData();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        isRotated = true;
        super.onSaveInstanceState(outState);
    }

    private void initUI() {
        adapter = new StockMovementHistoryAdapter(getActivity(), presenter.getStockMovementModelList());
        historyListView.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(this);
    }


    public void initData() {
        isFirstLoading = true;
        loadingView.loading();
        swipeRefreshLayout.post(new Runnable() {
{'fraction_non_alphanumeric': 0.07396264259746772, 'fraction_numerical': 0.0026325686348251222, 'mean_word_length': 3.8292978208232444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2019411', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2063, 'n_words': 590}