package de.metalmatze.krautreporter.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.List;

import de.metalmatze.krautreporter.R;
import de.metalmatze.krautreporter.adapters.ArticlesAdapter;
import de.metalmatze.krautreporter.models.ArticleModel;
import de.metalmatze.krautreporter.services.ArticleService;

public class MainActivity extends ActionBarActivity implements ArticlesAdapter.OnItemClickListener {

    protected RecyclerView recyclerView;
    protected ArticlesAdapter recyclerViewAdapter;
    protected ArticleService articleService;

    private List<ArticleModel> articles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.articleService = new ArticleService(getApplicationContext());

        this.setContentView(R.layout.activity_main);

        this.recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        this.recyclerView.setLayoutManager(layoutManager);

        this.articleService.update();
        this.articles = this.articleService.all();

        this.setArticles(this.articles);
    }

    @Override
    protected void onResume() {
        super.onResume();

        List<ArticleModel> articles = this.articleService.all();

        if (this.articles.size() < articles.size()) {
            this.setArticles(articles);
        }
    }

    public void setArticles(List<ArticleModel> articles) {
        this.articles = articles;
        ArticlesAdapter articlesAdapter = new ArticlesAdapter(getApplicationContext(), this, this.articles);
        this.recyclerView.setAdapter(articlesAdapter);
    }

    @Override
    public void onItemClick(ArticleModel articleModel) {
        Intent intent = new Intent(this, ArticleActivity.class);
        intent.putExtra("id", articleModel.getId());

        this.startActivity(intent);
    }
}

@@ -248,16 +248,165 @@
 lerView;
+%0Aimport android.widget.Toast;%0A%0Aimport com.android.volley.Response;%0Aimport com.android.volley.VolleyError;%0Aimport com.crashlytics.android.Crashlytics;
 %0A%0Aimport
@@ -637,16 +637,53 @@
 Service;
+%0Aimport io.fabric.sdk.android.Fabric;
 %0A%0Apublic
@@ -778,51 +778,131 @@
 ener
- %7B%0A%0A    protected RecyclerView recyclerView
+, Response.ErrorListener, Response.Listener %7B%0A%0A    private static final String LOG_TAG = MainActivity.class.getSimpleName()
 ;%0A  
@@ -913,31 +913,28 @@
 otected 
-ArticlesAdapter
+RecyclerView
  recycle
@@ -938,23 +938,16 @@
 clerView
-Adapter
 ;%0A    pr
@@ -1143,16 +1143,63 @@
 State);%0A
+        Fabric.with(this, new Crashlytics());%0A%0A
         
@@ -1635,19 +1635,30 @@
 .update(
+this, this
 );%0A
+%0A
         
@@ -1692,33 +1692,32 @@
 eService.all();%0A
-%0A
         this.set
@@ -1891,66 +1891,8 @@
 );%0A%0A
-        if (this.articles.size() %3C articles.size()) %7B%0A    
     
@@ -1927,18 +1927,8 @@
 s);%0A
-        %7D%0A
     
@@ -2426,10 +2426,441 @@
 ;%0A    %7D%0A
+%0A    @Override%0A    public void onResponse(Object response) %7B%0A        List%3CArticleModel%3E responseArticles = (List%3CArticleModel%3E) response;%0A%0A        List%3CArticleModel%3E articles = this.articleService.saveModels(responseArticles);%0A%0A        this.setArticles(articles);%0A    %7D%0A%0A    @Override%0A    public void onErrorResponse(VolleyError error) %7B%0A        Toast.makeText(this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();%0A    %7D%0A%0A
 %7D%0A

Add Response listeners to MainActivity and Crashlytics