Skip to content

Commit

Permalink
Improve performance of Link Preview when used in Places. (#4499)
Browse files Browse the repository at this point in the history
* Improve performance of Link Preview when used in Places.

* Also check if logged in.
  • Loading branch information
dbrant authored Mar 1, 2024
1 parent 4799fa2 commit 065f7c0
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.wikipedia.analytics.eventplatform.WatchlistAnalyticsHelper
import org.wikipedia.auth.AccountUtil
import org.wikipedia.database.AppDatabase
import org.wikipedia.dataclient.ServiceFactory
import org.wikipedia.extensions.parcelable
Expand Down Expand Up @@ -40,9 +42,12 @@ class LinkPreviewViewModel(bundle: Bundle) : ViewModel() {
viewModelScope.launch(CoroutineExceptionHandler { _, throwable ->
_uiState.value = LinkPreviewViewState.Error(throwable)
}) {
val response = ServiceFactory.getRest(pageTitle.wikiSite)
.getSummaryResponseSuspend(pageTitle.prefixedText, null, null, null, null, null)
val summaryCall = async { ServiceFactory.getRest(pageTitle.wikiSite)
.getSummaryResponseSuspend(pageTitle.prefixedText, null, null, null, null, null) }

val watchedCall = async { if (fromPlaces && AccountUtil.isLoggedIn) ServiceFactory.get(pageTitle.wikiSite).getWatchedStatus(pageTitle.prefixedText) else null }

val response = summaryCall.await()
val summary = response.body()!!
// Rebuild our PageTitle, since it may have been redirected or normalized.
val oldFragment = pageTitle.fragment
Expand All @@ -60,9 +65,7 @@ class LinkPreviewViewModel(bundle: Bundle) : ViewModel() {
}

if (fromPlaces) {
val watchStatus = ServiceFactory.get(pageTitle.wikiSite).getWatchedStatus(pageTitle.prefixedText).query?.firstPage()
isWatched = watchStatus?.watched ?: false

isWatched = watchedCall.await()?.query?.firstPage()?.watched ?: false
val readingList = AppDatabase.instance.readingListPageDao().findPageInAnyList(pageTitle)
isInReadingList = readingList != null
}
Expand Down

0 comments on commit 065f7c0

Please sign in to comment.