Search in sources :

Example 1 with RoomTag

use of org.matrix.androidsdk.data.RoomTag in project matrix-android-sdk by matrix-org.

the class MXSession method tagOrderToBeAtIndex.

/**
 * Compute the tag order to use for a room tag so that the room will appear in the expected position
 * in the list of rooms stamped with this tag.
 *
 * @param index       the targeted index of the room in the list of rooms with the tag `tag`.
 * @param originIndex the origin index. Integer.MAX_VALUE if there is none.
 * @param tag         the tag
 * @return the tag order to apply to get the expected position.
 */
public Double tagOrderToBeAtIndex(int index, int originIndex, String tag) {
    // Algo (and the [0.0, 1.0] assumption) inspired from matrix-react-sdk:
    // We sort rooms by the lexicographic ordering of the 'order' metadata on their tags.
    // For convenience, we calculate this for now a floating point number between 0.0 and 1.0.
    // by default we're next to the beginning of the list
    Double orderA = 0.0;
    // by default we're next to the end of the list too
    Double orderB = 1.0;
    List<Room> roomsWithTag = roomsWithTag(tag);
    if (roomsWithTag.size() > 0) {
        // because the object will be removed from the list to be inserted after its destination
        if ((originIndex != Integer.MAX_VALUE) && (originIndex < index)) {
            index++;
        }
        if (index > 0) {
            // Bound max index to the array size
            int prevIndex = (index < roomsWithTag.size()) ? index : roomsWithTag.size();
            RoomTag prevTag = roomsWithTag.get(prevIndex - 1).getAccountData().roomTag(tag);
            if (null == prevTag.mOrder) {
                Log.e(LOG_TAG, "computeTagOrderForRoom: Previous room in sublist has no ordering metadata. This should never happen.");
            } else {
                orderA = prevTag.mOrder;
            }
        }
        if (index <= roomsWithTag.size() - 1) {
            RoomTag nextTag = roomsWithTag.get(index).getAccountData().roomTag(tag);
            if (null == nextTag.mOrder) {
                Log.e(LOG_TAG, "computeTagOrderForRoom: Next room in sublist has no ordering metadata. This should never happen.");
            } else {
                orderB = nextTag.mOrder;
            }
        }
    }
    return (orderA + orderB) / 2.0;
}
Also used : RoomTag(org.matrix.androidsdk.data.RoomTag) Room(org.matrix.androidsdk.data.Room)

Aggregations

Room (org.matrix.androidsdk.data.Room)1 RoomTag (org.matrix.androidsdk.data.RoomTag)1