<commit_before>/*
 * Copyright (C) 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.doctoror.fuckoffmusicplayer.home;

import com.doctoror.fuckoffmusicplayer.base.BaseActivity;
import com.doctoror.fuckoffmusicplayer.R;
import com.doctoror.fuckoffmusicplayer.di.DaggerHolder;
import com.doctoror.fuckoffmusicplayer.library.albums.AlbumsFragment;
import com.doctoror.fuckoffmusicplayer.library.artists.ArtistsFragment;
import com.doctoror.fuckoffmusicplayer.library.genres.GenresFragment;
import com.doctoror.fuckoffmusicplayer.library.playlists.PlaylistsFragment;
import com.doctoror.fuckoffmusicplayer.library.tracks.TracksFragment;
import com.doctoror.fuckoffmusicplayer.playback.data.PlaybackData;
import com.doctoror.fuckoffmusicplayer.queue.Media;
import com.doctoror.fuckoffmusicplayer.settings.SettingsActivity;

import org.parceler.Parcel;
import org.parceler.Parcels;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "Library" activity
 */
public final class HomeActivity extends BaseActivity {

    private static final String KEY_INSTANCE_STATE = "INSTANCE_STATE";

    private ActionBarDrawerToggle mActionBarDrawerToggle;

    @BindView(R.id.playbackStatusCard)
    View mPlaybackStatusCard;

    @BindView(R.id.drawerLayout)
    DrawerLayout mDrawerLayout;

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

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

    private int mNavigationItem = R.id.navigationRecentActivity;

    private Integer mDrawerClosedAction;

