Files
pdlivexp/common/src/main/java/com/yunbao/common/adapter/LiveNewWishAdapter.java
2023-08-08 14:50:13 +08:00

179 lines
7.1 KiB
Java

package com.yunbao.common.adapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yunbao.common.R;
import com.yunbao.common.bean.WishModel;
import com.yunbao.common.event.LiveNewWishListCloseEvent;
import com.yunbao.common.utils.Bus;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.views.DayWishItemViewHolder;
import com.yunbao.common.views.LunarWishItemViewHolder;
import com.yunbao.common.views.SeasonalWishItemViewHolder;
import com.yunbao.common.views.WeekWishItemViewHolder;
import java.util.ArrayList;
import java.util.List;
public class LiveNewWishAdapter extends RecyclerView.Adapter {
private int type = 0;
private List<WishModel> wishList = new ArrayList<>();
public void addData(List<WishModel> wishModelList, int type) {
this.type = type;
wishList.clear();
wishList.addAll(wishModelList);
wishList.add(null);
notifyDataSetChanged();
}
public void addGiftListModel(WishModel model) {
switch (type) {
case 1:
Bus.get().post(new LiveNewWishListCloseEvent().setDayWish(true));
break;
case 2:
Bus.get().post(new LiveNewWishListCloseEvent().setZhouXin(true));
break;
case 3:
Bus.get().post(new LiveNewWishListCloseEvent().setLunarWish(true));
break;
case 4:
Bus.get().post(new LiveNewWishListCloseEvent().setSeasonalWish(true));
break;
}
boolean isAdd = false;
for (int i = 0; i < wishList.size(); i++) {
if (wishList.get(i)!=null){
if (!TextUtils.isEmpty(wishList.get(i).getLid()) && !TextUtils.isEmpty(model.getLid())
&& TextUtils.equals(wishList.get(i).getLid(), model.getLid())) {
ToastUtil.show(R.string.too_many_gifts);
isAdd = true;
}
}
}
if (!isAdd) {
wishList.add(0, model);
}
notifyDataSetChanged();
}
public List<WishModel> getWishList() {
return wishList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (type == 1) {
View dayWish = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_day_wish, parent, false);
return new DayWishItemViewHolder(dayWish);
} else if (type == 2) {
View weekWish = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_week_wish, parent, false);
return new WeekWishItemViewHolder(weekWish);
} else if (type == 3) {
View lunarWish = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lunar_wish, parent, false);
return new LunarWishItemViewHolder(lunarWish);
} else {
View seasonalWish = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_seasonal_wish, parent, false);
return new SeasonalWishItemViewHolder(seasonalWish);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (position == wishList.size()) return;
if (holder instanceof DayWishItemViewHolder) {
DayWishItemViewHolder dayWishItemViewHolder = (DayWishItemViewHolder) holder;
dayWishItemViewHolder.steDayWishData(wishList.get(position), position, new DayWishItemViewHolder.DayWishItemListener() {
@Override
public void onDelete(int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setDayWish(true));
wishList.remove(index);
notifyDataSetChanged();
}
@Override
public void onUpdate(WishModel model, int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setDayWish(true));
wishList.remove(index);
wishList.add(index, model);
notifyItemChanged(index);
}
});
} else if (holder instanceof WeekWishItemViewHolder) {
WeekWishItemViewHolder weekWishItemViewHolder = (WeekWishItemViewHolder) holder;
weekWishItemViewHolder.steWeekWishData(wishList.get(position), position, new WeekWishItemViewHolder.WeekWishItemListener() {
@Override
public void onDelete(int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setZhouXin(true));
wishList.remove(index);
notifyDataSetChanged();
}
@Override
public void onUpdate(WishModel model, int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setZhouXin(true));
wishList.remove(index);
wishList.add(index, model);
notifyItemChanged(index);
}
});
} else if (holder instanceof LunarWishItemViewHolder) {
LunarWishItemViewHolder lunarWishItemViewHolder = (LunarWishItemViewHolder) holder;
lunarWishItemViewHolder.steLunarWishData(wishList.get(position), position, new LunarWishItemViewHolder.LunarWishItemListener() {
@Override
public void onDelete(int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setLunarWish(true));
wishList.remove(index);
notifyDataSetChanged();
}
@Override
public void onUpdate(WishModel model, int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setLunarWish(true));
wishList.remove(index);
wishList.add(index, model);
notifyItemChanged(index);
}
});
} else if (holder instanceof SeasonalWishItemViewHolder) {
SeasonalWishItemViewHolder seasonalWishItemViewHolder = (SeasonalWishItemViewHolder) holder;
seasonalWishItemViewHolder.steSeasonalWish(wishList.get(position), position, new SeasonalWishItemViewHolder.SeasonalWishItemListener() {
@Override
public void onDelete(int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setSeasonalWish(true));
wishList.remove(index);
notifyDataSetChanged();
}
@Override
public void onUpdate(WishModel model, int index) {
Bus.get().post(new LiveNewWishListCloseEvent().setSeasonalWish(true));
wishList.remove(index);
wishList.add(index, model);
notifyItemChanged(index);
}
});
}
}
@Override
public int getItemCount() {
return wishList.size();
}
}