package de.fau.amos.virtualledger.android.api.contacts;

import android.util.Log;

import java.util.List;

import de.fau.amos.virtualledger.android.api.RestApi;
import de.fau.amos.virtualledger.android.api.auth.AuthenticationProvider;
import de.fau.amos.virtualledger.dtos.Contact;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Simon on 01.07.2017.
 */

public class HTTPContactsProvider implements ContactsProvider{

    private static final String TAG = "HTTPContactsProvider";

    private RestApi restApi;
    private AuthenticationProvider authenticationProvider;

    public HTTPContactsProvider(RestApi restApi, AuthenticationProvider authenticationProvider) {
        this.restApi = restApi;
        this.authenticationProvider = authenticationProvider;
    }

    @Override
    public Observable<List<Contact>> getContacts() {

        final PublishSubject<List<Contact>> observable = PublishSubject.create();

        authenticationProvider.getToken()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull final String token) throws Exception {
                        // got token
                        final retrofit2.Call<List<Contact>> responseMessage = restApi.getContacts(token);

                        responseMessage.enqueue(new Callback<List<Contact>>() {
                            @Override
                            public void onResponse(retrofit2.Call<List<Contact>> call, Response<List<Contact>> response) {
                                // got response from server
                                if (response.isSuccessful()) {
                                    List<Contact> contacts = response.body();
                                    Log.v(TAG, "Fetching of contacts was successful " + response.code());
                                    observable.onNext(contacts);
                                } else {
                                    Log.e(TAG, "Fetchin of contacts was not successful! ERROR " + response.code());
                                    observable.onError(new Throwable("Fetching of contacts was not successful!"));
                                }
                            }


                            @Override
                            public void onFailure(retrofit2.Call<List<Contact>> call, Throwable t) {
                                // response from server failed
                                Log.e(TAG, "No connection to server!");
                                observable.onError(new Throwable("No connection to server!"));
                            }
                        });

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull final Throwable throwable) throws Exception {
                        // did not get any token
                        Log.e(TAG, throwable.getMessage());
                        observable.onError(new Throwable("No authentication token available!"));
                    }
                });

        return observable;
    }


    @Override
    public Observable<Void> addContacts(Contact savingsAccount) {
        return null;
    }
}

@@ -224,24 +224,94 @@
 onProvider;%0A
+import de.fau.amos.virtualledger.android.api.shared.RetrofitCallback;%0A
 import de.fa
@@ -341,24 +341,24 @@
 os.Contact;%0A
-
 import io.re
@@ -607,62 +607,8 @@
 ect;
-%0Aimport retrofit2.Callback;%0Aimport retrofit2.Response;
 %0A%0A/*
@@ -1603,1469 +1603,79 @@
     
-final retrofit2.Call%3CList%3CContact%3E%3E responseMessage = restApi.getContacts(token);%0A%0A                        responseMessage.enqueue(new Callback%3CList%3CContact%3E%3E() %7B%0A                            @Override%0A                            public void onResponse(r
+restApi.getContacts(token).enqueue(new R
 etrofit
-2.
 Call
-%3CList%3CContact%3E%3E call, Response%3CList%3CContact%3E%3E response) %7B%0A                                // got response from server%0A                                if (response.isSuccessful()) %7B%0A                                    List%3CContact%3E contacts = response.body();%0A                                    Log.v(TAG, %22Fetching of contacts was successful %22 + response.code());%0A                                    observable.onNext(contacts);%0A                                %7D else %7B%0A                                    Log.e(TAG, %22Fetchin of contacts was not successful! ERROR %22 + response.code());%0A                                    observable.onError(new Throwable(%22Fetching of contacts was not successful!%22));%0A                                %7D%0A                            %7D%0A%0A%0A                            @Override%0A                            public void onFailure(retrofit2.Call%3CList%3CContact%3E%3E call, Throwable t) %7B%0A                                // response from server failed%0A                                Log.e(TAG, %22No connection to server!%22);%0A                                observable.onError(new Throwable(%22No connection to server!%22));%0A                            %7D%0A                        %7D);%0A
+back%3C%3E(observable));
 %0A   

Use RetrofitCallback in HTTPContactsProvider.java methods