<commit_before>package com.bookcrossing.mobile.ui.main;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.algolia.instantsearch.helpers.Searcher;
import com.algolia.instantsearch.ui.InstantSearch;
import com.algolia.instantsearch.ui.utils.ItemClickSupport;
import com.algolia.instantsearch.ui.views.Hits;
import com.bookcrossing.mobile.R;
import com.bookcrossing.mobile.ui.base.BaseActivity;
import com.bookcrossing.mobile.ui.bookpreview.BookActivity;
import com.bookcrossing.mobile.ui.create.BookCreateFragment;
import com.bookcrossing.mobile.ui.map.MapActivity;
import com.bookcrossing.mobile.ui.profile.ProfileFragment;
import com.bookcrossing.mobile.ui.settings.SettingsFragment;
import com.bookcrossing.mobile.ui.stash.StashFragment;
import com.bookcrossing.mobile.util.Constants;
import com.bookcrossing.mobile.util.listeners.BookListener;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.ResultCodes;
import com.google.firebase.auth.FirebaseAuth;

import org.json.JSONException;

import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements BookListener, NavigationView.OnNavigationItemSelectedListener {

    private static final int RC_SIGN_IN = 1236;

    @BindView(R.id.coord_layout)
    CoordinatorLayout coordinatorLayout;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.nav_view)
    NavigationView navigationView;

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;

    @BindView(R.id.hits)
    Hits hits;

    private ActionBarDrawerToggle drawerToggle;
    private Searcher searcher;
    private InstantSearch instantSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setupSearch();
        setSupportActionBar(toolbar);
        drawerToggle = setupDrawerToggle();
        drawer.addDrawerListener(drawerToggle);
        hits.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, int position, View v) {
                try {
                    onBookSelected(hits.get(position).getString("objectID"));
                } catch (JSONException e) {
                    Snackbar.make(coordinatorLayout, "Cannot open book info", Snackbar.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
        navigationView.setNavigationItemSelectedListener(this);

        FirebaseAuth auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() != null) {
            if (savedInstanceState != null) {
                Fragment recentFragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
                if (recentFragment != null) {
                    push(recentFragment);
                }
            } else {
                push(new MainFragment());
            }
        } else {
            startActivityForResult(
                    AuthUI.getInstance().createSignInIntentBuilder()
                            .setProviders(Collections.singletonList(
                                    new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()
                            ))
                            .build(),
                    RC_SIGN_IN);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        instantSearch.registerSearchView(this, menu, R.id.menu_action_search);
        return true;
    }

    private ActionBarDrawerToggle setupDrawerToggle() {
        return new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
    }

    private void setupSearch() {
        searcher = new Searcher(getString(R.string.algolia_app_id),
                getString(R.string.algolia_api_key),
                getString(R.string.algolia_index_name));
        instantSearch = new InstantSearch(hits, searcher);
    }

    public void push(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .addToBackStack(null)
                .replace(R.id.fragmentContainer, fragment)
                .commit();
    }

    public void replace(Fragment fragment) {
        getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, fragment)
                .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawer.openDrawer(GravityCompat.START);
                return true;
            case  R.id.menu_action_search:
                 if (hits.getVisibility() == View.GONE) {
                     hits.setVisibility(View.VISIBLE);
                 } else {
                     hits.setVisibility(View.GONE);
                 }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_catalogue:
                push(new MainFragment());
                drawer.closeDrawer(navigationView);
                return true;
            case R.id.nav_stash:
                toolbar.setTitle(R.string.stash_fragment_heading);
                push(new StashFragment());
                drawer.closeDrawer(navigationView);
                return true;
            case R.id.nav_books_map:
                navigateToMap();
                return true;
            case R.id.nav_profile:
                push(new ProfileFragment());
                drawer.closeDrawer(navigationView);
                return true;
            case R.id.nav_settings:
                push(new SettingsFragment());
                return true;
        }

        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            IdpResponse response = IdpResponse.fromResultIntent(data);
            if (resultCode == ResultCodes.OK) {
                push(new MainFragment());
                return;
            } else {

                if (response == null) {
                    Snackbar.make(coordinatorLayout, getResources().getString(R.string.sign_in_cancelled), Snackbar.LENGTH_LONG).show();
                    return;<commit_msg>Some small tweaks and refactorings in MainActivity<commit_after>package com.bookcrossing.mobile.ui.main;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.algolia.instantsearch.helpers.Searcher;
import com.algolia.instantsearch.ui.InstantSearch;
import com.algolia.instantsearch.ui.utils.ItemClickSupport;
import com.algolia.instantsearch.ui.views.Hits;
import com.bookcrossing.mobile.R;
import com.bookcrossing.mobile.ui.base.BaseActivity;
import com.bookcrossing.mobile.ui.bookpreview.BookActivity;
import com.bookcrossing.mobile.ui.create.BookCreateFragment;
import com.bookcrossing.mobile.ui.map.MapActivity;
import com.bookcrossing.mobile.ui.profile.ProfileFragment;
import com.bookcrossing.mobile.ui.settings.SettingsFragment;
import com.bookcrossing.mobile.ui.stash.StashFragment;
import com.bookcrossing.mobile.util.Constants;
import com.bookcrossing.mobile.util.listeners.BookListener;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.ResultCodes;
import com.google.firebase.auth.FirebaseAuth;

import org.json.JSONException;

import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements BookListener, NavigationView.OnNavigationItemSelectedListener {

    private static final int RC_SIGN_IN = 1236;

    @BindView(R.id.coord_layout)
    CoordinatorLayout coordinatorLayout;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.nav_view)
    NavigationView navigationView;

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;

    @BindView(R.id.hits)
    Hits hits;

    private ActionBarDrawerToggle drawerToggle;
    private Searcher searcher;
    private InstantSearch instantSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setupSearch();
        setSupportActionBar(toolbar);
        drawerToggle = setupDrawerToggle();
        drawer.addDrawerListener(drawerToggle);
        hits.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, int position, View v) {
                try {
                    onBookSelected(hits.get(position).getString("objectID"));
                } catch (JSONException e) {
                    Snackbar.make(coordinatorLayout, "Cannot open book info", Snackbar.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
        navigationView.setNavigationItemSelectedListener(this);

        FirebaseAuth auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() != null) {
            if (savedInstanceState != null) {
                pushRecentFragment();
            } else {
                push(new MainFragment());
            }
        } else {
            startActivityForResult(
                    AuthUI.getInstance().createSignInIntentBuilder()
                            .setProviders(Collections.singletonList(
                                    new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()
                            ))
                            .build(),
                    RC_SIGN_IN);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        instantSearch.registerSearchView(this, menu, R.id.menu_action_search);
        return true;
    }

    private ActionBarDrawerToggle setupDrawerToggle() {
        return new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
    }

    private void setupSearch() {
        searcher = new Searcher(getString(R.string.algolia_app_id),
                getString(R.string.algolia_api_key),
                getString(R.string.algolia_index_name));
        instantSearch = new InstantSearch(hits, searcher);
    }

    private void pushRecentFragment() {
        Fragment recentFragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
        if (recentFragment != null) {
            push(recentFragment);
        }
    }

    public void push(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .addToBackStack(null)
                .replace(R.id.fragmentContainer, fragment)
                .commit();
    }

    public void replace(Fragment fragment) {
        getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, fragment)
                .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawer.openDrawer(GravityCompat.START);
                return true;
            case  R.id.menu_action_search:
                 if (hits.getVisibility() == View.GONE) {
                     hits.setVisibility(View.VISIBLE);
                 } else {
                     hits.setVisibility(View.GONE);
                 }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_catalogue:
                push(new MainFragment());
                drawer.closeDrawer(navigationView);
                return true;
            case R.id.nav_stash:
                toolbar.setTitle(R.string.stash_fragment_heading);
                push(new StashFragment());
                drawer.closeDrawer(navigationView);
                return true;
            case R.id.nav_books_map:
                navigateToMap();
                return true;
            case R.id.nav_profile:
                push(new ProfileFragment());
                drawer.closeDrawer(navigationView);
                return true;
            case R.id.nav_settings:
                push(new SettingsFragment());
                drawer.closeDrawer(navigationView);
                return true;
        }

        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            IdpResponse response = IdpResponse.fromResultIntent(data);
            if (resultCode == ResultCodes.OK) {
                push(new MainFragment());
                return;
            } else {

                if (response == null) {
                    Snackbar.make(coordinatorLayout, getResources().getString(R.string.sign_in_cancelled), Snackbar.LENGTH_LONG).show();
                    return;
{'fraction_non_alphanumeric': 0.09158350840336134, 'fraction_numerical': 0.001444327731092437, 'mean_word_length': 2.4432640144665463, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '678937', 'n_tokens_mistral': 4179, 'n_tokens_neox': 4009, 'n_words': 684}