This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
C-2119 Show downloaded collections only #2842
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
109 changes: 109 additions & 0 deletions
109
packages/mobile/src/screens/favorites-screen/useCollectionScreenData.ts
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,109 @@ | ||
import { useMemo } from 'react' | ||
|
||
import { | ||
reachabilitySelectors, | ||
shallowCompare, | ||
useProxySelector | ||
} from '@audius/common' | ||
import { useSelector } from 'react-redux' | ||
|
||
import { useIsOfflineModeEnabled } from 'app/hooks/useIsOfflineModeEnabled' | ||
import type { AppState } from 'app/store' | ||
import { | ||
getIsDoneLoadingFromDisk, | ||
getOfflineCollectionsStatus, | ||
getOfflineTrackStatus | ||
} from 'app/store/offline-downloads/selectors' | ||
import { OfflineDownloadStatus } from 'app/store/offline-downloads/slice' | ||
|
||
import { getAccountCollections } from './selectors' | ||
import { buildCollectionIdsToNumPlayableTracksMap } from './utils' | ||
|
||
const { getIsReachable } = reachabilitySelectors | ||
|
||
export const useCollectionScreenData = ( | ||
filterValue = '', | ||
collectionType: 'albums' | 'playlists' | ||
) => { | ||
const isDoneLoadingFromDisk = useSelector(getIsDoneLoadingFromDisk) | ||
const isReachable = useSelector(getIsReachable) | ||
const isOfflineModeEnabled = useIsOfflineModeEnabled() | ||
const offlineTracksStatus = useProxySelector( | ||
(state: AppState) => { | ||
if (isDoneLoadingFromDisk && isOfflineModeEnabled && !isReachable) { | ||
return getOfflineTrackStatus(state) | ||
} | ||
// We don't need offline download status when we're not offline. This saves us rerenders while we're downloading things and updating the offline download slice. | ||
return undefined | ||
}, | ||
[isReachable, isOfflineModeEnabled, isDoneLoadingFromDisk] | ||
) | ||
|
||
const filteredCollections = useProxySelector( | ||
(state: AppState) => { | ||
if (isOfflineModeEnabled && !isReachable) { | ||
if (!isDoneLoadingFromDisk) { | ||
return [] | ||
} | ||
} | ||
const offlineCollectionsStatus = getOfflineCollectionsStatus(state) | ||
return getAccountCollections(state, filterValue).filter((collection) => { | ||
const isCollectionCorrectType = | ||
collectionType === 'albums' | ||
? collection.is_album | ||
: !collection.is_album | ||
if (!isCollectionCorrectType) { | ||
return false | ||
} | ||
if (isOfflineModeEnabled && !isReachable) { | ||
const trackIds = | ||
collection.playlist_contents.track_ids.map( | ||
(trackData) => trackData.track | ||
) ?? [] | ||
const collectionDownloadStatus = | ||
offlineCollectionsStatus[collection.playlist_id] | ||
// Don't show a playlist in Offline Mode if it has at least one track but none of the tracks have been downloaded yet OR if it is not marked for download | ||
return ( | ||
isCollectionCorrectType && | ||
Boolean(collectionDownloadStatus) && | ||
collectionDownloadStatus !== OfflineDownloadStatus.INACTIVE && | ||
(trackIds.length === 0 || | ||
trackIds.some((t) => { | ||
return ( | ||
offlineTracksStatus && | ||
offlineTracksStatus[t.toString()] === | ||
OfflineDownloadStatus.SUCCESS | ||
) | ||
})) | ||
) | ||
} | ||
return true | ||
}) | ||
}, | ||
[ | ||
filterValue, | ||
isReachable, | ||
isOfflineModeEnabled, | ||
isDoneLoadingFromDisk, | ||
offlineTracksStatus | ||
], | ||
shallowCompare | ||
) | ||
const numPlayableTracksMap = useMemo(() => { | ||
return buildCollectionIdsToNumPlayableTracksMap( | ||
filteredCollections, | ||
isOfflineModeEnabled && !isReachable, | ||
offlineTracksStatus || {} | ||
) | ||
}, [ | ||
isOfflineModeEnabled, | ||
isReachable, | ||
offlineTracksStatus, | ||
filteredCollections | ||
]) | ||
|
||
return { | ||
filteredCollections, | ||
collectionIdsToNumTracks: numPlayableTracksMap | ||
} | ||
} |
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.
The code in this file and the other file are pretty much identical so working on another PR to decomp them