Newer
Older
RxFlux / app / src / main / java / j4ckall / rxflux / ui / MainActivity.java
package j4ckall.rxflux.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import j4ckall.rxflux.R;

public class MainActivity extends AppCompatActivity {

    private MainStore mainStore = new MainStore();
    private MainActionCreater mainActionCreater = new MainActionCreater();
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    private TextView countText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        countText = findViewById(R.id.text_count);

        findViewById(R.id.btn_plus).setOnClickListener(__ -> {
            mainActionCreater.countUp(1);
        });

        findViewById(R.id.btn_minus).setOnClickListener(__ -> {
            mainActionCreater.countDown(1);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        compositeDisposable.add(
                mainStore.getCount().observeOn(AndroidSchedulers.mainThread()).subscribe(count -> countText.setText(count.toString()))
        );
        mainActionCreater.initialize(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
    }
}