跟着大佬学习了一下响应式编程,跟着敲了一下第三方登录的封装运用到自己项目中,顺便做一下笔记,加深理解。
用过rxpermissions的并看过源码可知,他是启动了一个透明的Fragment,而我们是启动一个透明的Activity
步入正题 看一下使用
RxLogin.create(MainActivity.this)
.doOauthVerify(RxLoginPlatform.Platform_QQ)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(RxLoginResult rxLoginResult) throws Exception {
Toast.makeText(MainActivity.this, rxLoginResult.getMsg(), Toast.LENGTH_LONG).show();
}
});
是不是很方便,不用再写那么多回调代码
1.创建RxLogin
public class RxLogin implements UMAuthListener {
private Activity activity;
private RxLogin(Activity activity){
this.activity = activity;
}
public static RxLogin create(Activity activity){
return new RxLogin(activity);
}
}
创建完构造之后,doOauthVerify()方法,该方法需要传入平台类型自定义一个枚举RxLoginPlatform
public enum RxLoginPlatform {
Platform_QQ,Platform_WX
}
doOauthVerify()方法启动一个透明的Acitivty,并传入平台类型
public Observable doOauthVerify(RxLoginPlatform platform) {
// 开启一个透明的 Activity ,参照 RxPermission 源码
Intent intent = new Intent(activity,RxLoginActivity.class);
intent.putExtra(RxLoginActivity.PLATFORM_KEY,platform);
activity.startActivity(intent);
//设置透明
activity.overridePendingTransition(0,0);
// 设置平台
mRxLoginResult.setPlatform(platform);
List> list = new ArrayList<>(1);
list.add(mEmitter);
return Observable.concat(Observable.fromIterable(list));
}
构造添加创建mEmitter ,RxLoginResult
private Activity activity;
private RxLoginResult mRxLoginResult;
static UMAuthListener STATIC_LISTENER;
private PublishSubject mEmitter;
private RxLogin(Activity activity){
this.activity = activity;
//监听暴露出去,方便Activity调用
STATIC_LISTENER = this;
mRxLoginResult = new RxLoginResult();
mEmitter = PublishSubject.create();
}
RxLoginResult是存储信息的工具类。如;用户信息、是否成功、传入平台信息
public class RxLoginResult {
// 是否成功
private boolean isSucceed;
private String msg;
private Map userInfoMaps;
private RxLoginPlatform platform;
public boolean isSucceed() {
return isSucceed;
}
public void setSucceed(boolean succeed) {
isSucceed = succeed;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map getUserInfoMaps() {
return userInfoMaps;
}
public void setUserInfoMaps(Map userInfoMaps) {
this.userInfoMaps = userInfoMaps;
}
public RxLoginPlatform getPlatform() {
return platform;
}
public void setPlatform(RxLoginPlatform platform) {
this.platform = platform;
}
}
RxLogin还要实现分享监听UMAuthListener接口,方便回调
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map map) {
mRxLoginResult.setSucceed(true);
mRxLoginResult.setUserInfoMaps(map);
mRxLoginResult.setMsg("获取用户信息成功");
// 拿到信息要回调回去 Activity -> Consumer -> accept
mEmitter.onNext(mRxLoginResult);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
mRxLoginResult.setSucceed(false);
mEmitter.onNext(mRxLoginResult);
mRxLoginResult.setMsg("获取用户信息失败");
throwable.printStackTrace();
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
mRxLoginResult.setSucceed(false);
mRxLoginResult.setMsg("用户取消第三方登录");
mEmitter.onNext(mRxLoginResult);
}
2.RxLoginActivity创建就和普通一样了,很简单
public class RxLoginActivity extends Activity implements UMAuthListener {
public static final String PLATFORM_KEY = "PLATFORM_KEY";
private UMShareAPI mUmShareAPI;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUmShareAPI = UMShareAPI.get(this);
RxLoginPlatform platform = (RxLoginPlatform) getIntent().getSerializableExtra(PLATFORM_KEY);
// mUmShareAPI.deleteOauth(this,platformChange(platform),this);
mUmShareAPI.getPlatformInfo(this, platformChange(platform), this);
}
/**
* 平台转换
* @param platform
* @return
*/
private SHARE_MEDIA platformChange(RxLoginPlatform platform) {
switch (platform) {
case Platform_QQ:
return SHARE_MEDIA.QQ;
case Platform_WX:
return SHARE_MEDIA.WEIXIN;
}
return SHARE_MEDIA.QQ;
}
@Override
public void onStart(SHARE_MEDIA share_media) {
RxLogin.STATIC_LISTENER.onStart(share_media);
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map map) {
// 结果要传回去
RxLogin.STATIC_LISTENER.onComplete(share_media,i,map);
finish();
overridePendingTransition(0,0);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
RxLogin.STATIC_LISTENER.onError(share_media,i,throwable);
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
RxLogin.STATIC_LISTENER.onCancel(share_media,i);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
}
看到上面代码封装简单,但是不看rxpermissions源码想不起来怎么把第三方登录,分享,支付抽取出来。
一个集成支付宝微信的支付工具完全兼容kotlin