From 0923bb18ba665ea159c4989726b6ca307061edcf Mon Sep 17 00:00:00 2001 From: aksonov Date: Thu, 5 Mar 2020 18:18:00 +0300 Subject: [PATCH] for #4849 --- src/store/HomeStore.ts | 39 +++++++++---------- third-party/wocky-client/src/index.ts | 1 + .../wocky-client/src/model/ClientData.ts | 26 ++++++++++--- .../wocky-client/src/model/OwnProfile.ts | 2 +- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/store/HomeStore.ts b/src/store/HomeStore.ts index ec3cb51b1..36274f980 100644 --- a/src/store/HomeStore.ts +++ b/src/store/HomeStore.ts @@ -1,6 +1,6 @@ /* tslint:disable:max-classes-per-file */ import {types, applySnapshot, getRoot, Instance} from 'mobx-state-tree' -import {IProfile, Location, IWocky} from 'wocky-client' +import {IProfile, Location, IWocky, MapOptionsEnum} from 'wocky-client' import {autorun} from 'mobx' import {INavStore} from './NavStore' @@ -8,8 +8,6 @@ const DEFAULT_DELTA = 0.00522 const TRANS_DELTA = DEFAULT_DELTA + 0.005 export const INIT_DELTA = 0.04 -const MapOptions = types.enumeration(['auto', 'satellite', 'street']) - const HomeStore = types .model('HomeStore', { fullScreenMode: false, @@ -17,7 +15,6 @@ const HomeStore = types mapCenterLocation: types.maybeNull(Location), selectedId: types.maybe(types.string), followingUser: false, - mapOptions: types.optional(MapOptions, 'auto'), }) .volatile(() => ({ latitudeDelta: INIT_DELTA, @@ -26,6 +23,9 @@ const HomeStore = types .views(self => { const {navStore, wocky}: {navStore: INavStore; wocky: IWocky} = getRoot(self) return { + get mapOptions(): MapOptionsEnum | null { + return wocky && wocky.profile ? wocky!.profile!.clientData.mapOptions : null + }, get creationMode() { return ( navStore && ['createBot', 'botCompose', 'botEdit', 'editNote'].includes(navStore.scene) @@ -52,19 +52,21 @@ const HomeStore = types } return [wocky.profile, ...wocky.profile!.allFriends] }, - get mapType() { - switch (self.mapOptions) { - case 'satellite': - return 'hybrid' - case 'street': - return 'standard' - case 'auto': - default: - return self.latitudeDelta <= TRANS_DELTA ? 'hybrid' : 'standard' - } - }, } }) + .views(self => ({ + get mapType() { + switch (self.mapOptions) { + case 'satellite': + return 'hybrid' + case 'street': + return 'standard' + case 'auto': + default: + return self.latitudeDelta <= TRANS_DELTA ? 'hybrid' : 'standard' + } + }, + })) .actions(self => ({ setFriendFilter(filter: string) { self.friendFilter = filter @@ -113,9 +115,10 @@ const HomeStore = types })) .actions(self => { let disposer: any = null + const {wocky}: {wocky: IWocky} = getRoot(self) return { setMapOptions(value) { - self.mapOptions = value + wocky!.profile!.clientData.setMapOptions(value) }, followUserOnMap(user: IProfile) { if (user.location) { @@ -135,10 +138,6 @@ const HomeStore = types }, } }) - .postProcessSnapshot((snapshot: any) => { - // store mapOptions - return {mapOptions: snapshot.mapOptions} - }) .views(self => ({ get filteredFriends() { const {wocky} = getRoot(self) diff --git a/third-party/wocky-client/src/index.ts b/third-party/wocky-client/src/index.ts index 198785529..59a0e7f01 100644 --- a/third-party/wocky-client/src/index.ts +++ b/third-party/wocky-client/src/index.ts @@ -24,6 +24,7 @@ export {IFileService} from './transport/FileService' export {Location, ILocationSnapshot, ILocation, createLocation} from './model/Location' export {Message, IMessage, Status as MessageStatus} from './model/Message' export {OwnProfile, IOwnProfile} from './model/OwnProfile' +export {ClientData, MapOptions, MapOptionsEnum} from './model/ClientData' export {Transport} from './transport/Transport' export {IPagingList} from './transport/types' export {createFactory} from './store/Factory' diff --git a/third-party/wocky-client/src/model/ClientData.ts b/third-party/wocky-client/src/model/ClientData.ts index 8ddd9ec3a..9966637f7 100644 --- a/third-party/wocky-client/src/model/ClientData.ts +++ b/third-party/wocky-client/src/model/ClientData.ts @@ -1,6 +1,14 @@ import {types, getSnapshot, Instance, applySnapshot} from 'mobx-state-tree' import {Base} from './Base' +export enum MapOptionsEnum { + AUTO = 'auto', + SATELLITE = 'satellite', + STREET = 'street', +} + +export const MapOptions = types.enumeration([...Object.values(MapOptionsEnum)]) + const Hidden = types .model('HiddenType', { enabled: false, @@ -28,11 +36,12 @@ const Hidden = types } }) -const ClientData = types +export const ClientData = types .compose( Base, types .model({ + mapOptions: types.optional(MapOptions, MapOptionsEnum.AUTO), sharePresencePrimed: false, guestOnce: false, onboarded: false, @@ -46,9 +55,18 @@ const ClientData = types ) .named('ClientData') .actions(self => ({ + persist() { + self.transport.updateProfile({clientData: getSnapshot(self)}) + }, + })) + .actions(self => ({ + setMapOptions(value) { + self.mapOptions = value + self.persist() + }, hide(value: boolean, expires: Date | undefined) { self.hidden = Hidden.create({enabled: value, expires}) - self.transport.updateProfile({clientData: getSnapshot(self)}) + self.persist() }, load: snapshot => { applySnapshot(self, JSON.parse(snapshot)) @@ -58,10 +76,8 @@ const ClientData = types }, flip: (property: 'sharePresencePrimed' | 'guestOnce' | 'onboarded') => { self[property] = true - self.transport.updateProfile({clientData: getSnapshot(self)}) + self.persist() }, })) -export default ClientData - export interface IClientData extends Instance {} diff --git a/third-party/wocky-client/src/model/OwnProfile.ts b/third-party/wocky-client/src/model/OwnProfile.ts index a68488e06..1b76aecfd 100644 --- a/third-party/wocky-client/src/model/OwnProfile.ts +++ b/third-party/wocky-client/src/model/OwnProfile.ts @@ -4,7 +4,7 @@ import {createUpdatable} from './Updatable' import {createUploadable} from './Uploadable' import {InvitationPaginableList, Invitation} from './Invitation' import {BlockedUserPaginableList, BlockedUser} from './BlockedUser' -import ClientData from './ClientData' +import {ClientData} from './ClientData' import {reaction, IReactionDisposer} from 'mobx' export const OwnProfile = types