    @Inject
    PlaybackData mPlaybackData;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerHolder.getInstance(this).mainComponent().inject(this);

        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        mNavigationView.setNavigationItemSelectedListener(new NavigationListener());
        mNavigationView.setCheckedItem(mNavigationItem);

        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
                R.string.Navigation_drawer, R.string.Navigation_drawer) {
            @Override
            public void onDrawerClosed(final View drawerView) {
                super.onDrawerClosed(drawerView);
                performDrawerClosedAction();
            }
        };
        mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);

        if (savedInstanceState == null) {
            mDrawerClosedAction = mNavigationItem;
            performDrawerClosedAction();
        } else {
            restoreInstanceState(savedInstanceState);
        }
    }

    private void restoreInstanceState(final Bundle savedInstanceState) {
        final InstanceState state = Parcels
                .unwrap(savedInstanceState.getParcelable(KEY_INSTANCE_STATE));
        if (state != null) {
            mNavigationItem = state.navigationItem;
            setTitle(state.title);
        }
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        final InstanceState state = new InstanceState();
        state.navigationItem = mNavigationItem;
        state.title = getTitle().toString();
        outState.putParcelable(KEY_INSTANCE_STATE, Parcels.wrap(state));
    }

    @Override
    protected void onPostCreate(@Nullable final Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarDrawerToggle.syncState();
    }

    @Override
    protected void onStart() {
        super.onStart();
        registerOnStartSubscription(mPlaybackData.queuePositionObservable()
                .subscribe(this::onQueuePositionChanged));
    }

    private void onQueuePositionChanged(final int position) {
        final List<Media> queue = mPlaybackData.getQueue();
        mPlaybackStatusCard.setVisibility(queue != null && position < queue.size()
                ? View.VISIBLE : View.GONE);
    }

    private void performDrawerClosedAction() {
        if (mDrawerClosedAction != null) {
            switch (mDrawerClosedAction) {
                case R.id.navigationRecentActivity:
                    setMainFragment(RecentActivityFragment.class.getCanonicalName());
                    setTitle(R.string.Recent_Activity);
                    break;

                case R.id.navigationPlaylists:
                    setMainFragment(PlaylistsFragment.class.getCanonicalName());
                    setTitle(R.string.Playlists);
                    break;

                case R.id.navigationArtists:
                    setMainFragment(ArtistsFragment.class.getCanonicalName());
                    setTitle(R.string.Artists);
                    break;

                case R.id.navigationAlbums:
                    setMainFragment(AlbumsFragment.class.getCanonicalName());
                    setTitle(R.string.Albums);
                    break;

                case R.id.navigationGenres:
                    setMainFragment(GenresFragment.class.getCanonicalName());
                    setTitle(R.string.Genres);
                    break;

                case R.id.navigationTracks:
                    setMainFragment(TracksFragment.class.getCanonicalName());
                    setTitle(R.string.Tracks);
                    break;

                case R.id.navigationSettings:
                    startActivity(new Intent(this, SettingsActivity.class));
                    break;
            }
            mDrawerClosedAction = null;
        }
    }

    private void setMainFragment(@NonNull final String name) {
        final FragmentManager fm = getFragmentManager();
        final Fragment fragment = fm.findFragmentById(R.id.content);
        FragmentTransaction ft = null;
        if (fragment == null) {
            ft = fm.beginTransaction();
            ft.add(R.id.content, Fragment.instantiate(this, name));
        } else if (!name.equals(fragment.getClass().getCanonicalName())) {
            ft = fm.beginTransaction();
            ft.replace(R.id.content, Fragment.instantiate(this, name));
        }
        if (ft != null) {
            ft.commit();
        }
    }

    private final class NavigationListener
            implements NavigationView.OnNavigationItemSelectedListener {

        @Override
        public boolean onNavigationItemSelected(@NonNull final MenuItem item) {

            final int id = item.getItemId();
            final boolean result;
            switch (id) {
                case R.id.navigationRecentActivity:
                case R.id.navigationArtists:
                case R.id.navigationAlbums:
                case R.id.navigationGenres:
                case R.id.navigationTracks:
                case R.id.navigationPlaylists:
                    result = true;
                    break;

                default:
                    result = false;
                    break;
            }

            if (result) {
                mNavigationItem = item.getItemId();
            }

            mDrawerClosedAction = id;
            mDrawerLayout.closeDrawers();
            return result;
        }
    }

    @Parcel
    static final class InstanceState {

        int navigationItem;
        String title;
    }
}
<commit_msg>Now NavigationDrawer is closed on back press, if opened.<commit_after>/*
 * Copyright (C) 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.doctoror.fuckoffmusicplayer.home;

import com.doctoror.fuckoffmusicplayer.base.BaseActivity;
import com.doctoror.fuckoffmusicplayer.R;
import com.doctoror.fuckoffmusicplayer.di.DaggerHolder;
import com.doctoror.fuckoffmusicplayer.library.albums.AlbumsFragment;
import com.doctoror.fuckoffmusicplayer.library.artists.ArtistsFragment;
import com.doctoror.fuckoffmusicplayer.library.genres.GenresFragment;
import com.doctoror.fuckoffmusicplayer.library.playlists.PlaylistsFragment;
import com.doctoror.fuckoffmusicplayer.library.tracks.TracksFragment;
import com.doctoror.fuckoffmusicplayer.playback.data.PlaybackData;
import com.doctoror.fuckoffmusicplayer.queue.Media;
import com.doctoror.fuckoffmusicplayer.settings.SettingsActivity;

import org.parceler.Parcel;
import org.parceler.Parcels;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "Library" activity
 */
public final class HomeActivity extends BaseActivity {

    private static final String KEY_INSTANCE_STATE = "INSTANCE_STATE";

    private ActionBarDrawerToggle mActionBarDrawerToggle;

    @BindView(R.id.playbackStatusCard)
    View mPlaybackStatusCard;

    @BindView(R.id.drawerLayout)
    DrawerLayout mDrawerLayout;

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

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

    private int mNavigationItem = R.id.navigationRecentActivity;

    private Integer mDrawerClosedAction;

