package com.mateoj.hack2help;

import android.location.Location;
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.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.model.LatLng;
import com.mateoj.hack2help.data.model.Tour;
import com.mateoj.hack2help.ui.adapter.TourAdapter;
import com.mateoj.hack2help.util.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends LocationActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    public static String TAG = MainActivity.class.getSimpleName();

    @Bind(R.id.recycler_main) RecyclerView recyclerView;

    private TourAdapter mAdapter;
    private Location mLastLocation;

    private boolean needsToFetch = true;

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

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

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

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

        mAdapter = new TourAdapter(this);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }


    private void fetchNearbyTours()
    {
        ApiHelper.getToursNearby(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), new Callback<List<Tour>>() {
            @Override
            public void done(List<Tour> result) {
                mAdapter.setTours(result);
            }

            @Override
            public void error(com.mateoj.hack2help.util.Error error) {
                needsToFetch = true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } 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) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings ("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camara) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

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

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            mLastLocation = location;
            if(needsToFetch) {
                mAdapter.setLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
                fetchNearbyTours();
                needsToFetch = false;
            }
        }
    }
}

@@ -2053,122 +2053,8 @@
  %7D%0A%0A
-    @Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        mGoogleApiClient.connect();%0A    %7D%0A%0A%0A
     

Update MainActivity.java