package com.github.cirorizzo.whitsquare.view;

import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;
import android.widget.Toast;

import com.github.cirorizzo.whitsquare.R;
import com.github.cirorizzo.whitsquare.model.Venue;
import com.github.cirorizzo.whitsquare.presenter.WhitSquarePresenterImpl;

import java.lang.ref.WeakReference;
import java.util.List;

public class MainActivity extends AppCompatActivity implements MainViewInterface {
    private final String TAG = MainActivity.class.getSimpleName();

    private WeakReference<WhitSquarePresenterImpl> whitSquarePresenter;
    private VenueAdapter venueAdapter;
    private RecyclerView containerRecyclerView;
    private SearchView searchVw;

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

        connectingToPresenter();

        initRecyclerView();

        searchVwSetUp();
    }

    @Override
    public void setMessage(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void setData(final List<Venue> venues) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                venueAdapter.setDataSet(venues);
                venueAdapter.notifyDataSetChanged();
            }
        });
    }

    private void connectingToPresenter() {
        // Using WeakReference to reduce Memory Leaks
        whitSquarePresenter = new WeakReference(WhitSquarePresenterImpl.getInstance());
        whitSquarePresenter.get().connect(this);
    }

    private void initRecyclerView() {
        containerRecyclerView = (RecyclerView) findViewById(R.id.data_recycler_view);
        containerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        venueAdapter = new VenueAdapter(this);
        containerRecyclerView.setAdapter(venueAdapter);
    }

    private void searchVwSetUp() {
        searchVw = (SearchView) findViewById(R.id.searchVw);
        searchVw.setSearchableInfo(((SearchManager) getSystemService(Context.SEARCH_SERVICE)).getSearchableInfo(getComponentName()));
        searchVw.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                whitSquarePresenter.get().getExplore(getApplicationContext(), query);
                searchVw.clearFocus();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }
}

@@ -106,16 +106,42 @@
 ontext;%0A
+import android.os.Bundle;%0A
 import a
@@ -185,34 +185,8 @@
 ty;%0A
-import android.os.Bundle;%0A
 impo
@@ -278,24 +278,80 @@
 cyclerView;%0A
+import android.view.Menu;%0Aimport android.view.MenuItem;%0A
 import andro
@@ -1135,24 +1135,88 @@
 ity_main);%0A%0A
+        getSupportActionBar().setDisplayHomeAsUpEnabled(true);%0A%0A
         conn
@@ -1290,32 +1290,564 @@
 SetUp();%0A    %7D%0A%0A
+    @Override%0A    public void onBackPressed() %7B%0A        whitSquarePresenter = null;%0A        super.onBackPressed();%0A    %7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        return super.onCreateOptionsMenu(menu);%0A    %7D%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case android.R.id.home:%0A                whitSquarePresenter = null;%0A                onBackPressed();%0A                return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A
     @Override%0A  

Fix for Icon