-
Notifications
You must be signed in to change notification settings - Fork 4
/
UpdatesApiDemoScreen.tsx
163 lines (144 loc) · 4.43 KB
/
UpdatesApiDemoScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { useUpdates } from "expo-updates"
import { lightTheme } from "@expo/styleguide-base"
import React, { useState } from "react"
import { ActivityIndicator, TextStyle, View, ViewStyle } from "react-native"
import { ExpoAssets, ExpoDemoCard, Screen, Text, UpdateMonitor } from "../components"
import { spacing } from "../theme"
import { DocsLogo } from "../svg"
import {
currentlyRunningTitle,
currentlyRunningDescription,
usePersistentDate,
} from "../utils/updates"
import { CheckInterval, checkIntervalFromSettings, useSettings } from "../utils/useSettings"
const expoVariant = "default"
export function UpdatesApiDemoScreen() {
const { currentlyRunning, isChecking, isDownloading, lastCheckForUpdateTimeSinceRestart } =
useUpdates()
const [showSettings, setShowSettings] = useState(false)
const { settings, changeSettings } = useSettings()
const setMonitorAlwaysVisible = (value: boolean) => {
settings.monitorAlwaysVisible = value
changeSettings(settings)
}
const setAutoLaunchCritical = (value: boolean) => {
settings.autoLaunchCritical = value
changeSettings(settings)
}
const setCheckOnForeground = (value: boolean) => {
settings.checkOnForeground = value
changeSettings(settings)
}
const setUpdateCheckInterval = (value: CheckInterval) => {
settings.checkInterval = value
changeSettings(settings)
}
const lastCheckForUpdateTime = usePersistentDate(lastCheckForUpdateTimeSinceRestart)
const monitorBooleanSettings = showSettings
? [
{
label: "Monitor always visible",
value: settings.monitorAlwaysVisible,
onChange: setMonitorAlwaysVisible,
},
{
label: "Auto launch critical updates",
value: settings.autoLaunchCritical,
onChange: setAutoLaunchCritical,
},
{
label: "Check when app foregrounds",
value: settings.checkOnForeground,
onChange: setCheckOnForeground,
},
]
: []
const monitorChoiceSettings = showSettings
? [
{
value: settings.checkInterval,
onChange: setUpdateCheckInterval,
choices: [
{
label: "Check every hour",
value: CheckInterval.LONG,
},
{
label: "Check every 10 seconds",
value: CheckInterval.SHORT,
},
],
},
]
: []
return (
<Screen preset="scroll" contentContainerStyle={$container} safeAreaEdges={["bottom", "top"]}>
<View style={$topHeading}>
<DocsLogo width={30} height={30} color={lightTheme.icon[expoVariant]} style={$logo} />
<Text
testID="welcome-heading"
style={$topHeadingText}
text="Updates API Demo"
preset="subheading"
/>
</View>
<ExpoDemoCard
variant={expoVariant}
title={currentlyRunningTitle(currentlyRunning)}
description={currentlyRunningDescription(currentlyRunning, lastCheckForUpdateTime)}
actions={[
{
label: showSettings ? "Hide monitor options" : "Show monitor options",
onPress: () => setShowSettings(!showSettings),
},
]}
booleanSettings={monitorBooleanSettings}
choiceSettings={monitorChoiceSettings}
/>
<ExpoAssets />
<View style={$spacer} />
<UpdateMonitor
alwaysVisible={settings.monitorAlwaysVisible}
autoLaunchCritical={settings.autoLaunchCritical}
checkOnForeground={settings.checkOnForeground}
updateCheckInterval={checkIntervalFromSettings(settings)}
buttonsAlwaysVisible={__DEV__}
/>
{isChecking || isDownloading ? (
<ActivityIndicator
style={$activityIndicator}
size="large"
color={lightTheme.icon[expoVariant]}
/>
) : null}
</Screen>
)
}
const $container: ViewStyle = {
flex: 1,
backgroundColor: lightTheme.background[expoVariant],
justifyContent: "flex-start",
alignItems: "center",
paddingHorizontal: spacing.lg,
}
const $topHeading: ViewStyle = {
flexDirection: "row",
justifyContent: "center",
alignContent: "center",
padding: 5,
}
const $logo: ViewStyle = {
margin: 5,
}
const $topHeadingText: TextStyle = {
marginBottom: spacing.md,
color: lightTheme.text[expoVariant],
}
const $spacer: ViewStyle = {
flex: 1,
}
const $activityIndicator: ViewStyle = {
position: "absolute",
top: 10,
right: 10,
}