like/unlike improvements WIP
parent
c4a666c221
commit
8071ae313d
|
@ -1,4 +1,4 @@
|
||||||
import {AppBskyFeedDefs} from '@atproto/api'
|
import {AppBskyFeedDefs, AtUri} from '@atproto/api'
|
||||||
import {makeAutoObservable} from 'mobx'
|
import {makeAutoObservable} from 'mobx'
|
||||||
import {RootStoreModel} from 'state/models/root-store'
|
import {RootStoreModel} from 'state/models/root-store'
|
||||||
|
|
||||||
|
@ -38,12 +38,32 @@ export class AlgoItemModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
get isLiked() {
|
get isLiked() {
|
||||||
return this.data.viewer?.liked
|
return this.data.viewer?.like
|
||||||
}
|
}
|
||||||
|
|
||||||
set toggleLiked(value: boolean) {
|
private toggleLiked(s?: string) {
|
||||||
if (this.data.viewer) {
|
if (this.data.viewer) {
|
||||||
this.data.viewer.liked = value
|
if (this.data.viewer.like) {
|
||||||
|
this.data.viewer.like = undefined
|
||||||
|
} else {
|
||||||
|
this.data.viewer.like = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private incrementLike() {
|
||||||
|
if (this.data.likeCount) {
|
||||||
|
this.data.likeCount += 1
|
||||||
|
} else {
|
||||||
|
this.data.likeCount = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private decrementLike() {
|
||||||
|
if (this.data.likeCount) {
|
||||||
|
this.data.likeCount -= 1
|
||||||
|
} else {
|
||||||
|
this.data.likeCount = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,8 +93,7 @@ export class AlgoItemModel {
|
||||||
|
|
||||||
async like() {
|
async like() {
|
||||||
try {
|
try {
|
||||||
this.toggleLiked = true
|
const res = await this.rootStore.agent.app.bsky.feed.like.create(
|
||||||
await this.rootStore.agent.app.bsky.feed.like.create(
|
|
||||||
{
|
{
|
||||||
repo: this.rootStore.me.did,
|
repo: this.rootStore.me.did,
|
||||||
},
|
},
|
||||||
|
@ -83,14 +102,29 @@ export class AlgoItemModel {
|
||||||
uri: this.data.uri,
|
uri: this.data.uri,
|
||||||
cid: this.data.cid,
|
cid: this.data.cid,
|
||||||
},
|
},
|
||||||
createdAt: new Date().toString(),
|
createdAt: new Date().toISOString(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
this.toggleLiked(res.uri)
|
||||||
|
this.incrementLike()
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
this.rootStore.log.error('Failed to like feed', e)
|
this.rootStore.log.error('Failed to like feed', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async unlike() {
|
||||||
|
try {
|
||||||
|
await this.rootStore.agent.app.bsky.feed.like.delete({
|
||||||
|
repo: this.rootStore.me.did,
|
||||||
|
rkey: new AtUri(this.data.uri).rkey,
|
||||||
|
})
|
||||||
|
this.toggleLiked()
|
||||||
|
this.decrementLike()
|
||||||
|
} catch (e: any) {
|
||||||
|
this.rootStore.log.error('Failed to unlike feed', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static async getView(store: RootStoreModel, uri: string) {
|
static async getView(store: RootStoreModel, uri: string) {
|
||||||
const res = await store.agent.app.bsky.feed.getFeedGenerator({
|
const res = await store.agent.app.bsky.feed.getFeedGenerator({
|
||||||
feed: uri,
|
feed: uri,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import {NativeStackScreenProps} from '@react-navigation/native-stack'
|
import {NativeStackScreenProps} from '@react-navigation/native-stack'
|
||||||
import {usePalette} from 'lib/hooks/usePalette'
|
import {usePalette} from 'lib/hooks/usePalette'
|
||||||
import {HeartIcon} from 'lib/icons'
|
import {HeartIcon, HeartIconSolid} from 'lib/icons'
|
||||||
import {CommonNavigatorParams} from 'lib/routes/types'
|
import {CommonNavigatorParams} from 'lib/routes/types'
|
||||||
import {colors, s} from 'lib/styles'
|
import {colors, s} from 'lib/styles'
|
||||||
import {observer} from 'mobx-react-lite'
|
import {observer} from 'mobx-react-lite'
|
||||||
|
@ -74,13 +74,21 @@ export const CustomFeed = withAuthRequired(
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
currentFeed?.like()
|
if (currentFeed?.isLiked) {
|
||||||
|
currentFeed?.unlike()
|
||||||
|
} else {
|
||||||
|
currentFeed?.like()
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
style={[styles.likeButton]}>
|
style={[styles.likeButton]}>
|
||||||
<Text style={[pal.text, s.semiBold]}>
|
<Text style={[pal.text, s.semiBold]}>
|
||||||
{currentFeed?.data.likeCount}
|
{currentFeed?.data.likeCount}
|
||||||
</Text>
|
</Text>
|
||||||
<HeartIcon strokeWidth={3} size={18} style={styles.liked} />
|
{currentFeed?.isLiked ? (
|
||||||
|
<HeartIconSolid size={18} style={styles.liked} />
|
||||||
|
) : (
|
||||||
|
<HeartIcon strokeWidth={3} size={18} style={styles.liked} />
|
||||||
|
)}
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
|
Loading…
Reference in New Issue