Skip to content

Commit

Permalink
Merge pull request #20135 from wordpress-mobile/issue/20011-subscript…
Browse files Browse the repository at this point in the history
…ions-chip-count-not-updated-after-subscribing-to-blog-on-search

[Reader IA] Refresh blogs count after subscribing/unsubscribing from search screen
  • Loading branch information
RenanLukas authored Feb 7, 2024
2 parents 69b9894 + 00dd473 commit 24b46d0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -67,6 +73,8 @@ class ReaderFragment : Fragment(R.layout.reader_fragment_layout), ScrollableView

private var binding: ReaderFragmentLayoutBinding? = null

private var readerSearchResultLauncher: ActivityResultLauncher<Intent>? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding = ReaderFragmentLayoutBinding.bind(view).apply {
initTopAppBar()
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}
}

0 comments on commit 24b46d0

Please sign in to comment.