package alobar.workout.features.exercise;

import android.content.res.Resources;

import javax.inject.Inject;

import alobar.util.MessageBuilder;
import alobar.util.NullObject;
import alobar.util.Numbers;
import alobar.workout.R;
import alobar.workout.data.Exercise;
import alobar.workout.db.ExerciseRepo;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.SerialDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Presenter for {@link ExerciseActivity}
 */
class ExercisePresenter {

    @Inject
    ExerciseRepo exercises;

    @Inject
    Resources resources;

    private View view = NullObject.get(View.class);
    private SerialDisposable exerciseDisposable = new SerialDisposable();
    private long exerciseId;

    @Inject
    ExercisePresenter() {
        // Default constructor for Dagger
    }

    void onStart(View view) {
        this.view = view;
    }

    void onStop() {
        this.view = NullObject.get(View.class);
        exerciseDisposable.dispose();
    }

    void setExerciseId(final long id) {
        exerciseId = id;
        exerciseDisposable.set(Observable.just(id)
                .flatMap(this::loadExercise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(exercise -> {
                    view.setName(exercise.name);
                    view.setWeight(Double.toString(exercise.weight));
                }));
    }

    private Observable<Exercise> loadExercise(final long exerciseId) {
        Exercise exercise = exercises.findById(exerciseId);
        if (exercise != null)
            return Observable.just(exercise);
        else
            return Observable.empty();
    }

    void onNameChanged(String value) {
        view.setNameHint(validateName(value));
    }

    void onWeightChanged(String value) {
        view.setWeightHint(validateWeight(value));
    }

    String validateName(String value) {
        if (value == null || value.trim().length() == 0)
            return resources.getString(R.string.exerciseNameRequired);
        return null;
    }

    String validateWeight(String value) {
        if (value == null || value.trim().length() == 0)
            return resources.getString(R.string.exerciseWeightRequired);
        if (!Numbers.isNumeric(value))
            return resources.getString(R.string.exerciseWeightMustBeNumber);
        return null;
    }

    String validate(String name, String weight) {
        MessageBuilder errors = new MessageBuilder(new StringBuilder());
        errors.appendLine(validateName(name));
        errors.appendLine(validateWeight(weight));
        return errors.length() > 0 ? errors.toString() : null;
    }

    void onSave(String name, String weight) {
        String error = validate(name, weight);
        if (error != null) {
            view.toastError(error);
            return;
        }

        double weightValue = Double.parseDouble(weight);
        exercises.save(new Exercise(exerciseId, name, weightValue));
        view.close();
    }

    public interface View {
        void setName(String value);
        void setNameHint(String message);
        void setWeight(String value);
        void setWeightHint(String message);
        void toastError(String message);
        void close();
    }
}

@@ -319,25 +319,20 @@
 activex.
-Observabl
+Mayb
 e;%0Aimpor
@@ -1153,71 +1153,55 @@
 set(
-Observable.just(id)%0A                .flatMap(this::loadExercise
+Maybe.fromCallable(() -%3E exercises.findById(id)
 )%0A  
@@ -1455,32 +1455,32 @@
 rcise.weight));%0A
+
                 
@@ -1495,274 +1495,8 @@
  %7D%0A%0A
-    private Observable%3CExercise%3E loadExercise(final long exerciseId) %7B%0A        Exercise exercise = exercises.findById(exerciseId);%0A        if (exercise != null)%0A            return Observable.just(exercise);%0A        else%0A            return Observable.empty();%0A    %7D%0A%0A
     

Change to RxJava 2 Maybe