Skip to content

Commit bc0d628

Browse files
committed
Line Settings with other headers, fix dc reasons.
Disconnect reason dialog will no longer randomly disappear.
1 parent da74e34 commit bc0d628

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

src/App.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import { createMaterialTopTabNavigator } from '@react-navigation/material-top-ta
1717

1818
import useAsyncStorage from './helpers/useAsyncStorage'
1919
import useJsonAsyncStorage from './helpers/useJsonAsyncStorage'
20-
import ConnectionContext, { Connection } from './context/connectionContext'
20+
import ConnectionContext, {
21+
Connection,
22+
DisconnectReason as Disconnect
23+
} from './context/connectionContext'
2124
import AccountsContext, { Accounts } from './context/accountsContext'
2225
import SettingsContext, { Settings } from './context/settingsContext'
2326
import ServersContext, { Servers } from './context/serversContext'
@@ -80,6 +83,7 @@ const HomeScreen = ({ navigation }: { navigation: HomeNavigationProp }) => {
8083
const App = () => {
8184
const colorScheme = useColorScheme()
8285
const [connection, setConnection] = React.useState<Connection | undefined>()
86+
const [disconnect, setDisconnect] = React.useState<Disconnect | undefined>()
8387

8488
const [settings, setSettings] = useJsonAsyncStorage<Settings>('@settings', {
8589
// TODO: Better defaults and settings e.g. join message is 140 chars (bad for <1.11).
@@ -120,7 +124,14 @@ const App = () => {
120124

121125
return (
122126
<SafeAreaView style={globalStyle.flexSpacer}>
123-
<ConnectionContext.Provider value={{ connection, setConnection }}>
127+
<ConnectionContext.Provider
128+
value={{
129+
connection,
130+
setConnection,
131+
disconnectReason: disconnect,
132+
setDisconnectReason: setDisconnect
133+
}}
134+
>
124135
<SettingsContext.Provider value={{ settings, setSettings }}>
125136
<ServersContext.Provider value={{ servers, setServers }}>
126137
<AccountsContext.Provider value={{ accounts, setAccounts }}>

src/context/connectionContext.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
// Consider using Recoil instead of Context?
22
import React from 'react'
3+
import { MinecraftChat } from '../minecraft/chatToJsx'
34
import { ServerConnection } from '../minecraft/connection'
45

56
export interface Connection {
67
connection: ServerConnection
78
serverName: string
89
}
910

11+
export interface DisconnectReason {
12+
server: string
13+
reason: MinecraftChat
14+
}
15+
1016
export interface ConnectionContext {
1117
connection?: Connection
1218
setConnection: (newConnection?: Connection) => void
19+
disconnectReason?: DisconnectReason
20+
setDisconnectReason: (newDisconnectReason?: DisconnectReason) => void
1321
}
1422

1523
const connectionContext = React.createContext<ConnectionContext>({
16-
setConnection: () => {}
24+
setConnection: () => {},
25+
setDisconnectReason: () => {}
1726
})
1827

1928
export default connectionContext

src/globalStyle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export default StyleSheet.create({
55
iconStyle: { marginRight: 5 },
66
title: {
77
fontSize: 24,
8-
fontWeight: 'bold'
8+
fontWeight: 'bold',
9+
height: 37.5
910
},
1011
horizontalLine: {
1112
backgroundColor: '#666',

src/screens/ChatScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
126126
.slice(chatVarIntLength, chatVarIntLength + chatLength)
127127
.toString('utf8')
128128
const position = packet.data.readInt8(chatVarIntLength + chatLength)
129-
// LOW-TODO: Support position 2 and sender.
129+
// TODO: Support position 2 and sender.
130130
if (position === 0 || position === 1) addMessage(JSON.parse(chatJson))
131131
} catch (e) {
132132
createErrorHandler(colorMap.dark_red, addMessage, parseMessageErr)(e)
@@ -183,7 +183,7 @@ const ChatScreen = ({ navigation }: { navigation: ChatNavigationProp }) => {
183183
/>
184184
</View>
185185
)
186-
// LOW-TODO: Use stack navigation for this so the physical back button works correctly.
186+
// TODO: Use stack navigation for this so the physical back button works correctly.
187187
if (settingsOpen) return <SettingScreen button={backButton} />
188188
return (
189189
<>

src/screens/ServerScreen.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { resolveHostname, protocolMap } from '../minecraft/utils'
3131
import initiateConnection from '../minecraft/connection'
3232
import {
3333
ChatToJsx,
34-
MinecraftChat,
3534
lightColorMap,
3635
mojangColorMap
3736
} from '../minecraft/chatToJsx'
@@ -52,7 +51,12 @@ const ServerScreen = () => {
5251
const darkMode = useDarkMode()
5352
const { servers, setServers } = useContext(ServersContext)
5453
const { accounts } = useContext(AccountsContext)
55-
const { connection, setConnection } = useContext(ConnectionContext)
54+
const {
55+
connection,
56+
setConnection,
57+
disconnectReason,
58+
setDisconnectReason
59+
} = useContext(ConnectionContext)
5660
const initiatingConnection = useRef(false)
5761

5862
const [ipAddr, setIpAddr] = useState('')
@@ -68,10 +72,6 @@ const ServerScreen = () => {
6872
const [pingResponses, setPingResponses] = useState<{
6973
[ip: string]: LegacyPing | Ping | null
7074
}>({})
71-
const [disconnectDialog, setDisconnectDialog] = useState<{
72-
server: string
73-
reason: MinecraftChat
74-
} | null>(null)
7575

7676
useEffect(() => {
7777
if (Object.keys(pingResponses).length > 0) {
@@ -139,7 +139,7 @@ const ServerScreen = () => {
139139
const activeAccount = Object.keys(accounts).find(e => accounts[e].active)
140140
if (!activeAccount) {
141141
initiatingConnection.current = false
142-
return setDisconnectDialog({
142+
return setDisconnectReason({
143143
server,
144144
reason:
145145
'No active account selected! Open the Accounts tab and add an account.'
@@ -156,7 +156,7 @@ const ServerScreen = () => {
156156
}
157157
if (protocolVersion < 754) {
158158
initiatingConnection.current = false
159-
return setDisconnectDialog({
159+
return setDisconnectReason({
160160
server,
161161
reason: 'EnderChat only supports 1.16.4 and newer for now.'
162162
})
@@ -174,8 +174,7 @@ const ServerScreen = () => {
174174
const onCloseOrError = () => {
175175
setConnection(undefined)
176176
if (newConn.disconnectReason) {
177-
// TODO: This doesn't always hit correctly, since screen may be unrendered.
178-
setDisconnectDialog({
177+
setDisconnectReason({
179178
server,
180179
reason: JSON.parse(newConn.disconnectReason)
181180
})
@@ -213,21 +212,21 @@ const ServerScreen = () => {
213212
</Text>
214213
</Pressable>
215214
</Dialog>
216-
{disconnectDialog && (
217-
<Dialog visible onRequestClose={() => setDisconnectDialog(null)}>
215+
{disconnectReason && (
216+
<Dialog visible onRequestClose={() => setDisconnectReason()}>
218217
<Text style={dialogStyles.modalTitle}>
219-
Disconnected from {disconnectDialog.server}
218+
Disconnected from {disconnectReason.server}
220219
</Text>
221220
<ChatToJsx
222-
chat={disconnectDialog.reason}
221+
chat={disconnectReason.reason}
223222
component={Text}
224223
colorMap={darkMode ? mojangColorMap : lightColorMap}
225224
componentProps={{ style: styles.serverDescription }}
226225
/>
227226
<View style={dialogStyles.modalButtons}>
228227
<View style={globalStyle.flexSpacer} />
229228
<Pressable
230-
onPress={() => setDisconnectDialog(null)}
229+
onPress={() => setDisconnectReason()}
231230
android_ripple={{ color: '#aaa' }}
232231
style={dialogStyles.modalButton}
233232
>

0 commit comments

Comments
 (0)