<commit_before>package org.stepic.droid.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.squareup.otto.Subscribe;

import org.jetbrains.annotations.NotNull;
import org.stepic.droid.R;
import org.stepic.droid.analytic.Analytic;
import org.stepic.droid.core.presenters.PersistentCourseListPresenter;
import org.stepic.droid.core.presenters.contracts.FilterForCoursesView;
import org.stepic.droid.events.courses.FailDropCourseEvent;
import org.stepic.droid.events.courses.SuccessDropCourseEvent;
import org.stepic.droid.events.joining_course.SuccessJoinEvent;
import org.stepic.droid.model.Course;
import org.stepic.droid.storage.operations.Table;
import org.stepic.droid.ui.fragments.CourseListFragmentBase;
import org.stepic.droid.ui.util.BackButtonHandler;
import org.stepic.droid.ui.util.ContextMenuRecyclerView;
import org.stepic.droid.ui.util.OnBackClickListener;
import org.stepic.droid.util.AppConstants;

import java.util.List;

import javax.inject.Inject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class CoursesDatabaseFragmentBase extends CourseListFragmentBase implements FilterForCoursesView, OnBackClickListener {
    private static final int FILTER_REQUEST_CODE = 776;

    private boolean needFilter = false;

    @Inject
    PersistentCourseListPresenter courseListPresenter;

    BackButtonHandler backButtonHandler = null;
    private boolean isScreenCreated;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Activity rootActivity = getActivity();
        if (rootActivity != null && rootActivity instanceof BackButtonHandler) {
            backButtonHandler = ((BackButtonHandler) rootActivity);
            backButtonHandler.setBackClickListener(this);
        }
    }

    @Override
    protected void injectComponent() {
        App.Companion.component()
                .courseListComponentBuilder()
                .build()
                .inject(this);
    }

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

        isScreenCreated = true;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.my_courses_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_filter_menu:
                screenManager.showFilterScreen(this, FILTER_REQUEST_CODE, getCourseType());
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    @Subscribe
    public void onSuccessJoin(SuccessJoinEvent e) {
        //We do not upgrade database, because when
        //Only for find courses event.
        super.onSuccessJoin(e);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bus.register(this);
        courseListPresenter.attachView(this);
        courseListPresenter.restoreState();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isScreenCreated) {
            //reset all data
            isScreenCreated = false;
            needFilter = false;
            courses.clear();
            courseListPresenter.refreshData(getCourseType(), needFilter, false);
        } else {
            //load if not
            courseListPresenter.downloadData(getCourseType(), needFilter);
        }
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onDestroyView() {
        bus.unregister(this);
        courseListPresenter.detachView(this);
        super.onDestroyView();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) menuInfo;
        int position = info.position;
        if (position >= courses.size() && position < 0) {
            return; // the context will not be displayed
        }

        MenuInflater inflater = getActivity().getMenuInflater();
        if (courses.get(position).getEnrollment() != 0) {
            inflater.inflate(R.menu.course_context_menu, menu);
        } else {
            inflater.inflate(R.menu.course_context_not_enrolled_menu, menu);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        analytic.reportEvent(Analytic.Interaction.LONG_TAP_COURSE);
        ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) item.getMenuInfo();<commit_msg>add extra check for null (analytic driven) in context menu of course<commit_after>package org.stepic.droid.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.squareup.otto.Subscribe;

import org.jetbrains.annotations.NotNull;
import org.stepic.droid.R;
import org.stepic.droid.analytic.Analytic;
import org.stepic.droid.core.presenters.PersistentCourseListPresenter;
import org.stepic.droid.core.presenters.contracts.FilterForCoursesView;
import org.stepic.droid.events.courses.FailDropCourseEvent;
import org.stepic.droid.events.courses.SuccessDropCourseEvent;
import org.stepic.droid.events.joining_course.SuccessJoinEvent;
import org.stepic.droid.model.Course;
import org.stepic.droid.storage.operations.Table;
import org.stepic.droid.ui.fragments.CourseListFragmentBase;
import org.stepic.droid.ui.util.BackButtonHandler;
import org.stepic.droid.ui.util.ContextMenuRecyclerView;
import org.stepic.droid.ui.util.OnBackClickListener;
import org.stepic.droid.util.AppConstants;

import java.util.List;

import javax.inject.Inject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class CoursesDatabaseFragmentBase extends CourseListFragmentBase implements FilterForCoursesView, OnBackClickListener {
    private static final int FILTER_REQUEST_CODE = 776;

    private boolean needFilter = false;

    @Inject
    PersistentCourseListPresenter courseListPresenter;

    BackButtonHandler backButtonHandler = null;
    private boolean isScreenCreated;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Activity rootActivity = getActivity();
        if (rootActivity != null && rootActivity instanceof BackButtonHandler) {
            backButtonHandler = ((BackButtonHandler) rootActivity);
            backButtonHandler.setBackClickListener(this);
        }
    }

    @Override
    protected void injectComponent() {
        App.Companion.component()
                .courseListComponentBuilder()
                .build()
                .inject(this);
    }

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

        isScreenCreated = true;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.my_courses_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_filter_menu:
                screenManager.showFilterScreen(this, FILTER_REQUEST_CODE, getCourseType());
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    @Subscribe
    public void onSuccessJoin(SuccessJoinEvent e) {
        //We do not upgrade database, because when
        //Only for find courses event.
        super.onSuccessJoin(e);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bus.register(this);
        courseListPresenter.attachView(this);
        courseListPresenter.restoreState();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isScreenCreated) {
            //reset all data
            isScreenCreated = false;
            needFilter = false;
            courses.clear();
            courseListPresenter.refreshData(getCourseType(), needFilter, false);
        } else {
            //load if not
            courseListPresenter.downloadData(getCourseType(), needFilter);
        }
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onDestroyView() {
        bus.unregister(this);
        courseListPresenter.detachView(this);
        super.onDestroyView();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) menuInfo;
        if (info == null) {
            return;
        }
        int position = info.position;
        if (position >= courses.size() && position < 0) {
            return; // the context will not be displayed
        }

        MenuInflater inflater = getActivity().getMenuInflater();
        if (courses.get(position).getEnrollment() != 0) {
            inflater.inflate(R.menu.course_context_menu, menu);
        } else {
            inflater.inflate(R.menu.course_context_not_enrolled_menu, menu);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        analytic.reportEvent(Analytic.Interaction.LONG_TAP_COURSE);
        ContextMenuRecyclerView.RecyclerViewContextMenuInfo info = (ContextMenuRecyclerView.RecyclerViewContextMenuInfo) item.getMenuInfo();
{'fraction_non_alphanumeric': 0.08761671225124965, 'fraction_numerical': 0.0016976327454494011, 'mean_word_length': 3.2365161805833003, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937755', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2879, 'n_words': 562}