public abstract class BaseViewModel extends BaseObservable { private CompositeDisposable disposables; // private Activity activity; protected BaseViewModel() { disposables = new CompositeDisposable(); } /** * */ protected void newDisposable(Disposable disposable) { disposables.add(disposable); } /** * */ public void globalDispose() { disposables.dispose(); } protected Activity getActivity() { return activity; } public void setActivity(Activity activity) { this.activity = activity; } public boolean isSetActivity() { return (activity != null); } }
protected static <T> Observable<T> toObservable(@NonNull final ObservableField<T> observableField) { return Observable.fromPublisher(asyncEmitter -> { final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() { @Override public void onPropertyChanged(android.databinding.Observable dataBindingObservable, int propertyId) { if (dataBindingObservable == observableField) { asyncEmitter.onNext(observableField.get()); } } }; observableField.addOnPropertyChangedCallback(callback); }); }
public abstract class BaseActivity<T extends BaseViewModel> extends AppCompatActivity { private static final String DATA = "data"; // private T data; //, ViewModel @Override protected void onCreate(@Nullable Bundle savedInstanceState) { if (savedInstanceState != null) data = savedInstanceState.getParcelable(DATA); // else connectData(); // - setActivity(); // ViewModel ( Dagger) super.onCreate(savedInstanceState); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (data != null) { Log.d("my", " "); outState.putParcelable(DATA, (Parcelable) data); } } /** * onDestroy , * , . */ @Override public void onDestroy() { super.onDestroy(); Log.d("my", "onDestroy"); if (isFinishing()) destroyData(); } /** * DI. * , - preferences DB */ private void setActivity() { if (data != null) { if (!data.isSetActivity()) data.setActivity(this); } } /** * * * @return ViewModel, */ public T getData() { Log.d("my", " "); return data; } /** * ViewModel * * @param data */ public void setData(T data) { this.data = data; } /** * , Rx */ public void destroyData() { if (data != null) { data.globalDispose(); data = null; Log.d("my", " "); } } /** * , , */ protected abstract void connectData(); }
if (isFinishing())
public class ViewModel extends BaseViewModel implements Parcelable { public static final Creator<ViewModel> CREATOR = new Creator<ViewModel>() { @Override public ViewModel createFromParcel(Parcel in) { return new ViewModel(in); } @Override public ViewModel[] newArray(int size) { return new ViewModel[size]; } }; private ObservableBoolean isButtonEnabled = new ObservableBoolean(false); private ObservableField<String> count = new ObservableField<>(); private ObservableField<String> inputText = new ObservableField<>(); public ViewModel() { count.set("0"); // setInputText(); } protected ViewModel(Parcel in) { isButtonEnabled = in.readParcelable(ObservableBoolean.class.getClassLoader()); inputText = (ObservableField<String>) in.readSerializable(); count = (ObservableField<String>) in.readSerializable(); setInputText(); } private void setInputText() { newDisposable(toObservable(inputText) .debounce(2000, TimeUnit.MILLISECONDS) // .subscribeOn(Schedulers.newThread()) // .subscribe(s -> { if (s.contains("7")) isButtonEnabled.set(true); else isButtonEnabled.set(false); }, Throwable::printStackTrace)); } /** * */ public void addCount() { count.set(String.valueOf(Integer.valueOf(count.get()) + 1)); } public ObservableField<String> getInputText() { return inputText; } public ObservableField<String> getCount() { return count; } public ObservableBoolean getIsButtonEnabled() { return isButtonEnabled; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeParcelable(isButtonEnabled, flags); dest.writeSerializable(inputText); dest.writeSerializable(count); } }
private void setInputText() { newDisposable(toObservable(inputText) .debounce(2000, TimeUnit.MILLISECONDS) // .subscribeOn(Schedulers.newThread()) // .subscribe(s -> { if (s.contains("7")) isButtonEnabled.set(true); else isButtonEnabled.set(false); }, Throwable::printStackTrace)); }
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="com.quinque.aether.reactivemvvm.ViewModel"/> </data> <RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.quinque.aether.reactivemvvm.MainActivity"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint=" " android:text="@={viewModel.inputText}"/> <Button android:id="@+id/add_count_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edit_text" android:enabled="@{viewModel.isButtonEnabled}" android:onClick="@{() -> viewModel.addCount()}" android:text="+"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/add_count_button" android:layout_centerHorizontal="true" android:layout_marginTop="7dp" android:text="@={viewModel.count}"/> </RelativeLayout> </layout>
public class MainActivity extends BaseActivity<ViewModel> { ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); // view binding.setViewModel(getData()); // ViewModel, getData, } // ViewModel @Override protected void connectData() { setData(new ViewModel()); // setData } }
Source: https://habr.com/ru/post/328512/
All Articles