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() { @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(); } }