Search in sources :

Example 26 with TimeLinePosition

use of org.qii.weiciyuan.bean.android.TimeLinePosition in project weiciyuan by qii.

the class MentionsWeiboTimeLineFragment method addNewDataAndRememberPosition.

private void addNewDataAndRememberPosition(final MessageListBean newValue) {
    AppLogger.i("Add new unread data to memory cache");
    if (getActivity() == null || newValue.getSize() == 0) {
        AppLogger.i("Activity is destroyed or new data count is zero, give up");
        return;
    }
    final boolean isDataSourceEmpty = getList().getSize() == 0;
    TimeLinePosition previousPosition = Utility.getCurrentPositionFromListView(getListView());
    getList().addNewData(newValue);
    if (isDataSourceEmpty) {
        newMsgTipBar.setValue(newValue, true);
        newMsgTipBar.clearAndReset();
        getAdapter().notifyDataSetChanged();
        AppLogger.i("Init data source is empty, ListView jump to zero position after refresh, first time open app? ");
        getListView().setSelection(0);
        saveTimeLinePositionToDB();
    } else {
        if (previousPosition.isEmpty() && timeLinePosition != null) {
            previousPosition = timeLinePosition;
        }
        AppLogger.i("Previous first visible item id " + previousPosition.firstItemId);
        getAdapter().notifyDataSetChanged();
        List<MessageBean> unreadData = newValue.getItemList();
        for (MessageBean message : unreadData) {
            if (message != null) {
                MentionsWeiboTimeLineFragment.this.timeLinePosition.newMsgIds.add(message.getIdLong());
            }
        }
        newMsgTipBar.setValue(MentionsWeiboTimeLineFragment.this.timeLinePosition.newMsgIds);
        int positionInAdapter = Utility.getAdapterPositionFromItemId(getAdapter(), previousPosition.firstItemId);
        //use 1 px to show newMsgTipBar
        AppLogger.i("ListView restore to previous position " + positionInAdapter);
        getListView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                getListView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                AppLogger.i("Save ListView position to database");
                saveTimeLinePositionToDB();
            }
        });
        Utility.setListViewAdapterPosition(getListView(), positionInAdapter, previousPosition.top - 1, null);
    }
    showUIUnreadCount(MentionsWeiboTimeLineFragment.this.timeLinePosition.newMsgIds.size());
    MentionWeiboTimeLineDBTask.asyncReplace(getList(), accountBean.getUid());
}
Also used : MessageBean(org.qii.weiciyuan.bean.MessageBean) TimeLinePosition(org.qii.weiciyuan.bean.android.TimeLinePosition) ViewTreeObserver(android.view.ViewTreeObserver)

Example 27 with TimeLinePosition

use of org.qii.weiciyuan.bean.android.TimeLinePosition in project weiciyuan by qii.

the class FriendsTimeLineFragment method savePositionToPositionsCache.

private void savePositionToPositionsCache() {
    TimeLinePosition current = Utility.getCurrentPositionFromListView(getListView());
    if (!current.isEmpty()) {
        positionCache.put(currentGroupId, current);
        AppLogger.i("Current ListView position first visible item id " + current.firstItemId + " , save to memory cache");
    } else {
        AppLogger.i("Cant get correct current ListView position, so use previous database data");
    }
}
Also used : TimeLinePosition(org.qii.weiciyuan.bean.android.TimeLinePosition)

Example 28 with TimeLinePosition

use of org.qii.weiciyuan.bean.android.TimeLinePosition in project weiciyuan by qii.

the class MentionWeiboTimeLineDBTask method getRepostLineMsgList.

public static MentionTimeLineData getRepostLineMsgList(String accountId) {
    TimeLinePosition position = getPosition(accountId);
    Gson gson = new Gson();
    MessageListBean result = new MessageListBean();
    int limit = position.position + AppConfig.DB_CACHE_COUNT_OFFSET > AppConfig.DEFAULT_MSG_COUNT_50 ? position.position + AppConfig.DB_CACHE_COUNT_OFFSET : AppConfig.DEFAULT_MSG_COUNT_50;
    List<MessageBean> msgList = new ArrayList<MessageBean>();
    String sql = "select * from " + RepostsTable.RepostDataTable.TABLE_NAME + " where " + RepostsTable.RepostDataTable.ACCOUNTID + "  = " + accountId + " order by " + RepostsTable.RepostDataTable.MBLOGID + " desc limit " + limit;
    Cursor c = getRsd().rawQuery(sql, null);
    while (c.moveToNext()) {
        String json = c.getString(c.getColumnIndex(RepostsTable.RepostDataTable.JSONDATA));
        if (!TextUtils.isEmpty(json)) {
            try {
                MessageBean value = gson.fromJson(json, MessageBean.class);
                if (!value.isMiddleUnreadItem()) {
                    value.getListViewSpannableString();
                }
                msgList.add(value);
            } catch (JsonSyntaxException e) {
                AppLogger.e(e.getMessage());
            }
        } else {
            msgList.add(null);
        }
    }
    result.setStatuses(msgList);
    c.close();
    MentionTimeLineData mentionTimeLineData = new MentionTimeLineData(result, position);
    return mentionTimeLineData;
}
Also used : MessageBean(org.qii.weiciyuan.bean.MessageBean) JsonSyntaxException(com.google.gson.JsonSyntaxException) MessageListBean(org.qii.weiciyuan.bean.MessageListBean) MentionTimeLineData(org.qii.weiciyuan.bean.android.MentionTimeLineData) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) TimeLinePosition(org.qii.weiciyuan.bean.android.TimeLinePosition) Cursor(android.database.Cursor)

Example 29 with TimeLinePosition

use of org.qii.weiciyuan.bean.android.TimeLinePosition in project weiciyuan by qii.

