<commit_before>/**
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tlongdev.bktf.presenter.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.tlongdev.bktf.BptfApplication;
import com.tlongdev.bktf.R;
import com.tlongdev.bktf.interactor.LoadAllPricesInteractor;
import com.tlongdev.bktf.interactor.LoadCurrencyPricesInteractor;
import com.tlongdev.bktf.interactor.TlongdevItemSchemaInteractor;
import com.tlongdev.bktf.interactor.TlongdevPriceListInteractor;
import com.tlongdev.bktf.model.Price;
import com.tlongdev.bktf.presenter.Presenter;
import com.tlongdev.bktf.ui.view.fragment.RecentsView;
import com.tlongdev.bktf.util.Utility;

import javax.inject.Inject;

/**
 * @author Long
 * @since 2016. 03. 10.
 */
public class RecentsPresenter implements Presenter<RecentsView>, LoadAllPricesInteractor.Callback,
        TlongdevPriceListInteractor.Callback, TlongdevItemSchemaInteractor.Callback, LoadCurrencyPricesInteractor.Callback {

    @Inject SharedPreferences mPrefs;
    @Inject SharedPreferences.Editor mEditor;
    @Inject Tracker mTracker;
    @Inject Context mContext;

    private RecentsView mView;
    private final BptfApplication mApplication;

    private boolean mLoading = false;

    public RecentsPresenter(BptfApplication application) {
        mApplication = application;
        application.getPresenterComponent().inject(this);
    }

    @Override
    public void attachView(RecentsView view) {
        mView = view;

        if (mView != null && mLoading) {
            mView.showRefreshAnimation();
        }
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public void onLoadPricesFinished(Cursor prices) {
        if (mView != null) {
            mView.showPrices(prices);
        }
    }

    public void loadPrices() {
        //Download whole database when the app is first opened.
        if (mPrefs.getBoolean(mContext.getString(R.string.pref_initial_load_v2), true)) {
            if (Utility.isNetworkAvailable(mContext)) {
                callTlongdevPrices(false, true);
                //Show the progress dialog
                mView.showLoadingDialog("Downloading prices...");
            } else {
                mView.showErrorDialog();
            }
        } else {
            LoadAllPricesInteractor interactor = new LoadAllPricesInteractor(mApplication, this);
            interactor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            loadCurrencyPrices();
            //Update database if the last update happened more than an hour ago
            if (System.currentTimeMillis() - mPrefs.getLong(mContext.getString(R.string.pref_last_price_list_update), 0) >= 3600000L
                    && Utility.isNetworkAvailable(mContext)) {
                callTlongdevPrices(true, false);
                mView.showRefreshAnimation();<commit_msg>view null checks in recents presenter<commit_after>/**
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tlongdev.bktf.presenter.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.widget.Toast;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.tlongdev.bktf.BptfApplication;
import com.tlongdev.bktf.R;
import com.tlongdev.bktf.interactor.LoadAllPricesInteractor;
import com.tlongdev.bktf.interactor.LoadCurrencyPricesInteractor;
import com.tlongdev.bktf.interactor.TlongdevItemSchemaInteractor;
import com.tlongdev.bktf.interactor.TlongdevPriceListInteractor;
import com.tlongdev.bktf.model.Price;
import com.tlongdev.bktf.presenter.Presenter;
import com.tlongdev.bktf.ui.view.fragment.RecentsView;
import com.tlongdev.bktf.util.Utility;

import javax.inject.Inject;

/**
 * @author Long
 * @since 2016. 03. 10.
 */
public class RecentsPresenter implements Presenter<RecentsView>, LoadAllPricesInteractor.Callback,
        TlongdevPriceListInteractor.Callback, TlongdevItemSchemaInteractor.Callback, LoadCurrencyPricesInteractor.Callback {

    @Inject SharedPreferences mPrefs;
    @Inject SharedPreferences.Editor mEditor;
    @Inject Tracker mTracker;
    @Inject Context mContext;

    private RecentsView mView;
    private final BptfApplication mApplication;

    private boolean mLoading = false;

    public RecentsPresenter(BptfApplication application) {
        mApplication = application;
        application.getPresenterComponent().inject(this);
    }

    @Override
    public void attachView(RecentsView view) {
        mView = view;

        if (mView != null && mLoading) {
            mView.showRefreshAnimation();
        }
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public void onLoadPricesFinished(Cursor prices) {
        if (mView != null) {
            mView.showPrices(prices);
        }
    }

    public void loadPrices() {
        //Download whole database when the app is first opened.
        if (mPrefs.getBoolean(mContext.getString(R.string.pref_initial_load_v2), true)) {
            if (Utility.isNetworkAvailable(mContext)) {
                callTlongdevPrices(false, true);
                //Show the progress dialog
                if (mView != null) {
                    mView.showLoadingDialog("Downloading prices...");
                }
            } else {
                if (mView != null) {
                    mView.showErrorDialog();
                }
            }
        } else {
            LoadAllPricesInteractor interactor = new LoadAllPricesInteractor(mApplication, this);
            interactor.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            loadCurrencyPrices();
            //Update database if the last update happened more than an hour ago
            if (System.currentTimeMillis() - mPrefs.getLong(mContext.getString(R.string.pref_last_price_list_update), 0) >= 3600000L
                    && Utility.isNetworkAvailable(mContext)) {
                callTlongdevPrices(true, false);
                if (mView != null) {
                    mView.showRefreshAnimation();
                }
{'fraction_non_alphanumeric': 0.08679396448123915, 'fraction_numerical': 0.006676458806249165, 'mean_word_length': 3.0399137001078747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2895337', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2104, 'n_words': 507}