com.fullmeadalchemist.mustwatch.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.fullmeadalchemist.mustwatch.MainActivity.java

Source

/*
 * Copyright (c) 2017 Full Mead Alchemist, LLC.
 *
 * 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.fullmeadalchemist.mustwatch;

import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LifecycleRegistryOwner;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
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.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.fullmeadalchemist.mustwatch.core.HeadlessLoadingFragment;
import com.fullmeadalchemist.mustwatch.ui.common.NavigationController;

import javax.inject.Inject;

import dagger.android.DispatchingAndroidInjector;
import dagger.android.support.HasSupportFragmentInjector;

public class MainActivity extends AppCompatActivity implements LifecycleRegistryOwner,
        NavigationView.OnNavigationItemSelectedListener, HasSupportFragmentInjector {
    private static final String TAG = MainActivity.class.getSimpleName();
    private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
    @Inject
    DispatchingAndroidInjector<Fragment> dispatchingAndroidInjector;
    @Inject
    NavigationController navigationController;

    HeadlessLoadingFragment headlessLoadingFragment;
    String HEADLESS_FRAGMENT_TAG = "HEADLESS_LOADING_FRAGMENT";

    @NonNull
    @Override
    public LifecycleRegistry getLifecycle() {
        return lifecycleRegistry;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        toggle.syncState();

        FragmentManager fm = getSupportFragmentManager();
        headlessLoadingFragment = (HeadlessLoadingFragment) fm.findFragmentByTag(HEADLESS_FRAGMENT_TAG);
        if (headlessLoadingFragment == null) {
            headlessLoadingFragment = new HeadlessLoadingFragment();
            fm.beginTransaction().add(headlessLoadingFragment, HEADLESS_FRAGMENT_TAG).commit();
        }

        if (savedInstanceState == null) {
            navigationController.navigateToBatches();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isFinishing()) {
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().remove(headlessLoadingFragment).commitAllowingStateLoss();
        }
    }

    @Override
    public DispatchingAndroidInjector<Fragment> supportFragmentInjector() {
        return dispatchingAndroidInjector;
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            int fragments = getSupportFragmentManager().getBackStackEntryCount();
            if (fragments == 1) {
                finish();
            } else {
                if (getFragmentManager().getBackStackEntryCount() > 1) {
                    getFragmentManager().popBackStack();
                } else {
                    super.onBackPressed();
                }
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            //            Intent intent = new Intent(this, SettingsActivity.class);
            //            startActivity(intent);
            Log.e(TAG, "Settings view not yet implemented");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        switch (item.getItemId()) {
        case R.id.nav_batches:
            Log.i(TAG, "nav_batches selected from Drawer");
            navigationController.navigateToBatches();
            break;
        case R.id.nav_recipes:
            Log.i(TAG, "nav_recipes selected from Drawer");
            navigationController.navigateToRecipes();
            break;
        //            case R.id.nav_groups:
        //                Log.i(TAG, "nav_groups selected from Drawer");
        //                Log.w(TAG, "Groups view not yet implemented");
        //                navigationController.navigateToBatches();
        //                break;
        //            case R.id.nav_settings:
        //                Log.i(TAG, "nav_settings selected from Drawer");
        //                Log.w(TAG, "Settings view not yet implemented");
        //                navigationController.navigateToBatches();
        //                break;
        case R.id.nav_about:
            Log.i(TAG, "nav_about selected from Drawer");
            navigationController.navigateToAbout();
            break;
        default:
            Log.w(TAG, String.format("unknown selected from Drawer: %s", item.getTitle()));
            break;
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}