From cf30c66c3392c948234f6e8cda2eaf89f7f0bfc6 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 14 Mar 2023 13:11:04 -0500 Subject: Move the profile-ui model to the new ui folder --- src/state/models/profile-ui.ts | 117 -------------------------------------- src/state/models/ui/profile-ui.ts | 117 ++++++++++++++++++++++++++++++++++++++ src/view/screens/Profile.tsx | 2 +- 3 files changed, 118 insertions(+), 118 deletions(-) delete mode 100644 src/state/models/profile-ui.ts create mode 100644 src/state/models/ui/profile-ui.ts (limited to 'src') diff --git a/src/state/models/profile-ui.ts b/src/state/models/profile-ui.ts deleted file mode 100644 index 89723361a..000000000 --- a/src/state/models/profile-ui.ts +++ /dev/null @@ -1,117 +0,0 @@ -import {makeAutoObservable} from 'mobx' -import {RootStoreModel} from './root-store' -import {ProfileViewModel} from './profile-view' -import {FeedModel} from './feed-view' - -export enum Sections { - Posts = 'Posts', - PostsWithReplies = 'Posts & replies', -} - -const USER_SELECTOR_ITEMS = [Sections.Posts, Sections.PostsWithReplies] - -export interface ProfileUiParams { - user: string -} - -export class ProfileUiModel { - // data - profile: ProfileViewModel - feed: FeedModel - - // ui state - selectedViewIndex = 0 - - constructor( - public rootStore: RootStoreModel, - public params: ProfileUiParams, - ) { - makeAutoObservable( - this, - { - rootStore: false, - params: false, - }, - {autoBind: true}, - ) - this.profile = new ProfileViewModel(rootStore, {actor: params.user}) - this.feed = new FeedModel(rootStore, 'author', { - author: params.user, - limit: 10, - }) - } - - get currentView(): FeedModel { - if ( - this.selectedView === Sections.Posts || - this.selectedView === Sections.PostsWithReplies - ) { - return this.feed - } - throw new Error(`Invalid selector value: ${this.selectedViewIndex}`) - } - - get isInitialLoading() { - const view = this.currentView - return view.isLoading && !view.isRefreshing && !view.hasContent - } - - get isRefreshing() { - return this.profile.isRefreshing || this.currentView.isRefreshing - } - - get isUser() { - return this.profile.isUser - } - - get selectorItems() { - if (this.isUser) { - return USER_SELECTOR_ITEMS - } else { - return USER_SELECTOR_ITEMS - } - } - - get selectedView() { - return this.selectorItems[this.selectedViewIndex] - } - - // public api - // = - - setSelectedViewIndex(index: number) { - this.selectedViewIndex = index - } - - async setup() { - await Promise.all([ - this.profile - .setup() - .catch(err => this.rootStore.log.error('Failed to fetch profile', err)), - this.feed - .setup() - .catch(err => this.rootStore.log.error('Failed to fetch feed', err)), - ]) - } - - async update() { - const view = this.currentView - if (view instanceof FeedModel) { - await view.update() - } - } - - async refresh() { - await Promise.all([this.profile.refresh(), this.currentView.refresh()]) - } - - async loadMore() { - if ( - !this.currentView.isLoading && - !this.currentView.hasError && - !this.currentView.isEmpty - ) { - await this.currentView.loadMore() - } - } -} diff --git a/src/state/models/ui/profile-ui.ts b/src/state/models/ui/profile-ui.ts new file mode 100644 index 000000000..1d4fe28cd --- /dev/null +++ b/src/state/models/ui/profile-ui.ts @@ -0,0 +1,117 @@ +import {makeAutoObservable} from 'mobx' +import {RootStoreModel} from '../root-store' +import {ProfileViewModel} from '../profile-view' +import {FeedModel} from '../feed-view' + +export enum Sections { + Posts = 'Posts', + PostsWithReplies = 'Posts & replies', +} + +const USER_SELECTOR_ITEMS = [Sections.Posts, Sections.PostsWithReplies] + +export interface ProfileUiParams { + user: string +} + +export class ProfileUiModel { + // data + profile: ProfileViewModel + feed: FeedModel + + // ui state + selectedViewIndex = 0 + + constructor( + public rootStore: RootStoreModel, + public params: ProfileUiParams, + ) { + makeAutoObservable( + this, + { + rootStore: false, + params: false, + }, + {autoBind: true}, + ) + this.profile = new ProfileViewModel(rootStore, {actor: params.user}) + this.feed = new FeedModel(rootStore, 'author', { + author: params.user, + limit: 10, + }) + } + + get currentView(): FeedModel { + if ( + this.selectedView === Sections.Posts || + this.selectedView === Sections.PostsWithReplies + ) { + return this.feed + } + throw new Error(`Invalid selector value: ${this.selectedViewIndex}`) + } + + get isInitialLoading() { + const view = this.currentView + return view.isLoading && !view.isRefreshing && !view.hasContent + } + + get isRefreshing() { + return this.profile.isRefreshing || this.currentView.isRefreshing + } + + get isUser() { + return this.profile.isUser + } + + get selectorItems() { + if (this.isUser) { + return USER_SELECTOR_ITEMS + } else { + return USER_SELECTOR_ITEMS + } + } + + get selectedView() { + return this.selectorItems[this.selectedViewIndex] + } + + // public api + // = + + setSelectedViewIndex(index: number) { + this.selectedViewIndex = index + } + + async setup() { + await Promise.all([ + this.profile + .setup() + .catch(err => this.rootStore.log.error('Failed to fetch profile', err)), + this.feed + .setup() + .catch(err => this.rootStore.log.error('Failed to fetch feed', err)), + ]) + } + + async update() { + const view = this.currentView + if (view instanceof FeedModel) { + await view.update() + } + } + + async refresh() { + await Promise.all([this.profile.refresh(), this.currentView.refresh()]) + } + + async loadMore() { + if ( + !this.currentView.isLoading && + !this.currentView.hasError && + !this.currentView.isEmpty + ) { + await this.currentView.loadMore() + } + } +} diff --git a/src/view/screens/Profile.tsx b/src/view/screens/Profile.tsx index b5073f28d..8344dcef2 100644 --- a/src/view/screens/Profile.tsx +++ b/src/view/screens/Profile.tsx @@ -6,7 +6,7 @@ import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types' import {withAuthRequired} from 'view/com/auth/withAuthRequired' import {ViewSelector} from '../com/util/ViewSelector' import {CenteredView} from '../com/util/Views' -import {ProfileUiModel, Sections} from 'state/models/profile-ui' +import {ProfileUiModel, Sections} from 'state/models/ui/profile-ui' import {useStores} from 'state/index' import {ProfileHeader} from '../com/profile/ProfileHeader' import {FeedItem} from '../com/posts/FeedItem' -- cgit 1.4.1