Last active
December 13, 2016 07:15
-
-
Save rohmanhakim/634948053722c5c335faab088c3d28e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rohmanhakim.androidreactivedemo; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.jakewharton.rxbinding.view.RxView; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import rx.Observable; | |
import rx.Observer; | |
import rx.Subscriber; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.functions.Action1; | |
import rx.functions.Func0; | |
import rx.functions.Func1; | |
import rx.functions.Func2; | |
import rx.schedulers.Schedulers; | |
import rx.subjects.PublishSubject; | |
public class MultipleTapDemoActivity extends AppCompatActivity { | |
int taps = 0; | |
TextView textCount; | |
TextView textTotal; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_multiple_click_demo); | |
getSupportActionBar().setTitle("Multiple Click Demo"); | |
final Button btnTap = (Button) findViewById(R.id.btn_tap); | |
textCount = (TextView) findViewById(R.id.text_count); | |
textTotal = (TextView) findViewById(R.id.text_total); | |
Observable<Void> tapStream = RxView.clicks(btnTap).share(); | |
Observable<Integer> multipleTapCountStream = tapStream.buffer(tapStream | |
.debounce(1,TimeUnit.SECONDS)) | |
.map(new Func1<List<Void>, Integer>() { | |
@Override | |
public Integer call(List<Void> voids) { | |
Log.d("multipleTapTotal map",String.valueOf(voids.size())); | |
return voids.size(); | |
} | |
}) | |
.filter(new Func1<Integer, Boolean>() { | |
@Override | |
public Boolean call(Integer integer) { | |
Log.d("multipleTapTotal filter",String.valueOf(integer)); | |
return integer > 2; | |
} | |
}); | |
multipleTapCountStream.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Observer<Integer>() { | |
@Override | |
public void onCompleted() { | |
Log.d("multipleTapTotalStream","Tap Stream Total Completed"); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Log.d("multipleTapTotalStream",e.getMessage()); | |
} | |
@Override | |
public void onNext(Integer integer) { | |
taps += integer; | |
textTotal.setText(String.valueOf(taps)); | |
showTapCount(integer); | |
} | |
}); | |
} | |
private void showTapCount(int size) { | |
textCount.setText(String.valueOf(size) + " x Taps"); | |
textCount.setVisibility(View.VISIBLE); | |
textCount.setScaleX(1f); | |
textCount.setScaleY(1f); | |
ViewCompat.animate(textCount) | |
.scaleXBy(-1f) | |
.scaleYBy(-1f) | |
.setDuration(800) | |
.setStartDelay(100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment