From b62e21a67489d47cb33443db706cc3881b834925 Mon Sep 17 00:00:00 2001 From: Renan Lukas <14964993+RenanLukas@users.noreply.github.com> Date: Tue, 6 Feb 2024 20:33:32 -0300 Subject: [PATCH 1/2] Refresh blogs count after subscribing/unsubscribing from search screen --- .../ui/reader/ReaderActivityLauncher.java | 7 ++-- .../android/ui/reader/ReaderFragment.kt | 35 ++++++++++++++++++- .../ui/reader/ReaderPostListFragment.java | 6 +++- .../android/ui/reader/ReaderSearchActivity.kt | 12 +++++++ 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderActivityLauncher.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderActivityLauncher.java index 1299d325b157..2aea5b62e6e1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderActivityLauncher.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderActivityLauncher.java @@ -184,8 +184,11 @@ public static void showReaderTagPreview(Context context, @NonNull ReaderTag tag, } public static void showReaderSearch(Context context) { - Intent intent = new Intent(context, ReaderSearchActivity.class); - context.startActivity(intent); + context.startActivity(createReaderSearchIntent(context)); + } + + public static Intent createReaderSearchIntent(@NonNull final Context context) { + return new Intent(context, ReaderSearchActivity.class); } /* diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderFragment.kt index 28397a28fb70..3dfb55e0eedc 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderFragment.kt @@ -1,7 +1,13 @@ package org.wordpress.android.ui.reader +import android.app.Activity +import android.content.Context +import android.content.Intent import android.os.Bundle import android.view.View +import androidx.activity.result.ActivityResult +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.platform.ViewCompositionStrategy @@ -67,6 +73,8 @@ class ReaderFragment : Fragment(R.layout.reader_fragment_layout), ScrollableView private var binding: ReaderFragmentLayoutBinding? = null + private var readerSearchResultLauncher: ActivityResultLauncher? = null + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { binding = ReaderFragmentLayoutBinding.bind(view).apply { initTopAppBar() @@ -89,6 +97,28 @@ class ReaderFragment : Fragment(R.layout.reader_fragment_layout), ScrollableView activity?.let { viewModel.onScreenInBackground(it.isChangingConfigurations) } } + override fun onAttach(context: Context) { + super.onAttach(context) + initReaderSearchActivityResultLauncher() + } + + private fun initReaderSearchActivityResultLauncher() { + readerSearchResultLauncher = registerForActivityResult( + ActivityResultContracts.StartActivityForResult() + ) { result: ActivityResult -> + if (result.resultCode == Activity.RESULT_OK) { + val data = result.data + if (data != null) { + val shouldRefreshSubscriptions = + data.getBooleanExtra(ReaderSearchActivity.RESULT_SHOULD_REFRESH_SUBSCRIPTIONS, false) + if (shouldRefreshSubscriptions) { + getSubFilterViewModel()?.loadSubFilters() + } + } + } + } + } + private fun ReaderFragmentLayoutBinding.initTopAppBar() { readerTopBarComposeView.apply { setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) @@ -125,7 +155,10 @@ class ReaderFragment : Fragment(R.layout.reader_fragment_layout), ScrollableView } viewModel.showSearch.observeEvent(viewLifecycleOwner) { - ReaderActivityLauncher.showReaderSearch(context) + context?.let { + val intent = ReaderActivityLauncher.createReaderSearchIntent(it) + readerSearchResultLauncher?.launch(intent) + } } viewModel.showReaderInterests.observeEvent(viewLifecycleOwner) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index ec3e175ad5bb..be390bc2ab39 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -28,6 +28,7 @@ import androidx.appcompat.widget.SearchView; import androidx.core.content.ContextCompat; import androidx.core.text.HtmlCompat; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; @@ -1261,7 +1262,10 @@ public boolean onMenuItemActionExpand(@NonNull MenuItem item) { } @Override - public boolean onMenuItemActionCollapse(@NonNull MenuItem item) { + public boolean onMenuItemActionCollapse(@NonNull final MenuItem item) { + if (getActivity() instanceof ReaderSearchActivity) { + ((ReaderSearchActivity) requireActivity()).finishWithRefreshSubscriptionsResult(); + } requireActivity().finish(); return false; } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSearchActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSearchActivity.kt index f3d3ea24d51a..63a8a4809421 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSearchActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSearchActivity.kt @@ -1,5 +1,6 @@ package org.wordpress.android.ui.reader +import android.content.Intent import android.os.Bundle import dagger.hilt.android.AndroidEntryPoint import org.wordpress.android.R @@ -46,4 +47,15 @@ class ReaderSearchActivity : LocaleAwareActivity() { super.onPause() readerTracker.stop(MAIN_READER) } + + fun finishWithRefreshSubscriptionsResult() { + val data = Intent() + data.putExtra(ReaderSubsActivity.RESULT_SHOULD_REFRESH_SUBSCRIPTIONS, true) + setResult(RESULT_OK, data) + finish() + } + + companion object { + const val RESULT_SHOULD_REFRESH_SUBSCRIPTIONS = "should_refresh_subscriptions" + } } From 00dd4734f886e50d8372a38c8d71e90786e2edcb Mon Sep 17 00:00:00 2001 From: Renan Lukas <14964993+RenanLukas@users.noreply.github.com> Date: Wed, 7 Feb 2024 13:37:47 -0300 Subject: [PATCH 2/2] Fix checkstyle --- .../org/wordpress/android/ui/reader/ReaderPostListFragment.java | 1 - 1 file changed, 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index be390bc2ab39..d7bbc9b13c4b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -28,7 +28,6 @@ import androidx.appcompat.widget.SearchView; import androidx.core.content.ContextCompat; import androidx.core.text.HtmlCompat; -import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView;