|
|
@@ -8,16 +8,25 @@ import androidx.lifecycle.LiveData;
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
|
|
import com.android.billingclient.api.BillingClient;
|
|
|
+import com.android.billingclient.api.BillingResult;
|
|
|
import com.atmob.app.lib.base.BaseViewModel;
|
|
|
+import com.atmob.app.lib.handler.RxHttpHandler;
|
|
|
import com.atmob.app.lib.livedata.SingleLiveEvent;
|
|
|
+import com.atmob.common.logging.AtmobLog;
|
|
|
+import com.atmob.common.runtime.ActivityUtil;
|
|
|
+import com.atmob.common.runtime.ContextUtil;
|
|
|
+import com.atmob.voiceai.R;
|
|
|
import com.atmob.voiceai.data.api.bean.SplashPageBean;
|
|
|
+import com.atmob.voiceai.data.api.response.OrderPayResponse;
|
|
|
import com.atmob.voiceai.data.api.response.SplashPageResponse;
|
|
|
import com.atmob.voiceai.data.api.response.UserInfoResponse;
|
|
|
import com.atmob.voiceai.data.consts.Constants;
|
|
|
import com.atmob.voiceai.data.repositories.MemberRepository;
|
|
|
+import com.atmob.voiceai.helper.ErrorHelper;
|
|
|
import com.atmob.voiceai.sdk.billing.GPBillingClient;
|
|
|
import com.atmob.voiceai.utils.BoxingUtil;
|
|
|
import com.atmob.voiceai.utils.CommonUtils;
|
|
|
+import com.atmob.voiceai.utils.ToastUtil;
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -30,15 +39,18 @@ import atmob.reactivex.rxjava3.disposables.Disposable;
|
|
|
import atmob.reactivex.rxjava3.schedulers.Schedulers;
|
|
|
import atmob.rxjava.utils.RxJavaUtil;
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel;
|
|
|
+import kotlin.Pair;
|
|
|
|
|
|
|
|
|
@HiltViewModel
|
|
|
public class NewSplashViewModel extends BaseViewModel {
|
|
|
|
|
|
+ private final String TAG = NewSplashViewModel.class.getSimpleName();
|
|
|
private final SingleLiveEvent<?> showMainEvent = new SingleLiveEvent<>();
|
|
|
private final MutableLiveData<Boolean> isShowRecommendPage = new MutableLiveData<>(false);
|
|
|
private final MutableLiveData<SplashPageBean> splashGoods = new MutableLiveData<>();
|
|
|
private final MutableLiveData<Boolean> isShowCloseBtn = new MutableLiveData<>();
|
|
|
+ private final SingleLiveEvent<Pair<Boolean, String>> showLoading = new SingleLiveEvent<>();
|
|
|
private final MemberRepository memberRepository;
|
|
|
private final GPBillingClient gpBillingClient;
|
|
|
private long requestStartTime;
|
|
|
@@ -46,6 +58,7 @@ public class NewSplashViewModel extends BaseViewModel {
|
|
|
|
|
|
private final long MAX_NET_REQUEST_TIME = 3000;
|
|
|
private final long MAX_NET_ERROR_TIME = 8000;
|
|
|
+ private boolean isRequestSubmitOrder;
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
@@ -55,6 +68,14 @@ public class NewSplashViewModel extends BaseViewModel {
|
|
|
refreshUserInfo();
|
|
|
}
|
|
|
|
|
|
+ public LiveData<Pair<Boolean, String>> getShowLoading() {
|
|
|
+ return showLoading;
|
|
|
+ }
|
|
|
+
|
|
|
+ public LiveData<SplashPageBean> getSplashGoods() {
|
|
|
+ return splashGoods;
|
|
|
+ }
|
|
|
+
|
|
|
public LiveData<Boolean> getIsShowCloseBtn() {
|
|
|
return isShowCloseBtn;
|
|
|
}
|
|
|
@@ -69,8 +90,11 @@ public class NewSplashViewModel extends BaseViewModel {
|
|
|
.timeout(MAX_NET_ERROR_TIME, TimeUnit.MILLISECONDS)
|
|
|
.onErrorReturn(throwable -> new UserInfoResponse())
|
|
|
.map(userInfoResponse -> {
|
|
|
- refreshSplashGoods();
|
|
|
- return userInfoResponse != null && userInfoResponse.getMemberInfo() != null && userInfoResponse.getMemberInfo().isIsMember();
|
|
|
+ boolean isMember = userInfoResponse != null && userInfoResponse.getMemberInfo() != null && userInfoResponse.getMemberInfo().isIsMember();
|
|
|
+ if (!isMember) {
|
|
|
+ refreshSplashGoods();
|
|
|
+ }
|
|
|
+ return isMember;
|
|
|
})
|
|
|
.subscribe(new SingleObserver<Boolean>() {
|
|
|
@Override
|
|
|
@@ -101,6 +125,7 @@ public class NewSplashViewModel extends BaseViewModel {
|
|
|
|
|
|
|
|
|
private void refreshSplashGoods() {
|
|
|
+ gpBillingClient.startUrgentConnection();
|
|
|
memberRepository.requestSplashPage()
|
|
|
.map(SplashPageResponse::getItem)
|
|
|
.flatMap(splashPageBean ->
|
|
|
@@ -150,4 +175,126 @@ public class NewSplashViewModel extends BaseViewModel {
|
|
|
public void onBackClick() {
|
|
|
showMainEvent.call();
|
|
|
}
|
|
|
+
|
|
|
+ public void onContinueClick() {
|
|
|
+ if (isRequestSubmitOrder) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SplashPageBean splashPageBean = splashGoods.getValue();
|
|
|
+ if (splashPageBean == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ memberRepository.requestGoodsPayOrder(splashPageBean.getId(), 1, 4)
|
|
|
+ .subscribe(new SingleObserver<OrderPayResponse>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ addDisposable(d);
|
|
|
+ isRequestSubmitOrder = true;
|
|
|
+ showLoading.setValue(new Pair<>(true, null));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull OrderPayResponse orderPayResponse) {
|
|
|
+ isRequestSubmitOrder = false;
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ requestPayment(splashPageBean, orderPayResponse.getOutTradeNo());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+ isRequestSubmitOrder = false;
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ ErrorHelper.errorThrowableToast(e, ToastUtil.LENGTH_SHORT);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void requestPayment(SplashPageBean bean, String orderNo) {
|
|
|
+ gpBillingClient.launchPurchaseGoods(ActivityUtil.getTopActivity(),
|
|
|
+ bean.getGpProductInfo().getProductDetails(), bean.getPlanId(), orderNo)
|
|
|
+ .compose(RxJavaUtil.SingleSchedule.subThread2Main())
|
|
|
+ .subscribe(new SingleObserver<BillingResult>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ addDisposable(d);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull BillingResult billingResult) {
|
|
|
+ AtmobLog.d(TAG, "requestPayment onSuccess: " + billingResult);
|
|
|
+ gpBillingClient.registerFlowListener(this, orderNo, purchase -> queryOrderStatus(orderNo, purchase.getPurchaseToken()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void queryOrderStatus(String orderNo, String token) {
|
|
|
+ memberRepository.orderStatus(orderNo, token)
|
|
|
+ .subscribe(new SingleObserver<Boolean>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ addDisposable(d);
|
|
|
+ addDisposable(RxJavaUtil.timer(25, TimeUnit.SECONDS, () -> {
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ ToastUtil.show(R.string.query_order_time_out, ToastUtil.LENGTH_SHORT);
|
|
|
+ }));
|
|
|
+ showLoading.setValue(new Pair<>(true, ContextUtil.getContext().getString(R.string.query_order_status_loading_txt)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull Boolean aBoolean) {
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ if (BoxingUtil.boxing(aBoolean)) {
|
|
|
+ ToastUtil.show(R.string.pay_success, ToastUtil.LENGTH_SHORT);
|
|
|
+ showMainEvent.call();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void onRestoreClick() {
|
|
|
+ memberRepository.subscriptionResume()
|
|
|
+ .subscribe(new SingleObserver<Object>() {
|
|
|
+ @Override
|
|
|
+ public void onSubscribe(@NonNull Disposable d) {
|
|
|
+ addDisposable(d);
|
|
|
+ showLoading.setValue(new Pair<>(true, ContextUtil.getContext().getString(R.string.query_order_status_loading_txt)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(@NonNull Object object) {
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ showMainEvent.call();
|
|
|
+ ToastUtil.show(R.string.member_restore_success, ToastUtil.LENGTH_SHORT);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull Throwable e) {
|
|
|
+ showLoading.setValue(new Pair<>(false, null));
|
|
|
+ if (e instanceof RxHttpHandler.ServerErrorException) {
|
|
|
+ RxHttpHandler.ServerErrorException serverErrorException = (RxHttpHandler.ServerErrorException) e;
|
|
|
+ ToastUtil.show(serverErrorException.getMsg(), ToastUtil.LENGTH_SHORT);
|
|
|
+ } else {
|
|
|
+ ToastUtil.show(R.string.net_error_message, ToastUtil.LENGTH_SHORT);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCleared() {
|
|
|
+ super.onCleared();
|
|
|
+ gpBillingClient.endUrgentConnection();
|
|
|
+ gpBillingClient.unregisterAllFlowListener(this);
|
|
|
+ }
|
|
|
}
|