Search in sources :

Example 1 with StatusContents

use of org.aisen.weibo.sina.sinasdk.bean.StatusContents in project AisenWeiBo by wangdan.

the class TimelineCacheUtility method findCacheData.

@Override
public IResult findCacheData(Setting action, Params params) {
    if (AppSettings.isDisableCache())
        return null;
    if (!AppContext.isLoggedIn())
        return null;
    // 如果是用户微博,只返回用户的微博
    if (action.getValue().equals("statuses/user_timeline.json")) {
        // 是当前登录用户
        if (params.containsKey("user_id") && params.getParameter("user_id").equals(AppContext.getAccount().getUser().getIdstr())) {
        } else if (params.containsKey("screen_name") && params.getParameter("screen_name").equals(AppContext.getAccount().getUser().getScreen_name())) {
        } else {
            return null;
        }
    }
    try {
        long time = System.currentTimeMillis();
        Extra extra = new Extra(AppContext.getAccount().getUser().getId(), getCacheKey(action, params));
        List<StatusContent> statusList = SinaDB.getTimelineDB().select(extra, StatusContent.class);
        if (statusList.size() > 0) {
            StatusContents statusContents = new StatusContents();
            statusContents.setFromCache(true);
            statusContents.setOutofdate(CacheTimeUtils.isOutofdate(getCacheKey(action, params), AppContext.getAccount().getUser()));
            statusContents.setStatuses(statusList);
            Logger.w(TAG, String.format("读取缓存耗时%sms", String.valueOf(System.currentTimeMillis() - time)));
            Logger.d(TAG, String.format("返回微博数据%d条, expired = %s", statusContents.getStatuses().size(), String.valueOf(statusContents.isOutofdate())));
            return statusContents;
        }
    } catch (Exception e) {
    }
    return null;
}
Also used : StatusContent(org.aisen.weibo.sina.sinasdk.bean.StatusContent) Extra(org.aisen.android.component.orm.extra.Extra) StatusContents(org.aisen.weibo.sina.sinasdk.bean.StatusContents)

Example 2 with StatusContents

use of org.aisen.weibo.sina.sinasdk.bean.StatusContents in project AisenWeiBo by wangdan.

the class TimelineCacheUtility method addCacheData.

@Override
public void addCacheData(Setting action, Params params, IResult responseObj) {
    // 如果是离线请求,忽略数据缓存
    if (action.getExtras() != null && action.getExtras().containsKey("offline_action"))
        return;
    // 如果是用户微博
    if (action.getValue().equals("statuses/user_timeline.json")) {
        // 是当前登录用户
        if (params.containsKey("user_id") && params.getParameter("user_id").equals(AppContext.getAccount().getUser().getIdstr())) {
        } else if (params.containsKey("screen_name") && params.getParameter("screen_name").equals(AppContext.getAccount().getUser().getScreen_name())) {
        } else {
            return;
        }
    }
    try {
        StatusContents statusContents = (StatusContents) responseObj;
        if (statusContents.getStatuses().size() == 0)
            return;
        boolean clear = false;
        // 刷新
        if (!TextUtils.isEmpty(params.getParameter("since_id"))) {
            int diff = Math.abs(statusContents.getStatuses().size() - AppSettings.getTimelineCount());
            clear = diff <= 3;
        } else // 加载更多
        if (!TextUtils.isEmpty(params.getParameter("max_id"))) {
        } else // 重置
        {
            clear = true;
        }
        Extra extra = new Extra(AppContext.getAccount().getUser().getId(), getCacheKey(action, params));
        if (clear) {
            SinaDB.getTimelineDB().deleteAll(extra, StatusContent.class);
            Logger.d(TAG, "清理数据");
        }
        long time = System.currentTimeMillis();
        SinaDB.getTimelineDB().insert(extra, statusContents.getStatuses());
        Logger.w(TAG, String.format("写入微博数据,共%d条,共耗时%sms", statusContents.getStatuses().size(), String.valueOf(System.currentTimeMillis() - time)));
        CacheTimeUtils.saveTime(getCacheKey(action, params), AppContext.getAccount().getUser());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Extra(org.aisen.android.component.orm.extra.Extra) StatusContents(org.aisen.weibo.sina.sinasdk.bean.StatusContents)

Aggregations

Extra (org.aisen.android.component.orm.extra.Extra)2 StatusContents (org.aisen.weibo.sina.sinasdk.bean.StatusContents)2 StatusContent (org.aisen.weibo.sina.sinasdk.bean.StatusContent)1