Browse Source

结果页增加点击返回首页自动选中功能

zk 1 year ago
parent
commit
7f61e55556

+ 23 - 2
app/src/main/java/com/atmob/voiceai/module/voiceai/VoiceAIFragment.java

@@ -22,7 +22,9 @@ import com.atmob.voiceai.databinding.FragmentVoiceAiBinding;
 import com.atmob.voiceai.databinding.ItemVoiceAiTabBinding;
 import com.atmob.voiceai.module.clonevoice.CloneVoiceFragment;
 import com.atmob.voiceai.module.main.MainActivity;
+import com.atmob.voiceai.utils.BoxingUtil;
 import com.atmob.voiceai.utils.GridLayoutItemDecoration;
+import com.atmob.voiceai.utils.SmoothScrollGridLayoutManager;
 import com.atmob.voiceai.utils.ToastUtil;
 import com.google.android.material.tabs.TabLayout;
 import com.gyf.immersionbar.ImmersionBar;
@@ -47,12 +49,31 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
     }
 
     private void initObserver() {
+        voiceViewModel.getScrollToBean().observe(getViewLifecycleOwner(), bean -> {
+            if (bean != null) {
+                int beanPosition = voiceAIListAdapter.getBeanPosition(bean);
+                if (beanPosition != -1) {
+                    binding.ryVoiceView.smoothScrollToPosition(beanPosition);
+                }
+            }
+        });
+        voiceViewModel.getNotifyBean().observe(getViewLifecycleOwner(), bean -> {
+            if (bean == null) {
+                return;
+            }
+            voiceAIListAdapter.notify(bean);
+        });
+        voiceViewModel.getScrollToTabPosition().observe(getViewLifecycleOwner(), index -> binding.voiceTab.selectTab(binding.voiceTab.getTabAt(BoxingUtil.boxing(index))));
         voiceViewModel.getPlayExampleVoice().observe(getViewLifecycleOwner(), this::playExampleVoice);
         voiceViewModel.getShowGenerateAd().observe(getViewLifecycleOwner(), o -> {
             //TODO 显示广告
             voiceViewModel.seeAdGenerate();
         });
-        voiceViewModel.getVoiceDetailList().observe(getViewLifecycleOwner(), list -> voiceAIListAdapter.submit(list)
+        voiceViewModel.getVoiceDetailList().observe(getViewLifecycleOwner(), list -> voiceAIListAdapter.submit(list.first, () -> {
+                    if (list.second != null) {
+                        voiceViewModel.scrollToListPosition(list.second);
+                    }
+                })
         );
         voiceViewModel.getVoiceTypeList().observe(getViewLifecycleOwner(), list -> {
             if (list == null || list.isEmpty()) {
@@ -102,7 +123,7 @@ public class VoiceAIFragment extends BaseFragment<FragmentVoiceAiBinding> {
             }
         });
         binding.ryVoiceView.setAdapter(voiceAIListAdapter);
-        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
+        SmoothScrollGridLayoutManager gridLayoutManager = new SmoothScrollGridLayoutManager(getContext(), 4);
         binding.ryVoiceView.setLayoutManager(gridLayoutManager);
         GridLayoutItemDecoration gridLayoutItemDecoration = new GridLayoutItemDecoration(4, 0.0222222222222222f, 0.0222222222222222f, false);
         binding.ryVoiceView.addItemDecoration(gridLayoutItemDecoration);

+ 15 - 2
app/src/main/java/com/atmob/voiceai/module/voiceai/VoiceAIListAdapter.java

@@ -70,6 +70,19 @@ public class VoiceAIListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
         });
     }
 
+    public int getBeanPosition(@NonNull VoiceListBean bean) {
+        return listDiffer.getCurrentList().indexOf(bean);
+    }
+
+    public void notify(@NonNull VoiceListBean bean) {
+        int position = listDiffer.getCurrentList().indexOf(bean);
+        if (position == -1) {
+            return;
+        }
+        notifyItemChanged(position);
+    }
+
+
     @NonNull
     @Override
     public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@@ -91,11 +104,11 @@ public class VoiceAIListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
         }
     }
 
