/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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 com.pyamsoft.pydroid.version;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.pyamsoft.pydroid.presenter.PresenterBase;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

class VersionCheckPresenterImpl extends PresenterBase<VersionCheckPresenter.View>
    implements VersionCheckPresenter {

  @NonNull private final VersionCheckInteractor interactor;
  @Nullable private Call<VersionCheckResponse> call;

  VersionCheckPresenterImpl(@NonNull VersionCheckInteractor interactor) {
    this.interactor = interactor;
  }

  @Override protected void onUnbind() {
    super.onUnbind();
    cancelCall();
  }

  @Override public void checkForUpdates(int currentVersionCode) {
    cancelCall();
    call = interactor.checkVersion();
    call.enqueue(new Callback<VersionCheckResponse>() {
      @Override public void onResponse(Call<VersionCheckResponse> call,
          Response<VersionCheckResponse> response) {
        final VersionCheckResponse versionCheckResponse = response.body();
        Timber.i("Update check finished");
        Timber.i("Current version: %d", currentVersionCode);
        Timber.i("Latest version: %d", versionCheckResponse.currentVersion());
        getView(View::onVersionCheckFinished);
        if (currentVersionCode < versionCheckResponse.currentVersion()) {
          getView(view -> view.onUpdatedVersionFound(currentVersionCode,
              versionCheckResponse.currentVersion()));
          cancelCall();
        }
      }

      @Override public void onFailure(Call<VersionCheckResponse> call, Throwable t) {
        Timber.e(t, "onError checkForUpdates");
        cancelCall();
      }
    });
  }

  @SuppressWarnings("WeakerAccess") void cancelCall() {
    if (call != null && !call.isCanceled()) {
      call.cancel();
    }
  }
}

@@ -1614,24 +1614,65 @@
 response) %7B%0A
+        if (response.isSuccessful()) %7B%0A  
         fina
@@ -1734,32 +1734,34 @@
 body();%0A        
+ 
+ 
 Timber.i(%22Update
@@ -1775,24 +1775,26 @@
 finished%22);%0A
+  
         Timb
@@ -1842,16 +1842,18 @@
 nCode);%0A
+  
         
@@ -1923,32 +1923,34 @@
 ion());%0A        
+  
 getView(View::on
@@ -1972,16 +1972,18 @@
 ished);%0A
+  
         
@@ -2044,24 +2044,26 @@
 ersion()) %7B%0A
+  
           ge
@@ -2137,16 +2137,18 @@
         
+  
 versionC
@@ -2176,24 +2176,155 @@
 ersion()));%0A
+            cancelCall();%0A          %7D%0A        %7D else %7B%0A          Timber.w(%22onResponse: Not successful CODE: %25d%22, response.code());%0A
           ca

Make sure response is succcessful before checking