UploadCoverTipDialog.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.cooleshow.musicmerge.widget;
  2. import android.content.DialogInterface;
  3. import android.text.Editable;
  4. import android.text.TextUtils;
  5. import android.text.TextWatcher;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import com.cooleshow.base.utils.GlideImageLoaderUtils;
  11. import com.cooleshow.base.widgets.BaseDialog;
  12. import com.cooleshow.base.widgets.ViewHolder;
  13. import com.cooleshow.musicmerge.R;
  14. import com.cooleshow.musicmerge.bean.MusicInfoBean;
  15. import com.cooleshow.musicmerge.viewmodel.MusicMergeViewModel;
  16. import com.cooleshow.usercenter.helper.UserHelper;
  17. import java.util.Locale;
  18. import androidx.annotation.NonNull;
  19. import androidx.constraintlayout.widget.Group;
  20. import androidx.lifecycle.Observer;
  21. import androidx.lifecycle.ViewModelProvider;
  22. /**
  23. * Author by pq, Date on 2023/11/2.
  24. */
  25. public class UploadCoverTipDialog extends BaseDialog implements View.OnClickListener {
  26. private ImageView mIvClose;
  27. private TextView mTvPublish;
  28. private EditText mEtContent;
  29. private TextWatcher mTextWatcher = new TextWatcher() {
  30. @Override
  31. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  32. }
  33. @Override
  34. public void onTextChanged(CharSequence s, int start, int before, int count) {
  35. }
  36. @Override
  37. public void afterTextChanged(Editable s) {
  38. String text = mEtContent.getText().toString().trim();
  39. int cLength = text.length();
  40. mTvTextNum.setText(String.format(Locale.getDefault(), "%d/150", cLength));
  41. }
  42. };
  43. private TextView mTvTextNum;
  44. private ImageView mIvIcon;
  45. private TextView mTvMusicTitle;
  46. private TextView mTvName;
  47. private MusicMergeViewModel mViewModel;
  48. private Group mGroupVideoCover;
  49. private TextView mTvByAlbum;
  50. private TextView mTvByCrop;
  51. private ImageView mIvVideoCover;
  52. private boolean isVideoFile = false;
  53. @Override
  54. public int intLayoutId() {
  55. return R.layout.dialog_upload_cover_tip_layout;
  56. }
  57. @Override
  58. public void convertView(ViewHolder holder, BaseDialog dialog) {
  59. mIvClose = holder.getView(R.id.iv_close);
  60. mTvPublish = holder.getView(R.id.tv_publish);
  61. mEtContent = holder.getView(R.id.et_content);
  62. mTvTextNum = holder.getView(R.id.tv_text_num);
  63. mTvMusicTitle = holder.getView(R.id.tv_music_title);
  64. mTvName = holder.getView(R.id.tv_name);
  65. mIvIcon = holder.getView(R.id.iv_icon);
  66. mGroupVideoCover = holder.getView(R.id.group_video_cover);
  67. mTvByAlbum = holder.getView(R.id.tv_by_album);
  68. mTvByCrop = holder.getView(R.id.tv_by_crop);
  69. mIvVideoCover = holder.getView(R.id.iv_video_cover);
  70. String userName = UserHelper.getUserName();
  71. mTvName.setText(userName);
  72. setCancelable(false);
  73. setOutCancel(false);
  74. initViewModel();
  75. initData();
  76. initListener();
  77. }
  78. private void initData() {
  79. mGroupVideoCover.setVisibility(isVideoFile ?View.VISIBLE:View.GONE);
  80. }
  81. private void initViewModel() {
  82. ViewModelProvider.AndroidViewModelFactory instance =
  83. ViewModelProvider.AndroidViewModelFactory
  84. .getInstance(getActivity().getApplication());
  85. mViewModel = new ViewModelProvider(getActivity(), instance)
  86. .get(MusicMergeViewModel.class);
  87. Boolean value = mViewModel.getIsVideoFile().getValue();
  88. isVideoFile = value != null && value;
  89. mViewModel.getMusicInfoLiveData().observe(this, new Observer<MusicInfoBean>() {
  90. @Override
  91. public void onChanged(MusicInfoBean musicInfoBean) {
  92. if (musicInfoBean != null) {
  93. String imgCover = musicInfoBean.getPreCover() != null ? musicInfoBean.getPreCover() : musicInfoBean.getCover();
  94. String videoCover = musicInfoBean.getVideoCover();
  95. setData(imgCover, musicInfoBean.getMusicTitle(),videoCover);
  96. }
  97. }
  98. });
  99. }
  100. private void initListener() {
  101. mEtContent.setOnTouchListener(new SolveEditTextScrollClash(mEtContent));
  102. mIvClose.setOnClickListener(this);
  103. mTvPublish.setOnClickListener(this);
  104. mIvIcon.setOnClickListener(this);
  105. mTvByAlbum.setOnClickListener(this);
  106. mTvByCrop.setOnClickListener(this);
  107. mEtContent.addTextChangedListener(mTextWatcher);
  108. }
  109. public void setData(String cover, String musicName,String videoCover) {
  110. if (mTvMusicTitle != null) {
  111. mTvMusicTitle.setText(musicName);
  112. }
  113. if (mIvVideoCover != null) {
  114. GlideImageLoaderUtils.getInstance().loadImage(getContext(), videoCover, mIvVideoCover, R.drawable.icon_upload_video_cover_default);
  115. }
  116. // if (mEtContent != null) {
  117. // mEtContent.setText(des);
  118. // }
  119. if (mIvIcon != null) {
  120. GlideImageLoaderUtils.getInstance().loadImage(getContext(), cover, mIvIcon, R.drawable.icon_default_music_song_cover);
  121. }
  122. }
  123. @Override
  124. public void onClick(View v) {
  125. int id = v.getId();
  126. if (id == R.id.iv_close) {
  127. saveDes("", null,null);
  128. dismiss();
  129. return;
  130. }
  131. if (id == R.id.iv_icon) {
  132. if (mEventListener != null) {
  133. mEventListener.onUploadCover();
  134. }
  135. }
  136. if (id == R.id.tv_publish) {
  137. String trim = mEtContent.getText().toString().trim();
  138. dismiss();
  139. if (mEventListener != null) {
  140. if (TextUtils.isEmpty(trim)) {
  141. trim = "我发布了一首演奏作品,快来听听吧~";
  142. }
  143. mEventListener.onPublish(trim);
  144. }
  145. return;
  146. }
  147. if (id == R.id.tv_by_crop) {
  148. if (mEventListener != null) {
  149. mEventListener.onSelectVideoCover(false);
  150. }
  151. return;
  152. }
  153. if (id == R.id.tv_by_album) {
  154. if (mEventListener != null) {
  155. mEventListener.onSelectVideoCover(true);
  156. }
  157. return;
  158. }
  159. }
  160. @Override
  161. public void onDismiss(@NonNull DialogInterface dialog) {
  162. super.onDismiss(dialog);
  163. }
  164. private void saveDes(String des, String url,String videoCover) {
  165. if (mEtContent != null) {
  166. mViewModel.resetMusicInfo(des, url,videoCover);
  167. }
  168. }
  169. private OnEventListener mEventListener;
  170. public void setEventListener(OnEventListener eventListener) {
  171. mEventListener = eventListener;
  172. }
  173. public interface OnEventListener {
  174. void onUploadCover();
  175. void onPublish(String des);
  176. void onSelectVideoCover(boolean isByAlbum);
  177. }
  178. @Override
  179. public void onDestroy() {
  180. if (mEtContent != null) {
  181. mEtContent.removeTextChangedListener(mTextWatcher);
  182. mTextWatcher = null;
  183. }
  184. super.onDestroy();
  185. }
  186. }