Created
April 16, 2015 21:05
-
-
Save osamarao/cc8bc34ec30dbf88bd06 to your computer and use it in GitHub Desktop.
RxJava for Shaanay!
This file contains 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.app.testrxandroid; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import com.app.testrxandroid.retro.Country; | |
import com.app.testrxandroid.retro.WebResponse; | |
import com.app.testrxandroid.retro.WebService; | |
import com.app.testrxandroid.retro.WebServiceFactory; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import rx.Observable; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.android.view.OnClickEvent; | |
import rx.android.view.ViewObservable; | |
import rx.android.widget.OnTextChangeEvent; | |
import rx.android.widget.WidgetObservable; | |
import rx.functions.Action1; | |
import rx.functions.Func1; | |
import rx.functions.Func3; | |
public class MainActivity extends ActionBarActivity { | |
private TextView txt1; | |
private Button btn; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
WebService webService = WebServiceFactory.getInstanceWithBasicGsonConversion(); | |
Observable<WebResponse<ArrayList<Country>>> countryListAsObservable = webService.getCountryListAsObservable(); | |
countryListAsObservable | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
.flatMap( | |
new Func1<WebResponse<ArrayList<Country>>, Observable<?>>() { | |
@Override | |
public Observable<Country> call(WebResponse<ArrayList<Country>> arrayListWebResponse) { | |
return Observable.from(arrayListWebResponse.getResult()); | |
} | |
}) | |
.cast(Country.class) | |
.filter(new Func1<Country, Boolean>() { | |
@Override | |
public Boolean call(Country country) { | |
return country.getName().split(" ").length == 1; | |
} | |
}) | |
.map(new Func1<Country, String>() { | |
@Override | |
public String call(Country country) { | |
return new String(country.getName() + " Osama "); | |
} | |
}) | |
.subscribe( | |
new Action1<String>() { | |
@Override | |
public void call(String o) { | |
Log.i("object type", o); | |
} | |
}); | |
EditText edt = (EditText) findViewById(R.id.edt); | |
EditText edt1 = (EditText) findViewById(R.id.edt1); | |
EditText edt2 = (EditText) findViewById(R.id.edt2); | |
txt1 = (TextView) findViewById(R.id.txt1); | |
btn = (Button) findViewById(R.id.btn); | |
Observable<OnTextChangeEvent> text = WidgetObservable.text(edt); | |
Observable<OnTextChangeEvent> text1 = WidgetObservable.text(edt1); | |
Observable<OnTextChangeEvent> text2 = WidgetObservable.text(edt2); | |
Observable | |
.combineLatest(text, text1, text2, | |
new Func3<OnTextChangeEvent, OnTextChangeEvent, OnTextChangeEvent, Boolean>() { | |
@Override | |
public Boolean call(OnTextChangeEvent onTextChangeEvent1, OnTextChangeEvent onTextChangeEvent2, OnTextChangeEvent onTextChangeEvent3) { | |
return onTextChangeEvent1.text().toString().trim().equalsIgnoreCase("Is") && | |
onTextChangeEvent2.text().toString().trim().equalsIgnoreCase("Thing") && | |
onTextChangeEvent3.text().toString().trim().equalsIgnoreCase("On"); | |
} | |
}) | |
.subscribe( | |
new Action1<Boolean>() { | |
@Override | |
public void call(Boolean isTextComplete) { | |
if (isTextComplete) { | |
txt1.setText("Yeah"); | |
} else { | |
txt1.setText("Not yet"); | |
} | |
} | |
}, | |
new Action1<Throwable>() { | |
@Override | |
public void call(Throwable throwable) { | |
throwable.printStackTrace(); | |
} | |
}); | |
Observable<OnClickEvent> debouncedClickObservable = ViewObservable.clicks(btn).debounce(250, TimeUnit.MILLISECONDS); | |
ViewObservable.clicks(btn) | |
.buffer(debouncedClickObservable) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Action1<List<OnClickEvent>>() { | |
@Override | |
public void call(List<OnClickEvent> onClickEvents) { | |
if (onClickEvents.size() >=2) { | |
txt1.setText("Double Click"); | |
} else if (onClickEvents.size() == 0) { | |
txt1.setText("No Click"); | |
} else { | |
txt1.setText("Single Click"); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aw man. why does this have webservice