<commit_before>package com.alexstyl.specialdates.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.transition.Fade;
import android.support.transition.Transition;
import android.support.transition.TransitionManager;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.alexstyl.resources.Colors;
import com.alexstyl.specialdates.AppComponent;
import com.alexstyl.specialdates.CrashAndErrorTracker;
import com.alexstyl.specialdates.MementoApplication;
import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.Strings;
import com.alexstyl.specialdates.analytics.Analytics;
import com.alexstyl.specialdates.analytics.Screen;
import com.alexstyl.specialdates.contact.Contact;
import com.alexstyl.specialdates.contact.ContactsProvider;
import com.alexstyl.specialdates.date.AndroidDateLabelCreator;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.date.DateLabelCreator;
import com.alexstyl.specialdates.events.namedays.NameCelebrations;
import com.alexstyl.specialdates.events.namedays.NamedayUserSettings;
import com.alexstyl.specialdates.events.namedays.calendar.resource.NamedayCalendarProvider;
import com.alexstyl.specialdates.events.peopleevents.PeopleEventsProvider;
import com.alexstyl.specialdates.images.ImageLoader;
import com.alexstyl.specialdates.permissions.AndroidPermissionChecker;
import com.alexstyl.specialdates.permissions.ContactPermissionRequest;
import com.alexstyl.specialdates.permissions.PermissionNavigator;
import com.alexstyl.specialdates.transition.FadeInTransition;
import com.alexstyl.specialdates.transition.FadeOutTransition;
import com.alexstyl.specialdates.transition.SimpleTransitionListener;
import com.alexstyl.specialdates.ui.ViewFader;
import com.alexstyl.specialdates.ui.base.ThemedMementoActivity;
import com.alexstyl.specialdates.ui.widget.SpacesItemDecoration;
import com.novoda.notils.caster.Views;
import com.novoda.notils.meta.AndroidUtils;
import com.novoda.notils.text.SimpleTextWatcher;

import javax.inject.Inject;

import static android.view.View.GONE;
import static com.alexstyl.specialdates.permissions.ContactPermissionRequest.PermissionCallbacks;


public class SearchActivity extends ThemedMementoActivity {

    private static final String KEY_QUERY = "alexstyl:key_query";
    private static final int ID_CONTACTS = 31;
    private static final int ID_NAMEDAYS = 32;
    private static final int INITAL_COUNT = 5;
    private static final int HALF = 2;
    private static final int COLUMNS = 3;

    private int searchCounter = INITAL_COUNT;
    private SearchBar searchbar;
    private RecyclerView namesSuggestionsView;
    private SearchResultAdapter adapter;
    private NameSuggestionsAdapter namesAdapter;
    private String searchQuery;

    private ViewFader fader = new ViewFader();
    private ViewGroup content;
    private ContactPermissionRequest permissions;
    private RecyclerView resultView;
    private PeopleEventsSearch peopleEventsSearch;
    private ContactEventViewModelFactory viewModelFactory;
    private SearchNavigator searchNavigator;
    @Inject Analytics analytics;
    @Inject Strings strings;
    @Inject Colors colors;
    @Inject ImageLoader imageLoader;
    @Inject NamedayUserSettings namedayUserSettings;
    @Inject ContactsProvider contactsProvider;
    @Inject DateLabelCreator labelCreator;
    @Inject PeopleEventsProvider peopleEventsProvider;
    @Inject NamedayCalendarProvider namedayCalendarProvider;
    @Inject NamedayCalendarProvider calendarProvider;
    @Inject CrashAndErrorTracker tracker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        AppComponent applicationModule = ((MementoApplication) getApplication()).getApplicationModule();
        applicationModule.inject(this);

        peopleEventsSearch = new PeopleEventsSearch(peopleEventsProvider, NameMatcher.INSTANCE);
        DateLabelCreator dateLabelCreator = new AndroidDateLabelCreator(this);
        viewModelFactory = new ContactEventViewModelFactory(new ContactEventLabelCreator(Date.Companion.today(), strings, dateLabelCreator), colors);

        analytics.trackScreen(Screen.SEARCH);
        searchNavigator = new SearchNavigator(this, analytics);

        searchbar = Views.findById(this, R.id.search_searchbar);
        setSupportActionBar(searchbar);
        content = Views.findById(this, R.id.search_content);