    @Inject
    PlaybackData mPlaybackData;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerHolder.getInstance(this).mainComponent().inject(this);

        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        mNavigationView.setNavigationItemSelectedListener(new NavigationListener());
        mNavigationView.setCheckedItem(mNavigationItem);

        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
                R.string.Navigation_drawer, R.string.Navigation_drawer) {
            @Override
            public void onDrawerClosed(final View drawerView) {
                super.onDrawerClosed(drawerView);
                performDrawerClosedAction();
            }
        };
        mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);

        if (savedInstanceState == null) {
            mDrawerClosedAction = mNavigationItem;
            performDrawerClosedAction();
        } else {
            restoreInstanceState(savedInstanceState);
        }
    }

    private void restoreInstanceState(final Bundle savedInstanceState) {
        final InstanceState state = Parcels
                .unwrap(savedInstanceState.getParcelable(KEY_INSTANCE_STATE));
        if (state != null) {
            mNavigationItem = state.navigationItem;
            setTitle(state.title);
        }
    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        final InstanceState state = new InstanceState();
        state.navigationItem = mNavigationItem;
        state.title = getTitle().toString();
        outState.putParcelable(KEY_INSTANCE_STATE, Parcels.wrap(state));
    }

    @Override
    protected void onPostCreate(@Nullable final Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarDrawerToggle.syncState();
    }

    @Override
    protected void onStart() {
        super.onStart();
        registerOnStartSubscription(mPlaybackData.queuePositionObservable()
                .subscribe(this::onQueuePositionChanged));
    }

    private void onQueuePositionChanged(final int position) {
        final List<Media> queue = mPlaybackData.getQueue();
        mPlaybackStatusCard.setVisibility(queue != null && position < queue.size()
                ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(mNavigationView)) {
            mDrawerLayout.closeDrawer(mNavigationView);
        } else {
            super.onBackPressed();
        }
    }

    private void performDrawerClosedAction() {
        if (mDrawerClosedAction != null) {
            switch (mDrawerClosedAction) {
                case R.id.navigationRecentActivity:
                    setMainFragment(RecentActivityFragment.class.getCanonicalName());
                    setTitle(R.string.Recent_Activity);
                    break;

                case R.id.navigationPlaylists:
                    setMainFragment(PlaylistsFragment.class.getCanonicalName());
                    setTitle(R.string.Playlists);
                    break;

                case R.id.navigationArtists:
                    setMainFragment(ArtistsFragment.class.getCanonicalName());
                    setTitle(R.string.Artists);
                    break;

                case R.id.navigationAlbums:
                    setMainFragment(AlbumsFragment.class.getCanonicalName());
                    setTitle(R.string.Albums);
                    break;

                case R.id.navigationGenres:
                    setMainFragment(GenresFragment.class.getCanonicalName());
                    setTitle(R.string.Genres);
                    break;

                case R.id.navigationTracks:
                    setMainFragment(TracksFragment.class.getCanonicalName());
                    setTitle(R.string.Tracks);
                    break;

                case R.id.navigationSettings:
                    startActivity(new Intent(this, SettingsActivity.class));
                    break;
            }
            mDrawerClosedAction = null;
        }
    }

    private void setMainFragment(@NonNull final String name) {
        final FragmentManager fm = getFragmentManager();
        final Fragment fragment = fm.findFragmentById(R.id.content);
        FragmentTransaction ft = null;
        if (fragment == null) {
            ft = fm.beginTransaction();
            ft.add(R.id.content, Fragment.instantiate(this, name));
        } else if (!name.equals(fragment.getClass().getCanonicalName())) {
            ft = fm.beginTransaction();
            ft.replace(R.id.content, Fragment.instantiate(this, name));
        }
        if (ft != null) {
            ft.commit();
        }
    }

    private final class NavigationListener
            implements NavigationView.OnNavigationItemSelectedListener {

        @Override
        public boolean onNavigationItemSelected(@NonNull final MenuItem item) {

            final int id = item.getItemId();
            final boolean result;
            switch (id) {
                case R.id.navigationRecentActivity:
                case R.id.navigationArtists:
                case R.id.navigationAlbums:
                case R.id.navigationGenres:
                case R.id.navigationTracks:
                case R.id.navigationPlaylists:
                    result = true;
                    break;

                default:
                    result = false;
                    break;
            }

            if (result) {
                mNavigationItem = item.getItemId();
            }

            mDrawerClosedAction = id;
            mDrawerLayout.closeDrawers();
            return result;
        }
    }

    @Parcel
    static final class InstanceState {

        int navigationItem;
        String title;
    }
}

{'fraction_non_alphanumeric': 0.0827449378537667, 'fraction_numerical': 0.0012837719554181011, 'mean_word_length': 2.448983698933387, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '6722083', 'n_tokens_mistral': 4648, 'n_tokens_neox': 4357, 'n_words': 877}