-
-
Notifications
You must be signed in to change notification settings - Fork 686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor preview list fetch mechanism #4501
Open
Pinaki93
wants to merge
7
commits into
wikimedia:main
Choose a base branch
from
Pinaki93:preview_reading_list_refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a207372
refactors preview list fetch mechanism
Pinaki93 735438b
Merge branch 'main' into preview_reading_list_refactor
Pinaki93 f485ded
Merge branch 'main' into preview_reading_list_refactor
cooltey 847891b
fix lint issue
Pinaki93 ae80c1c
Merge remote-tracking branch 'origin/preview_reading_list_refactor' i…
Pinaki93 dc93958
Merge branch 'main' into preview_reading_list_refactor
Pinaki93 86f8070
Merge branch 'main' into preview_reading_list_refactor
cooltey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
app/src/main/java/org/wikipedia/readinglist/PreviewReadingListRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.wikipedia.readinglist | ||
|
||
import android.content.Context | ||
import android.util.Base64 | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.int | ||
import org.wikipedia.R | ||
import org.wikipedia.WikipediaApp | ||
import org.wikipedia.dataclient.Service | ||
import org.wikipedia.dataclient.ServiceFactory | ||
import org.wikipedia.dataclient.WikiSite | ||
import org.wikipedia.dataclient.mwapi.MwQueryPage | ||
import org.wikipedia.json.JsonUtil | ||
import org.wikipedia.readinglist.database.ReadingList | ||
import org.wikipedia.readinglist.database.ReadingListPage | ||
import org.wikipedia.settings.Prefs | ||
import org.wikipedia.util.DateUtil | ||
import org.wikipedia.util.ImageUrlUtil | ||
import org.wikipedia.util.StringUtil | ||
import java.util.Date | ||
|
||
class PreviewReadingListRepository( | ||
private val context: Context = WikipediaApp.instance, | ||
private val prefs: Prefs = Prefs | ||
) { | ||
|
||
suspend fun getPreviewReadingList(): ReadingList? { | ||
val encodedJson = prefs.receiveReadingListsData | ||
if (encodedJson.isNullOrEmpty()) { | ||
return null | ||
} | ||
|
||
return fetchReadingListsFromServer(encodedJson) | ||
} | ||
|
||
private suspend fun fetchReadingListsFromServer(encodedJson: String): ReadingList? { | ||
val readingListData = getExportedReadingLists(encodedJson) ?: return null | ||
|
||
val listTitle = readingListData.name.orEmpty() | ||
.ifEmpty { context.getString(R.string.reading_lists_preview_header_title) } | ||
val listDescription = readingListData.description.orEmpty() | ||
.ifEmpty { DateUtil.getTimeAndDateString(context, Date()) } | ||
|
||
val readingLists = readingListData.list | ||
val listPages = readingLists | ||
.map { fetchPagesForReadingList(it) } | ||
.reduce { allPages, currentPayload -> allPages + currentPayload } | ||
.sortedBy { it.apiTitle } | ||
|
||
val readingList = ReadingList(listTitle, listDescription) | ||
readingList.pages.addAll(listPages) | ||
|
||
return readingList | ||
} | ||
|
||
private suspend fun fetchPagesForReadingList( | ||
list: Map.Entry<String, Collection<JsonPrimitive>> | ||
): List<ReadingListPage> { | ||
val wikiSite = WikiSite.forLanguageCode(list.key) | ||
val chunks = list.value.chunked(ReadingListsShareHelper.API_MAX_SIZE) | ||
val pages = mutableListOf<MwQueryPage>() | ||
for (chunk in chunks) { | ||
pages += fetchPagesForChunk(wikiSite, chunk) | ||
} | ||
|
||
return pages.map { page -> | ||
ReadingListPage( | ||
wikiSite, | ||
page.namespace(), | ||
page.displayTitle(wikiSite.languageCode), | ||
StringUtil.addUnderscores(page.title), | ||
page.description, | ||
ImageUrlUtil.getUrlForPreferredSize( | ||
original = page.thumbUrl().orEmpty(), | ||
size = Service.PREFERRED_THUMB_SIZE | ||
), | ||
lang = wikiSite.languageCode | ||
) | ||
} | ||
} | ||
|
||
private suspend fun fetchPagesForChunk( | ||
wikiSite: WikiSite, | ||
payload: List<JsonPrimitive> | ||
): MutableList<MwQueryPage> { | ||
val pages = mutableListOf<MwQueryPage>() | ||
val listOfTitles = payload.filter { it.isString }.map { it.content } | ||
val listOfIds = payload.filter { !it.isString }.map { it.int } | ||
if (listOfIds.isNotEmpty()) { | ||
val pagesById = ServiceFactory.get(wikiSite) | ||
.getInfoByPageIdsOrTitles( | ||
pageIds = listOfIds.joinToString(separator = "|") | ||
) | ||
.query?.pages.orEmpty() | ||
pages.addAll(pagesById) | ||
} | ||
if (listOfTitles.isNotEmpty()) { | ||
val pagesByTitles = ServiceFactory.get(wikiSite) | ||
.getInfoByPageIdsOrTitles(titles = listOfTitles.joinToString(separator = "|")) | ||
.query?.pages.orEmpty() | ||
pages.addAll(pagesByTitles) | ||
} | ||
|
||
return pages | ||
} | ||
|
||
private fun getExportedReadingLists( | ||
encodedJson: String | ||
): ReadingListsShareHelper.ExportedReadingLists? { | ||
return JsonUtil.decodeFromString(String(Base64.decode(encodedJson, Base64.NO_WRAP))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
app/src/main/java/org/wikipedia/readinglist/ReadingListsReceiveHelper.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if use
Prefs.receiveReadingListsData
directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've converted it to a variable so as to support constructor injection (and easily write tests later with mocks). What do you think?