-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
151 lines (139 loc) · 3.49 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
import React, { Component } from "react";
import { StyleSheet, View, Button, Text, AsyncStorage } from "react-native";
import Alert from "./components/Alert";
import NewQuote from "./components/NewQuote";
import Quote from "./components/Quote";
const data = [
{
text: "Erfolg ist die beste Rache.",
author: "Michael Douglas"
},
{
text:
"Das Geheimnis des Erfolges ist, den Standpunkt des anderen zu verstehen.",
author: "Henry Ford"
},
{
text:
"Bei den meisten Erfolgsmenschen ist der Erfolg größer als die Menschlichkeit.",
author: "Daphne du Maurier"
}
];
export default class App extends Component {
state = {
quotes: data,
index: 0,
showAlert: false,
alertType: "success",
screenNewQuote: false
};
_hideAlert = () => {
this.setState({ showAlert: false });
};
_showAlert = alertType => {
this.setState({ showAlert: true, alertType: alertType });
setTimeout(this._hideAlert, 3000);
};
_retrieveData = async () => {
let value = await AsyncStorage.getItem("QUOTES");
if (value !== null) {
value = JSON.parse(value);
this.setState({ quotes: value });
}
};
_storeData = quotes => {
AsyncStorage.setItem("QUOTES", JSON.stringify(quotes));
};
_onSaveBehavior = (author, text) => {
let { quotes } = this.state;
if (author && text) {
quotes.push({ author, text });
this._storeData(quotes);
this._showAlert("success");
} else {
this._showAlert("warning");
}
this.setState({ index: quotes.length - 1, screenNewQuote: false, quotes });
};
_onCancelBehavior = () => {
this.setState({ screenNewQuote: false });
this._showAlert("warning");
};
componentDidMount() {
this._retrieveData();
}
render() {
const { index, quotes, showAlert, alertType, screenNewQuote } = this.state;
const quote = quotes[index];
let nextIndex = index + 1;
if (nextIndex === quotes.length) nextIndex = 0;
return (
<View style={styles.container}>
<View style={styles.navbar}>
<Text style={styles.headerText}>NiceQuotes</Text>
<View style={styles.newQuoteButton}>
<Button
title="Neues Zitat"
onPress={() => this.setState({ screenNewQuote: true })}
/>
</View>
</View>
<Alert type={alertType} visible={showAlert} />
<NewQuote
visible={screenNewQuote}
onSave={this._onSaveBehavior}
onCancel={this._onCancelBehavior}
/>
<Quote text={quote.text} author={quote.author} />
<View style={styles.nextQuoteButton}>
<Button
title="Nächstes Zitat"
onPress={() => this.setState({ index: nextIndex })}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "#1b85b8"
},
navbar: {
position: "absolute",
top: 0,
width: "100%",
height: 100,
padding: 0,
backgroundColor: "#fff"
},
headerText: {
top: 45,
textAlign: "center",
fontSize: 30
},
newQuoteButton: {
position: "absolute",
top: 50,
right: 10
},
quoteText: {
textAlign: "center",
fontSize: 30
},
quoteAuthor: {
fontSize: 18,
marginTop: 10,
marginBottom: 60
},
nextQuoteButton: {
position: "absolute",
bottom: 60,
padding: 10,
borderRadius: 10,
backgroundColor: "#fff"
}
});