text: package me.jcala.xmarket.mvp.main;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.speech.RecognizerIntent;
  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.text.TextUtils;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.TextView;
  
  import com.ashokvarma.bottomnavigation.BottomNavigationBar;
  import com.miguelcatalan.materialsearchview.MaterialSearchView;
  
  import java.util.ArrayList;
  
  import butterknife.BindView;
  import butterknife.ButterKnife;
  import butterknife.Unbinder;
  import io.realm.Realm;
  import me.jcala.xmarket.R;
  import me.jcala.xmarket.mvp.a_base.BaseActivity;
  import me.jcala.xmarket.mvp.message.MessageService;
  import me.jcala.xmarket.util.PollingUtils;
  
  public class MainActivity  extends BaseActivity
          implements NavigationView.OnNavigationItemSelectedListener{
  
      @BindView(R.id.toolbar)
      Toolbar toolbar;
      @BindView(R.id.drawer_layout)
      DrawerLayout drawer;
      @BindView(R.id.nav_view)
      NavigationView navigationView;
      @BindView(R.id.search_view)
      MaterialSearchView searchView;
      private MainPresenter presenter;
      @BindView(R.id.toolbar_title)
      TextView toolbarTitle;
      @BindView(R.id.bottom_navigation_bar)
      BottomNavigationBar mBottomNavigationBar;
      private Realm realm;
      private Unbinder unbinder;
  
      @Override
      protected void initViews(Bundle savedInstanceState) {
          setContentView(R.layout.main_activity);
          realm=Realm.getDefaultInstance();
          PollingUtils.startPollingService(this, 5, MessageService.class, MessageService.ACTION);
          unbinder=ButterKnife.bind(this);
  
          presenter=new MainPresenterImpl(this,realm);
  
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayShowTitleEnabled(false);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
          View headerLayout= navigationView.inflateHeaderView(R.layout.main_slide);
          navigationView.setNavigationItemSelectedListener(this);
          presenter.init(searchView,headerLayout);
      }
  
      @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          int id = item.getItemId();
          presenter.slideJump(id);
          return false;
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main_top, menu);
          MenuItem item = menu.findItem(R.id.action_search);
          searchView.setMenuItem(item);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_search) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onBackPressed() {
          if (searchView.isSearchOpen()) {
              searchView.closeSearch();
          } else {
              super.onBackPressed();
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
              ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
              if (matches != null && matches.size() > 0) {
                  String searchWrd = matches.get(0);
                  if (!TextUtils.isEmpty(searchWrd)) {
                      searchView.setQuery(searchWrd, false);
                  }
              }
  
              return;
          }
          super.onActivityResult(requestCode, resultCode, data);
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          PollingUtils.stopPollingService(this, MessageService.class, MessageService.ACTION);
          unbinder.unbind();
          realm.close();
      }
  }
  