package de.geeksfactory.opacclient.webservice;


import org.joda.time.DateTime;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import de.geeksfactory.opacclient.BuildConfig;
import de.geeksfactory.opacclient.objects.Library;
import de.geeksfactory.opacclient.storage.PreferenceDataSource;
import de.geeksfactory.opacclient.storage.SearchFieldDataSource;
import retrofit2.Response;

public class UpdateHandler {

    public int updateConfig(WebService service, PreferenceDataSource prefs,
            LibraryConfigUpdateService.FileOutput output,
            SearchFieldDataSource searchFields)
            throws IOException, JSONException {
        DateTime last_update = prefs.getLastLibraryConfigUpdate();
        if (prefs.getLastLibraryConfigUpdateVersion() != BuildConfig.VERSION_CODE) {
            last_update = prefs.getBundledConfigUpdateTime();
        }

        Response<List<Library>>
                response = service.getLibraryConfigs(last_update, BuildConfig.VERSION_CODE, 0, null)
                                  .execute();
        if (!response.isSuccessful()) {
            throw new IOException(String.valueOf(response.code()));
        }

        if (prefs.getLastLibraryConfigUpdateVersion() != BuildConfig.VERSION_CODE) {
            output.clearFiles();
        }

        List<Library> updatedLibraries = response.body();

        for (Library lib : updatedLibraries) {
            if (lib == null) {
                continue;
            }
            String filename = lib.getIdent() + ".json";
            JSONObject json = lib.toJSON();
            output.writeFile(filename, json.toString());

            if (searchFields.hasSearchFields(lib.getIdent())) {
                // clear cached search fields when configuration was updated
                searchFields.clearSearchFields(lib.getIdent());
            }
        }

        DateTime lastUpdate = new DateTime(response.headers().get("X-Page-Generated"));
        prefs.setLastLibraryConfigUpdate(lastUpdate);
        prefs.setLastLibraryConfigUpdateVersion(BuildConfig.VERSION_CODE);

        return updatedLibraries.size();
    }
}

@@ -470,16 +470,277 @@
 dler %7B%0A%0A
+    protected Response%3CList%3CLibrary%3E%3E getServerResponse(WebService service, DateTime last_update)%0A            throws IOException %7B%0A        return service.getLibraryConfigs(last_update, BuildConfig.VERSION_CODE, 0, null)%0A                      .execute();%0A    %7D%0A%0A
     publ
@@ -1221,153 +1221,58 @@
 ry%3E%3E
-%0A                response = service.getLibraryConfigs(last_update, BuildConfig.VERSION_CODE, 0, null)%0A                                  .execute(
+ response = getServerResponse(service, last_update
 );%0A 

Improve abstraction in UpdateHandler