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 android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.crashlytics.android.Crashlytics;

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;
import io.fabric.sdk.android.Fabric;

public class MainActivity extends ActionBarActivity implements ArticlesAdapter.OnItemClickListener, Response.ErrorListener, Response.Listener {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    protected RecyclerView recyclerView;
    protected ArticleService articleService;

    private List<ArticleModel> articles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());

        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, this);

        this.articles = this.articleService.all();
        this.setArticles(this.articles);
    }

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

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

        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);
    }

    @Override
    public void onResponse(Object response) {
        List<ArticleModel> responseArticles = (List<ArticleModel>) response;

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

        this.setArticles(articles);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
    }

}

@@ -940,16 +940,63 @@
 erView;%0A
+    protected ArticlesAdapter articlesAdapter;%0A
     prot
@@ -1237,37 +1237,81 @@
 cs());%0A%0A        
-this.
+setContentView(R.layout.activity_main);%0A%0A        
 articleService =
@@ -1352,33 +1352,32 @@
 Context());%0A
-%0A
         
 this.setCont
@@ -1368,127 +1368,38 @@
     
-this.setContentView(R.layout.activity_main);%0A%0A        this.recyclerView = (RecyclerView) findViewById(R.id.recyclerView
+articles = articleService.all(
 );%0A%0A
@@ -1541,24 +1541,25 @@
 TICAL);%0A
+%0A
         
 this.rec
@@ -1542,37 +1542,192 @@
 ICAL);%0A%0A        
-this.
+recyclerView = (RecyclerView) findViewById(R.id.recyclerView);%0A        articlesAdapter = new ArticlesAdapter(getApplicationContext(), this, articles);%0A%0A        
 recyclerView.set
@@ -1756,25 +1756,24 @@
 nager);%0A
-%0A
         
 this.art
@@ -1768,45 +1768,47 @@
     
-this.articleService.up
+recyclerView.setA
 da
+p
 te
-(this, this
+r(articlesAdapter
 );%0A%0A
@@ -1819,59 +1819,40 @@
     
-this.a
+setA
 rticles
- = this.articleService.all(
+(articles
 );%0A
+%0A
         
 this
@@ -1851,37 +1851,39 @@
     
-this.setArticles(this.article
+articleService.update(this, thi
 s);%0A
@@ -2027,25 +2027,24 @@
 .all();%0A
-%0A
         
 this.set
@@ -2027,37 +2027,32 @@
 .all();%0A        
-this.
 setArticles(arti
@@ -2124,24 +2124,25 @@
 cles) %7B%0A
+%0A
         
 this.art
@@ -2133,29 +2133,24 @@
         
-this.
 articles
  = artic
@@ -2145,19 +2145,32 @@
 cles
- = 
+.removeAll(this.
 articles
 ;%0A  
@@ -2165,16 +2165,17 @@
 articles
+)
 ;%0A      
@@ -2180,98 +2180,29 @@
     
-ArticlesAdapter articlesAdapter = new ArticlesAdapter(getApplicationContext(), this, this.
+this.articles.addAll(
 arti
@@ -2204,32 +2204,33 @@
 (articles);%0A
+%0A
         
 this.recycle
@@ -2221,52 +2221,45 @@
     
-this.recyclerView.setAdapter(articlesAdapter
+articlesAdapter.notifyDataSetChanged(
 );%0A 

Improve recyclerview and articlesAdapter communication