二阶段开发冲刺三
今天完成随机匹配用户的算法,给用户匹配好友,基本思路就是从bmob中随机抽取用户然后调用队友写的添加好友的方法跳转到那个页面
结果:
代码:
//随机 private void randomUser(final List<MyMeetUser> list) { //过滤掉自己 for (int i=0;i<list.size();i++) { if(list.get(i).getObjectId().equals(meUserId)) { list.remove(i); } } rxJavaParingResult(new OnRxJavaResultListener() { @Override public void rxJavaParingResult() { int r=mRandom.nextInt( list.size()); MyMeetUser myMeetUser= list.get(r); if(myMeetUser!=null) { onPairResultListener.OnPairListener(myMeetUser.getObjectId()); } } }); } //5.4 private void rxJavaParingResult(final OnRxJavaResultListener listener) { mDisposable=Observable.timer(DELAY_TIME,TimeUnit.SECONDS) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<Long>() { @Override public void accept(Long aLong) throws Exception { listener.rxJavaParingResult(); } }); }
定义一个借口
public interface OnPairResultListener { void OnPairListener(String userId); //失败 void OnPairFailListener(); }
PairFriendHelper.getInstance().setOnPairResultListener(new PairFriendHelper.OnPairResultListener() { @Override public void OnPairListener(String userId) { startUserInfo(userId); } @Override public void OnPairFailListener() { mLoadingview.hide(); Toast.makeText(getActivity(),"暂无匹配对象",Toast.LENGTH_SHORT).show(); } });