1
1
import { StatusBar } from "expo-status-bar" ;
2
2
import { SafeAreaProvider , SafeAreaView } from "react-native-safe-area-context" ;
3
- import { StyleSheet , FlatList } from "react-native" ;
3
+ import {
4
+ Platform ,
5
+ PlatformColor ,
6
+ StyleSheet ,
7
+ FlatList ,
8
+ useColorScheme ,
9
+ } from "react-native" ;
4
10
import { useState } from "react" ;
11
+ import { white , black } from "./constants/colors" ;
5
12
import TaskListItem from "./components/TaskListItem" ;
6
13
import TaskCreator from "./components/TaskCreator" ;
7
14
import Task from "./utils/Task" ;
8
15
import removeFromArray from "./utils/arrayHelpers" ;
9
16
10
- const white = "#fff" ;
11
17
const styles = StyleSheet . create ( {
12
18
container : {
13
19
alignItems : "flex-start" ,
14
- backgroundColor : white ,
15
20
flex : 1 ,
16
21
justifyContent : "flex-start" ,
17
22
marginHorizontal : 10 ,
18
23
} ,
24
+ containerDark : {
25
+ ...Platform . select ( {
26
+ ios : {
27
+ backgroundColor : PlatformColor ( "systemBackground" ) ,
28
+ } ,
29
+ android : {
30
+ backgroundColor : PlatformColor (
31
+ "@android:color/background_dark"
32
+ ) ,
33
+ } ,
34
+ default : { backgroundColor : black } ,
35
+ } ) ,
36
+ } ,
37
+ containerLight : {
38
+ ...Platform . select ( {
39
+ ios : {
40
+ backgroundColor : PlatformColor ( "systemBackground" ) ,
41
+ } ,
42
+ android : {
43
+ backgroundColor : PlatformColor (
44
+ "@android:color/background_light"
45
+ ) ,
46
+ } ,
47
+ default : { backgroundColor : white } ,
48
+ } ) ,
49
+ } ,
19
50
creator : {
20
51
flex : 1 ,
21
52
} ,
53
+ creatorDark : {
54
+ borderColor : white , // PlatformColor crashes the app when used with borderColor
55
+ ...Platform . select ( {
56
+ ios : {
57
+ color : PlatformColor ( "label" ) ,
58
+ } ,
59
+ android : {
60
+ color : PlatformColor ( "@android:color/primary_text_dark" ) ,
61
+ } ,
62
+ default : { color : white } ,
63
+ } ) ,
64
+ } ,
65
+ creatorLight : {
66
+ borderColor : black ,
67
+ ...Platform . select ( {
68
+ ios : {
69
+ color : PlatformColor ( "label" ) ,
70
+ } ,
71
+ android : {
72
+ color : PlatformColor ( "@android:color/primary_text_light" ) ,
73
+ } ,
74
+ default : { color : black } ,
75
+ } ) ,
76
+ } ,
22
77
task : {
23
78
marginBottom : 5 ,
24
79
} ,
80
+ taskDark : {
81
+ ...Platform . select ( {
82
+ ios : {
83
+ color : PlatformColor ( "label" ) ,
84
+ } ,
85
+ android : {
86
+ color : PlatformColor ( "@android:color/primary_text_dark" ) ,
87
+ } ,
88
+ default : { color : "white" } ,
89
+ } ) ,
90
+ } ,
91
+ taskLight : {
92
+ ...Platform . select ( {
93
+ ios : {
94
+ color : PlatformColor ( "label" ) ,
95
+ } ,
96
+ android : {
97
+ color : PlatformColor ( "@android:color/primary_text_light" ) ,
98
+ } ,
99
+ default : { color : "black" } ,
100
+ } ) ,
101
+ } ,
25
102
taskList : {
26
103
flex : 3 ,
27
104
marginTop : 10 ,
28
105
} ,
29
106
} ) ;
30
107
31
108
export default function App ( ) : JSX . Element {
109
+ const colourScheme = useColorScheme ( ) ;
110
+ const containerColor =
111
+ colourScheme === "dark" ? styles . containerDark : styles . containerLight ;
112
+ const creatorColor =
113
+ colourScheme === "dark" ? styles . creatorDark : styles . creatorLight ;
114
+ const taskColor =
115
+ colourScheme === "dark" ? styles . taskDark : styles . taskLight ;
116
+
32
117
const initialTask = new Task ( "Welcome to my minimal todo list!" ) ;
33
118
const [ tasks , setTasks ] = useState ( [ initialTask ] ) ;
34
119
@@ -41,13 +126,20 @@ export default function App(): JSX.Element {
41
126
} ;
42
127
43
128
const renderItem = ( { item } : { item : Task } ) => (
44
- < TaskListItem task = { item } onClick = { removeTask } style = { styles . task } />
129
+ < TaskListItem
130
+ task = { item }
131
+ onClick = { removeTask }
132
+ style = { [ styles . task , taskColor ] }
133
+ />
45
134
) ;
46
135
47
136
return (
48
- < SafeAreaProvider >
137
+ < SafeAreaProvider style = { containerColor } >
49
138
< SafeAreaView style = { styles . container } >
50
- < TaskCreator onAdd = { addTask } style = { styles . creator } />
139
+ < TaskCreator
140
+ onAdd = { addTask }
141
+ style = { [ styles . creator , creatorColor ] }
142
+ />
51
143
< FlatList
52
144
data = { tasks }
53
145
renderItem = { renderItem }
0 commit comments