Add logging for unread thread ids.
This commit is contained in:
parent
359a39ddaf
commit
e69d944f11
4 changed files with 43 additions and 12 deletions
|
@ -25,6 +25,7 @@ import android.net.Uri;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.arch.core.util.Function;
|
||||
|
||||
import com.annimon.stream.Collectors;
|
||||
import com.annimon.stream.Stream;
|
||||
|
@ -516,17 +517,21 @@ public class ThreadDatabase extends Database {
|
|||
}
|
||||
}
|
||||
|
||||
public long getUnreadThreadCount() {
|
||||
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
|
||||
String[] projection = SqlUtil.buildArgs("COUNT(*)");
|
||||
String where = READ + " != " + ReadStatus.READ.serialize() + " AND " + ARCHIVED + " = 0 AND " + MEANINGFUL_MESSAGES + " != 0";
|
||||
public @NonNull Long getUnreadThreadCount() {
|
||||
return getUnreadThreadIdAggregate(SqlUtil.COUNT, cursor -> CursorUtil.getAggregateOrDefault(cursor, 0L, cursor::getLong));
|
||||
}
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, projection, where, null, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getLong(0);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
public @Nullable String getUnreadThreadIdList() {
|
||||
return getUnreadThreadIdAggregate(SqlUtil.buildArgs("GROUP_CONCAT(" + ID + ")"),
|
||||
cursor -> CursorUtil.getAggregateOrDefault(cursor, null, cursor::getString));
|
||||
}
|
||||
|
||||
private @NonNull <T> T getUnreadThreadIdAggregate(@NonNull String[] aggregator, @NonNull Function<Cursor, T> mapCursorToType) {
|
||||
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
|
||||
String where = READ + " != " + ReadStatus.READ.serialize() + " AND " + ARCHIVED + " = 0 AND " + MEANINGFUL_MESSAGES + " != 0";
|
||||
|
||||
try (Cursor cursor = db.query(TABLE_NAME, aggregator, where, null, null, null, null)) {
|
||||
return mapCursorToType.apply(cursor);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.stories.tabs
|
|||
|
||||
import io.reactivex.rxjava3.core.Observable
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.database.DatabaseObserver
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
|
||||
|
@ -9,15 +10,27 @@ import org.thoughtcrime.securesms.recipients.Recipient
|
|||
|
||||
class ConversationListTabRepository {
|
||||
|
||||
companion object {
|
||||
private val TAG = Log.tag(ConversationListTabRepository::class.java)
|
||||
}
|
||||
|
||||
fun getNumberOfUnreadConversations(): Observable<Long> {
|
||||
return Observable.create<Long> {
|
||||
val listener = DatabaseObserver.Observer {
|
||||
fun refresh() {
|
||||
it.onNext(SignalDatabase.threads.unreadThreadCount)
|
||||
|
||||
val ids = SignalDatabase.threads.unreadThreadIdList
|
||||
Log.d(TAG, "Unread threads: { $ids }")
|
||||
}
|
||||
|
||||
val listener = DatabaseObserver.Observer {
|
||||
refresh()
|
||||
}
|
||||
|
||||
ApplicationDependencies.getDatabaseObserver().registerConversationListObserver(listener)
|
||||
it.setCancellable { ApplicationDependencies.getDatabaseObserver().unregisterObserver(listener) }
|
||||
it.onNext(SignalDatabase.threads.unreadThreadCount)
|
||||
|
||||
refresh()
|
||||
}.subscribeOn(Schedulers.io())
|
||||
}
|
||||
|
||||
|
|
|
@ -3,8 +3,10 @@ package org.signal.core.util;
|
|||
import android.database.Cursor;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
public final class CursorUtil {
|
||||
|
@ -94,4 +96,12 @@ public final class CursorUtil {
|
|||
|
||||
return row.toString();
|
||||
}
|
||||
|
||||
public static @Nullable <T> T getAggregateOrDefault(@NonNull Cursor cursor, @Nullable T defaultValue, @NonNull Function<Integer, T> cursorColumnFn) {
|
||||
if (cursor.moveToFirst()) {
|
||||
return cursorColumnFn.apply(0);
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,9 @@ object SqlUtil {
|
|||
/** The maximum number of arguments (i.e. question marks) allowed in a SQL statement. */
|
||||
private const val MAX_QUERY_ARGS = 999
|
||||
|
||||
@JvmField
|
||||
val COUNT = arrayOf("COUNT(*)")
|
||||
|
||||
@JvmStatic
|
||||
fun tableExists(db: SupportSQLiteDatabase, table: String): Boolean {
|
||||
db.query("SELECT name FROM sqlite_master WHERE type=? AND name=?", arrayOf("table", table)).use { cursor ->
|
||||
|
|
Loading…
Add table
Reference in a new issue