Skip to content

Commit

Permalink
Fix UI state rendering for unsubscribe from custom RSS feed podcasts #…
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchdowney committed Sep 20, 2022
1 parent 5e900c5 commit a63486a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
55 changes: 30 additions & 25 deletions src/screens/PodcastsScreen.tsx
Expand Up @@ -857,8 +857,6 @@ export class PodcastsScreen extends React.Component<Props, State> {
this.setState({ isUnsubscribing: true }, () => {
(async () => {
try {
const { flatListData } = this.state

if (queryFrom === PV.Filters._subscribedKey || queryFrom === PV.Filters._customFeedsKey) {
addByRSSPodcastFeedUrl
? await removeAddByRSSPodcast(addByRSSPodcastFeedUrl)
Expand All @@ -867,19 +865,15 @@ export class PodcastsScreen extends React.Component<Props, State> {
} else if (queryFrom === PV.Filters._downloadedKey) {
await removeDownloadedPodcast(selectedId || addByRSSPodcastFeedUrl)
}
const newFlatListData = flatListData.filter((x) => x.id !== selectedId)

const row = rowMap[selectedId] || rowMap[addByRSSPodcastFeedUrl]
row.closeRow()

this.setState({
flatListData: newFlatListData,
flatListDataTotalCount: newFlatListData.length,
isUnsubscribing: false
})
// TODO: the safeKeyExtractor is breaking the logic below
// by appending an index to the rowMap key
// const row = rowMap[selectedId] || rowMap[addByRSSPodcastFeedUrl]
// row.closeRow()
} catch (error) {
this.setState({ isUnsubscribing: false })
console.log('_handleHiddenItemPress', error)
}
this.setState({ isUnsubscribing: false })
})()
})
}
Expand Down Expand Up @@ -980,6 +974,27 @@ export class PodcastsScreen extends React.Component<Props, State> {
Linking.openURL(createEmailLinkUrl(PV.Emails.PODCAST_REQUEST))
}

_getFlatListData = () => {
const { isLoadingMore, queryFrom } = this.state
const { subscribedPodcasts = [], subscribedPodcastsTotalCount = 0 } = this.global
let flatListData = []
let flatListDataTotalCount = null
if (isLoadingMore && queryFrom === PV.Filters._subscribedKey) {
// do nothing
} else if (queryFrom === PV.Filters._subscribedKey) {
flatListData = subscribedPodcasts
flatListDataTotalCount = subscribedPodcastsTotalCount
} else {
flatListData = this.state.flatListData
flatListDataTotalCount = this.state.flatListDataTotalCount
}

return {
flatListData,
flatListDataTotalCount
}
}

render() {
const { navigation } = this.props
const {
Expand All @@ -995,26 +1010,16 @@ export class PodcastsScreen extends React.Component<Props, State> {
showDataSettingsConfirmDialog,
showNoInternetConnectionMessage
} = this.state
const { session, subscribedPodcasts = [], subscribedPodcastsTotalCount = 0, podcastsGridViewEnabled } = this.global
const { session, podcastsGridViewEnabled } = this.global
const { subscribedPodcastIds } = session?.userInfo

let flatListData = []
let flatListDataTotalCount = null
if (isLoadingMore && queryFrom === PV.Filters._subscribedKey) {
// do nothing
} else if (queryFrom === PV.Filters._subscribedKey) {
flatListData = subscribedPodcasts
flatListDataTotalCount = subscribedPodcastsTotalCount
} else {
flatListData = this.state.flatListData
flatListDataTotalCount = this.state.flatListDataTotalCount
}

const noSubscribedPodcasts =
queryFrom === PV.Filters._subscribedKey && (!subscribedPodcastIds || subscribedPodcastIds.length === 0)

const isCategoryScreen = queryFrom === PV.Filters._categoryKey

const { flatListData, flatListDataTotalCount } = this._getFlatListData()

return (
<View style={styles.view} testID={`${testIDPrefix}_view`}>
<RNView style={{ flex: 1 }}>
Expand Down
3 changes: 2 additions & 1 deletion src/services/podcast.ts
Expand Up @@ -259,7 +259,8 @@ const toggleSubscribeToPodcastLocally = async (id: string) => {
}

const index = items.indexOf(id)
if (index > -1) {
const isUnsubscribing = index > -1
if (isUnsubscribing) {
items.splice(index, 1)
await removeAutoDownloadSetting(id)
} else {
Expand Down
18 changes: 16 additions & 2 deletions src/state/actions/podcast.ts
Expand Up @@ -72,9 +72,9 @@ export const toggleSubscribeToPodcast = async (id: string) => {
return new Promise<void>((resolve, reject) => {
(async () => {
try {
const globalState = getGlobal()
const subscribedPodcastIds = await toggleSubscribeToPodcastService(id)
const subscribedPodcast = await getPodcastService(id)
const globalState = getGlobal()
let { subscribedPodcasts = [] } = globalState
subscribedPodcasts = insertOrRemovePodcastFromAlphabetizedArray(subscribedPodcasts, subscribedPodcast) as any
await setSubscribedPodcasts(subscribedPodcasts)
Expand Down Expand Up @@ -108,7 +108,21 @@ export const toggleSubscribeToPodcast = async (id: string) => {

export const removeAddByRSSPodcast = async (feedUrl: string) => {
await removeAddByRSSPodcastService(feedUrl)
await combineWithAddByRSSPodcasts()

const globalState = getGlobal()
const { subscribedPodcasts = [] } = globalState

const filteredPodcasts = subscribedPodcasts.filter((x: any) => {
return !x.addByRSSPodcastFeedUrl || x.addByRSSPodcastFeedUrl !== feedUrl
})

await setSubscribedPodcasts(filteredPodcasts)

setGlobal({
subscribedPodcasts: filteredPodcasts,
subscribedPodcastsTotalCount: filteredPodcasts.length
})

PVEventEmitter.emit(PV.Events.PODCAST_SUBSCRIBE_TOGGLED)
}

Expand Down

0 comments on commit a63486a

Please sign in to comment.