From 2ee70be3e46b0a63e187edda559562bf7844f118 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 20 May 2020 17:28:54 +0300 Subject: [PATCH] search: fix reacting with emojis --- .../components/search/SearchViewModel.kt | 18 +++++++++ .../fragments/SearchStatusesFragment.kt | 40 +++++++++++++++++-- .../interfaces/StatusActionListener.java | 4 +- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt index cf87fab1..017abec6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/SearchViewModel.kt @@ -1,6 +1,7 @@ package com.keylesspalace.tusky.components.search import android.util.Log +import android.view.View import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.paging.PagedList @@ -217,6 +218,23 @@ class SearchViewModel @Inject constructor( search(currentQuery) } + fun setEmojiReactionForStatus(idx: Int, newStatus: Status) { + val newPair = Pair(newStatus, + ViewDataUtils.statusToViewData(newStatus, alwaysShowSensitiveMedia, alwaysOpenSpoiler)!!) + loadedStatuses[idx] = newPair + repoResultStatus.value?.refresh?.invoke() + } + + fun emojiReact(react: Boolean, emoji: String, statusId: String) { + loadedStatuses.indexOfFirst { it.first.id == statusId }.let { idx -> + timelineCases.react(emoji, statusId, react) + .subscribe( + { newStatus -> setEmojiReactionForStatus(idx, newStatus)}, + { Log.d(TAG,"Failed to react with $emoji to ${loadedStatuses[idx].first.id}", it)} + ) + .autoDispose() + } + } companion object { private const val TAG = "SearchViewModel" diff --git a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt index 24b13dfa..07245338 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/search/fragments/SearchStatusesFragment.kt @@ -25,6 +25,7 @@ import android.content.pm.PackageManager import android.net.Uri import android.os.Environment import android.util.Log +import android.view.MenuItem import android.view.View import android.widget.Toast import androidx.appcompat.app.AlertDialog @@ -38,16 +39,15 @@ import androidx.paging.PagedListAdapter import androidx.preference.PreferenceManager import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager -import com.keylesspalace.tusky.BaseActivity -import com.keylesspalace.tusky.MainActivity -import com.keylesspalace.tusky.R -import com.keylesspalace.tusky.ViewMediaActivity +import com.keylesspalace.tusky.* +import com.keylesspalace.tusky.AccountListActivity.Companion.newIntent import com.keylesspalace.tusky.components.compose.ComposeActivity import com.keylesspalace.tusky.components.compose.ComposeActivity.ComposeOptions import com.keylesspalace.tusky.components.report.ReportActivity import com.keylesspalace.tusky.components.search.adapter.SearchStatusesAdapter import com.keylesspalace.tusky.db.AccountEntity import com.keylesspalace.tusky.entity.Attachment +import com.keylesspalace.tusky.entity.EmojiReaction import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.entity.Status.Mention import com.keylesspalace.tusky.interfaces.AccountSelectionListener @@ -466,4 +466,36 @@ class SearchStatusesFragment : SearchFragment + when (item.itemId) { + R.id.emoji_react -> { + onEmojiReact(true, reaction.name, statusId) + return@setOnMenuItemClickListener true + } + R.id.emoji_unreact -> { + onEmojiReact(false, reaction.name, statusId) + return@setOnMenuItemClickListener true + } + R.id.emoji_reacted_by -> { + val intent = newIntent(context, AccountListActivity.Type.REACTED, statusId, reaction.name) + (requireActivity() as BaseActivity).startActivityWithSlideInAnimation(intent) + return@setOnMenuItemClickListener true + } + } + false + } + popup.show() + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/interfaces/StatusActionListener.java b/app/src/main/java/com/keylesspalace/tusky/interfaces/StatusActionListener.java index a30fffd2..16d28eac 100644 --- a/app/src/main/java/com/keylesspalace/tusky/interfaces/StatusActionListener.java +++ b/app/src/main/java/com/keylesspalace/tusky/interfaces/StatusActionListener.java @@ -65,7 +65,7 @@ public interface StatusActionListener extends LinkListener { void onVoteInPoll(int position, @NonNull List choices); default void onMute(int position, boolean isMuted) {} - default void onEmojiReact(final boolean react, final String emoji, final String statusId) {}; - default void onEmojiReactMenu(@NonNull View view, final EmojiReaction emoji, final String statusId) {}; + default void onEmojiReact(@NonNull final boolean react, @NonNull final String emoji, @NonNull final String statusId) {}; + default void onEmojiReactMenu(@NonNull View view, @NonNull final EmojiReaction emoji, @NonNull final String statusId) {}; }