Files
assistant-android/app/src/main/java/com/gh/common/util/GameViewUtils.java
2024-03-26 10:34:13 +08:00

142 lines
5.5 KiB
Java

package com.gh.common.util;
import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.core.AppExecutor;
import com.gh.gamecenter.common.view.DrawableView;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.feature.entity.TagStyleEntity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
/**
* 设置列表中game item视图上的工具类
*/
public class GameViewUtils {
public static void setLabelList(Context context, LinearLayout labelLayout, List<TagStyleEntity> tagStyle) {
setLabelList(context, labelLayout, tagStyle, 8);
}
public static void setLabelList(Context context, LinearLayout labelLayout, List<TagStyleEntity> tagEntityList, int margin) {
int childTvCount = labelLayout.getChildCount();
for (int k = 0; k < childTvCount; k++) {
labelLayout.getChildAt(k).setVisibility(View.GONE);
}
if (tagEntityList == null || tagEntityList.isEmpty()) {
return;
}
for (int i = 0, size = tagEntityList.size(); i < size; i++) {
TextView tagTv;
TagStyleEntity tagEntity = tagEntityList.get(i);
int marginRight = i == size - 1 ? 0 : DisplayUtils.dip2px(context, margin);
if (i < childTvCount) {
tagTv = (TextView) labelLayout.getChildAt(i);
updateTagStyle(context, tagTv, tagEntity, marginRight);
} else {
AppExecutor.getLightWeightIoExecutor().execute(() -> {
TextView tv = getNewGameTagView(context, tagEntity, marginRight);
AppExecutor.getUiExecutor().execute(() -> {
// 快速刷新列表会出现重复标签被添加到布局中的问题,这里在实际执行代码前再检查一次数量
if (labelLayout.getChildCount() >= tagEntityList.size()) return;
labelLayout.addView(tv);
});
});
}
if (i >= 2) {
break;
}
}
}
// 新的游戏标签样式 version>=4.0.0
private static TextView getNewGameTagView(Context context, TagStyleEntity tagEntity, int rightMargin) {
TextView tag = new TextView(context);
updateTagStyle(context, tag, tagEntity, rightMargin);
return tag;
}
private static void updateTagStyle(Context context, TextView textView, TagStyleEntity tagEntity, int rightMargin) {
textView.setVisibility(View.VISIBLE);
// 同样的内容不用再更新
if (textView.getText() == tagEntity.getName()) return;
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.rightMargin = rightMargin;
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
textView.setSingleLine(true);
textView.setText(tagEntity.getName());
textView.setLayoutParams(lparams);
textView.setPadding(
DisplayUtils.dip2px(context, 4),
0,
DisplayUtils.dip2px(context, 4),
DisplayUtils.dip2px(context, 1));
textView.setTextColor(ExtensionsKt.hexStringToIntColor("#" + tagEntity.getColor(), Color.WHITE));
textView.setBackground(DrawableView.getServerDrawable(ExtensionsKt.hexStringToIntColor("#" + tagEntity.getBackground(), Color.WHITE)));
}
public static String getGameTestDate(long testTime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String testDate;
try {
long today = format.parse(format.format(new Date())).getTime();
long day = Long.parseLong(testTime + "000");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
String time = timeFormat.format(day);
if (day >= today && day < today + 86400 * 1000) {
testDate = "今天 " + time;
} else if (day >= today + 86400 * 1000
&& day < today + 86400 * 1000 * 2) {
testDate = "明天 " + time;
} else if (day >= today + 86400 * 1000 * 2
&& day < today + 86400 * 1000 * 3) {
testDate = "后天 " + time;
} else if (day >= today - 86400 * 1000 && day < today) {
testDate = "昨天 " + time;
} else {
testDate = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(day);
}
return testDate;
} catch (ParseException e) {
e.printStackTrace();
long day = Long.parseLong(testTime + "000");
Calendar calendar = Calendar.getInstance(TimeZone
.getTimeZone("Asia/Shanghai"));
calendar.setTimeInMillis(day);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
format = new SimpleDateFormat("MM-dd", Locale.CHINA);
testDate = format.format(day) + " " + hour + "";
return testDate;
}
}
}