mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 01:40:07 +01:00
Add CFV2 Sticker Suggestions.
This commit is contained in:
committed by
Nicholas Tinsley
parent
2fbcc23451
commit
4ce05a064c
@@ -1,9 +1,9 @@
|
||||
package org.thoughtcrime.securesms.stickers;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.thoughtcrime.securesms.components.emoji.EmojiUtil;
|
||||
@@ -12,52 +12,80 @@ import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.StickerTable;
|
||||
import org.thoughtcrime.securesms.database.StickerTable.StickerRecordReader;
|
||||
import org.thoughtcrime.securesms.database.model.StickerRecord;
|
||||
import org.thoughtcrime.securesms.emoji.EmojiSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import io.reactivex.rxjava3.core.Single;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public final class StickerSearchRepository {
|
||||
|
||||
private final StickerTable stickerDatabase;
|
||||
private final AttachmentTable attachmentDatabase;
|
||||
|
||||
public StickerSearchRepository(@NonNull Context context) {
|
||||
public StickerSearchRepository() {
|
||||
this.stickerDatabase = SignalDatabase.stickers();
|
||||
this.attachmentDatabase = SignalDatabase.attachments();
|
||||
}
|
||||
|
||||
public @NonNull Single<List<StickerRecord>> searchByEmoji(@NonNull String emoji) {
|
||||
if (emoji.isEmpty() || emoji.length() > EmojiSource.getLatest().getMaxEmojiLength()) {
|
||||
return Single.just(Collections.emptyList());
|
||||
}
|
||||
|
||||
return Single.fromCallable(() -> searchByEmojiSync(emoji));
|
||||
}
|
||||
|
||||
public void searchByEmoji(@NonNull String emoji, @NonNull Callback<List<StickerRecord>> callback) {
|
||||
SignalExecutors.BOUNDED.execute(() -> {
|
||||
String searchEmoji = EmojiUtil.getCanonicalRepresentation(emoji);
|
||||
List<StickerRecord> out = new ArrayList<>();
|
||||
Set<String> possible = EmojiUtil.getAllRepresentations(searchEmoji);
|
||||
callback.onResult(searchByEmojiSync(emoji));
|
||||
});
|
||||
}
|
||||
|
||||
for (String candidate : possible) {
|
||||
try (StickerRecordReader reader = new StickerRecordReader(stickerDatabase.getStickersByEmoji(candidate))) {
|
||||
StickerRecord record = null;
|
||||
while ((record = reader.getNext()) != null) {
|
||||
out.add(record);
|
||||
}
|
||||
@WorkerThread
|
||||
private List<StickerRecord> searchByEmojiSync(@NonNull String emoji) {
|
||||
String searchEmoji = EmojiUtil.getCanonicalRepresentation(emoji);
|
||||
List<StickerRecord> out = new ArrayList<>();
|
||||
Set<String> possible = EmojiUtil.getAllRepresentations(searchEmoji);
|
||||
|
||||
for (String candidate : possible) {
|
||||
try (StickerRecordReader reader = new StickerRecordReader(stickerDatabase.getStickersByEmoji(candidate))) {
|
||||
StickerRecord record = null;
|
||||
while ((record = reader.getNext()) != null) {
|
||||
out.add(record);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
callback.onResult(out);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
public @NonNull Single<Boolean> getStickerFeatureAvailability() {
|
||||
return Single.fromCallable(this::getStickerFeatureAvailabilitySync)
|
||||
.observeOn(Schedulers.io());
|
||||
}
|
||||
|
||||
public void getStickerFeatureAvailability(@NonNull Callback<Boolean> callback) {
|
||||
SignalExecutors.BOUNDED.execute(() -> {
|
||||
try (Cursor cursor = stickerDatabase.getAllStickerPacks("1")) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
callback.onResult(true);
|
||||
} else {
|
||||
callback.onResult(attachmentDatabase.hasStickerAttachments());
|
||||
}
|
||||
}
|
||||
callback.onResult(getStickerFeatureAvailabilitySync());
|
||||
});
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
private Boolean getStickerFeatureAvailabilitySync() {
|
||||
try (Cursor cursor = stickerDatabase.getAllStickerPacks("1")) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return true;
|
||||
} else {
|
||||
return attachmentDatabase.hasStickerAttachments();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface Callback<T> {
|
||||
void onResult(T result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user