123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package com.cooleshow.musicmerge.widget;
- import android.content.DialogInterface;
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.cooleshow.base.utils.GlideImageLoaderUtils;
- import com.cooleshow.base.widgets.BaseDialog;
- import com.cooleshow.base.widgets.ViewHolder;
- import com.cooleshow.musicmerge.R;
- import com.cooleshow.musicmerge.bean.MusicInfoBean;
- import com.cooleshow.musicmerge.viewmodel.MusicMergeViewModel;
- import com.cooleshow.usercenter.helper.UserHelper;
- import java.util.Locale;
- import androidx.annotation.NonNull;
- import androidx.constraintlayout.widget.Group;
- import androidx.lifecycle.Observer;
- import androidx.lifecycle.ViewModelProvider;
- /**
- * Author by pq, Date on 2023/11/2.
- */
- public class UploadCoverTipDialog extends BaseDialog implements View.OnClickListener {
- private ImageView mIvClose;
- private TextView mTvPublish;
- private EditText mEtContent;
- private TextWatcher mTextWatcher = new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- String text = mEtContent.getText().toString().trim();
- int cLength = text.length();
- mTvTextNum.setText(String.format(Locale.getDefault(), "%d/150", cLength));
- }
- };
- private TextView mTvTextNum;
- private ImageView mIvIcon;
- private TextView mTvMusicTitle;
- private TextView mTvName;
- private MusicMergeViewModel mViewModel;
- private Group mGroupVideoCover;
- private TextView mTvByAlbum;
- private TextView mTvByCrop;
- private ImageView mIvVideoCover;
- private boolean isVideoFile = false;
- @Override
- public int intLayoutId() {
- return R.layout.dialog_upload_cover_tip_layout;
- }
- @Override
- public void convertView(ViewHolder holder, BaseDialog dialog) {
- mIvClose = holder.getView(R.id.iv_close);
- mTvPublish = holder.getView(R.id.tv_publish);
- mEtContent = holder.getView(R.id.et_content);
- mTvTextNum = holder.getView(R.id.tv_text_num);
- mTvMusicTitle = holder.getView(R.id.tv_music_title);
- mTvName = holder.getView(R.id.tv_name);
- mIvIcon = holder.getView(R.id.iv_icon);
- mGroupVideoCover = holder.getView(R.id.group_video_cover);
- mTvByAlbum = holder.getView(R.id.tv_by_album);
- mTvByCrop = holder.getView(R.id.tv_by_crop);
- mIvVideoCover = holder.getView(R.id.iv_video_cover);
- String userName = UserHelper.getUserName();
- mTvName.setText(userName);
- setCancelable(false);
- setOutCancel(false);
- initViewModel();
- initData();
- initListener();
- }
- private void initData() {
- mGroupVideoCover.setVisibility(isVideoFile ?View.VISIBLE:View.GONE);
- }
- private void initViewModel() {
- ViewModelProvider.AndroidViewModelFactory instance =
- ViewModelProvider.AndroidViewModelFactory
- .getInstance(getActivity().getApplication());
- mViewModel = new ViewModelProvider(getActivity(), instance)
- .get(MusicMergeViewModel.class);
- Boolean value = mViewModel.getIsVideoFile().getValue();
- isVideoFile = value != null && value;
- mViewModel.getMusicInfoLiveData().observe(this, new Observer<MusicInfoBean>() {
- @Override
- public void onChanged(MusicInfoBean musicInfoBean) {
- if (musicInfoBean != null) {
- String imgCover = musicInfoBean.getPreCover() != null ? musicInfoBean.getPreCover() : musicInfoBean.getCover();
- String videoCover = musicInfoBean.getVideoCover();
- setData(imgCover, musicInfoBean.getMusicTitle(),videoCover);
- }
- }
- });
- }
- private void initListener() {
- mEtContent.setOnTouchListener(new SolveEditTextScrollClash(mEtContent));
- mIvClose.setOnClickListener(this);
- mTvPublish.setOnClickListener(this);
- mIvIcon.setOnClickListener(this);
- mTvByAlbum.setOnClickListener(this);
- mTvByCrop.setOnClickListener(this);
- mEtContent.addTextChangedListener(mTextWatcher);
- }
- public void setData(String cover, String musicName,String videoCover) {
- if (mTvMusicTitle != null) {
- mTvMusicTitle.setText(musicName);
- }
- if (mIvVideoCover != null) {
- GlideImageLoaderUtils.getInstance().loadImage(getContext(), videoCover, mIvVideoCover, R.drawable.icon_upload_video_cover_default);
- }
- // if (mEtContent != null) {
- // mEtContent.setText(des);
- // }
- if (mIvIcon != null) {
- GlideImageLoaderUtils.getInstance().loadImage(getContext(), cover, mIvIcon, R.drawable.icon_default_music_song_cover);
- }
- }
- @Override
- public void onClick(View v) {
- int id = v.getId();
- if (id == R.id.iv_close) {
- saveDes("", null,null);
- dismiss();
- return;
- }
- if (id == R.id.iv_icon) {
- if (mEventListener != null) {
- mEventListener.onUploadCover();
- }
- }
- if (id == R.id.tv_publish) {
- String trim = mEtContent.getText().toString().trim();
- dismiss();
- if (mEventListener != null) {
- if (TextUtils.isEmpty(trim)) {
- trim = "我发布了一首演奏作品,快来听听吧~";
- }
- mEventListener.onPublish(trim);
- }
- return;
- }
- if (id == R.id.tv_by_crop) {
- if (mEventListener != null) {
- mEventListener.onSelectVideoCover(false);
- }
- return;
- }
- if (id == R.id.tv_by_album) {
- if (mEventListener != null) {
- mEventListener.onSelectVideoCover(true);
- }
- return;
- }
- }
- @Override
- public void onDismiss(@NonNull DialogInterface dialog) {
- super.onDismiss(dialog);
- }
- private void saveDes(String des, String url,String videoCover) {
- if (mEtContent != null) {
- mViewModel.resetMusicInfo(des, url,videoCover);
- }
- }
- private OnEventListener mEventListener;
- public void setEventListener(OnEventListener eventListener) {
- mEventListener = eventListener;
- }
- public interface OnEventListener {
- void onUploadCover();
- void onPublish(String des);
- void onSelectVideoCover(boolean isByAlbum);
- }
- @Override
- public void onDestroy() {
- if (mEtContent != null) {
- mEtContent.removeTextChangedListener(mTextWatcher);
- mTextWatcher = null;
- }
- super.onDestroy();
- }
- }
|