Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Room Tag Comparator : use a separate class and probably fix a crash #333

Merged
merged 2 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bugfix:
- Add API CallSoundsManager.startRingingSilently() to fix issue when incoming call sound is disable (vector-im/riot-android#2417)
- Use same TxId when resending an event. The eventId is used as a TxId. (vector-im/riot-android#1997)
- Fix bad bing on '@room' pattern. (vector-im/riot-android#2461)
- Fix Crash loop reported by RageShake (vector-im/riot-android#2501)

API Change:
- Parameter historyVisibility removed from MxSession.createRoom(). It had no effect.
Expand Down
45 changes: 5 additions & 40 deletions matrix-sdk/src/main/java/org/matrix/androidsdk/MXSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.matrix.androidsdk.data.RoomState;
import org.matrix.androidsdk.data.RoomSummary;
import org.matrix.androidsdk.data.RoomTag;
import org.matrix.androidsdk.data.comparator.RoomComparatorWithTag;
import org.matrix.androidsdk.data.cryptostore.IMXCryptoStore;
import org.matrix.androidsdk.data.cryptostore.MXFileCryptoStore;
import org.matrix.androidsdk.data.metrics.MetricsListener;
Expand Down Expand Up @@ -1575,61 +1576,25 @@ public boolean isVoipCallSupported() {
* @return the rooms list.
*/
public List<Room> roomsWithTag(final String tag) {
List<Room> taggedRooms = new ArrayList<>();
final List<Room> taggedRooms = new ArrayList<>();

// sanity check
if (null == mDataHandler.getStore()) {
return taggedRooms;
}

if (!TextUtils.equals(tag, RoomTag.ROOM_TAG_NO_TAG)) {
Collection<Room> rooms = mDataHandler.getStore().getRooms();

final Collection<Room> rooms = mDataHandler.getStore().getRooms();
for (Room room : rooms) {
if (null != room.getAccountData().roomTag(tag)) {
taggedRooms.add(room);
}
}

if (taggedRooms.size() > 0) {
Collections.sort(taggedRooms, new Comparator<Room>() {
@Override
public int compare(Room r1, Room r2) {
int res = 0;

RoomTag tag1 = r1.getAccountData().roomTag(tag);
RoomTag tag2 = r2.getAccountData().roomTag(tag);

if ((null != tag1.mOrder) && (null != tag2.mOrder)) {
double diff = (tag1.mOrder - tag2.mOrder);
res = (diff == 0) ? 0 : (diff > 0) ? +1 : -1;
} else if (null != tag1.mOrder) {
res = -1;
} else if (null != tag2.mOrder) {
res = +1;
}

// In case of same order, order rooms by their last event
if (0 == res) {
IMXStore store = mDataHandler.getStore();

Event latestEvent1 = store.getLatestEvent(r1.getRoomId());
Event latestEvent2 = store.getLatestEvent(r2.getRoomId());

// sanity check
if ((null != latestEvent2) && (null != latestEvent1)) {
long diff = (latestEvent2.getOriginServerTs() - latestEvent1.getOriginServerTs());
res = (diff == 0) ? 0 : (diff > 0) ? +1 : -1;
}
}

return res;
}
});
Collections.sort(taggedRooms, new RoomComparatorWithTag(tag));
}
} else {
Collection<Room> rooms = mDataHandler.getStore().getRooms();

final Collection<Room> rooms = mDataHandler.getStore().getRooms();
for (Room room : rooms) {
if (!room.getAccountData().hasTags()) {
taggedRooms.add(room);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.androidsdk.data.comparator;

import org.matrix.androidsdk.data.Room;
import org.matrix.androidsdk.data.RoomTag;

import java.util.Comparator;

/**
* This class is responsible for comparing rooms by the tag's order
*/
public class RoomComparatorWithTag implements Comparator<Room> {

private final String mTag;

public RoomComparatorWithTag(final String tag) {
mTag = tag;
}

@Override
public int compare(final Room r1, final Room r2) {
final int res;
final RoomTag tag1 = r1.getAccountData().roomTag(mTag);
final RoomTag tag2 = r2.getAccountData().roomTag(mTag);

if (tag1.mOrder != null && tag2.mOrder != null) {
res = Double.compare(tag1.mOrder, tag2.mOrder);
} else if (tag1.mOrder != null) {
res = 1;
} else {
res = -1;
}
return res;
}
}