/*
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.plaidapp.data.api.dribbble;

import android.content.Context;

import java.util.List;

import io.plaidapp.data.PaginatedDataManager;
import io.plaidapp.data.api.dribbble.model.Like;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Loads the dribbble players who like a given shot.
 */
public abstract class ShotLikesDataManager extends PaginatedDataManager<List<Like>> {

    private final long shotId;
    private Call shotLikesCall;

    public ShotLikesDataManager(Context context, long shotId) {
        super(context);
        this.shotId = shotId;
    }

    @Override
    public void cancelLoading() {
        if (shotLikesCall != null) shotLikesCall.cancel();
    }

    @Override
    protected void loadData(int page) {
        shotLikesCall = getDribbbleApi()
                .getShotLikes(shotId, page, DribbbleService.PER_PAGE_DEFAULT);
        shotLikesCall.enqueue(new Callback<List<Like>>() {

            @Override
            public void onResponse(Call<List<Like>> call, Response<List<Like>> response) {
                loadFinished();
                final List<Like> likes = response.body();
                moreDataAvailable = likes.size() == DribbbleService.PER_PAGE_DEFAULT;
                onDataLoaded(likes);
                shotLikesCall = null;
            }

            @Override
            public void onFailure(Call<List<Like>> call, Throwable t) {
                loadFinished();
                moreDataAvailable = false;
                shotLikesCall = null;
            }
        });
    }
}

@@ -1654,24 +1654,75 @@
 response) %7B%0A
+                if (response.isSuccessful()) %7B%0A    
             
@@ -1733,32 +1733,36 @@
 loadFinished();%0A
+    
                 
@@ -1811,32 +1811,36 @@
                 
+    
 moreDataAvailabl
@@ -1909,16 +1909,20 @@
         
+    
 onDataLo
@@ -1934,16 +1934,20 @@
 likes);%0A
+    
         
@@ -1968,32 +1968,106 @@
 kesCall = null;%0A
+                %7D else %7B%0A                    failure();%0A                %7D%0A
             %7D%0A%0A 
@@ -2155,24 +2155,103 @@
 owable t) %7B%0A
+                failure();%0A            %7D%0A%0A            private void failure() %7B%0A
             

Check response status before reading body