-
Notifications
You must be signed in to change notification settings - Fork 7
/
App.js
193 lines (182 loc) · 4.4 KB
/
App.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import React from 'react';
import {
StyleSheet,
Text,
View,
StatusBar,
Platform,
ScrollView,
FlatList,
KeyboardAvoidingView,
AsyncStorage,
TouchableOpacity,
} from 'react-native';
import {
SearchBar,
Input,
Button,
ListItem,
} from 'react-native-elements'
import Icon from 'react-native-vector-icons/Feather'
import Icon2 from 'react-native-vector-icons/MaterialIcons'
import { ifIphoneX, getStatusBarHeight } from 'react-native-iphone-x-helper'
const STATUSBAR_HEIGHT = getStatusBarHeight()
const TODO = "@todoapp.todo"
const TodoItem = (props) => {
let icon = null
if (props.done === true) {
icon = <Icon2 name="done" />
}
return (
<TouchableOpacity onPress={props.onTapTodoItem}>
<ListItem
title={props.title}
rightIcon={icon}
bottomDivider
/>
</TouchableOpacity>
)
}
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
todo: [],
currentIndex: 0,
inputText: "",
filterText: "",
}
}
componentDidMount() {
this.loadTodo()
}
loadTodo = async () => {
try {
const todoString = await AsyncStorage.getItem(TODO)
if (todoString) {
const todo = JSON.parse(todoString)
const currentIndex = todo.length
this.setState({todo: todo, currentIndex: currentIndex})
}
} catch (e) {
console.log(e)
}
}
saveTodo = async (todo) => {
try {
const todoString = JSON.stringify(todo)
await AsyncStorage.setItem(TODO, todoString)
} catch (e) {
console.log(e)
}
}
onAddItem = () => {
const title = this.state.inputText
if (title == "") {
return
}
const index = this.state.currentIndex + 1
const newTodo = {index: index, title: title, done: false}
const todo = [...this.state.todo, newTodo]
this.setState({
todo: todo,
currentIndex: index,
inputText: ""
})
this.saveTodo(todo)
}
onTapTodoItem = (todoItem) => {
const todo = this.state.todo
const index = todo.indexOf(todoItem)
todoItem.done = !todoItem.done
todo[index] = todoItem
this.setState({todo: todo})
this.saveTodo(todo)
}
render() {
const filterText = this.state.filterText
let todo = this.state.todo
if (filterText !== "") {
todo = todo.filter(t => t.title.includes(filterText))
}
const platform = Platform.OS == 'ios' ? 'ios' : 'android'
return (
<KeyboardAvoidingView style={styles.container} behavior="padding">
<SearchBar
platform={platform}
cancelButtonTitle="Cancel"
onChangeText={(text) => this.setState({filterText: text})}
onClear={() => this.setState({filterText: ""})}
value={this.state.filterText}
placeholder="Type filter text"
/>
<ScrollView style={styles.todolist}>
<FlatList data={todo}
extraData={this.state}
renderItem={({item}) =>
<TodoItem
title={item.title}
done={item.done}
onTapTodoItem={() => this.onTapTodoItem(item)}
/>
}
keyExtractor={(item, index) => "todo_" + item.index}
/>
</ScrollView>
<View style={styles.input}>
<Input
onChangeText={(text) => this.setState({inputText: text})}
value={this.state.inputText}
containerStyle={styles.inputText}
/>
<Button
icon={
<Icon
name='plus'
size={30}
color='white'
/>
}
title=""
onPress={this.onAddItem}
buttonStyle={styles.inputButton}
/>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
paddingTop: STATUSBAR_HEIGHT,
},
todolist: {
flex: 1
},
input: {
...ifIphoneX({
paddingBottom: 30,
height: 80
}, {
height: 50,
}),
height: 70,
flexDirection: 'row',
paddingRight: 10,
},
inputText: {
paddingLeft: 10,
paddingRight: 10,
flex: 1,
},
inputButton: {
width: 48,
height: 48,
borderWidth: 0,
borderColor: 'transparent',
borderRadius: 48,
backgroundColor: '#ff6347', // ポモドーロを意識してトマト色
},
});