工具类
package com.ms.security.rx;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.jakewharton.rxbinding3.widget.RxTextView;
import com.ms.security.bean.Bean;
import com.ms.security.utils.LogUtils;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle3.components.support.RxFragmentActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import static android.graphics.Color.BLACK;
public class RxCommonUtils {
/**
* 倒计时秒数
*
* @param seconds 倒计时秒数
* @param view 控件
*/
public static void countDown(RxAppCompatActivity context, int seconds, Button view) {
Observable.interval(0, 1, TimeUnit.SECONDS)
.take(seconds + 1)
.map(new Function<Long, Long>() {
@Override
public Long apply(Long aLong) throws Exception {
LogUtils.E(aLong + "s--" + (seconds - aLong));
return seconds - aLong;
}
})
.compose(context.bindToLifecycle())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
LogUtils.E("disposable");
view.setEnabled(false);
view.setTextColor(Color.RED);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
LogUtils.E("Disposable");
}
@Override
public void onNext(Long aLong) {
view.setText(aLong + "s");
}
@Override
public void onError(Throwable e) {
LogUtils.E("onError--" + e.getMessage());
view.setEnabled(true);
view.setTextColor(BLACK);
view.setText("发送验证码");
}
@Override
public void onComplete() {
view.setEnabled(true);
view.setTextColor(BLACK);
view.setText("发送验证码");
}
});
}
/**
* 多少时间内
*
* @param seconds
* @param
* @return
*/
public static boolean returnTimeCount(RxFragmentActivity context, int seconds) {
final boolean[] isbool = {false};
Observable.interval(0, 1, TimeUnit.SECONDS)
.take(seconds + 1)
.map(new Function<Long, Long>() {
@Override
public Long apply(Long aLong) throws Exception {
LogUtils.E(aLong + "s--" + (seconds - aLong));
return seconds - aLong;
}
})
.compose(context.bindToLifecycle())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
LogUtils.E(aLong + "s");
if (aLong <= 0) {
isbool[0] = true;
} else {
isbool[0] = false;
}
}
});
return isbool[0];
}
/**
* 输入框隔多少毫秒
*
* @param editText
* @param milliseconds
*/
public static void editSearch(EditText editText, int milliseconds) {
RxTextView.textChanges(editText)
.debounce(milliseconds, TimeUnit.MILLISECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
/**switchMap 将Observable发射的数据集合变换为Observables集合,然后只发射这些Observables最近发射的数据*/
.switchMap(new Function<CharSequence, ObservableSource<List<String>>>() {
@Override
public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
/**根据字符charSequence大于0去 处理网络请求 并返回结果 这里用string集合表示*/
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
return Observable.just(list);
}
})
/**去请求网络 但不能保证是否是用户想要的数据 例如 输入1,12,返回可能是1搜索的数据*/
// .flatMap(new Function<CharSequence, ObservableSource<List<String>>>() {
// @Override
// public ObservableSource<List<String>> apply(CharSequence charSequence) throws Exception {
// /**根据字符charSequence大于0去 处理网络请求 并返回结果 这里用string集合表示*/
// List<String> list = new ArrayList<>();
// list.add("1");
// list.add("2");
// list.add("3");
// list.add("4");
// return Observable.just(list);
// }
// })
.observeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<String>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<String> integer) {
for (String ss : integer) {
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public static Observable<List<List<Bean>>> chooseData(Map<String, Bean> map) {
//把map转换成list
List<Bean> beans = new ArrayList<>();
for (Bean b : map.values()) {
beans.add(b);
}
return Observable.just(beans)
.flatMap(new Function<List<Bean>, ObservableSource<List<List<Bean>>>>() {
@Override
public ObservableSource<List<List<Bean>>> apply(List<Bean> beans) throws Exception {
//string字段相同存入map
Map<String, String> mapstring = new HashMap<>();
for (Bean b : beans) {
mapstring.put(b.getString(), b.getString());
}
LogUtils.E(mapstring.toString());
//组合分类数据
List<List<Bean>> listList = new ArrayList<>();
for (Map.Entry<String, String> entry : mapstring.entrySet()) {
List<Bean> lisdan = new ArrayList<>();
for (Bean b : map.values()) {
if (entry.getKey().equals(b.getString())) {
lisdan.add(b);
}
}
LogUtils.E(lisdan.toString());
listList.add(lisdan);
}
return Observable.just(listList);
}
}).observeOn(AndroidSchedulers.mainThread());
}
}