package ru.dyatel.tsuschedule;

import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import ru.dyatel.tsuschedule.data.DatabaseManager;
import ru.dyatel.tsuschedule.events.EventBus;
import ru.dyatel.tsuschedule.fragments.MainFragment;
import ru.dyatel.tsuschedule.layout.NavigationDrawerHandler;

public class MainActivity extends AppCompatActivity {

    private EventBus eventBus;
    private DatabaseManager databaseManager;

    private NavigationHandler navigationHandler;
    private NavigationDrawerHandler drawerHandler;

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

        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        eventBus = new EventBus();
        databaseManager = new DatabaseManager(this);

        FragmentManager fragmentManager = getSupportFragmentManager();

        // Replace ActionBar with Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        drawerHandler = new NavigationDrawerHandler(
                this, fragmentManager,
                (DrawerLayout) findViewById(R.id.drawer_layout)
        );

        navigationHandler = new NavigationHandler(fragmentManager, drawerHandler);
        fragmentManager.addOnBackStackChangedListener(navigationHandler);

        fragmentManager.beginTransaction()
                .replace(R.id.content_fragment, new MainFragment())
                .commit();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerHandler.syncState();
    }

    @Override
    public void onBackPressed() {
        if (!navigationHandler.onBackPressed()) super.onBackPressed();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return navigationHandler.onOptionsItemSelected(item)
                || super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerHandler.onConfigurationChanged(newConfig);
    }

    public EventBus getEventBus() {
        return eventBus;
    }

    public DatabaseManager getDatabaseManager() {
        return databaseManager;
    }

}

@@ -686,24 +686,41 @@
 Bus eventBus
+ = new EventBus()
 ;%0A    privat
@@ -752,16 +752,44 @@
 eManager
+ = new DatabaseManager(this)
 ;%0A%0A    p
@@ -1124,97 +1124,8 @@
 );%0A%0A
-        eventBus = new EventBus();%0A        databaseManager = new DatabaseManager(this);%0A%0A
     

Fix #10