[APP-720] Paginate custom feeds (#1030)
* paginate custom feeds * Fix loading state bug * DRY code upzio/stable
parent
bf00d49863
commit
bb99a234e1
|
@ -24,7 +24,7 @@
|
||||||
"e2e:run": "detox test --configuration ios.sim.debug --take-screenshots all"
|
"e2e:run": "detox test --configuration ios.sim.debug --take-screenshots all"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@atproto/api": "^0.3.13",
|
"@atproto/api": "^0.4.2",
|
||||||
"@bam.tech/react-native-image-resizer": "^3.0.4",
|
"@bam.tech/react-native-image-resizer": "^3.0.4",
|
||||||
"@braintree/sanitize-url": "^6.0.2",
|
"@braintree/sanitize-url": "^6.0.2",
|
||||||
"@expo/html-elements": "^0.4.2",
|
"@expo/html-elements": "^0.4.2",
|
||||||
|
|
|
@ -5,12 +5,15 @@ import {bundleAsync} from 'lib/async/bundle'
|
||||||
import {cleanError} from 'lib/strings/errors'
|
import {cleanError} from 'lib/strings/errors'
|
||||||
import {CustomFeedModel} from '../feeds/custom-feed'
|
import {CustomFeedModel} from '../feeds/custom-feed'
|
||||||
|
|
||||||
|
const DEFAULT_LIMIT = 50
|
||||||
|
|
||||||
export class FeedsDiscoveryModel {
|
export class FeedsDiscoveryModel {
|
||||||
// state
|
// state
|
||||||
isLoading = false
|
isLoading = false
|
||||||
isRefreshing = false
|
isRefreshing = false
|
||||||
hasLoaded = false
|
hasLoaded = false
|
||||||
error = ''
|
error = ''
|
||||||
|
loadMoreCursor: string | undefined = undefined
|
||||||
|
|
||||||
// data
|
// data
|
||||||
feeds: CustomFeedModel[] = []
|
feeds: CustomFeedModel[] = []
|
||||||
|
@ -26,6 +29,9 @@ export class FeedsDiscoveryModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
get hasMore() {
|
get hasMore() {
|
||||||
|
if (this.loadMoreCursor) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,9 +54,9 @@ export class FeedsDiscoveryModel {
|
||||||
this._xLoading()
|
this._xLoading()
|
||||||
try {
|
try {
|
||||||
const res =
|
const res =
|
||||||
await this.rootStore.agent.app.bsky.unspecced.getPopularFeedGenerators(
|
await this.rootStore.agent.app.bsky.unspecced.getPopularFeedGenerators({
|
||||||
{},
|
limit: DEFAULT_LIMIT,
|
||||||
)
|
})
|
||||||
this._replaceAll(res)
|
this._replaceAll(res)
|
||||||
this._xIdle()
|
this._xIdle()
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
@ -58,6 +64,24 @@ export class FeedsDiscoveryModel {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
loadMore = bundleAsync(async () => {
|
||||||
|
if (!this.hasMore) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this._xLoading()
|
||||||
|
try {
|
||||||
|
const res =
|
||||||
|
await this.rootStore.agent.app.bsky.unspecced.getPopularFeedGenerators({
|
||||||
|
limit: DEFAULT_LIMIT,
|
||||||
|
cursor: this.loadMoreCursor,
|
||||||
|
})
|
||||||
|
this._append(res)
|
||||||
|
} catch (e: any) {
|
||||||
|
this._xIdle(e)
|
||||||
|
}
|
||||||
|
this._xIdle()
|
||||||
|
})
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
this.isLoading = false
|
this.isLoading = false
|
||||||
this.isRefreshing = false
|
this.isRefreshing = false
|
||||||
|
@ -89,9 +113,18 @@ export class FeedsDiscoveryModel {
|
||||||
// =
|
// =
|
||||||
|
|
||||||
_replaceAll(res: AppBskyUnspeccedGetPopularFeedGenerators.Response) {
|
_replaceAll(res: AppBskyUnspeccedGetPopularFeedGenerators.Response) {
|
||||||
|
// 1. set feeds data to empty array
|
||||||
this.feeds = []
|
this.feeds = []
|
||||||
|
// 2. call this._append()
|
||||||
|
this._append(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
_append(res: AppBskyUnspeccedGetPopularFeedGenerators.Response) {
|
||||||
|
// 1. push data into feeds array
|
||||||
for (const f of res.data.feeds) {
|
for (const f of res.data.feeds) {
|
||||||
this.feeds.push(new CustomFeedModel(this.rootStore, f))
|
this.feeds.push(new CustomFeedModel(this.rootStore, f))
|
||||||
}
|
}
|
||||||
|
// 2. set loadMoreCursor
|
||||||
|
this.loadMoreCursor = res.data.cursor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,6 +85,7 @@ export const DiscoverFeedsScreen = withAuthRequired(
|
||||||
renderItem={renderItem}
|
renderItem={renderItem}
|
||||||
initialNumToRender={10}
|
initialNumToRender={10}
|
||||||
ListEmptyComponent={renderListEmptyComponent}
|
ListEmptyComponent={renderListEmptyComponent}
|
||||||
|
onEndReached={() => feeds.loadMore()}
|
||||||
extraData={feeds.isLoading}
|
extraData={feeds.isLoading}
|
||||||
/>
|
/>
|
||||||
</CenteredView>
|
</CenteredView>
|
||||||
|
|
13
yarn.lock
13
yarn.lock
|
@ -29,7 +29,7 @@
|
||||||
jsonpointer "^5.0.0"
|
jsonpointer "^5.0.0"
|
||||||
leven "^3.1.0"
|
leven "^3.1.0"
|
||||||
|
|
||||||
"@atproto/api@*", "@atproto/api@^0.3.13":
|
"@atproto/api@*":
|
||||||
version "0.3.13"
|
version "0.3.13"
|
||||||
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.3.13.tgz#e5ccaa83bb909e662286cdf74a77a76de6562a47"
|
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.3.13.tgz#e5ccaa83bb909e662286cdf74a77a76de6562a47"
|
||||||
integrity sha512-smDlomgipca16G+jKXAZSMfsAmA5wG8WR3Z1dj29ZShVJlhs6+HHdxX7dWVDYEdSeb2rp/wyHN/tQhxGDAkz/g==
|
integrity sha512-smDlomgipca16G+jKXAZSMfsAmA5wG8WR3Z1dj29ZShVJlhs6+HHdxX7dWVDYEdSeb2rp/wyHN/tQhxGDAkz/g==
|
||||||
|
@ -40,6 +40,17 @@
|
||||||
tlds "^1.234.0"
|
tlds "^1.234.0"
|
||||||
typed-emitter "^2.1.0"
|
typed-emitter "^2.1.0"
|
||||||
|
|
||||||
|
"@atproto/api@^0.4.2":
|
||||||
|
version "0.4.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/@atproto/api/-/api-0.4.2.tgz#7790eb049f72437e7454c8ecc29a5ef4201c1ade"
|
||||||
|
integrity sha512-bwaT+kIJp6wpzlHc1Rus3yi29GKlwvYp4wOWAFmcyYT4qH2ZlE6NfElgi6QwdQD285EhiIKYTv7CAMRp/QO7DQ==
|
||||||
|
dependencies:
|
||||||
|
"@atproto/common-web" "*"
|
||||||
|
"@atproto/uri" "*"
|
||||||
|
"@atproto/xrpc" "*"
|
||||||
|
tlds "^1.234.0"
|
||||||
|
typed-emitter "^2.1.0"
|
||||||
|
|
||||||
"@atproto/aws@*":
|
"@atproto/aws@*":
|
||||||
version "0.0.1"
|
version "0.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/@atproto/aws/-/aws-0.0.1.tgz#b26e47ec4a7ddab4a5d41ec8dbc476cfce88139d"
|
resolved "https://registry.yarnpkg.com/@atproto/aws/-/aws-0.0.1.tgz#b26e47ec4a7ddab4a5d41ec8dbc476cfce88139d"
|
||||||
|
|
Loading…
Reference in New Issue