package de.metalmatze.krautreporter.services;

import android.content.Context;
import android.widget.Toast;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.query.Select;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.List;
import java.util.concurrent.ExecutionException;

import de.metalmatze.krautreporter.KrautreporterRssParser;
import de.metalmatze.krautreporter.models.ArticleModel;

public class ArticleService {

    private static final String RSS_HOST = "https://krautreporter.de/";
    private static final String RSS_FILE = "rss.rss";

    protected Context context;

    public ArticleService(Context applicationContext) {

        this.context = applicationContext;
    }

    public List<ArticleModel> all()
    {
        return new Select().from(ArticleModel.class).orderBy("date DESC").execute();
    }

    public ArticleModel find(long id)
    {
        return new Select().from(ArticleModel.class).where("id = ?", id).executeSingle();
    }

    public void update()
    {
        RequestQueue requestQueue = Volley.newRequestQueue(this.context);
        StringRequest rssRequest = new StringRequest(
            Request.Method.GET,
            RSS_HOST + RSS_FILE,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    try {

                        List<ArticleModel> articleModels = new KrautreporterRssParser.KrautreporterRssParserTask().execute(s).get();
                        saveModels(articleModels);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    volleyError.printStackTrace();
                    Toast.makeText(context, "Error loading feed", Toast.LENGTH_SHORT).show();
                }
            }
        );

        requestQueue.add(rssRequest);
    }

    private void saveModels(List<ArticleModel> models)
    {
        ActiveAndroid.beginTransaction();
        try {
            for (ArticleModel article : models)
            {
                article.save();
            }
            ActiveAndroid.setTransactionSuccessful();
        }
        finally {
            ActiveAndroid.endTransaction();
        }
    }

}

@@ -75,37 +75,8 @@
 ext;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -293,182 +293,46 @@
 ley.
-VolleyError;%0Aimport com.android.volley.toolbox.StringRequest;%0Aimport com.android.volley.toolbox.Volley;%0A%0Aimport java.util.List;%0Aimport java.util.concurrent.ExecutionException
+toolbox.Volley;%0A%0Aimport java.util.List
 ;%0A%0Ai
@@ -365,38 +365,34 @@
 eporter.
-KrautreporterRssParser
+helpers.RssRequest
 ;%0Aimport
@@ -1037,16 +1037,80 @@
  update(
+Response.Listener listener, Response.ErrorListener errorListener
 )%0A    %7B%0A
@@ -1195,66 +1195,40 @@
     
-StringRequest rssRequest = new StringRequest(%0A            
+requestQueue.add(new RssRequest(
 Requ
@@ -1242,28 +1242,16 @@
 hod.GET,
-%0A           
  RSS_HOS
@@ -1267,987 +1267,72 @@
 ILE,
-%0A            new Response.Listener%3CString%3E() %7B%0A                @Override%0A                public void onResponse(String s) %7B%0A                    try %7B%0A%0A                        List%3CArticleModel%3E articleModels = new KrautreporterRssParser.KrautreporterRssParserTask().execute(s).get();%0A                        saveModels(articleModels);%0A%0A                    %7D catch (InterruptedException e) %7B%0A                        e.printStackTrace();%0A                    %7D catch (ExecutionException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D,%0A            new Response.ErrorListener() %7B%0A                @Override%0A                public void onErrorResponse(VolleyError volleyError) %7B%0A                    volleyError.printStackTrace();%0A                    Toast.makeText(context, %22Error loading feed%22, Toast.LENGTH_SHORT).show();%0A                %7D%0A            %7D%0A        );%0A%0A        requestQueue.add(rssRequest);%0A    %7D%0A%0A    private void
+ listener, errorListener));%0A    %7D%0A%0A    public List%3CArticleModel%3E
  sav
@@ -1667,22 +1667,50 @@
 );%0A        %7D
+%0A%0A        return this.all();
 %0A    %7D%0A%0A%7D%0A

Use RssRequest instead of doing it all in ArticleService