package com.shing.aputimetable;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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.view.MenuItem;
import android.view.View;

import com.shing.aputimetable.Fragments.TimetableFragment;
import com.shing.aputimetable.Fragments.TodayclassFragment;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private final Handler mDrawerHandler = new Handler();
    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private int selectedDrawerItem;

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

        mToolbar = findViewById(R.id.toolbar);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView = findViewById(R.id.nav_view);

        setSupportActionBar(mToolbar);
        setupNavigationDrawer();

        if (savedInstanceState == null) {
            selectedDrawerItem = R.id.item_timetable_fragment;
            navigate(selectedDrawerItem);
            mNavigationView.setCheckedItem(selectedDrawerItem);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("drawer", selectedDrawerItem);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        navigate(savedInstanceState.getInt("drawer"));
    }

    private void setupNavigationDrawer() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                super.onDrawerSlide(drawerView, 0); // this disables the arrow @ completed tate
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, 0); // this disables the animation
            }
        };


        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        mNavigationView.setNavigationItemSelectedListener(this);

    }

    private void navigate(int id) {
        selectedDrawerItem = id;
        mNavigationView.setCheckedItem(id);
        switch (id) {
            case R.id.item_timetable_fragment:
                displayFragment(new TimetableFragment(), "timetable");
                break;
            case R.id.item_todayclass_fragment:
                displayFragment(new TodayclassFragment(), "todayclass");
                break;
        }

    }

    private void displayFragment(@NonNull Fragment navFragment, String tag) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        supportInvalidateOptionsMenu();
        transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        transaction.replace(R.id.content_frame, navFragment, tag).commitNow();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
        mDrawerHandler.removeCallbacksAndMessages(null);
        mDrawerHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                navigate(item.getItemId());
            }
        }, 250);
        mDrawerLayout.closeDrawer(mNavigationView);
        return true;
    }

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

@@ -457,16 +457,41 @@
 oolbar;%0A
+import android.util.Log;%0A
 import a
@@ -561,33 +561,33 @@
 ng.aputimetable.
-F
+f
 ragments.Timetab
@@ -628,17 +628,17 @@
 metable.
-F
+f
 ragments
@@ -773,16 +773,84 @@
 ener %7B%0A%0A
+    private final String TAG = MainActivity.class.getSimpleName();%0A%0A
     priv
@@ -2982,16 +2982,17 @@
 em(id);%0A
+%0A
         
@@ -3044,32 +3044,124 @@
 table_fragment:%0A
+                if (getSupportFragmentManager().findFragmentByTag(%22timetable%22) == null)%0A    
                 
@@ -3278,32 +3278,125 @@
 class_fragment:%0A
+                if (getSupportFragmentManager().findFragmentByTag(%22todayclass%22) == null)%0A    
                 
@@ -3476,32 +3476,64 @@
 break;%0A        %7D
+%0A        Log.d(TAG, %22navigate%22);
 %0A%0A    %7D%0A%0A    pri

Create and display a new fragment if not created before