From f6d21534e0e06ba8ac993ce811bb28b788cb380a Mon Sep 17 00:00:00 2001 From: Al Lansley Date: Thu, 4 Apr 2024 13:34:08 +1100 Subject: [PATCH] Addressed PR feedback --- .../thoughtcrime/securesms/home/HomeActivity.kt | 9 --------- .../home/search/GlobalSearchInputLayout.kt | 4 ++++ .../home/search/GlobalSearchViewModel.kt | 16 +++++----------- .../securesms/search/SearchRepository.java | 13 ++++--------- 4 files changed, 13 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt index 5be0d275df..ccfa16beef 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/HomeActivity.kt @@ -12,7 +12,6 @@ import android.os.Build import android.os.Bundle import android.text.SpannableString import android.widget.Toast - import androidx.activity.viewModels import androidx.core.os.bundleOf import androidx.core.view.isVisible @@ -22,25 +21,20 @@ import androidx.lifecycle.repeatOnLifecycle import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView - import dagger.hilt.android.AndroidEntryPoint - import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext - import network.loki.messenger.R import network.loki.messenger.databinding.ActivityHomeBinding import network.loki.messenger.databinding.ViewMessageRequestBannerBinding import network.loki.messenger.libsession_util.ConfigBase - import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode - import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.jobs.JobQueue import org.session.libsession.messaging.sending_receiving.MessageSender @@ -53,7 +47,6 @@ import org.session.libsession.utilities.recipients.Recipient import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.ThreadUtils import org.session.libsignal.utilities.toHexString - import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.conversation.start.NewConversationFragment @@ -89,10 +82,8 @@ import org.thoughtcrime.securesms.util.disableClipping import org.thoughtcrime.securesms.util.push import org.thoughtcrime.securesms.util.show import org.thoughtcrime.securesms.util.themeState - import java.io.IOException import java.util.Locale - import javax.inject.Inject @AndroidEntryPoint diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchInputLayout.kt b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchInputLayout.kt index 1537769cdc..c70979f852 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchInputLayout.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchInputLayout.kt @@ -2,6 +2,8 @@ package org.thoughtcrime.securesms.home.search import android.content.Context import android.text.Editable +import android.text.InputFilter +import android.text.InputFilter.LengthFilter import android.text.TextWatcher import android.util.AttributeSet import android.view.KeyEvent @@ -15,6 +17,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import network.loki.messenger.databinding.ViewGlobalSearchInputBinding + class GlobalSearchInputLayout @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : LinearLayout(context, attrs), @@ -34,6 +37,7 @@ class GlobalSearchInputLayout @JvmOverloads constructor( binding.searchInput.onFocusChangeListener = this binding.searchInput.addTextChangedListener(this) binding.searchInput.setOnEditorActionListener(this) + binding.searchInput.setFilters( arrayOf(LengthFilter(100)) ) // 100 char search limit binding.searchCancel.setOnClickListener(this) binding.searchClear.setOnClickListener(this) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt index 4a917fd291..4232754c4e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt @@ -36,8 +36,6 @@ class GlobalSearchViewModel @Inject constructor(private val searchRepository: Se private val _queryText: MutableStateFlow = MutableStateFlow("") - private var settableFuture = SettableFuture() - fun postQuery(charSequence: CharSequence?) { charSequence ?: return _queryText.value = charSequence @@ -48,19 +46,15 @@ class GlobalSearchViewModel @Inject constructor(private val searchRepository: Se _queryText .buffer(onBufferOverflow = BufferOverflow.DROP_OLDEST) .mapLatest { query -> - // Minimum search term is 2 characters - if (query.trim().length < 2) { + // Early exit on empty search query + if (query.trim().isEmpty()) { SearchResult.EMPTY } else { - // user input delay here in case we get a new query within a few hundred ms - // this coroutine will be cancelled and expensive query will not be run if typing quickly - // first query of 2 characters will be instant however + // User input delay in case we get a new query within a few hundred ms this + // coroutine will be cancelled and expensive query will not be run delay(300) - // If we already have a search running then stop it - if (!settableFuture.isDone) { settableFuture.cancel(true) } - - settableFuture = SettableFuture() + val settableFuture = SettableFuture() searchRepository.query(query.toString(), settableFuture::set) try { // search repository doesn't play nicely with suspend functions (yet) diff --git a/app/src/main/java/org/thoughtcrime/securesms/search/SearchRepository.java b/app/src/main/java/org/thoughtcrime/securesms/search/SearchRepository.java index d2c51ad10f..f2adbf2349 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/search/SearchRepository.java +++ b/app/src/main/java/org/thoughtcrime/securesms/search/SearchRepository.java @@ -4,11 +4,8 @@ import android.content.Context; import android.database.Cursor; import android.database.DatabaseUtils; import android.database.MergeCursor; - import androidx.annotation.NonNull; - import com.annimon.stream.Stream; - import org.session.libsession.messaging.contacts.Contact; import org.session.libsession.utilities.Address; import org.session.libsession.utilities.GroupRecord; @@ -26,13 +23,11 @@ import org.thoughtcrime.securesms.database.model.ThreadRecord; import org.thoughtcrime.securesms.search.model.MessageResult; import org.thoughtcrime.securesms.search.model.SearchResult; import org.thoughtcrime.securesms.util.Stopwatch; - import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.Executor; - import kotlin.Pair; // Class to manage data retrieval for search @@ -75,9 +70,9 @@ public class SearchRepository { } public void query(@NonNull String query, @NonNull Callback callback) { - // If the sanitized search is empty or is less than 2 chars then abort without search + // If the sanitized search is empty then abort without search String cleanQuery = sanitizeQuery(query).trim(); - if (cleanQuery.isEmpty() || cleanQuery.length() < 2) { + if (cleanQuery.isEmpty()) { callback.onResult(SearchResult.EMPTY); return; } @@ -102,9 +97,9 @@ public class SearchRepository { } public void query(@NonNull String query, long threadId, @NonNull Callback> callback) { - // If the sanitized search query is empty or less than 2 chars then abort the search + // If the sanitized search query is empty then abort the search String cleanQuery = sanitizeQuery(query).trim(); - if (cleanQuery.isEmpty() || cleanQuery.length() < 2) { + if (cleanQuery.isEmpty()) { callback.onResult(CursorList.emptyList()); return; }