Add stack&bottom-tab navigation to App&move out closeDB&notifications

This commit is contained in:
MariaZ
2022-09-28 19:58:33 +02:00
parent d17e7ed6a2
commit b6024ae921
5 changed files with 77 additions and 105 deletions
+3 -1
View File
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react'
import { getLicenseFlag, saveEncryptionFlag } from '../local-storage'
import { openDb } from '../db'
import { closeDb, openDb } from '../db'
import App from './app'
import AppLoadingView from './common/app-loading'
@@ -29,6 +29,8 @@ export default function AppWrapper() {
useEffect(() => {
checkIsLicenseAccepted()
checkIsDbEncrypted()
return () => closeDb()
}, [])
if (isLoading) {
+64 -53
View File
@@ -1,71 +1,82 @@
import React, { useState, useEffect } from 'react'
import { BackHandler, StyleSheet, View } from 'react-native'
import PropTypes from 'prop-types'
import { LocalDate } from '@js-joda/core'
import React from 'react'
import 'react-native-gesture-handler'
import { StyleSheet, Text } from 'react-native'
import { NavigationContainer } from '@react-navigation/native'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { createStackNavigator } from '@react-navigation/stack'
import Home from './Home'
import Chart from './chart/chart'
import CalendarView from './calendar'
import CycleDayOverview from './cycle-day/cycle-day-overview'
import Stats from './stats'
import Icon from './common/menu-icon'
import Header from './header'
import Menu from './menu'
import { viewsList } from './views'
import { pages } from './pages'
import setupNotifications from '../lib/notifications'
import { closeDb } from '../db'
import { Colors, Fonts, Sizes } from '../styles'
const App = ({ restartApp }) => {
const [date, setDate] = useState(LocalDate.now().toString())
const [currentPage, setCurrentPage] = useState('Home')
const goBack = () => {
if (currentPage === 'Home') {
closeDb()
BackHandler.exitApp()
} else {
const { parent } = pages.find((p) => p.component === currentPage)
setCurrentPage(parent)
}
return true
}
useEffect(() => {
const backHandler = BackHandler.addEventListener(
'hardwareBackPress',
goBack
)
return () => backHandler.remove()
})
useEffect(() => setupNotifications(setCurrentPage, setDate), [])
const Page = viewsList[currentPage]
const isTemperatureEditView = currentPage === 'TemperatureEditView'
const headerProps = { navigate: setCurrentPage }
const pageProps = {
date,
setDate,
isTemperatureEditView,
navigate: setCurrentPage,
}
const HomeStack = createStackNavigator()
function HomeStackScreen() {
return (
<View style={styles.container}>
<Header {...headerProps} />
<Page {...pageProps} restartApp={restartApp} />
<Menu currentPage={currentPage} navigate={setCurrentPage} />
</View>
<HomeStack.Navigator screenOptions={{ headerShown: false }}>
<HomeStack.Screen name="Home" component={Home} />
<HomeStack.Screen name="CycleDayOverview" component={CycleDayOverview} />
</HomeStack.Navigator>
)
}
App.propTypes = {
restartApp: PropTypes.func,
const Tab = createBottomTabNavigator()
const App = () => {
return (
<NavigationContainer>
<Tab.Navigator
screenOptions={({ route }) => ({
header: ({ navigation }) => <Header navigation={navigation} />,
tabBarIcon: ({ focused }) => {
let icon = 'chart'
if (route.name === 'CalendarStackScreen') {
icon = 'calendar'
} else if (route.name === 'Stats') {
icon = 'statistics'
}
return <Icon name={icon} isActive={focused} />
},
tabBarLabel: ({ color }) => {
return (
<Text style={[styles.text, { color: color }]}>{route.name}</Text>
)
},
tabBarActiveTintColor: Colors.orange,
tabBarInactiveTintColor: Colors.grey,
tabBarStyle: { height: 80 },
})}
>
<Tab.Screen
name="HomeStackScreen"
component={HomeStackScreen}
options={{ tabBarButton: () => null, tabBarVisible: false }}
/>
<Tab.Screen name="Calendar" component={CalendarView} />
<Tab.Screen name="Chart" component={Chart} />
<Tab.Screen name="Stats" component={Stats} />
</Tab.Navigator>
</NavigationContainer>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
fontFamily: Fonts.bold,
fontSize: Sizes.small,
textTransform: 'uppercase',
},
})
export default App
+6 -4
View File
@@ -7,17 +7,17 @@ import HamburgerMenu from './hamburger-menu'
import { Colors, Containers, Sizes } from '../../styles'
const Header = ({ isStatic, navigate }) => {
const Header = ({ isStatic, navigation }) => {
return (
<View style={styles.header}>
{isStatic ? (
<Logo />
) : (
<>
<TouchableOpacity onPress={() => navigate('Home')}>
<TouchableOpacity onPress={() => navigation.navigate('Home')}>
<Logo />
</TouchableOpacity>
<HamburgerMenu navigate={navigate} />
<HamburgerMenu navigate={navigation.navigate} />
</>
)}
</View>
@@ -26,7 +26,9 @@ const Header = ({ isStatic, navigate }) => {
Header.propTypes = {
isStatic: PropTypes.bool,
navigate: PropTypes.func,
navigation: PropTypes.shape({
navigate: PropTypes.func.isRequired,
}).isRequired,
}
Header.defaultProps = {
-42
View File
@@ -1,42 +0,0 @@
import React from 'react'
import { StyleSheet, View } from 'react-native'
import PropTypes from 'prop-types'
import MenuItem from './menu-item'
import { Containers } from '../../styles'
import { pages } from '../pages'
const Menu = ({ currentPage, navigate }) => {
const menuItems = pages.filter((page) => page.isInMenu)
return (
<View style={styles.container}>
{menuItems.map(({ icon, label, component }) => {
return (
<MenuItem
isActive={component === currentPage}
onPress={() => navigate(component)}
icon={icon}
key={label}
label={label}
/>
)
})}
</View>
)
}
Menu.propTypes = {
currentPage: PropTypes.string,
navigate: PropTypes.func,
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'white',
...Containers.rowContainer,
},
})
export default Menu
+4 -5
View File
@@ -11,16 +11,15 @@ import { getBleedingDaysSortedByDate } from '../db'
import cycleModule from './cycle'
import nothingChanged from '../db/db-unchanged'
export default function setupNotifications(navigate, setDate) {
export default function setupNotifications(navigation) {
Notification.configure({
onNotification: (notification) => {
const date = LocalDate.now().toString()
// https://github.com/zo0r/react-native-push-notification/issues/966#issuecomment-479069106
if (notification.data?.id === '1' || notification.id === '1') {
const todayDate = LocalDate.now().toString()
setDate(todayDate)
navigate('TemperatureEditView')
navigation.navigate('TemperatureEditView', { date })
} else {
navigate('Home')
navigation.navigate('Home', { date })
}
},
})