make sure state is being synced across components

zio/stable
Ansh Nanda 2023-05-16 16:07:07 -07:00
parent f2e39d8ad2
commit 3f41d3db26
6 changed files with 39 additions and 38 deletions

View File

@ -67,30 +67,12 @@ export class AlgoItemModel {
} }
} }
private rewriteData(data: AppBskyFeedDefs.GeneratorView) {
this.data = data
}
// public apis // public apis
// = // =
async save() {
try {
this.toggleSaved = true
await this.rootStore.agent.app.bsky.feed.saveFeed({
feed: this.data.uri,
})
} catch (e: any) {
this.rootStore.log.error('Failed to save feed', e)
}
}
async unsave() {
try {
this.toggleSaved = false
await this.rootStore.agent.app.bsky.feed.unsaveFeed({
feed: this.data.uri,
})
} catch (e: any) {
this.rootStore.log.error('Failed to unsanve feed', e)
}
}
async like() { async like() {
try { try {
const res = await this.rootStore.agent.app.bsky.feed.like.create( const res = await this.rootStore.agent.app.bsky.feed.like.create(
@ -151,7 +133,7 @@ export class AlgoItemModel {
const res = await this.rootStore.agent.app.bsky.feed.getFeedGenerator({ const res = await this.rootStore.agent.app.bsky.feed.getFeedGenerator({
feed: this.data.uri, feed: this.data.uri,
}) })
this.data = res.data.view this.rewriteData(res.data.view)
} }
serialize() { serialize() {

View File

@ -71,6 +71,12 @@ export class SavedFeedsModel {
) )
} }
get listOfPinnedFeedNames() {
return this.pinned.map(
f => f.data.displayName ?? f.data.creator.displayName + "'s feed",
)
}
get savedFeedsWithoutPinned() { get savedFeedsWithoutPinned() {
return this.feeds.filter( return this.feeds.filter(
f => !this.pinned.find(p => p.data.uri === f.data.uri), f => !this.pinned.find(p => p.data.uri === f.data.uri),
@ -81,10 +87,14 @@ export class SavedFeedsModel {
if (!this.isPinned(feed)) { if (!this.isPinned(feed)) {
this.pinned.push(feed) this.pinned.push(feed)
} else { } else {
this.pinned = this.pinned.filter(f => f.data.uri !== feed.data.uri) this.removePinnedFeed(feed.data.uri)
} }
} }
removePinnedFeed(uri: string) {
this.pinned = this.pinned.filter(f => f.data.uri !== uri)
}
reorderPinnedFeeds(temp: AlgoItemModel[]) { reorderPinnedFeeds(temp: AlgoItemModel[]) {
this.pinned = temp this.pinned = temp
} }
@ -144,18 +154,22 @@ export class SavedFeedsModel {
await this.rootStore.agent.app.bsky.feed.saveFeed({ await this.rootStore.agent.app.bsky.feed.saveFeed({
feed: algoItem.getUri, feed: algoItem.getUri,
}) })
algoItem.toggleSaved = true
this.addFeed(algoItem) this.addFeed(algoItem)
} catch (e: any) { } catch (e: any) {
this.rootStore.log.error('Failed to save feed', e) this.rootStore.log.error('Failed to save feed', e)
} }
} }
async unsave(uri: string) { async unsave(algoItem: AlgoItemModel) {
const uri = algoItem.getUri
try { try {
await this.rootStore.agent.app.bsky.feed.unsaveFeed({ await this.rootStore.agent.app.bsky.feed.unsaveFeed({
feed: uri, feed: uri,
}) })
algoItem.toggleSaved = false
this.removeFeed(uri) this.removeFeed(uri)
this.removePinnedFeed(uri)
} catch (e: any) { } catch (e: any) {
this.rootStore.log.error('Failed to unsanve feed', e) this.rootStore.log.error('Failed to unsanve feed', e)
} }

View File

@ -13,7 +13,7 @@ import {UserAvatar} from '../util/UserAvatar'
import {Button} from '../util/forms/Button' import {Button} from '../util/forms/Button'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {AlgoItemModel} from 'state/models/feeds/algo/algo-item' import {AlgoItemModel} from 'state/models/feeds/algo/algo-item'
import {useNavigation} from '@react-navigation/native' import {useFocusEffect, useNavigation} from '@react-navigation/native'
import {NavigationProp} from 'lib/routes/types' import {NavigationProp} from 'lib/routes/types'
import {useStores} from 'state/index' import {useStores} from 'state/index'
import {HeartIconSolid} from 'lib/icons' import {HeartIconSolid} from 'lib/icons'
@ -34,6 +34,11 @@ const AlgoItem = observer(
const pal = usePalette('default') const pal = usePalette('default')
const navigation = useNavigation<NavigationProp>() const navigation = useNavigation<NavigationProp>()
// TODO: this is pretty hacky, but it works for now
useFocusEffect(() => {
item.reload()
})
return ( return (
<TouchableOpacity <TouchableOpacity
accessibilityRole="button" accessibilityRole="button"
@ -78,14 +83,12 @@ const AlgoItem = observer(
</View> </View>
<View> <View>
<Button <Button
type="inverted" type={item.isSaved ? 'default' : 'inverted'}
onPress={() => { onPress={() => {
if (item.data.viewer?.saved) { if (item.data.viewer?.saved) {
item.unsave() store.me.savedFeeds.unsave(item)
store.me.savedFeeds.removeFeed(item.data.uri)
} else { } else {
item.save() store.me.savedFeeds.save(item)
store.me.savedFeeds.addFeed(item)
} }
}} }}
label={item.data.viewer?.saved ? 'Unsave' : 'Save'} label={item.data.viewer?.saved ? 'Unsave' : 'Save'}

View File

@ -33,8 +33,12 @@ export const FeedsTabBar = observer(
}, [store]) }, [store])
const items = useMemo( const items = useMemo(
() => ['Following', "What's hot", ...store.me.savedFeeds.listOfFeedNames], () => [
[store.me.savedFeeds.listOfFeedNames], 'Following',
"What's hot",
...store.me.savedFeeds.listOfPinnedFeedNames,
],
[store.me.savedFeeds.listOfPinnedFeedNames],
) )
return ( return (

View File

@ -64,11 +64,9 @@ export const CustomFeed = withAuthRequired(
style={[styles.saveButton]} style={[styles.saveButton]}
onPress={() => { onPress={() => {
if (currentFeed?.data.viewer?.saved) { if (currentFeed?.data.viewer?.saved) {
currentFeed?.unsave() rootStore.me.savedFeeds.unsave(currentFeed!)
rootStore.me.savedFeeds.removeFeed(currentFeed!.data.uri)
} else { } else {
currentFeed!.save() rootStore.me.savedFeeds.save(currentFeed!)
rootStore.me.savedFeeds.addFeed(currentFeed!)
} }
}} }}
label={currentFeed?.data.viewer?.saved ? 'Unsave' : 'Save'} label={currentFeed?.data.viewer?.saved ? 'Unsave' : 'Save'}

View File

@ -112,7 +112,7 @@ export const HomeScreen = withAuthRequired(
feed={algoFeed} feed={algoFeed}
renderEmptyState={renderWhatsHotEmptyState} renderEmptyState={renderWhatsHotEmptyState}
/> />
{store.me.savedFeeds.feeds.map((f, index) => { {store.me.savedFeeds.pinned.map((f, index) => {
return ( return (
<FeedPage <FeedPage
key={String(2 + index + 1)} key={String(2 + index + 1)}