com.josecalles.porridge.home.HomeActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.josecalles.porridge.home.HomeActivity.java

Source

/*
 * Copyright 2015,  Jose Calles
 *
 * 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.josecalles.porridge.home;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.transition.Slide;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.Toolbar;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.josecalles.porridge.PorridgeApp;
import com.josecalles.porridge.R;
import com.josecalles.porridge.data.db.Group;
import com.josecalles.porridge.events.list.EventsListFragment;
import com.josecalles.porridge.game.GameActivity;
import com.josecalles.porridge.group.detail.GroupDetailActivity;
import com.josecalles.porridge.group.list.GroupListFragment;
import com.josecalles.porridge.injection.component.DaggerHomeComponent;
import com.josecalles.porridge.injection.component.HomeComponent;
import com.josecalles.porridge.injection.module.HomeModule;
import com.josecalles.porridge.welcome.WelcomeActivity;
import javax.inject.Inject;

public class HomeActivity extends AppCompatActivity implements HomeContract.View {

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

    @Bind(R.id.navigation_view)
    NavigationView mNavigationView;

    @Bind(R.id.drawer)
    DrawerLayout mDrawerLayout;

    @Bind(R.id.fragment_container)
    FrameLayout mFragmentContainer;

    @Inject
    HomeContract.Presenter mPresenter;

    private HomeComponent mHomeComponent;
    private FragmentManager mFragmentManager;

    private int mSelectedFragment = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        injectPresenter();
        setActionBar(mToolbar);
        setupDrawerLayout();
        setupNavigationView();
        getWindow().setEnterTransition(new Slide(Gravity.END));
        checkIfCameFromEvents();
    }

    private void checkIfCameFromEvents() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (getIntent().getExtras().getBoolean("from_event", false)) {
                getActionBar().setTitle("Events");
                EventsListFragment eventsListFragment = EventsListFragment.newInstance();
                mFragmentManager.beginTransaction().replace(mSelectedFragment, eventsListFragment)
                        //.addToBackStack(null)
                        .commit();
                mSelectedFragment = R.id.events_item;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //checkIfLoggedIn();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onActivityPause();
    }

    private void checkIfLoggedIn() {
        if (!mPresenter.isUserLoggedIn()) {
            startActivity(new Intent(this, WelcomeActivity.class));
        } else {
            //if (!mPresenter.isUserInGroup()) {
            mPresenter.updateUserGroup();
            mNavigationView.getMenu().clear();
            mNavigationView.inflateMenu(R.menu.drawer_in_group_menu);

            //} else {
            //  mNavigationView.getMenu().clear();
            //  mNavigationView.inflateMenu(R.menu.drawer_in_group_menu);
            //}
        }
    }

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

    private void setupNavigationView() {
        mNavigationView.setItemIconTintList(null);
        mFragmentManager = getFragmentManager();

        if (getActionBar() != null) {
            mNavigationView.setNavigationItemSelectedListener(item -> {
                getActionBar().setTitle(item.getTitle());
                mDrawerLayout.closeDrawer(GravityCompat.START);
                int id = item.getItemId();
                switch (id) {
                case R.id.browse_groups_item:
                    if (mSelectedFragment != R.id.browse_groups_item) {
                        getActionBar().setTitle("Groups");
                        GroupListFragment browseGroupFragment = GroupListFragment.newInstance();
                        Bundle bundle = new Bundle();
                        browseGroupFragment.setArguments(bundle);
                        mFragmentManager.beginTransaction().replace(R.id.fragment_container, browseGroupFragment)
                                //.addToBackStack(null)
                                .commit();
                        mSelectedFragment = R.id.browse_groups_item;
                    } else {
                        mDrawerLayout.closeDrawer(GravityCompat.START);
                    }

                    break;
                case R.id.join_group_item:
                    if (mSelectedFragment != R.id.join_group_item) {
                        getActionBar().setTitle("Select group");
                        GroupListFragment joinGroupFragment = GroupListFragment.newInstance();
                        Bundle joinBundle = new Bundle();
                        joinGroupFragment.setArguments(joinBundle);
                        mFragmentManager.beginTransaction().replace(R.id.fragment_container, joinGroupFragment)
                                //.addToBackStack(null)
                                .commit();

                        mSelectedFragment = R.id.join_group_item;
                    } else {
                        mDrawerLayout.closeDrawer(GravityCompat.START);
                    }
                    break;
                case R.id.events_item:
                    if (mSelectedFragment != R.id.events_item) {
                        getActionBar().setTitle("Events");
                        EventsListFragment eventsListFragment = EventsListFragment.newInstance();
                        mFragmentManager.beginTransaction().replace(R.id.fragment_container, eventsListFragment)
                                //.addToBackStack(null)
                                .commit();
                        mSelectedFragment = R.id.events_item;
                    } else {
                        mDrawerLayout.closeDrawer(GravityCompat.START);
                    }

                    break;

                }
                return true;
            });
        }
    }

    private void setupDrawerLayout() {
        if (getActionBar() != null) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationIcon(R.drawable.vector_drawer_menu);
            mToolbar.setNavigationOnClickListener(v -> mDrawerLayout.openDrawer(GravityCompat.START));
        }
    }

    private void injectPresenter() {
        PorridgeApp app = (PorridgeApp) getApplication();
        mHomeComponent = DaggerHomeComponent.builder().appComponent(app.getAppComponent())
                .homeModule(new HomeModule(this)).build();
        mHomeComponent.inject(this);
    }

    public HomeComponent getHomeComponent() {
        return mHomeComponent;
    }

    public void onGroupItemClicked(Group group) {
        Intent intent = new Intent(this, GroupDetailActivity.class);
        intent.putExtra("group_id", group.getId());
        startActivity(intent);
    }

    public void onEventItemClicked(long eventId) {
        Intent intent = new Intent(this, GameActivity.class);
        intent.putExtra("event_id", eventId);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_up_from_bottom, R.anim.slide_down_from_top);
    }
}