the class CommentsToMeTimeLineFragment method addNewDataAndRememberPosition.

private void addNewDataAndRememberPosition(final CommentListBean newValue, boolean ignored) /**unused**/
{
    AppLogger.i("Add new unread data to memory cache");
    if (getActivity() == null || newValue.getSize() == 0) {
        AppLogger.i("Activity is destroyed or new data count is zero, give up");
        return;
    }
    final boolean isDataSourceEmpty = getList().getSize() == 0;
    TimeLinePosition previousPosition = Utility.getCurrentPositionFromListView(getListView());
    getList().addNewData(newValue);
    if (isDataSourceEmpty) {
        newMsgTipBar.setValue(newValue, true);
        newMsgTipBar.clearAndReset();
        getAdapter().notifyDataSetChanged();
        AppLogger.i("Init data source is empty, ListView jump to zero position after refresh, first time open app? ");
        getListView().setSelection(0);
        saveTimeLinePositionToDB();
    } else {
        if (previousPosition.isEmpty() && timeLinePosition != null) {
            previousPosition = timeLinePosition;
        }
        AppLogger.i("Previous first visible item id " + previousPosition.firstItemId);
        getAdapter().notifyDataSetChanged();
        List<CommentBean> unreadData = newValue.getItemList();
        for (CommentBean comment : unreadData) {
            if (comment != null) {
                CommentsToMeTimeLineFragment.this.timeLinePosition.newMsgIds.add(comment.getIdLong());
            }
        }
        newMsgTipBar.setValue(CommentsToMeTimeLineFragment.this.timeLinePosition.newMsgIds);
        int positionInAdapter = Utility.getAdapterPositionFromItemId(getAdapter(), previousPosition.firstItemId);
        //use 1 px to show newMsgTipBar
        AppLogger.i("ListView restore to previous position " + positionInAdapter);
        getListView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                getListView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
                AppLogger.i("Save ListView position to database");
                saveTimeLinePositionToDB();
            }
        });
        Utility.setListViewAdapterPosition(getListView(), positionInAdapter, previousPosition.top - 1, null);
    }
    showUIUnreadCount(CommentsToMeTimeLineFragment.this.timeLinePosition.newMsgIds.size());
    CommentToMeTimeLineDBTask.asyncReplace(getList(), accountBean.getUid());
}
Also used : TimeLinePosition(org.qii.weiciyuan.bean.android.TimeLinePosition) CommentBean(org.qii.weiciyuan.bean.CommentBean) ViewTreeObserver(android.view.ViewTreeObserver)

Example 30 with TimeLinePosition

use of org.qii.weiciyuan.bean.android.TimeLinePosition in project weiciyuan by qii.

the class Utility method getCurrentPositionFromListView.

public static TimeLinePosition getCurrentPositionFromListView(ListView listView) {
    if (listView.getChildCount() == 0) {
        AppLogger.i("ListView child count is empty, maybe this ListView have not be visible to user");
        return TimeLinePosition.empty();
    }
    AppLogger.i("ListView child count " + listView.getChildCount());
    View view = listView.getChildAt(0);
    int top = (view != null ? view.getTop() : 0);
    /**
         * warning: listView.getFirstVisiblePosition() return position include headerview count (HeaderAdapter) and use java
         * reflection to set mFirstPosition when call setSelectionFromTop/setSelectionAfterHeaderView/setSelection
         * see HeaderListView.java
         *
         */
    int firstVisiblePosition = listView.getFirstVisiblePosition();
    if (firstVisiblePosition < listView.getHeaderViewsCount()) {
        /**
             * for example, if header view count is 2, firstVisiblePosition is 1, then listView getItemIdAtPosition(1)
             * will return the second header view id -1, so we have to set firstVisiblePosition to 2 to get your
             * actual adapter's first item id
             */
        AppLogger.i("ListView first visible position is " + firstVisiblePosition + " it  below header view count " + listView.getHeaderViewsCount() + " so set header view count to it");
        firstVisiblePosition = listView.getHeaderViewsCount();
    }
    AppLogger.i("ListView first visible position " + firstVisiblePosition);
    long firstVisibleId = listView.getItemIdAtPosition(firstVisiblePosition);
    AppLogger.i("ListView first visible id " + firstVisibleId);
    return new TimeLinePosition(firstVisibleId, top, listView.getFirstVisiblePosition());
}
Also used : TimeLinePosition(org.qii.weiciyuan.bean.android.TimeLinePosition) ImageView(android.widget.ImageView) HeaderListView(org.qii.weiciyuan.support.lib.HeaderListView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) ListView(android.widget.ListView)

Aggregations

TimeLinePosition (org.qii.weiciyuan.bean.android.TimeLinePosition)30 Cursor (android.database.Cursor)12 Gson (com.google.gson.Gson)12 JsonSyntaxException (com.google.gson.JsonSyntaxException)12 CommentBean (org.qii.weiciyuan.bean.CommentBean)7 ArrayList (java.util.ArrayList)5 CommentTimeLineData (org.qii.weiciyuan.bean.android.CommentTimeLineData)5 MessageListBean (org.qii.weiciyuan.bean.MessageListBean)4 ViewTreeObserver (android.view.ViewTreeObserver)3 HashMap (java.util.HashMap)3 CommentListBean (org.qii.weiciyuan.bean.CommentListBean)3 MessageBean (org.qii.weiciyuan.bean.MessageBean)3 MessageTimeLineData (org.qii.weiciyuan.bean.android.MessageTimeLineData)3 MentionTimeLineData (org.qii.weiciyuan.bean.android.MentionTimeLineData)2 View (android.view.View)1 WebView (android.webkit.WebView)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 GroupBean (org.qii.weiciyuan.bean.GroupBean)1