-    public void submit(@NonNull List<VoiceListBean> itemBeanList) {
+    public void submit(@NonNull List<VoiceListBean> itemBeanList, Runnable runnable) {
         VoiceListBean voiceListBean = new VoiceListBean();
         voiceListBean.setViewType(TYPE_FOOT);
         itemBeanList.add(voiceListBean);
-        listDiffer.submitList(itemBeanList);
+        listDiffer.submitList(itemBeanList, runnable);
     }
 
     @Override

+ 76 - 18
app/src/main/java/com/atmob/voiceai/module/voiceai/VoiceAIViewModel.java

@@ -1,6 +1,7 @@
 package com.atmob.voiceai.module.voiceai;
 
 import android.text.TextUtils;
+import android.util.Pair;
 
 import androidx.annotation.ColorInt;
 import androidx.lifecycle.LiveData;
@@ -14,7 +15,6 @@ import com.atmob.common.data.KVUtils;
 import com.atmob.common.runtime.ActivityUtil;
 import com.atmob.common.runtime.ContextUtil;
 import com.atmob.voiceai.R;
-import com.atmob.voiceai.data.api.bean.CloneVoiceListBean;
 import com.atmob.voiceai.data.api.bean.TypeListBean;
 import com.atmob.voiceai.data.api.bean.VoiceListBean;
 import com.atmob.voiceai.data.api.response.VoiceInfoResponse;
@@ -56,12 +56,16 @@ public class VoiceAIViewModel extends BaseViewModel {
 
 
     private final MutableLiveData<List<TypeListBean>> voiceTypeList = new MutableLiveData<>();
-    private final MutableLiveData<List<VoiceListBean>> voiceDetailList = new MutableLiveData<>();
+    private final MutableLiveData<Pair<List<VoiceListBean>, VoiceListBean>> voiceDetailList = new MutableLiveData<>();
 
     private VoiceListBean choiceVoiceBean;
-    private final SingleLiveEvent<VoiceListBean> playExampleVoice = new SingleLiveEvent<>();
 
+    private final SingleLiveEvent<VoiceListBean> scrollToBean = new SingleLiveEvent<>();
+
+    private final SingleLiveEvent<VoiceListBean> playExampleVoice = new SingleLiveEvent<>();
+    private final SingleLiveEvent<Integer> scrollToTabPosition = new SingleLiveEvent<>();
     private final SingleLiveEvent<?> showGenerateAd = new SingleLiveEvent<>();
+    private final SingleLiveEvent<VoiceListBean> notifyBean = new SingleLiveEvent<>();
 
     private String currentTypeId;
     private Disposable voiceListDisposable;
@@ -97,6 +101,14 @@ public class VoiceAIViewModel extends BaseViewModel {
         iniVoiceAI();
     }
 
+    public LiveData<Integer> getScrollToTabPosition() {
+        return scrollToTabPosition;
+    }
+
+    public LiveData<VoiceListBean> getNotifyBean() {
+        return notifyBean;
+    }
+
     public LiveData<VoiceListBean> getPlayExampleVoice() {
         return playExampleVoice;
     }
@@ -105,7 +117,7 @@ public class VoiceAIViewModel extends BaseViewModel {
         return showGenerateAd;
     }
 
-    public LiveData<List<VoiceListBean>> getVoiceDetailList() {
+    public LiveData<Pair<List<VoiceListBean>, VoiceListBean>> getVoiceDetailList() {
         return voiceDetailList;
     }
 
@@ -113,6 +125,9 @@ public class VoiceAIViewModel extends BaseViewModel {
         return voiceTypeList;
     }
 
+    public LiveData<VoiceListBean> getScrollToBean() {
+        return scrollToBean;
+    }
 
     public LiveData<Integer> getAdFreeGenerateNumber() {
         return adFreeGenerateNumber;
@@ -178,8 +193,50 @@ public class VoiceAIViewModel extends BaseViewModel {
     private void iniVoiceAI() {
         refreshVoiceInfo();
         requestVoiceTypeList();
+        fromOtherRecommendSelectVoice();
+    }
+
+    private void fromOtherRecommendSelectVoice() {
+        voiceAIRepository.getRecommendClickBean().observeForever(bean -> {
+            if (bean == null) {
+                return;
+            }
+            voiceAIRepository.setRecommendClickBean(null);
+            voicePrintTxt.setValue(bean.getContent());
+            List<TypeListBean> typeList = voiceTypeList.getValue();
+            if (typeList == null || typeList.isEmpty()) {
+                return;
+            }
+            if (Objects.equals(this.currentTypeId, String.valueOf(bean.getTypeId())) || Objects.equals(String.valueOf(typeList.get(0).getId()), this.currentTypeId)) {
+                //在当前列表项或当前列表项为第一个(一般为ALL)
+                scrollToListPosition(bean);
+            } else {
+                //将列表项设置为第一个
+                scrollToTabPosition.setValue(0);
+                refreshVoiceList(String.valueOf(typeList.get(0).getId()), bean);
+            }
+        });
+    }
+
+    public void scrollToListPosition(@NonNull VoiceListBean bean) {
+        Pair<List<VoiceListBean>, VoiceListBean> voiceDetail = voiceDetailList.getValue();
+        if (voiceDetail == null || voiceDetail.first == null) {
+            return;
+        }
+        for (VoiceListBean voiceListBean : voiceDetail.first) {
+            if (TextUtils.isEmpty(voiceListBean.getVoiceUrl())) {
+                continue;
+            }
+            if (voiceListBean.getId() == bean.getId()) {
+                scrollToBean.setValue(voiceListBean);
+                setChoiceVoiceBean(voiceListBean, true);
+                notifyBean.setValue(voiceListBean);
+                break;
+            }
+        }
     }
 
+
     private void refreshVoiceInfo() {
         voiceAIRepository.requestVoiceInfo().subscribe(new SingleObserver<VoiceInfoResponse>() {
             @Override
@@ -223,7 +280,12 @@ public class VoiceAIViewModel extends BaseViewModel {
         KVUtils.getDefault().putString(LAST_GENERATE_TEXT, generateTxt);
     }
 
+
     public void refreshVoiceList(String id) {
+        refreshVoiceList(id, null);
+    }
+
+    public void refreshVoiceList(String id, VoiceListBean bean) {
         if (Objects.equals(this.currentTypeId, id)) {
             return;
         }
@@ -251,14 +313,18 @@ public class VoiceAIViewModel extends BaseViewModel {
                     voiceListBean.setName(ContextUtil.getContext().getString(R.string.add_voice));
                     voiceList.add(0, voiceListBean);
                 }
-                for (VoiceListBean voiceListBean : voiceList) {
-                    if (voiceListBean.isAddIcon()) {
-                        continue;
+                if (bean != null) {
+                    voiceDetailList.setValue(new Pair<>(voiceList, bean));
+                } else {
+                    for (VoiceListBean voiceListBean : voiceList) {
+                        if (voiceListBean.isAddIcon()) {
+                            continue;
+                        }
+                        setChoiceVoiceBean(voiceListBean, false);
+                        break;
                     }
-                    setChoiceVoiceBean(voiceListBean, false);
-                    break;
+                    voiceDetailList.setValue(new Pair<>(voiceList, null));
                 }
-                voiceDetailList.setValue(voiceList);
             }
 
             @Override
@@ -268,14 +334,6 @@ public class VoiceAIViewModel extends BaseViewModel {
         });
     }
 
-    @NonNull
-    private VoiceListBean getVoiceBeans(@NonNull CloneVoiceListBean bean) {
-        VoiceListBean voiceListBean = new VoiceListBean();
-        voiceListBean.setName(bean.getCloneVoiceName());
-        voiceListBean.setAvatarUrl(bean.getCloneVoiceAvatar());
-        voiceListBean.setId(bean.getId());
-        return voiceListBean;
-    }
 
     private void setChoiceVoiceBean(VoiceListBean voiceListBean, boolean playVoice) {
         if (this.choiceVoiceBean != null) {