Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[C-2107] Add local saves to offline queue (#2824)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjeffers authored Feb 11, 2023
1 parent 90198e7 commit 96d3d11
Showing 1 changed file with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { accountSelectors, getContext } from '@audius/common'
import {
accountSelectors,
getContext,
savedPageSelectors
} from '@audius/common'
import { takeEvery, select, call, put } from 'typed-redux-saga'

import { getAccountCollections } from 'app/screens/favorites-screen/selectors'
Expand All @@ -9,6 +13,8 @@ import { addOfflineItems, requestDownloadAllFavorites } from '../slice'

const { getUserId } = accountSelectors

const { getLocalSaves } = savedPageSelectors

export function* requestDownloadAllFavoritesSaga() {
yield* takeEvery(requestDownloadAllFavorites.type, downloadAllFavorites)
}
Expand All @@ -25,31 +31,49 @@ function* downloadAllFavorites() {
metadata: { reasons_for_download: [{ is_from_favorites: true }] }
})

const trackReasonsForDownload = [
{ is_from_favorites: true, collection_id: DOWNLOAD_REASON_FAVORITES }
]

// Add local saves
const now = Date.now().toString()
const localSaves = yield* select(getLocalSaves)
const localSavesToAdd: OfflineItem[] = Object.keys(localSaves)
.map((id) => parseInt(id, 10))
.map((id) => ({
type: 'track',
id,
metadata: {
favorite_created_at: now,
reasons_for_download: trackReasonsForDownload
}
}))

offlineItemsToAdd.push(...localSavesToAdd)

// Add favorited tracks from api
const apiClient = yield* getContext('apiClient')
const allFavoritedTracks = yield* call([apiClient, apiClient.getFavorites], {
currentUserId,
limit: 10000
})

if (!allFavoritedTracks) return
if (allFavoritedTracks) {
for (const favoritedTrack of allFavoritedTracks) {
const { save_item_id: trackId, created_at } = favoritedTrack

for (const favoritedTrack of allFavoritedTracks) {
const { save_item_id: trackId, created_at } = favoritedTrack
const downloadReason = {
is_from_favorites: true,
collection_id: DOWNLOAD_REASON_FAVORITES
offlineItemsToAdd.push({
type: 'track',
id: trackId,
metadata: {
favorite_created_at: created_at,
reasons_for_download: trackReasonsForDownload
}
})
}

offlineItemsToAdd.push({
type: 'track',
id: trackId,
metadata: {
favorite_created_at: created_at,
reasons_for_download: [downloadReason]
}
})
}

// Add favorited collections and their tracks
const favoritedCollections = yield* select(getAccountCollections)

for (const favoritedCollection of favoritedCollections) {
Expand Down

0 comments on commit 96d3d11

Please sign in to comment.