import Button from '@app/components/Button'
import GradientScrollView from '@app/components/GradientScrollView'
import Header from '@app/components/Header'
import PressableOpacity from '@app/components/PressableOpacity'
import SettingsItem from '@app/components/SettingsItem'
import SettingsSwitch from '@app/components/SettingsSwitch'
import TextInput from '@app/components/TextInput'
import { useSwitchActiveServer } from '@app/hooks/server'
import { Server } from '@app/models/settings'
import { selectCache } from '@app/state/cache'
import { selectSettings } from '@app/state/settings'
import { useStore } from '@app/state/store'
import colors from '@app/styles/colors'
import font from '@app/styles/font'
import { useNavigation } from '@react-navigation/core'
import React, { useCallback, useState } from 'react'
import { KeyboardTypeOptions, Modal, Pressable, StatusBar, StyleSheet, Text, View } from 'react-native'
import { ScrollView } from 'react-native-gesture-handler'
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'
const ServerItem = React.memo<{
server: Server
}>(({ server }) => {
const activeServer = useStore(selectSettings.activeServer)
const switchActiveServer = useSwitchActiveServer()
const navigation = useNavigation()
const setActive = useCallback(() => {
switchActiveServer(server.id)
}, [server.id, switchActiveServer])
return (
navigation.navigate('server', { id: server.id })}>
{activeServer && activeServer.id === server.id ? (
) : (
)}
)
})
const ModalChoice = React.memo<{
text: string
value: any
setValue: (value: any) => void
current: boolean
closeModal?: () => void
}>(({ text, value, setValue, current, closeModal }) => {
const onPress = useCallback(() => {
setValue(value)
if (closeModal) {
closeModal()
}
}, [closeModal, setValue, value])
return (
{current ? (
) : (
)}
{text}
)
})
function bitrateString(bitrate: number): string {
return bitrate === 0 ? 'Unlimited' : `${bitrate}kbps`
}
const BitrateModal = React.memo<{
title: string
bitrate: number
setBitrate: (bitrate: number) => void
}>(({ title, bitrate, setBitrate }) => {
const [visible, setVisible] = useState(false)
const toggleModal = useCallback(() => setVisible(!visible), [visible])
const BitrateChoice: React.FC<{ value: number }> = useCallback(
({ value }) => {
const text = bitrateString(value)
return (
)
},
[bitrate, toggleModal, setBitrate],
)
return (
<>
>
)
})
const SettingsTextModal = React.memo<{
title: string
value: string
setValue: (text: string) => void
getUnit?: (text: string) => string
keyboardType?: KeyboardTypeOptions
}>(({ title, value, setValue, getUnit, keyboardType }) => {
const [visible, setVisible] = useState(false)
const [inputText, setInputText] = useState(value)
const toggleModal = useCallback(() => setVisible(!visible), [visible])
const submit = useCallback(() => {
setValue(inputText)
toggleModal()
}, [inputText, setValue, toggleModal])
const getSubtitle = useCallback(() => {
if (!getUnit) {
return value
}
return value + ' ' + getUnit(value)
}, [getUnit, value])
return (
<>
>
)
})
function secondsUnit(seconds: string): string {
const numberValue = parseFloat(seconds)
if (Math.abs(numberValue) !== 1) {
return 'seconds'
}
return 'second'
}
const SettingsContent = React.memo(() => {
const servers = useStore(selectSettings.servers)
const scrobble = useStore(selectSettings.scrobble)
const setScrobble = useStore(selectSettings.setScrobble)
// doesn't seem to ever be a case where we want this off
// will remove later if there isn't a use case for disabling
// const estimateContentLength = useStore(selectSettings.estimateContentLength)
// const setEstimateContentLength = useStore(selectSettings.setEstimateContentLength)
const maxBitrateWifi = useStore(selectSettings.maxBitrateWifi)
const setMaxBitrateWifi = useStore(selectSettings.setMaxBitrateWifi)
const maxBitrateMobile = useStore(selectSettings.maxBitrateMobile)
const setMaxBitrateMobile = useStore(selectSettings.setMaxBitrateMobile)
const minBuffer = useStore(selectSettings.minBuffer)
const setMinBuffer = useStore(selectSettings.setMinBuffer)
const maxBuffer = useStore(selectSettings.maxBuffer)
const setMaxBuffer = useStore(selectSettings.setMaxBuffer)
const clearImageCache = useStore(selectCache.clearImageCache)
const [clearing, setClearing] = useState(false)
const navigation = useNavigation()
const clear = useCallback(() => {
setClearing(true)
const waitForClear = async () => {
try {
await clearImageCache()
} catch (err) {
console.log(err)
} finally {
setClearing(false)
}
}
waitForClear()
}, [clearImageCache])
const setMinBufferText = useCallback((text: string) => setMinBuffer(parseFloat(text)), [setMinBuffer])
const setMaxBufferText = useCallback((text: string) => setMaxBuffer(parseFloat(text)), [setMaxBuffer])
return (
{servers.map(s => (
))}
)
})
const Settings = () => {
return (
)
}
const styles = StyleSheet.create({
scroll: {
flex: 1,
},
scrollContentContainer: {
paddingTop: StatusBar.currentHeight,
},
content: {
paddingHorizontal: 20,
paddingBottom: 40,
},
text: {
color: 'white',
},
serverActive: {
paddingLeft: 12,
},
header: {
marginTop: 26,
},
button: {
marginTop: 16,
},
modalBackdrop: {
flex: 1,
},
centeredView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
modalView: {
backgroundColor: colors.gradient.high,
elevation: 5,
},
modalChoice: {
paddingHorizontal: 20,
paddingVertical: 10,
paddingRight: 15,
flexDirection: 'row',
flex: 1,
},
modalChoiceText: {
fontFamily: font.regular,
fontSize: 18,
color: colors.text.primary,
textAlign: 'left',
flex: 1,
},
modalRadio: {
height: 30,
width: 30,
marginRight: 20,
},
modalHeader: {
marginVertical: 20,
marginHorizontal: 30,
},
modalScroll: {
maxHeight: 475,
},
modalTextInputLine: {
flexDirection: 'row',
margin: 20,
},
modalTextInput: {
flex: 1,
paddingLeft: 12,
},
modalTextSubmit: {
marginLeft: 15,
// backgroundColor: 'green',
},
})
export default Settings