|
|
@@ -7,6 +7,7 @@ import androidx.annotation.ColorInt;
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
import androidx.lifecycle.MediatorLiveData;
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
+import androidx.lifecycle.Observer;
|
|
|
import androidx.lifecycle.Transformations;
|
|
|
|
|
|
import com.atmob.app.lib.base.BaseViewModel;
|
|
|
@@ -20,10 +21,11 @@ import com.atmob.voiceai.data.api.bean.VoiceListBean;
|
|
|
import com.atmob.voiceai.data.api.response.VoiceInfoResponse;
|
|
|
import com.atmob.voiceai.data.api.response.VoiceListResponse;
|
|
|
import com.atmob.voiceai.data.api.response.VoiceTypeResponse;
|
|
|
-import com.atmob.voiceai.data.repositories.MemberRepository;
|
|
|
+import com.atmob.voiceai.data.repositories.AccountRepository;
|
|
|
import com.atmob.voiceai.data.repositories.VoiceAIRepository;
|
|
|
import com.atmob.voiceai.helper.ErrorHelper;
|
|
|
import com.atmob.voiceai.module.generating.VoiceGeneratingActivity;
|
|
|
+import com.atmob.voiceai.module.setting.SettingActivity;
|
|
|
import com.atmob.voiceai.module.subscription.SubscriptionPageActivity;
|
|
|
import com.atmob.voiceai.utils.BoxingUtil;
|
|
|
import com.atmob.voiceai.utils.SpannableUtil;
|
|
|
@@ -45,11 +47,11 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
|
|
|
private final String LAST_GENERATE_TEXT = "last_generate_text";
|
|
|
|
|
|
- private final MediatorLiveData<CharSequence> voiceProbationTxt = new MediatorLiveData<>();
|
|
|
+ private final LiveData<CharSequence> voiceProbationTxt;
|
|
|
private final MutableLiveData<String> voicePrintTxt = new MutableLiveData<>();
|
|
|
private final LiveData<String> voicePrintLimitTxt;
|
|
|
- private final MemberRepository memberRepository;
|
|
|
- private final MutableLiveData<Integer> adFreeGenerateNumber = new MutableLiveData<>();
|
|
|
+ private final AccountRepository accountRepository;
|
|
|
+
|
|
|
@ColorInt
|
|
|
private final int freeTxtColor;
|
|
|
private final VoiceAIRepository voiceAIRepository;
|
|
|
@@ -70,9 +72,11 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
private String currentTypeId;
|
|
|
private Disposable voiceListDisposable;
|
|
|
|
|
|
+ Observer<VoiceListBean> recommendObserver;
|
|
|
+
|
|
|
@Inject
|
|
|
- public VoiceAIViewModel(MemberRepository memberRepository, VoiceAIRepository voiceAIRepository) {
|
|
|
- this.memberRepository = memberRepository;
|
|
|
+ public VoiceAIViewModel(AccountRepository accountRepository, VoiceAIRepository voiceAIRepository) {
|
|
|
+ this.accountRepository = accountRepository;
|
|
|
this.voiceAIRepository = voiceAIRepository;
|
|
|
voicePrintLimitTxt = Transformations.map(voicePrintTxt, txt -> {
|
|
|
int length = 0;
|
|
|
@@ -82,25 +86,17 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
return length + "/200";
|
|
|
});
|
|
|
freeTxtColor = ContextUtil.getApplication().getResources().getColor(R.color.voice_free_crux_txt_color);
|
|
|
-
|
|
|
- voiceProbationTxt.addSource(adFreeGenerateNumber, number -> {
|
|
|
- if (BoxingUtil.boxing(memberRepository.getIsMember().getValue())) {
|
|
|
- voiceProbationTxt.setValue("");
|
|
|
- return;
|
|
|
- }
|
|
|
- setFreeGenerateText(BoxingUtil.boxing(number));
|
|
|
- });
|
|
|
voicePrintTxt.setValue(KVUtils.getDefault().getString(LAST_GENERATE_TEXT, ""));
|
|
|
- voiceProbationTxt.addSource(memberRepository.getIsMember(), isMember -> {
|
|
|
- if (BoxingUtil.boxing(isMember)) {
|
|
|
- voiceProbationTxt.setValue("");
|
|
|
- return;
|
|
|
+ voiceProbationTxt = Transformations.map(voiceAIRepository.getAdFreeGenerateNumber(), number -> {
|
|
|
+ if (number == null || BoxingUtil.boxing(accountRepository.getIsMember().getValue())) {
|
|
|
+ return "";
|
|
|
}
|
|
|
- setFreeGenerateText(BoxingUtil.boxing(adFreeGenerateNumber.getValue()));
|
|
|
+ return SpannableUtil.getSpannableStringBuilder(ContextUtil.getContext().getString(R.string.voice_ai_get_free_number, number), String.valueOf(number), freeTxtColor, false);
|
|
|
});
|
|
|
iniVoiceAI();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public LiveData<Integer> getScrollToTabPosition() {
|
|
|
return scrollToTabPosition;
|
|
|
}
|
|
|
@@ -130,11 +126,11 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
}
|
|
|
|
|
|
public LiveData<Integer> getAdFreeGenerateNumber() {
|
|
|
- return adFreeGenerateNumber;
|
|
|
+ return voiceAIRepository.getAdFreeGenerateNumber();
|
|
|
}
|
|
|
|
|
|
public LiveData<Boolean> isMember() {
|
|
|
- return memberRepository.getIsMember();
|
|
|
+ return accountRepository.getIsMember();
|
|
|
}
|
|
|
|
|
|
public LiveData<CharSequence> getVoiceProbationTxt() {
|
|
|
@@ -153,11 +149,6 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
voicePrintTxt.setValue("");
|
|
|
}
|
|
|
|
|
|
- private void setFreeGenerateText(int number) {
|
|
|
- voiceProbationTxt.setValue(SpannableUtil.getSpannableStringBuilder(
|
|
|
- ContextUtil.getContext().getString(R.string.voice_ai_get_free_number, number),
|
|
|
- String.valueOf(number), freeTxtColor, false));
|
|
|
- }
|
|
|
|
|
|
public void onGenerateClick() {
|
|
|
String content = voicePrintTxt.getValue();
|
|
|
@@ -177,7 +168,7 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
SubscriptionPageActivity.start(ActivityUtil.getTopActivity());
|
|
|
return;
|
|
|
}
|
|
|
- if (BoxingUtil.boxing(adFreeGenerateNumber.getValue()) > 0) {
|
|
|
+ if (BoxingUtil.boxing(voiceAIRepository.getAdFreeGenerateNumber().getValue()) > 0) {
|
|
|
showGenerateAd.call();
|
|
|
return;
|
|
|
}
|
|
|
@@ -191,13 +182,15 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
|
|
|
|
|
|
private void iniVoiceAI() {
|
|
|
- refreshVoiceInfo();
|
|
|
requestVoiceTypeList();
|
|
|
fromOtherRecommendSelectVoice();
|
|
|
}
|
|
|
|
|
|
private void fromOtherRecommendSelectVoice() {
|
|
|
- voiceAIRepository.getRecommendClickBean().observeForever(bean -> {
|
|
|
+ if (recommendObserver != null) {
|
|
|
+ voiceAIRepository.getRecommendClickBean().removeObserver(recommendObserver);
|
|
|
+ }
|
|
|
+ recommendObserver = bean -> {
|
|
|
if (bean == null) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -215,7 +208,8 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
scrollToTabPosition.setValue(0);
|
|
|
refreshVoiceList(String.valueOf(typeList.get(0).getId()), bean);
|
|
|
}
|
|
|
- });
|
|
|
+ };
|
|
|
+ voiceAIRepository.getRecommendClickBean().observeForever(recommendObserver);
|
|
|
}
|
|
|
|
|
|
public void scrollToListPosition(@NonNull VoiceListBean bean) {
|
|
|
@@ -237,26 +231,6 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private void refreshVoiceInfo() {
|
|
|
- voiceAIRepository.requestVoiceInfo().subscribe(new SingleObserver<VoiceInfoResponse>() {
|
|
|
- @Override
|
|
|
- public void onSubscribe(@NonNull Disposable d) {
|
|
|
- addDisposable(d);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onSuccess(@NonNull VoiceInfoResponse voiceInfoResponse) {
|
|
|
- adFreeGenerateNumber.setValue(voiceInfoResponse.getVoiceTimes());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onError(@NonNull Throwable e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
private void requestVoiceTypeList() {
|
|
|
voiceAIRepository.requestVoiceType().subscribe(new SingleObserver<VoiceTypeResponse>() {
|
|
|
@Override
|
|
|
@@ -366,6 +340,21 @@ public class VoiceAIViewModel extends BaseViewModel {
|
|
|
}
|
|
|
|
|
|
public void onSettingClick() {
|
|
|
+ SettingActivity.start(ActivityUtil.getTopActivity());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void onGoSubscriptionClick() {
|
|
|
+ SubscriptionPageActivity.start(ActivityUtil.getTopActivity());
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ protected void onCleared() {
|
|
|
+ super.onCleared();
|
|
|
+ if (recommendObserver != null) {
|
|
|
+ voiceAIRepository.getRecommendClickBean().removeObserver(recommendObserver);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|