package com.zhaohaoting.framework.abs.presenter;

import android.arch.lifecycle.LifecycleOwner;

import com.zhaohaoting.framework.mvchelper.task.TaskHelper;
import com.zhaohaoting.framework.abs.contract.BaseContract;

import java.lang.ref.WeakReference;


/**
 * author：Created by ZHT on 2018/2/1 16:51
 * email：526309416@qq.com
 * desc：
 */
public class BasePresenter<VIEW extends BaseContract.BaseView> implements BaseContract.BasePresenter {

    private WeakReference<VIEW> view;
    protected TaskHelper taskHelper;

    public BasePresenter(VIEW view) {
        this.view = new WeakReference<>(view);
        taskHelper = view.getTaskHelper();
    }

    @Override
    public VIEW getView() {
        return view.get();
    }

    @Override
    public void onCreate(LifecycleOwner owner) {
        owner.getLifecycle().addObserver(taskHelper);
    }

    @Override
    public void onStart(LifecycleOwner owner) {

    }

    @Override
    public void onResume(LifecycleOwner owner) {

    }

    @Override
    public void onPause(LifecycleOwner owner) {

    }

    @Override
    public void onStop(LifecycleOwner owner) {

    }

    @Override
    public void onDestroy(LifecycleOwner owner) {
    }

    @Override
    public void cancel(Object task) {
        if (taskHelper != null) {
            taskHelper.cancel(task);
        }
    }
}
