package ca.antonious.habittracker.habitlist;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
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 android.view.View;

import java.util.List;

import ca.antonious.habittracker.ArrayAdapter;
import ca.antonious.habittracker.BaseActivity;
import ca.antonious.habittracker.Constants;
import ca.antonious.habittracker.HabitRepository;
import ca.antonious.habittracker.IHabitRepository;
import ca.antonious.habittracker.R;
import ca.antonious.habittracker.addhabit.AddHabitActivity;
import ca.antonious.habittracker.habitdetails.HabitDetailsActivity;
import ca.antonious.habittracker.models.Habit;
import ca.antonious.habittracker.observable.IObserver;

public class HabitListActivity extends BaseActivity implements IHabitListView {
    private RecyclerView habitRecyclerView;
    private HabitAdapter habitAdapter = new HabitAdapter();

    private FloatingActionButton fab;

    private HabitListController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        controller = getHabitTrackerApplication().getHabitListController();

        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(HabitListActivity.this, AddHabitActivity.class));
            }
        });

        habitRecyclerView = (RecyclerView) findViewById(R.id.habit_recycler_view);
        habitRecyclerView.setAdapter(habitAdapter);
        habitRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        habitRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy){
                if (dy > 10) {
                    fab.hide();
                } else if (dy < 0) {
                    fab.show();
                }
            }
        });

        habitAdapter.setItemClickedListener(new ArrayAdapter.ItemClickedListener<Habit>() {
            @Override
            public void onItemClicked(Habit item, int position) {
                Intent intent = new Intent(HabitListActivity.this, HabitDetailsActivity.class);
                intent.putExtra(Constants.EXTRA_HABIT_ID, item.getId());

                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        controller.attachView(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        controller.detachView();
    }

    @Override
    public void displayHabits(List<Habit> habit) {
        habitAdapter.setAll(habit);
        habitAdapter.notifyDataSetChanged();
    }
}

@@ -302,64 +302,8 @@
 ar;%0A
-import android.view.Menu;%0Aimport android.view.MenuItem;%0A
 impo
@@ -491,109 +491,8 @@
 ts;%0A
-import ca.antonious.habittracker.HabitRepository;%0Aimport ca.antonious.habittracker.IHabitRepository;%0A
 impo
@@ -701,63 +701,8 @@
 bit;
-%0Aimport ca.antonious.habittracker.observable.IObserver;
 %0A%0Apu
@@ -779,16 +779,54 @@
 tView %7B%0A
+    private FloatingActionButton fab;%0A
     priv
@@ -926,47 +926,8 @@
 );%0A%0A
-    private FloatingActionButton fab;%0A%0A
     
@@ -1245,76 +1245,215 @@
     
-controller = getHabitTrackerApplication().getHabitListController();%0A
+bindViews();%0A        resolveDependencies();%0A        setUpRecyclerView();%0A%0A        handleAddButtonClicks();%0A        handleListScrolling();%0A        handleListItemClicks();%0A    %7D%0A%0A    private void bindViews() %7B
 %0A   
@@ -1522,324 +1522,252 @@
     
-fab.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A  
+habitRecyclerView = (RecyclerView) findViewById(R.id.habit_recycler_view);%0A
     
-  
+%7D%0A%0A
     p
-ublic void onClick(View v) %7B%0A                startActivity(new Intent(HabitListActivity.this, AddHabitActivity.class));%0A            %7D%0A        %7D);%0A%0A        habitRecyclerView = (RecyclerView) findViewById(R.id.habit_r
+rivate void resolveDependencies() %7B%0A        controller = getHabitTrackerApplication().getHabitListController();%0A    %7D%0A%0A    private void setUpR
 ecycler
-_view);
+View() %7B
 %0A   
@@ -1923,24 +1923,362 @@
 L, false));%0A
+    %7D%0A%0A    private void handleAddButtonClicks() %7B%0A        fab.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                startActivity(new Intent(HabitListActivity.this, AddHabitActivity.class));%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void handleListScrolling() %7B
 %0A        hab
@@ -2627,16 +2627,64 @@
     %7D);%0A
+    %7D%0A%0A    private void handleListItemClicks() %7B
 %0A       
@@ -2856,32 +2856,159 @@
 tion) %7B%0A        
+        navigateToDetails(item.getId());%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void navigateToDetails(String habitID) %7B%0A
         Intent i
@@ -3079,24 +3079,16 @@
 class);%0A
-        
         
@@ -3133,32 +3133,19 @@
 ID, 
-item.getId());%0A%0A        
+habitID);%0A%0A
     
@@ -3175,34 +3175,8 @@
 t);%0A
-            %7D%0A        %7D);%0A
     

Refactor HabitListActivity