        resultView = Views.findById(this, R.id.search_results);
        resultView.setHasFixedSize(true);

        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.search_result_card_vertical_padding) / HALF;
        resultView.addItemDecoration(new SpacesItemDecoration(spacingInPixels, COLUMNS));

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context());
        resultView.setLayoutManager(mLayoutManager);

        namesSuggestionsView = Views.findById(this, R.id.search_nameday_suggestions);
        PermissionNavigator navigator = new PermissionNavigator(this, analytics);
        AndroidPermissionChecker checker = new AndroidPermissionChecker(tracker, this);
        permissions = new ContactPermissionRequest(navigator, checker, permissionCallbacks);

        if (savedInstanceState != null) {
            searchQuery = savedInstanceState.getString(KEY_QUERY);
        }

        setupSearchField();

        adapter = new SearchResultAdapter(imageLoader, labelCreator);
        adapter.setSearchResultClickListener(listener);
        resultView.setAdapter(adapter);

        searchbar.setOnBackKeyPressedListener(onBackKeyPressedListener);

        setupSearchbarHint(namedayUserSettings);

        if (namedayUserSettings.isEnabled()) {
            GridLayoutManager namedayManager = new GridLayoutManager(context(), 1, RecyclerView.HORIZONTAL, false);
            namesAdapter = NameSuggestionsAdapter.newInstance(onNameSelectedListener, namedayUserSettings, namedayCalendarProvider);
            namesSuggestionsView.setHasFixedSize(true);
            namesSuggestionsView.setLayoutManager(namedayManager);
            namesSuggestionsView.setAdapter(namesAdapter);

            searchbar.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            searchbar.setOnFocusChangeListener(new ToggleVisibilityOnFocus(namesSuggestionsView));
        } else {
            namesSuggestionsView.setVisibility(GONE);
        }

        if (savedInstanceState == null) {
            fader.hideContentOf(searchbar);
            ViewTreeObserver viewTreeObserver = searchbar.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    searchbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    animateShowSearch();
                }

                private void animateShowSearch() {
                    TransitionManager.beginDelayedTransition(searchbar, FadeInTransition.createTransition());
                    fader.showContent(searchbar);
                }
            });
        }

        if (!permissions.permissionIsPresent()) {
            permissions.requestForPermission();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        searchbar.requestFocus();
        AndroidUtils.toggleKeyboard(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        permissions.onActivityResult(requestCode, resultCode, data);
    }

    private void setupSearchbarHint(NamedayUserSettings preferences) {
        SearchHintCreator searchHintCreator = new SearchHintCreator(getResources(), preferences);
        searchbar.setHint(searchHintCreator.createHint());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;<commit_msg>Remove toggling of keyboard when resuming SearchActivity<commit_after>package com.alexstyl.specialdates.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.transition.Fade;
import android.support.transition.Transition;
import android.support.transition.TransitionManager;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.alexstyl.resources.Colors;
import com.alexstyl.specialdates.AppComponent;
import com.alexstyl.specialdates.CrashAndErrorTracker;
import com.alexstyl.specialdates.MementoApplication;
import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.Strings;
import com.alexstyl.specialdates.analytics.Analytics;
import com.alexstyl.specialdates.analytics.Screen;
import com.alexstyl.specialdates.contact.Contact;
import com.alexstyl.specialdates.contact.ContactsProvider;
import com.alexstyl.specialdates.date.AndroidDateLabelCreator;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.date.DateLabelCreator;
import com.alexstyl.specialdates.events.namedays.NameCelebrations;
import com.alexstyl.specialdates.events.namedays.NamedayUserSettings;
import com.alexstyl.specialdates.events.namedays.calendar.resource.NamedayCalendarProvider;
import com.alexstyl.specialdates.events.peopleevents.PeopleEventsProvider;
import com.alexstyl.specialdates.images.ImageLoader;
import com.alexstyl.specialdates.permissions.AndroidPermissionChecker;
import com.alexstyl.specialdates.permissions.ContactPermissionRequest;
import com.alexstyl.specialdates.permissions.PermissionNavigator;
import com.alexstyl.specialdates.transition.FadeInTransition;
import com.alexstyl.specialdates.transition.FadeOutTransition;
import com.alexstyl.specialdates.transition.SimpleTransitionListener;
import com.alexstyl.specialdates.ui.ViewFader;
import com.alexstyl.specialdates.ui.base.ThemedMementoActivity;
import com.alexstyl.specialdates.ui.widget.SpacesItemDecoration;
import com.novoda.notils.caster.Views;
import com.novoda.notils.meta.AndroidUtils;
import com.novoda.notils.text.SimpleTextWatcher;

import javax.inject.Inject;

import static android.view.View.GONE;
import static com.alexstyl.specialdates.permissions.ContactPermissionRequest.PermissionCallbacks;


public class SearchActivity extends ThemedMementoActivity {

    private static final String KEY_QUERY = "alexstyl:key_query";
    private static final int ID_CONTACTS = 31;
    private static final int ID_NAMEDAYS = 32;
    private static final int INITAL_COUNT = 5;
    private static final int HALF = 2;
    private static final int COLUMNS = 3;

    private int searchCounter = INITAL_COUNT;
    private SearchBar searchbar;
    private RecyclerView namesSuggestionsView;
    private SearchResultAdapter adapter;
    private NameSuggestionsAdapter namesAdapter;
    private String searchQuery;

    private ViewFader fader = new ViewFader();
    private ViewGroup content;
    private ContactPermissionRequest permissions;
    private RecyclerView resultView;
    private PeopleEventsSearch peopleEventsSearch;
    private ContactEventViewModelFactory viewModelFactory;
    private SearchNavigator searchNavigator;
    @Inject Analytics analytics;
    @Inject Strings strings;
    @Inject Colors colors;
    @Inject ImageLoader imageLoader;
    @Inject NamedayUserSettings namedayUserSettings;
    @Inject ContactsProvider contactsProvider;
    @Inject DateLabelCreator labelCreator;
    @Inject PeopleEventsProvider peopleEventsProvider;
    @Inject NamedayCalendarProvider namedayCalendarProvider;
    @Inject NamedayCalendarProvider calendarProvider;
    @Inject CrashAndErrorTracker tracker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        AppComponent applicationModule = ((MementoApplication) getApplication()).getApplicationModule();
        applicationModule.inject(this);

        peopleEventsSearch = new PeopleEventsSearch(peopleEventsProvider, NameMatcher.INSTANCE);
        DateLabelCreator dateLabelCreator = new AndroidDateLabelCreator(this);
        viewModelFactory = new ContactEventViewModelFactory(new ContactEventLabelCreator(Date.Companion.today(), strings, dateLabelCreator), colors);

        analytics.trackScreen(Screen.SEARCH);
        searchNavigator = new SearchNavigator(this, analytics);

        searchbar = Views.findById(this, R.id.search_searchbar);
        setSupportActionBar(searchbar);
        content = Views.findById(this, R.id.search_content);

        resultView = Views.findById(this, R.id.search_results);
        resultView.setHasFixedSize(true);

        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.search_result_card_vertical_padding) / HALF;
        resultView.addItemDecoration(new SpacesItemDecoration(spacingInPixels, COLUMNS));

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context());
        resultView.setLayoutManager(mLayoutManager);

        namesSuggestionsView = Views.findById(this, R.id.search_nameday_suggestions);
        PermissionNavigator navigator = new PermissionNavigator(this, analytics);
        AndroidPermissionChecker checker = new AndroidPermissionChecker(tracker, this);
        permissions = new ContactPermissionRequest(navigator, checker, permissionCallbacks);

        if (savedInstanceState != null) {
            searchQuery = savedInstanceState.getString(KEY_QUERY);
        }

        setupSearchField();

        adapter = new SearchResultAdapter(imageLoader, labelCreator);
        adapter.setSearchResultClickListener(listener);
        resultView.setAdapter(adapter);

        searchbar.setOnBackKeyPressedListener(onBackKeyPressedListener);

        setupSearchbarHint(namedayUserSettings);

        if (namedayUserSettings.isEnabled()) {
            GridLayoutManager namedayManager = new GridLayoutManager(context(), 1, RecyclerView.HORIZONTAL, false);
            namesAdapter = NameSuggestionsAdapter.newInstance(onNameSelectedListener, namedayUserSettings, namedayCalendarProvider);
            namesSuggestionsView.setHasFixedSize(true);
            namesSuggestionsView.setLayoutManager(namedayManager);
            namesSuggestionsView.setAdapter(namesAdapter);

            searchbar.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            searchbar.setOnFocusChangeListener(new ToggleVisibilityOnFocus(namesSuggestionsView));
        } else {
            namesSuggestionsView.setVisibility(GONE);
        }

        if (savedInstanceState == null) {
            fader.hideContentOf(searchbar);
            ViewTreeObserver viewTreeObserver = searchbar.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    searchbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    animateShowSearch();
                }

                private void animateShowSearch() {
                    TransitionManager.beginDelayedTransition(searchbar, FadeInTransition.createTransition());
                    fader.showContent(searchbar);
                }
            });
        }

        if (!permissions.permissionIsPresent()) {
            permissions.requestForPermission();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        searchbar.requestFocus();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        permissions.onActivityResult(requestCode, resultCode, data);
    }

    private void setupSearchbarHint(NamedayUserSettings preferences) {
        SearchHintCreator searchHintCreator = new SearchHintCreator(getResources(), preferences);
        searchbar.setHint(searchHintCreator.createHint());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
{'fraction_non_alphanumeric': 0.07667919210897135, 'fraction_numerical': 0.0015265382808830436, 'mean_word_length': 4.354605469977995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2565056', 'n_tokens_mistral': 4612, 'n_tokens_neox': 4410, 'n_words': 786}