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

154 lines
5.6 KiB
Java

package com.gh.common.util;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.TextView;
import com.gh.gamecenter.*;
import com.gh.gamecenter.entity.NewsEntity;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import okhttp3.ResponseBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class NewsUtils {
/**
* 根据新闻类型获取标签背景资源
*/
public static int getDrawableIdByType(String type) {
if ("活动".equals(type) || "高阶".equals(type)) {
return R.drawable.textview_red_up;
} else if ("公告".equals(type) || "中期".equals(type)) {
return R.drawable.textview_orange_up;
} else if ("新游".equals(type)) {
return R.drawable.textview_green_up;
} else if ("热门".equals(type) || "置顶".equals(type)) {
return R.drawable.textview_all_red_up;
} else {
return R.drawable.textview_blue_up;
}
}
/**
* 启动新闻详情页面
*/
public static void startNewsDetailActivity(Context context, NewsEntity newsEntity, String entrance) {
if (!TextUtils.isEmpty(newsEntity.getLink())) {
Intent intent = new Intent(context, WebActivity.class);
intent.putExtra("url", newsEntity.getLink());
intent.putExtra("gameName", newsEntity.getGameName());
intent.putExtra("newsId", newsEntity.getId());
intent.putExtra("entrance", entrance + "+(消息详情[" + newsEntity.getGameName() + "])");
context.startActivity(intent);
} else {
Intent intent = new Intent(context, NewsDetailActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id", newsEntity.getId());
intent.putExtra("title", newsEntity.getTitle());
intent.putExtra("type", newsEntity.getType());
intent.putExtra("entrance", entrance);
context.startActivity(intent);
}
}
/**
* 统计阅读量
*/
public static void statNewsViews(String news_id) {
RetrofitManager.getData().postNewsViews(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("置顶");
} else {
textView.setText("热门");
}
} else {
textView.setText(type);
}
textView.setTextColor(Color.WHITE);
if ("活动".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_orange_style);
} else if ("公告".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_red_style);
} else if ("评测".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_red_style);
} else if ("杂谈".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_orange_style);
} else if ("专题".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_blue_style);
} else if ("置顶".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_all_red_style);
} else if ("热门".equals(type)) {
textView.setBackgroundResource(R.drawable.textview_all_red_style);
} else {
textView.setBackgroundResource(R.drawable.textview_blue_style);
}
}
/**
* 设置新闻发布时间
*/
public static void setNewsPublishOn(TextView textView, long time) {
time = time * 1000;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
try {
long today = format.parse(format.format(new Date())).getTime();
if (time >= today && time < today + 86400 * 1000) {
format.applyPattern("HH:mm");
textView.setText(format.format(time));
} else if (time >= today - 86400 * 1000 && time < today) {
format.applyPattern("HH:mm");
textView.setText(String.format("昨天 %s", format.format(time)));
} else {
format.applyPattern("yyyy年MM月dd日 HH:mm");
textView.setText(format.format(time));
}
} catch (ParseException e) {
e.printStackTrace();
format.applyPattern("yyyy年MM月dd日 HH:mm");
textView.setText(format.format(time));
}
}
}