-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added local configs screen to support dynamic configs
- Loading branch information
Showing
8 changed files
with
188 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React, { createContext, useState } from 'react'; | ||
|
||
import { uikitLocalConfigStorage } from '../factory/mmkv'; | ||
|
||
const KEY = 'uikitOptions'; | ||
const defaultOptions = { | ||
rtl: false, | ||
replyType: 'thread' as 'none' | 'thread' | 'quote_reply', | ||
threadReplySelectType: 'thread' as 'thread' | 'parent', | ||
}; | ||
|
||
type ContextValue = typeof defaultOptions; | ||
|
||
export const UIKitLocalConfigsContext = createContext<{ | ||
localConfigs: ContextValue; | ||
setLocalConfigs: React.Dispatch<React.SetStateAction<ContextValue>>; | ||
}>({ | ||
localConfigs: defaultOptions, | ||
setLocalConfigs: () => {}, | ||
}); | ||
|
||
export const UIKitLocalConfigsProvider = ({ children }: React.PropsWithChildren) => { | ||
const [state, setState] = useState<ContextValue>(() => { | ||
const data = uikitLocalConfigStorage.getString(KEY); | ||
if (data) { | ||
try { | ||
return JSON.parse(data); | ||
} catch { | ||
return defaultOptions; | ||
} | ||
} else { | ||
return defaultOptions; | ||
} | ||
}); | ||
|
||
return ( | ||
<UIKitLocalConfigsContext.Provider | ||
value={{ | ||
localConfigs: state, | ||
setLocalConfigs: (value) => { | ||
setState((prev) => { | ||
const next = typeof value === 'function' ? value(prev) : value; | ||
uikitLocalConfigStorage.set(KEY, JSON.stringify(next)); | ||
return next; | ||
}); | ||
}, | ||
}} | ||
> | ||
{children} | ||
</UIKitLocalConfigsContext.Provider> | ||
); | ||
}; | ||
|
||
export const withUIKitLocalConfigs = (Component: React.ComponentType<object>) => { | ||
return (props: object) => ( | ||
<UIKitLocalConfigsProvider> | ||
<Component {...props} /> | ||
</UIKitLocalConfigsProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { MMKV } from 'react-native-mmkv'; | ||
|
||
export const mmkv = new MMKV(); | ||
|
||
export const uikitLocalConfigStorage = new MMKV({ id: 'uikit.local.config' }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import React, { useContext } from 'react'; | ||
import { I18nManager, Pressable, ScrollView, View } from 'react-native'; | ||
|
||
import { Icon, Switch, Text } from '@sendbird/uikit-react-native-foundation'; | ||
|
||
import { UIKitLocalConfigsContext } from '../context/uikitLocalConfigs'; | ||
|
||
const Divider = () => <View style={{ height: 1, backgroundColor: '#dcdcdc' }} />; | ||
|
||
const UIKitLocalConfigsScreen = () => { | ||
const { localConfigs, setLocalConfigs } = useContext(UIKitLocalConfigsContext); | ||
return ( | ||
<ScrollView | ||
style={{ backgroundColor: 'white' }} | ||
contentContainerStyle={{ | ||
flexGrow: 1, | ||
paddingHorizontal: 16, | ||
paddingVertical: 12, | ||
gap: 20, | ||
backgroundColor: 'white', | ||
}} | ||
> | ||
<Switchable | ||
title={'RTL'} | ||
description={'Right to left, please restart the app to apply the changes'} | ||
value={localConfigs.rtl} | ||
onChange={(value) => { | ||
I18nManager.forceRTL(value); | ||
setLocalConfigs((prev) => ({ ...prev, rtl: value })); | ||
}} | ||
/> | ||
<Divider /> | ||
|
||
<Selectable | ||
title={'Reply Type'} | ||
value={localConfigs.replyType} | ||
values={['none', 'thread', 'quote_reply']} | ||
onChange={(value) => setLocalConfigs((prev) => ({ ...prev, replyType: value }))} | ||
/> | ||
{localConfigs.replyType === 'thread' && ( | ||
<View style={{ marginStart: 12, flexDirection: 'row' }}> | ||
<View style={{ width: 4, height: '100%', backgroundColor: '#dcdcdc', marginEnd: 12 }} /> | ||
<Selectable | ||
title={'Reply Select Type'} | ||
value={localConfigs.threadReplySelectType} | ||
values={['thread', 'parent']} | ||
onChange={(value) => setLocalConfigs((prev) => ({ ...prev, threadReplySelectType: value }))} | ||
/> | ||
</View> | ||
)} | ||
</ScrollView> | ||
); | ||
}; | ||
|
||
const Switchable = ({ | ||
title, | ||
description, | ||
value, | ||
onChange, | ||
}: { | ||
value: boolean; | ||
onChange: (value: boolean) => void; | ||
title?: string; | ||
description?: string; | ||
}) => { | ||
return ( | ||
<View style={{ flexDirection: 'column', alignItems: 'flex-start', gap: 8 }}> | ||
{!!title && <Text h2>{title}</Text>} | ||
{!!description && <Text caption2>{description}</Text>} | ||
|
||
<Switch value={value} onChangeValue={onChange} /> | ||
</View> | ||
); | ||
}; | ||
|
||
interface SelectableProps<T> { | ||
value: T; | ||
values: T[]; | ||
onChange: (value: T) => void; | ||
title?: string; | ||
} | ||
const Selectable = <T extends string>({ title, values, value, onChange }: SelectableProps<T>) => { | ||
return ( | ||
<View style={{ flexDirection: 'column', alignItems: 'flex-start', gap: 8 }}> | ||
{!!title && <Text h2>{title}</Text>} | ||
<View style={{ flexDirection: 'row', alignItems: 'center' }}> | ||
{values.map((v) => ( | ||
<Pressable | ||
key={v} | ||
onPress={() => onChange(v)} | ||
style={{ flexDirection: 'row', alignItems: 'center', gap: 4, marginEnd: 8 }} | ||
> | ||
<Icon icon={value === v ? 'checkbox-on' : 'checkbox-off'} size={24} /> | ||
<Text caption1>{v}</Text> | ||
</Pressable> | ||
))} | ||
</View> | ||
</View> | ||
); | ||
}; | ||
|
||
export default UIKitLocalConfigsScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters