|
package com.example.observablebinding.core.ui; |
|
|
|
import android.content.Context; |
|
import android.databinding.DataBindingUtil; |
|
import android.databinding.Observable; |
|
import android.databinding.ViewDataBinding; |
|
import android.os.Parcelable; |
|
import android.util.AttributeSet; |
|
import android.widget.RelativeLayout; |
|
|
|
import com.example.observablebinding.BR; |
|
|
|
public abstract class BoundRelativeLayout<T extends BasePresenter, VM extends Observable> |
|
extends RelativeLayout { |
|
|
|
protected T presenter; |
|
protected VM viewModel; |
|
|
|
public BoundRelativeLayout(Context context, AttributeSet attrs) { |
|
super(context, attrs); |
|
} |
|
|
|
public abstract VM getViewModel(); |
|
|
|
@Override |
|
protected void onFinishInflate() { |
|
super.onFinishInflate(); |
|
presenter = getPresenter(); |
|
} |
|
|
|
@Override |
|
protected void onAttachedToWindow() { |
|
super.onAttachedToWindow(); |
|
ViewDataBinding binding = DataBindingUtil.bind(this); |
|
binding.setVariable(BR.listener, this); |
|
binding.setVariable(BR.viewModel, getViewModel()); |
|
if (presenter == null) return; |
|
presenter.onAttach(getViewModel()); |
|
} |
|
|
|
@Override |
|
protected void onDetachedFromWindow() { |
|
super.onDetachedFromWindow(); |
|
if (presenter == null) return; |
|
presenter.onDetach(); |
|
} |
|
|
|
@Override |
|
protected Parcelable onSaveInstanceState() { |
|
Parcelable superState = super.onSaveInstanceState(); |
|
BaseSavedState state = presenter == null ? null : presenter.saveState(superState); |
|
return state != null ? state : superState; |
|
} |
|
|
|
@Override |
|
protected void onRestoreInstanceState(Parcelable state) { |
|
if(!(state instanceof BaseSavedState)) { |
|
super.onRestoreInstanceState(state); |
|
return; |
|
} |
|
|
|
BaseSavedState ss = (BaseSavedState)state; |
|
super.onRestoreInstanceState(ss.getSuperState()); |
|
//end |
|
if (presenter == null) return; |
|
presenter.restoreState(getViewModel(), ss); |
|
} |
|
|
|
} |