1
0
mirror of https://github.com/tooot-app/app synced 2025-04-25 15:38:42 +02:00

Refine account switching

This commit is contained in:
xmflsct 2023-01-29 22:18:33 +01:00
parent 062e6825b5
commit aee206fc95
3 changed files with 60 additions and 23 deletions

View File

@ -1,9 +1,13 @@
import { useNavigation } from '@react-navigation/native' import { useNavigation } from '@react-navigation/native'
import { ReadableAccountType, setAccount } from '@utils/storage/actions' import { ReadableAccountType, setAccount } from '@utils/storage/actions'
import { StyleConstants } from '@utils/styles/constants' import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import React from 'react' import React from 'react'
import Button from './Button' import { Pressable } from 'react-native'
import GracefullyImage from './GracefullyImage'
import haptics from './haptics' import haptics from './haptics'
import Icon from './Icon'
import CustomText from './Text'
interface Props { interface Props {
account: ReadableAccountType account: ReadableAccountType
@ -11,26 +15,56 @@ interface Props {
} }
const AccountButton: React.FC<Props> = ({ account, additionalActions }) => { const AccountButton: React.FC<Props> = ({ account, additionalActions }) => {
const { colors } = useTheme()
const navigation = useNavigation() const navigation = useNavigation()
return ( return (
<Button <Pressable
type='text'
selected={account.active}
style={{ style={{
marginBottom: StyleConstants.Spacing.M, flexDirection: 'row',
marginRight: StyleConstants.Spacing.M alignItems: 'center',
paddingVertical: StyleConstants.Spacing.S,
paddingHorizontal: StyleConstants.Spacing.S * 1.5,
borderColor: account.active ? colors.blue : colors.border,
borderWidth: 1,
borderRadius: 99,
marginBottom: StyleConstants.Spacing.S
}} }}
content={account.acct} onPress={async () => {
onPress={() => { await setAccount(account.key)
haptics('Light') haptics('Light')
setAccount(account.key)
navigation.goBack() navigation.goBack()
if (additionalActions) { if (additionalActions) {
additionalActions() additionalActions()
} }
}} }}
/> >
<GracefullyImage
uri={{ original: account.avatar_static }}
dimension={{
width: StyleConstants.Font.Size.L,
height: StyleConstants.Font.Size.L
}}
style={{ borderRadius: StyleConstants.Font.Size.L / 2, overflow: 'hidden' }}
/>
<CustomText
fontStyle='M'
fontWeight={account.active ? 'Bold' : 'Normal'}
style={{
color: account.active ? colors.blue : colors.primaryDefault,
marginLeft: StyleConstants.Spacing.S
}}
children={account.acct}
/>
{account.active ? (
<Icon
name='check'
size={StyleConstants.Font.Size.L}
color={colors.blue}
style={{ marginLeft: StyleConstants.Spacing.S }}
/>
) : null}
</Pressable>
) )
} }

View File

@ -92,7 +92,7 @@ const ScreenAccountSelection = ({
const { colors } = useTheme() const { colors } = useTheme()
const { t } = useTranslation('screenAccountSelection') const { t } = useTranslation('screenAccountSelection')
const accounts = getReadableAccounts(true) const accounts = getReadableAccounts()
return ( return (
<ScrollView <ScrollView
@ -129,7 +129,7 @@ const ScreenAccountSelection = ({
return ( return (
<AccountButton <AccountButton
key={index} key={index}
account={account} account={{ ...account, active: false }}
additionalActions={() => additionalActions={() =>
navigationRef.navigate('Screen-Compose', { navigationRef.navigate('Screen-Compose', {
type: 'share', type: 'share',

View File

@ -323,27 +323,30 @@ export const removeAccount = async (account: string, warning: boolean = true) =>
} }
export type ReadableAccountType = { export type ReadableAccountType = {
avatar_static: string
acct: string acct: string
key: string key: string
active: boolean active: boolean
} }
export const getReadableAccounts = (withoutActive: boolean = false): ReadableAccountType[] => { export const getReadableAccounts = (): ReadableAccountType[] => {
const accountActive = !withoutActive && getGlobalStorage.string('account.active') const accountActive = getGlobalStorage.string('account.active')
const accounts = getGlobalStorage.object('accounts')?.sort((a, b) => a.localeCompare(b)) const accounts = getGlobalStorage.object('accounts')
!withoutActive &&
accounts?.splice(
accounts.findIndex(a => a === accountActive),
1
)
!withoutActive && accounts?.unshift(accountActive || '')
return ( return (
accounts?.map(account => { accounts?.map(account => {
const details = getAccountDetails( const details = getAccountDetails(
['auth.account.acct', 'auth.account.domain', 'auth.domain', 'auth.account.id'], [
'auth.account.avatar_static',
'auth.account.acct',
'auth.account.domain',
'auth.domain',
'auth.account.id'
],
account account
) )
if (details) { if (details) {
return { return {
avatar_static: details['auth.account.avatar_static'],
acct: `@${details['auth.account.acct']}@${details['auth.account.domain']}`, acct: `@${details['auth.account.acct']}@${details['auth.account.domain']}`,
key: generateAccountKey({ key: generateAccountKey({
domain: details['auth.domain'], domain: details['auth.domain'],
@ -352,7 +355,7 @@ export const getReadableAccounts = (withoutActive: boolean = false): ReadableAcc
active: account === accountActive active: account === accountActive
} }
} else { } else {
return { acct: '', key: '', active: false } return { avatar_static: '', acct: '', key: '', active: false }
} }
}) || [] }) || []
).filter(a => a.acct.length) ).filter(a => a.acct.length)