2021-12-29 12:29:29 +01:00
|
|
|
import React, { Component } from 'react'; // Bei React bzw. Nativ immer importieren!
|
2021-12-28 15:50:53 +01:00
|
|
|
import { StatusBar } from 'expo-status-bar';
|
2021-12-29 14:06:22 +01:00
|
|
|
import { Button, StyleSheet, View } from 'react-native';
|
2021-12-28 15:50:53 +01:00
|
|
|
|
2022-01-04 18:05:37 +01:00
|
|
|
import Quote from './js/components/Quote'; // Eigene Componente
|
|
|
|
import NewQuote from './js/components/NewQuote';// Eigene Componente
|
|
|
|
|
2021-12-29 12:29:29 +01:00
|
|
|
|
|
|
|
const data = [
|
2022-01-04 12:23:46 +01:00
|
|
|
{ text: "Lernen ist Erfahrung. Alles andere ist einfach nur Information.", author: "Albert Einstein" },
|
2021-12-29 12:29:29 +01:00
|
|
|
{ text: "Zitat2", author: "Author 2" },
|
|
|
|
{ text: "Zitat3", author: "Author 3" },
|
|
|
|
{ text: "Zitat4", author: "Author 4" },
|
|
|
|
{ text: "Zitat5", author: "Author 5" },
|
|
|
|
];
|
|
|
|
export default class App extends Component {
|
2022-01-21 23:54:30 +01:00
|
|
|
state = { index: 0, showNewQuoteScreen: false, quotes: data }; //initialer Zustand
|
2022-01-04 18:05:37 +01:00
|
|
|
|
|
|
|
// Eigene Methoden mit unterstrich am Anfang
|
2022-01-21 23:54:30 +01:00
|
|
|
_addQuote = (text, author) => {
|
|
|
|
|
|
|
|
// 1. weise aktuelle Liste der Zitate (aus state) einer Variable zu
|
|
|
|
let { quotes } = this.state;
|
|
|
|
|
|
|
|
// 2. füge neues Zitat an ds Ende der Liste an
|
|
|
|
if (text !== null && author !== null) quotes.push({ text, author });
|
|
|
|
|
|
|
|
|
|
|
|
// 3 aktuallisiere die Liste im Stat
|
|
|
|
|
|
|
|
|
2022-01-04 18:05:37 +01:00
|
|
|
// NewQuote Ausblenden
|
2022-01-21 23:54:30 +01:00
|
|
|
this.setState({ showNewQuoteScreen: false, quotes });
|
2022-01-04 18:05:37 +01:00
|
|
|
}
|
2021-12-29 12:29:29 +01:00
|
|
|
|
|
|
|
// Darstellung der Komponente im UI
|
2021-12-29 14:06:22 +01:00
|
|
|
// Render wird automatisch ausgeführt:
|
2021-12-29 12:29:29 +01:00
|
|
|
// a) Komponente erscheint im Ui (initialer Zustand)
|
|
|
|
// b) Zustand ändert sich (state) => this.setstate(...)
|
2021-12-29 14:06:22 +01:00
|
|
|
// c) probs sich ändern
|
2021-12-29 12:29:29 +01:00
|
|
|
render() {
|
2022-01-21 23:54:30 +01:00
|
|
|
let { index, quotes } = this.state;
|
|
|
|
const quote = quotes[index];
|
2021-12-29 12:29:29 +01:00
|
|
|
let nextIndex = index + 1;
|
2022-01-21 23:54:30 +01:00
|
|
|
if (nextIndex === quotes.length) nextIndex = 0;
|
2021-12-29 14:06:22 +01:00
|
|
|
let prevIndex = index - 1;
|
2022-01-21 23:54:30 +01:00
|
|
|
if (index === 0) prevIndex = quotes.length - 1;
|
2021-12-29 12:29:29 +01:00
|
|
|
return (
|
|
|
|
//JSX
|
|
|
|
<View style={styles.container}>
|
2022-01-04 18:05:37 +01:00
|
|
|
<View style={styles.buttonNew}>
|
|
|
|
<Button
|
|
|
|
title="New"
|
|
|
|
onPress={() => this.setState({ showNewQuoteScreen: true })}>
|
|
|
|
</Button>
|
2022-01-04 12:34:49 +01:00
|
|
|
</View>
|
2022-01-04 18:05:37 +01:00
|
|
|
<NewQuote
|
|
|
|
visible={this.state.showNewQuoteScreen}
|
|
|
|
onSave={this._addQuote} />
|
|
|
|
<Quote
|
|
|
|
text={quote.text}
|
|
|
|
author={quote.author} />
|
|
|
|
<View style={styles.buttonNext}>
|
|
|
|
<Button
|
|
|
|
color='grey'
|
|
|
|
title="Nächstes Zitat"
|
|
|
|
onPress={() => this.setState({ index: nextIndex })} />
|
|
|
|
</View>
|
|
|
|
<View style={styles.buttonPrev}>
|
|
|
|
<Button
|
|
|
|
title="Letztes Zitat"
|
|
|
|
onPress={() => this.setState({ index: prevIndex })} />
|
2022-01-04 12:23:46 +01:00
|
|
|
</View>
|
2021-12-29 12:29:29 +01:00
|
|
|
<StatusBar style="auto" />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2021-12-28 15:50:53 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 12:29:29 +01:00
|
|
|
//Aussehen und Layout
|
2021-12-28 15:50:53 +01:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
2022-01-04 12:23:46 +01:00
|
|
|
backgroundColor: '#F2F0FF',
|
2022-01-04 18:05:37 +01:00
|
|
|
alignItems: 'center', //FlexBox Layout
|
2021-12-28 15:50:53 +01:00
|
|
|
justifyContent: 'center',
|
2022-01-04 12:23:46 +01:00
|
|
|
},
|
2022-01-04 18:05:37 +01:00
|
|
|
buttonNext: {
|
2022-01-04 12:34:49 +01:00
|
|
|
position: 'absolute',
|
|
|
|
bottom: 50,
|
|
|
|
},
|
2022-01-04 18:05:37 +01:00
|
|
|
buttonPrev: {
|
2022-01-04 12:23:46 +01:00
|
|
|
position: 'absolute',
|
|
|
|
bottom: 5,
|
2021-12-28 15:50:53 +01:00
|
|
|
},
|
2022-01-04 18:05:37 +01:00
|
|
|
buttonNew: {
|
|
|
|
position: 'absolute',
|
|
|
|
right: 0,
|
|
|
|
top: 25,
|
|
|
|
}
|
2021-12-28 15:50:53 +01:00
|
|
|
});
|