
price.2019-12-03 11_58_17.gif
實(shí)現(xiàn)思路
- 新價(jià)格跟老價(jià)格對比,定義三個(gè)狀態(tài)--上漲/不變/下跌。
- 數(shù)據(jù)刷新使用websocket長連接進(jìn)行實(shí)時(shí)刷新。
- 頻率控制--websocket有的時(shí)候推數(shù)據(jù)太快,客戶端有時(shí)候處理不過來,造成ui卡頓,這個(gè)時(shí)候可以使用Rxjava的debounce/throttleFirst等操作符進(jìn)行控制。
- 根據(jù)價(jià)格的三個(gè)狀態(tài)(上漲/不變/下跌)進(jìn)行ui刷新。
編碼實(shí)現(xiàn)
- 抽象實(shí)體接口
根據(jù)上面的實(shí)現(xiàn)思路,價(jià)格狀態(tài)主要是根據(jù)同一個(gè)實(shí)體的新/老 價(jià)格進(jìn)行對比得到。那么使用Map數(shù)據(jù)結(jié)構(gòu)是合適的,實(shí)體根據(jù)自己的需求提供key,map的value就是“價(jià)格”。
代碼如下:
public interface IPrice {
/**
* 價(jià)格上漲
*/
int STATE_UP = 1;
/**
* 價(jià)格不變
*/
int STATE_STABLE = 2;
/**
* 價(jià)格下跌
*/
int STATE_DOWN = 3;
/**
* 不同類型的實(shí)體自己生成對應(yīng)的key
* @return
*/
String generateKey();
/**
* 設(shè)置價(jià)格漲跌狀態(tài)
* @param state
*/
void setPriceState(int state);
/**
* 得到價(jià)格漲跌狀態(tài)
* @return
*/
int getPriceChangeState();
/**
* 得到價(jià)格數(shù)據(jù)
* @return
*/
String getPriceStr();
/**
* 設(shè)置價(jià)格數(shù)據(jù)
* @param priceStr
*/
void setPriceStr(String priceStr);
/**
* 對比新舊價(jià)格
* @param priceStr
* @param oldPrice
*/
void compareOldPrice(String priceStr, String oldPrice);
}
- 價(jià)格對比工具類的實(shí)現(xiàn)
public class PriceStateCompat {
private static PriceStateCompat instance;
private PriceStateCompat(){}
public static PriceStateCompat getInstance(){
if (instance == null) {
instance = new PriceStateCompat();
}
return instance;
}
public static Map<String, String> storeOldPricesAndInitState(@NonNull List<? extends IPrice> items) {
Map<String, String> oldPriceMap = new HashMap<>();
int size = items.size();
for (int i = 0; i < size; i++) {
IPrice item = items.get(i);
oldPriceMap.put(item.generateKey(), item.getPriceStr());
}
return oldPriceMap;
}
public static Map<String, String> storeOldPricesAndInitState(Map<String, String> oldPriceMap, @NonNull List<? extends IPrice> items) {
if (oldPriceMap == null) {
oldPriceMap = new HashMap<>();
}
int size = items.size();
for (int i = 0; i < size; i++) {
IPrice item = items.get(i);
item.setPriceStr(item.getPriceStr());
String oldPrice = oldPriceMap.get(item.generateKey());
if (!TextUtils.isEmpty(oldPrice)) {
item.compareOldPrice(item.getPriceStr(), oldPrice);
}
oldPriceMap.put(item.generateKey(), item.getPriceStr());
}
return oldPriceMap;
}
public static void startAnimation(ImageView imageView, Drawable drawable, IPrice bean) {
imageView.setImageDrawable(drawable);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(1500);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(alphaAnimation);
bean.setPriceState(IPrice.STATE_STABLE);
}
public static void startAnimation(ImageView imageView, Drawable drawable, IPrice bean, int visibility) {
imageView.setImageDrawable(drawable);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(1500);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
imageView.setVisibility(visibility);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(alphaAnimation);
bean.setPriceState(IPrice.STATE_STABLE);
}
}
剩下的工作就是數(shù)據(jù)請求-》從map中對比老的數(shù)據(jù)-》更新價(jià)格狀態(tài)-》渲染到ui上