Files
assistant-android/app/src/main/java/com/gh/common/util/NewsUtils.java

172 lines
5.9 KiB
Java

package com.gh.common.util;
import android.content.Context;
import android.graphics.Color;
import android.widget.TextView;
import com.gh.gamecenter.R;
import com.gh.gamecenter.entity.NewsEntity;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
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 io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
public class NewsUtils {
/**
* 根据新闻类型获取标签背景资源
*/
public static int getDrawableIdByType(String type) {
switch (type) {
case "活动":
case "高阶":
return R.drawable.textview_red_up;
case "公告":
case "中期":
return R.drawable.textview_orange_up;
case "新游":
return R.drawable.textview_green_up;
case "热门":
case "置顶":
return R.drawable.textview_all_red_up;
default:
return R.drawable.textview_blue_up;
}
}
/**
* 统计阅读量
*/
public static void statNewsViews(Context context, String news_id) {
RetrofitManager.getInstance(context).getApi().postArticleVisit(news_id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>());
}
/**
* 去除与重复sourceList相同的数据
*/
public static List<NewsEntity> removeDuplicateData(List<NewsEntity> sourceList, List<NewsEntity> rawList) {
if (sourceList == null || sourceList.isEmpty()
|| rawList == null || rawList.isEmpty()) {
return rawList;
}
String id;
for (int i = 0; i < rawList.size(); i++) {
id = rawList.get(i).getId();
for (NewsEntity newsEntity : sourceList) {
if (id.equals(newsEntity.getId())) {
rawList.remove(i);
i--;
break;
}
}
}
return rawList;
}
/**
* 设置新闻类型
*/
public static void setNewsType(TextView textView, String type, int priority, int position) {
if (priority != 0) {
if (position == 0) {
textView.setText(R.string.article_top);
} else {
textView.setText(R.string.article_hot);
}
textView.setBackgroundResource(R.drawable.textview_all_red_style);
return;
} else {
textView.setText(type);
}
textView.setTextColor(Color.WHITE);
switch (type) {
case "活动":
textView.setBackgroundResource(R.drawable.textview_orange_style);
break;
case "公告":
textView.setBackgroundResource(R.drawable.textview_red_style);
break;
case "评测":
textView.setBackgroundResource(R.drawable.textview_red_style);
break;
case "杂谈":
textView.setBackgroundResource(R.drawable.textview_orange_style);
break;
case "专题":
textView.setBackgroundResource(R.drawable.textview_blue_style);
break;
default:
textView.setBackgroundResource(R.drawable.textview_blue_style);
break;
}
}
/**
* 设置新闻发布时间
*/
public static void setNewsPublishOn(TextView textView, long time) {
CommentUtils.setCommentTime(textView, time);
}
public static void setNewsDetailTime(TextView textView, long time) {
CommentUtils.setCommentTime(textView, time);
}
public static String getFormattedTime(long time) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
try {
long day = time * 1000;
String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
format.applyPattern("yyyy");
String currentYear = format.format(day);
format.applyPattern("yyyyMMdd");
long today = format.parse(format.format(new Date())).getTime();
if (day >= today && day < today + 86400 * 1000) {
long min = new Date().getTime() / 1000 - day / 1000;
int hour = (int) (min / (60 * 60));
if (hour == 0) {
if (min < 60) {
return "刚刚";
} else {
return (String.format(Locale.getDefault(), "%d分钟前", (int) (min / 60)));
}
} else {
return (String.format(Locale.getDefault(), "%d小时前", hour));
}
} else if (day >= today - 86400 * 1000 && day < today) {
format.applyPattern("HH:mm");
return ("昨天 ");
} else if (day >= today - 86400 * 1000 * 7 && day < today - 86400 * 1000) {
format.applyPattern("HH:mm");
long days = (today - day) / 86400000 + 1;
return String.format(Locale.getDefault(), "%d天前 ", days);
} else if (day < today - 86400 * 1000 * 7 && year.equals(currentYear)) {
format.applyPattern("MM-dd");
return (format.format(day));
} else {
format.applyPattern("yyyy-MM-dd");
return (format.format(day));
}
} catch (ParseException e) {
e.printStackTrace();
format.applyPattern("yyyy-MM-dd");
return (format.format(time * 1000));
}
}
}