Skip to content

Commit

Permalink
Multiple langs
Browse files Browse the repository at this point in the history
  • Loading branch information
cooltey committed Aug 29, 2024
1 parent 677ac32 commit 7ab8b3b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Int>) = RecommendedContentFragment().apply {
fun newInstance(wikiSite: WikiSite, inHistory: Boolean, sectionIds: List<Int>) = RecommendedContentFragment().apply {
arguments = bundleOf(
Constants.ARG_WIKISITE to wikiSite,
ARG_IN_HISTORY to inHistory,
ARG_SECTION_IDS to sectionIds
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +36,7 @@ import java.util.Date

class RecommendedContentViewModel(bundle: Bundle) : ViewModel() {

val wikiSite = WikipediaApp.instance.wikiSite
var wikiSite = bundle.parcelable<WikiSite>(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) }
Expand All @@ -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)
}
Expand Down Expand Up @@ -158,7 +165,7 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() {
private suspend fun loadRecentSearches(): List<PageTitle> {
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()
}
Expand All @@ -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)
Expand Down Expand Up @@ -214,7 +220,6 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() {

private suspend fun loadExplore(searchTerm: String): List<PageSummary> {
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()

Expand Down Expand Up @@ -253,7 +258,6 @@ class RecommendedContentViewModel(bundle: Bundle) : ViewModel() {
}

private suspend fun loadPlaces(): List<PageSummary> {
val wikiSite = WikipediaApp.instance.wikiSite
return withContext(Dispatchers.IO) {
Prefs.placesLastLocationAndZoomLevel?.let { pair ->
val location = pair.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class RecentSearchesFragment : Fragment() {
private val namespaceHints = listOf(Namespace.USER, Namespace.PORTAL, Namespace.HELP)
private val namespaceMap = ConcurrentHashMap<String, Map<Namespace, String>>()
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable -> L.e(throwable) }
private var recommendedContentFragment: RecommendedContentFragment? = null
var callback: Callback? = null
val recentSearchList = mutableListOf<RecentSearch>()

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -134,6 +137,10 @@ class RecentSearchesFragment : Fragment() {
}
}

fun reloadRecommendedContent(wikiSite: WikiSite) {
recommendedContentFragment?.reload(wikiSite)
}

suspend fun updateList() {
val searches: List<RecentSearch>
val nsMap: Map<String, MwQueryResult.Namespace>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/wikipedia/search/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 7ab8b3b

Please sign in to comment.