From 7ab8b3bb4470a7c9c6524c32a3c61ff9b7963f68 Mon Sep 17 00:00:00 2001 From: cooltey Date: Thu, 29 Aug 2024 16:49:15 -0700 Subject: [PATCH] Multiple langs --- .../RecommendedContentFragment.kt | 8 +++++++- .../RecommendedContentViewModel.kt | 14 +++++++++----- .../org/wikipedia/search/RecentSearchesFragment.kt | 9 ++++++++- .../java/org/wikipedia/search/SearchFragment.kt | 2 ++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentFragment.kt b/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentFragment.kt index 6fd8944338c..75bb26bf60d 100644 --- a/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentFragment.kt +++ b/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentFragment.kt @@ -20,6 +20,7 @@ import org.wikipedia.analytics.ABTest import org.wikipedia.analytics.metricsplatform.RecommendedContentAnalyticsHelper import org.wikipedia.databinding.FragmentRecommendedContentBinding import org.wikipedia.databinding.ItemRecommendedContentSearchHistoryBinding +import org.wikipedia.dataclient.WikiSite import org.wikipedia.dataclient.page.PageSummary import org.wikipedia.history.HistoryEntry import org.wikipedia.history.HistoryFragment @@ -187,12 +188,17 @@ class RecommendedContentFragment : Fragment() { } } + fun reload(wikiSite: WikiSite) { + viewModel.reload(wikiSite) + } + companion object { const val ARG_IN_HISTORY = "inHistory" const val ARG_SECTION_IDS = "sectionIds" - fun newInstance(inHistory: Boolean, sectionIds: List) = RecommendedContentFragment().apply { + fun newInstance(wikiSite: WikiSite, inHistory: Boolean, sectionIds: List) = RecommendedContentFragment().apply { arguments = bundleOf( + Constants.ARG_WIKISITE to wikiSite, ARG_IN_HISTORY to inHistory, ARG_SECTION_IDS to sectionIds ) diff --git a/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentViewModel.kt b/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentViewModel.kt index 1360720c130..592b8ab64d8 100644 --- a/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentViewModel.kt +++ b/app/src/main/java/org/wikipedia/recommendedcontent/RecommendedContentViewModel.kt @@ -18,7 +18,9 @@ import org.wikipedia.R import org.wikipedia.WikipediaApp import org.wikipedia.database.AppDatabase import org.wikipedia.dataclient.ServiceFactory +import org.wikipedia.dataclient.WikiSite import org.wikipedia.dataclient.page.PageSummary +import org.wikipedia.extensions.parcelable import org.wikipedia.feed.aggregated.AggregatedFeedContent import org.wikipedia.feed.topread.TopRead import org.wikipedia.history.HistoryEntry @@ -34,7 +36,7 @@ import java.util.Date class RecommendedContentViewModel(bundle: Bundle) : ViewModel() { - val wikiSite = WikipediaApp.instance.wikiSite + var wikiSite = bundle.parcelable(Constants.ARG_WIKISITE)!! val inHistory = bundle.getBoolean(RecommendedContentFragment.ARG_IN_HISTORY) private val sectionIds = bundle.getIntegerArrayList(RecommendedContentFragment.ARG_SECTION_IDS)!! val sections = sectionIds.map { RecommendedContentSection.find(it) } @@ -52,6 +54,11 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() { val recommendedContentState = _recommendedContentState.asStateFlow() init { + reload(wikiSite) + } + + fun reload(wikiSite: WikiSite) { + this.wikiSite = wikiSite loadSearchHistory() loadRecommendedContent(sections) } @@ -158,7 +165,7 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() { private suspend fun loadRecentSearches(): List { return withContext(Dispatchers.IO) { AppDatabase.instance.recentSearchDao().getRecentSearches().map { - PageTitle(it.text, WikipediaApp.instance.wikiSite).apply { + PageTitle(it.text, wikiSite).apply { // Put timestamp in description for the delete action. description = it.timestamp.time.toString() } @@ -173,7 +180,6 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() { return@withContext it } - val wikiSite = WikipediaApp.instance.wikiSite val hasParentLanguageCode = !WikipediaApp.instance.languageState.getDefaultLanguageCode(wikiSite.languageCode).isNullOrEmpty() val date = DateUtil.getUtcRequestDateFor(0) var feedContentResponse = ServiceFactory.getRest(wikiSite).getFeedFeatured(date.year, date.month, date.day) @@ -214,7 +220,6 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() { private suspend fun loadExplore(searchTerm: String): List { return withContext(Dispatchers.IO) { - val wikiSite = WikipediaApp.instance.wikiSite val moreLikeResponse = ServiceFactory.get(wikiSite).searchMoreLike("morelike:$searchTerm", Constants.SUGGESTION_REQUEST_ITEMS, Constants.SUGGESTION_REQUEST_ITEMS) val hasParentLanguageCode = !WikipediaApp.instance.languageState.getDefaultLanguageCode(wikiSite.languageCode).isNullOrEmpty() @@ -253,7 +258,6 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() { } private suspend fun loadPlaces(): List { - val wikiSite = WikipediaApp.instance.wikiSite return withContext(Dispatchers.IO) { Prefs.placesLastLocationAndZoomLevel?.let { pair -> val location = pair.first diff --git a/app/src/main/java/org/wikipedia/search/RecentSearchesFragment.kt b/app/src/main/java/org/wikipedia/search/RecentSearchesFragment.kt index 6d159affc0e..21457324f59 100644 --- a/app/src/main/java/org/wikipedia/search/RecentSearchesFragment.kt +++ b/app/src/main/java/org/wikipedia/search/RecentSearchesFragment.kt @@ -45,6 +45,7 @@ class RecentSearchesFragment : Fragment() { private val namespaceHints = listOf(Namespace.USER, Namespace.PORTAL, Namespace.HELP) private val namespaceMap = ConcurrentHashMap>() private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable -> L.e(throwable) } + private var recommendedContentFragment: RecommendedContentFragment? = null var callback: Callback? = null val recentSearchList = mutableListOf() @@ -103,8 +104,10 @@ class RecentSearchesFragment : Fragment() { } else { RecommendedContentSection.personalizeList().map { it.id } // Group 3 } + val langeCode = callback?.getLangCode() ?: WikipediaApp.instance.appOrSystemLanguageCode + recommendedContentFragment = RecommendedContentFragment.newInstance(wikiSite = WikiSite.forLanguageCode(langeCode), inHistory = false, sectionIds) childFragmentManager.beginTransaction() - .add(R.id.fragmentOverlayContainer, RecommendedContentFragment.newInstance(inHistory = false, sectionIds), null) + .add(R.id.fragmentOverlayContainer, recommendedContentFragment!!, null) .addToBackStack(null) .commit() } @@ -134,6 +137,10 @@ class RecentSearchesFragment : Fragment() { } } + fun reloadRecommendedContent(wikiSite: WikiSite) { + recommendedContentFragment?.reload(wikiSite) + } + suspend fun updateList() { val searches: List val nsMap: Map diff --git a/app/src/main/java/org/wikipedia/search/SearchFragment.kt b/app/src/main/java/org/wikipedia/search/SearchFragment.kt index 56ade72be88..8fabbc3c105 100644 --- a/app/src/main/java/org/wikipedia/search/SearchFragment.kt +++ b/app/src/main/java/org/wikipedia/search/SearchFragment.kt @@ -25,6 +25,7 @@ import org.wikipedia.WikipediaApp import org.wikipedia.analytics.eventplatform.PlacesEvent import org.wikipedia.database.AppDatabase import org.wikipedia.databinding.FragmentSearchBinding +import org.wikipedia.dataclient.WikiSite import org.wikipedia.history.HistoryEntry import org.wikipedia.json.JsonUtil import org.wikipedia.page.PageActivity @@ -336,6 +337,7 @@ class SearchFragment : Fragment(), SearchResultsFragment.Callback, RecentSearche searchLanguageCode = selectedLanguageCode searchResultsFragment.setLayoutDirection(searchLanguageCode) recentSearchesFragment.reloadRecentSearches() + recentSearchesFragment.reloadRecommendedContent(WikiSite.forLanguageCode(searchLanguageCode)) startSearch(query, false) }