/*
 * 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.pasterino.dagger.service;

import android.support.annotation.NonNull;
import com.pyamsoft.pasterino.app.service.SinglePastePresenter;
import com.pyamsoft.pydroid.presenter.PresenterBase;
import com.pyamsoft.pydroid.tool.ExecutedOffloader;
import timber.log.Timber;

class SinglePastePresenterImpl extends PresenterBase<SinglePastePresenter.SinglePasteProvider>
    implements SinglePastePresenter {

  @SuppressWarnings("WeakerAccess") @NonNull final PasteServiceInteractor interactor;
  @NonNull private ExecutedOffloader pasteTime = new ExecutedOffloader.Empty();

  SinglePastePresenterImpl(@NonNull PasteServiceInteractor interactor) {
    this.interactor = interactor;
  }

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

  @Override public void onPostDelayedEvent() {
    unsubPasteTime();
    pasteTime = interactor.getPasteDelayTime()
        .onError(throwable -> Timber.e(throwable, "onError onPostDelayedEvent"))
        .onResult(delay -> getView(view -> view.postDelayedEvent(delay)))
        .execute();
  }

  private void unsubPasteTime() {
    if (!pasteTime.isCancelled()) {
      pasteTime.cancel();
    }
  }
}

@@ -854,24 +854,74 @@
 dOffloader;%0A
+import com.pyamsoft.pydroid.tool.OffloaderHelper;%0A
 import timbe
@@ -1410,38 +1410,56 @@
 bind();%0A    
-unsubP
+OffloaderHelper.cancel(p
 asteTime
 ();%0A  %7D%0A%0A  @
@@ -1442,25 +1442,24 @@
 el(pasteTime
-(
 );%0A  %7D%0A%0A  @O
@@ -1505,30 +1505,48 @@
 ) %7B%0A    
-unsubP
+OffloaderHelper.cancel(p
 asteTime
 ();%0A    
@@ -1537,17 +1537,16 @@
 asteTime
-(
 );%0A    p
@@ -1755,125 +1755,77 @@
    .
-execute();%0A  %7D%0A%0A  private void unsubPasteTime() %7B%0A    if (!pasteTime.isCancelled()) %7B%0A      pasteTime.cancel();%0A    %7D
+onFinish(() -%3E OffloaderHelper.cancel(pasteTime))%0A        .execute();
 %0A  %7D

Use helper