Skip to content

Commit

Permalink
listen to AccountConfig model instead of isLoaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdelmjid-ASOUAB committed Jan 21, 2022
1 parent 1c3a74e commit feff284
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
15 changes: 7 additions & 8 deletions YoucanPaySDK/src/main/java/com/youcanPay/YCPay.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.youcanPay.networking.YCPayRetrofitHTTPAdapter;
import com.youcanPay.services.YCPayApiService;
import com.youcanPay.services.YCPayCashPlusService;
import com.youcanPay.services.YCPayGetConfigService;
import com.youcanPay.services.YCPayConfigService;

import static com.youcanPay.utils.YCPayLocale.setLocale;

Expand All @@ -22,7 +22,7 @@ public class YCPay {
private final Context context;
private YCPayApiService apiService = new YCPayApiService(new YCPayRetrofitHTTPAdapter());
private YCPayCashPlusService cashPushService = new YCPayCashPlusService(new YCPayRetrofitHTTPAdapter());
private YCPayGetConfigService ycPayGetConfigService = new YCPayGetConfigService(new YCPayRetrofitHTTPAdapter());
private YCPayConfigService ycPayGetConfigService = new YCPayConfigService(new YCPayRetrofitHTTPAdapter());
private LiveData<Boolean> isConfigLoaded;

public YCPay(
Expand Down Expand Up @@ -59,7 +59,7 @@ public void payWithCard(
YCPayCardInformation cardInformation,
PayCallbackImpl payCallBack
) throws YCPayInvalidArgumentException, NullPointerException, YCPayInvalidPaymentMethodException {
if (!this.ycPayGetConfigService.ycPayAccountConfig.isAcceptsCashPlus()) {
if (!this.ycPayGetConfigService.accountConfig.isAcceptsCashPlus()) {
throw new YCPayInvalidPaymentMethodException("Payment with Credit Card is Invalid for your account");
}

Expand All @@ -76,7 +76,7 @@ public void payWithCashPlus(
String tokenId,
CashPlusCallbackImpl cashPlusCallback
) throws YCPayInvalidArgumentException, NullPointerException, YCPayInvalidPaymentMethodException {
if (!this.ycPayGetConfigService.ycPayAccountConfig.isAcceptsCashPlus()) {
if (!this.ycPayGetConfigService.accountConfig.isAcceptsCashPlus()) {
throw new YCPayInvalidPaymentMethodException("Payment with CashPlus is Invalid for your account");
}

Expand All @@ -89,18 +89,17 @@ public void payWithCashPlus(

private void loadAccountConfig() {
try {
this.isConfigLoaded = this.ycPayGetConfigService.getIsLoaded();
this.ycPayGetConfigService.getAccountConfig(pubKey);
} catch (YCPayInvalidArgumentException e) {
e.printStackTrace();
}
}

public LiveData<Boolean> getIsConfigLoaded() {
return this.isConfigLoaded;
public LiveData<YCPayAccountConfig> getAccountConfigLiveData() {
return this.ycPayGetConfigService.getAccountConfig();
}

public YCPayAccountConfig getAccountConfig() {
return this.ycPayGetConfigService.ycPayAccountConfig;
return this.ycPayGetConfigService.accountConfig;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.youcanPay.services;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;

import com.youcanPay.exception.YCPayInvalidArgumentException;
Expand All @@ -20,15 +19,15 @@
import static com.youcanPay.config.YCPayConfig.API_URL_SANDBOX;
import static com.youcanPay.utils.YCPayLocale.getLocale;

public class YCPayGetConfigService implements HttpCallBackImpl {
public class YCPayConfigService implements HttpCallBackImpl {
private final YCPayHTTPAdapter httpAdapter;
public YCPayAccountConfig ycPayAccountConfig = new YCPayAccountConfig();
private MutableLiveData<Boolean> isLoaded;
public MutableLiveData<YCPayAccountConfig> ycPayAccountConfigLiveData;
public YCPayAccountConfig accountConfig = new YCPayAccountConfig();

public YCPayGetConfigService(YCPayHTTPAdapter httpAdapter) {
public YCPayConfigService(YCPayHTTPAdapter httpAdapter) {
this.httpAdapter = httpAdapter;
this.httpAdapter.setHttpCallback(this);
this.isLoaded = new MutableLiveData<>();
this.ycPayAccountConfigLiveData = new MutableLiveData<>();
}

public void getAccountConfig(String pubKey) throws YCPayInvalidArgumentException {
Expand All @@ -46,23 +45,26 @@ private String getEndpoint(String path) {
return httpAdapter.getSandboxMode() ? API_URL_SANDBOX + path : API_URL + path;
}

public LiveData<Boolean> getIsLoaded() {
return this.isLoaded;
public LiveData<YCPayAccountConfig> getAccountConfig() {
return this.ycPayAccountConfigLiveData;
}

@Override
public void onResponse(HttpResponse response) {
this.isLoaded.postValue(true);
try {
this.ycPayAccountConfig = YCPAccountConfigFactory.getResponse(response.getBody());
accountConfig = YCPAccountConfigFactory.getResponse(response.getBody());
this.ycPayAccountConfigLiveData.postValue(accountConfig);
} catch (JSONException | YCPayInvalidResponseException e) {
e.printStackTrace();
accountConfig.setMessage(e.getMessage());
this.onError(e.getMessage());
}
}

@Override
public void onError(String message) {
this.isLoaded.postValue(true);
YCPayAccountConfig accountConfig = new YCPayAccountConfig();
accountConfig.setMessage(message);
this.ycPayAccountConfigLiveData.postValue(accountConfig);
}

}

0 comments on commit feff284

Please sign in to comment.