🤖 Fix #14: Make this app work with Android (#21)

Co-authored-by: Alexis DRAI <alexis.drai@etu.uca.fr>
Reviewed-on: #21
main
Alexis Drai 2 years ago
parent cc86eb36c9
commit faf7d8c34b

@ -1,9 +1,9 @@
// App.tsx
import React from 'react';
import Navigation from "./navigation/Navigation";
import store from "./redux/store";
import { Provider } from "react-redux";
import React from 'react';
import Navigation from "./navigation/Navigation";
import store from "./redux/store";
import { Provider } from "react-redux";
import { SafeAreaProvider } from "react-native-safe-area-context";
export default function App() {

@ -60,31 +60,47 @@ This app will contain a home page, and a "master/detail" tab for `Moves` with ba
The home screen provides a logo, and tab navigation options to other parts of the application.
<img src="./docs/home.png" width="410" style="margin:20px">
<img src="./docs/home.png" width="410" style="margin:20px" alt="">
### Collection
The collection screen displays a list of `Moves` fetched from the API.
<img src="./docs/moves.png" width="410" style="margin:20px">
<img src="./docs/moves.png" width="410" style="margin:20px" alt="">>
### Detail
The detail screen displays detailed information about a selected `Move`.
<img src="./docs/move.png" width="410" style="margin:20px">
<img src="./docs/move.png" width="410" style="margin:20px" alt="">>
### Creating
The creating screen provides a form for creating a new `Move`.
<img src="./docs/create.png" width="410" style="margin:20px">
<img src="./docs/create.png" width="410" style="margin:20px" alt="">>
### Updating
The updating screen provides a form for updating an existing `Move`.
<img src="./docs/update.png" width="410" style="margin:20px">
<img src="./docs/update.png" width="410" style="margin:20px" alt="">>
## Some business logic
While the back end doesn't forbid a `Move` from being both weak against and effective against the same one type,
it should. That's one of the flaws of that API.
In this app, we did implement the business logic described above in our callbacks for our `MultiSelect` elements, like
so:
```typescript
const handleSelectType = (selectedTypes: string[], setTypes: React.Dispatch<React.SetStateAction<string[]>>, otherSelectedTypes: string[]) => {
const uniqueSelectedTypes = Array.from(new Set(selectedTypes));
const withoutDuplicatesFromOtherColumn = uniqueSelectedTypes.filter(type => !otherSelectedTypes.includes(type));
setTypes(withoutDuplicatesFromOtherColumn);
};
```
## Using the app
@ -94,9 +110,16 @@ In order to use this app, you will need to run the dedicated backend. A `README`
with [instructions](https://github.com/draialexis/pokemong_api#user-content-prep-steps) is provided
for that purpose.
### Connecting to the backend locally
First, please find the `config.ts` file at the root of this project, and replace ~~`192.168.0.15`~~
with the IPv4 address associated with your own Wi-Fi adapter.
To find that address out, you can run `ipconfig` on Windows or `ifconfig` on macOS/Linux in your terminal.
### Running this app
With the [Expo CLI](https://docs.expo.dev/more/expo-cli/) installed, at the root of the project, simply run
Then, with the [Expo CLI](https://docs.expo.dev/more/expo-cli/) installed, at the root of the project, simply run
```bash
npx expo start

@ -1,6 +1,6 @@
// components/AlertModal.tsx
import { Button, Modal, StyleSheet, Text, View } from 'react-native';
import React from 'react';
import React from 'react';
type AlertModalProps = {
visible: boolean;

@ -1,7 +1,7 @@
// components/MoveListItem.test.ts
import { Move } from "../entities/Move";
import React from "react";
import { Move } from "../entities/Move";
import React from "react";
import { StyleSheet, Text, TouchableOpacity, ViewStyle } from "react-native";
type MoveListItemProps = {

@ -1,9 +1,9 @@
// components/TypeTacticsInfoList.test.ts
import React from 'react';
import { render } from '@testing-library/react-native';
import React from 'react';
import { render } from '@testing-library/react-native';
import TypeTacticsInfoList from './TypeTacticsInfoList';
import { TypeName } from "../entities/TypeName";
import { TypeName } from "../entities/TypeName";
describe('TypeTacticsInfoList component', () => {
it('renders types correctly', () => {

@ -1,6 +1,6 @@
// components/TypeTacticsInfoList.ts
import React from "react";
import React from "react";
import { ScrollView, StyleSheet, Text, View } from "react-native";
type TypeListProps = {

@ -1,2 +1,2 @@
// config.ts
export const API_BASE_URL = 'http://localhost:8080';
export const API_BASE_URL = 'http://192.168.0.15:8080';

@ -1,6 +1,6 @@
// entities/Move.ts
import { Type } from "./Type";
import { Type } from "./Type";
import { MoveCategoryName } from "./MoveCategoryName";
export interface Move {

@ -1,15 +1,15 @@
// navigation/Navigation.tsx
import React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';
import MoveListScreen from '../screens/moves/MoveListScreen';
import MoveDetailScreen from '../screens/moves/MoveDetailScreen';
import HomeScreen from '../screens/HomeScreen';
import { createStackNavigator } from '@react-navigation/stack';
import React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';
import MoveListScreen from '../screens/moves/MoveListScreen';
import MoveDetailScreen from '../screens/moves/MoveDetailScreen';
import HomeScreen from '../screens/HomeScreen';
import { createStackNavigator } from '@react-navigation/stack';
import { RootStackParamList, RootTabParamList } from "./navigationTypes";
import { Image, StyleSheet } from 'react-native';
import MoveFormScreen from "../screens/moves/MoveFormScreen";
import { Image, StyleSheet } from 'react-native';
import MoveFormScreen from "../screens/moves/MoveFormScreen";
const Stack = createStackNavigator<RootStackParamList>();
const Tab = createBottomTabNavigator<RootTabParamList>();

@ -1,10 +1,8 @@
// redux/actions/moveAction.ts
import { CREATE_MOVE, DELETE, DELETE_MOVE, GET, GET_MOVES, MOVE_ERROR, POST, PUT, UPDATE_MOVE } from '../constants';
import {
Move
} from "../../entities/Move";
import { Dispatch } from "redux";
import { API_BASE_URL } from "../../config";
import { Move } from "../../entities/Move";
import { Dispatch } from "redux";
import { API_BASE_URL } from "../../config";
export const createMove = (move: Move) => {

@ -1,6 +1,6 @@
// redux/reducers/moveReducer.ts
import { CREATE_MOVE, DELETE_MOVE, GET_MOVES, MOVE_ERROR, UPDATE_MOVE } from '../constants';
import { Move } from "../../entities/Move";
import { Move } from "../../entities/Move";
export type MoveState = {
moves: Move[];

@ -1,6 +1,6 @@
// redux/store.ts
import { configureStore } from '@reduxjs/toolkit'
import { configureStore } from '@reduxjs/toolkit'
import moveReducer, { MoveState } from './reducers/moveReducer';
export type AppDispatch = typeof store.dispatch;

@ -1,6 +1,6 @@
// screens/HomeScreen.tsx
import React from 'react';
import React from 'react';
import { Image, View } from 'react-native';
const HomeScreen = () => {

@ -1,15 +1,15 @@
// screens/moves/MoveDetailScreen.tsx
import React, { useEffect } from 'react';
import React, { useEffect } from 'react';
import { Button, ScrollView, StyleSheet, Text, View } from 'react-native';
import { RouteProp } from '@react-navigation/native';
import { RootStackParamList } from "../../navigation/navigationTypes";
import TypeTacticsInfoList from "../../components/TypeTacticsInfoList"
import { StackNavigationProp } from "@react-navigation/stack";
import { MOVE_DETAIL, MOVE_FORM } from '../../navigation/constants';
import { useSelector } from "react-redux";
import { RootState } from "../../redux/store";
import { Move } from "../../entities/Move";
import { RouteProp } from '@react-navigation/native';
import { RootStackParamList } from "../../navigation/navigationTypes";
import TypeTacticsInfoList from "../../components/TypeTacticsInfoList"
import { StackNavigationProp } from "@react-navigation/stack";
import { MOVE_DETAIL, MOVE_FORM } from '../../navigation/constants';
import { useSelector } from "react-redux";
import { RootState } from "../../redux/store";
import { Move } from "../../entities/Move";
type MoveDetailScreenNavigationProp = StackNavigationProp<RootStackParamList, typeof MOVE_DETAIL>;
@ -35,7 +35,10 @@ const MoveDetailScreen = ({ navigation, route }: Props) => {
return (
<ScrollView style={styles.container}>
<Button title="Edit Move" onPress={() => navigation.navigate(MOVE_FORM, { move: move })}/>
<Button
title="Edit Move"
color={styles.updateButton.backgroundColor}
onPress={() => navigation.navigate(MOVE_FORM, { move: move })}/>
<Text style={styles.title}>Name: {move?.name}</Text>
<Text style={styles.detail}>Category: {move?.category}</Text>
<Text style={styles.detail}>Power: {move?.power}</Text>
@ -50,6 +53,9 @@ const MoveDetailScreen = ({ navigation, route }: Props) => {
};
const styles = StyleSheet.create({
updateButton: {
backgroundColor: '#BA22DA',
},
container: {
flex: 1,
backgroundColor: '#FFFFFF',

@ -1,23 +1,32 @@
// screens/moves/MoveFormScreen.tsx
import React, { useEffect, useState } from 'react';
import { Button, StyleSheet, Text, TextInput } from 'react-native';
import { StackNavigationProp } from '@react-navigation/stack';
import { RootStackParamList } from "../../navigation/navigationTypes";
import { useDispatch, useSelector } from 'react-redux';
import { createMove, updateMove } from '../../redux/actions/moveActions';
import { AppDispatch, RootState } from "../../redux/store";
import { Move } from "../../entities/Move";
import { RouteProp } from "@react-navigation/native";
import { MOVE_FORM } from "../../navigation/constants";
import { Picker } from "@react-native-community/picker";
import { ItemValue } from "@react-native-community/picker/typings/Picker";
import { MoveCategoryName } from "../../entities/MoveCategoryName";
import { TypeName } from "../../entities/TypeName";
import MultiSelect from "react-native-multiple-select";
import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view";
import AlertModal from "../../components/AlertModal";
import { MOVE_ERROR } from "../../redux/constants";
import React, { useEffect, useState } from 'react';
import {
Button,
KeyboardAvoidingView,
Modal,
Platform,
ScrollView,
StyleSheet,
Text,
TextInput,
View
} from 'react-native';
import { StackNavigationProp } from '@react-navigation/stack';
import { RootStackParamList } from "../../navigation/navigationTypes";
import { useDispatch, useSelector } from 'react-redux';
import { createMove, updateMove } from '../../redux/actions/moveActions';
import { AppDispatch, RootState } from "../../redux/store";
import { Move } from "../../entities/Move";
import { RouteProp } from "@react-navigation/native";
import { MOVE_FORM } from "../../navigation/constants";
import { Picker } from "@react-native-community/picker";
import { ItemValue } from "@react-native-community/picker/typings/Picker";
import { MoveCategoryName } from "../../entities/MoveCategoryName";
import { TypeName } from "../../entities/TypeName";
import MultiSelect from "react-native-multiple-select";
import AlertModal from "../../components/AlertModal";
import { MOVE_ERROR } from "../../redux/constants";
type MoveFormScreenNavigationProp = StackNavigationProp<RootStackParamList, typeof MOVE_FORM>;
type MoveFormScreenRouteProp = RouteProp<RootStackParamList, typeof MOVE_FORM>;
@ -50,6 +59,14 @@ const MoveFormScreen = ({ navigation, route }: Props) => {
});
}, [navigation, route.params?.move]);
const [isModalVisible, setModalVisible] = useState(false);
const [currentMultiSelect, setCurrentMultiSelect] = useState<'weakAgainst' | 'effectiveAgainst'>('weakAgainst');
const handleOpenModal = (multiSelect: 'weakAgainst' | 'effectiveAgainst') => {
setCurrentMultiSelect(multiSelect);
setModalVisible(true);
};
const [selectedWeakAgainst, setSelectedWeakAgainst] = useState<string[]>(move.type.weakAgainst);
const [selectedEffectiveAgainst, setSelectedEffectiveAgainst] = useState<string[]>(move.type.effectiveAgainst);
@ -76,89 +93,113 @@ const MoveFormScreen = ({ navigation, route }: Props) => {
};
return (
<KeyboardAwareScrollView style={styles.container}>
<AlertModal
visible={!!error}
message={error || ''}
onClose={() => dispatch({ type: MOVE_ERROR, payload: null })}
/>
<Text style={styles.label}>Name: </Text>
<TextInput
value={move.name}
onChangeText={(text) => setMove({ ...move, name: text })}
style={styles.input}
/>
<Text style={styles.label}>Category: </Text>
<Picker
selectedValue={move.category}
style={styles.input}
onValueChange={(itemValue: ItemValue) =>
setMove({ ...move, category: itemValue as MoveCategoryName })
}>
{Object.values(MoveCategoryName).map((value) =>
<Picker.Item key={value} label={value} value={value}/>
)}
</Picker>
<Text style={styles.label}>Power: </Text>
<TextInput
value={move.power.toString()}
onChangeText={(text) => {
if (!isNaN(Number(text))) {
setMove({ ...move, power: Number(text) });
}
}}
style={styles.input}
keyboardType="numeric"
/>
<Text style={styles.label}>Accuracy: </Text>
<TextInput
value={move.accuracy.toString()}
onChangeText={(text) => {
if (!isNaN(Number(text))) {
setMove({ ...move, accuracy: Number(text) });
}
}}
style={styles.input}
keyboardType="numeric"
/>
<Text style={styles.label}>Type: </Text>
<Picker
selectedValue={move.type.name}
style={styles.input}
onValueChange={(itemValue: ItemValue) =>
setMove({ ...move, type: { ...move.type, name: itemValue as TypeName } })
}>
{Object.values(TypeName).map((value) =>
<Picker.Item key={value} label={value} value={value}/>
)}
</Picker>
<Text style={styles.label}>Weak Against: </Text>
<MultiSelect
items={
Object.values(TypeName).map((value) => ({ id: value, name: value }))
}
uniqueKey="id"
onSelectedItemsChange={
(selectedItems) => handleSelectType(selectedItems, setSelectedWeakAgainst, selectedEffectiveAgainst)
}
selectedItems={selectedWeakAgainst}
displayKey="name"
/>
<Text style={styles.label}>Effective Against: </Text>
<MultiSelect
items={
Object.values(TypeName).map((value) => ({ id: value, name: value }))
}
uniqueKey="id"
onSelectedItemsChange={
(selectedItems) => handleSelectType(selectedItems, setSelectedEffectiveAgainst, selectedWeakAgainst)
}
selectedItems={selectedEffectiveAgainst}
displayKey="name"
/>
<Button title="Save" onPress={handleSave}/>
</KeyboardAwareScrollView>
<KeyboardAvoidingView
style={styles.container}
behavior={Platform.OS === "ios" ? "padding" : "height"}
>
<ScrollView contentContainerStyle={{ paddingBottom: 20 }}>
<AlertModal
visible={!!error}
message={error || ''}
onClose={() => dispatch({ type: MOVE_ERROR, payload: null })}
/>
<View style={styles.row}>
<Text style={styles.label}>Name: </Text>
<TextInput
value={move.name}
onChangeText={(text) => setMove({ ...move, name: text })}
style={styles.input}
/>
</View>
<View style={styles.row}>
<Text style={styles.label}>Category: </Text>
<Picker
selectedValue={move.category}
style={styles.input}
onValueChange={(itemValue: ItemValue) =>
setMove({ ...move, category: itemValue as MoveCategoryName })
}>
{Object.values(MoveCategoryName).map((value) =>
<Picker.Item key={value} label={value} value={value}/>
)}
</Picker>
</View>
<View style={styles.row}>
<Text style={styles.label}>Power: </Text>
<TextInput
value={move.power.toString()}
onChangeText={(text) => {
if (!isNaN(Number(text))) {
setMove({ ...move, power: Number(text) });
}
}}
style={styles.input}
keyboardType="numeric"
/>
</View>
<View style={styles.row}>
<Text style={styles.label}>Accuracy: </Text>
<TextInput
value={move.accuracy.toString()}
onChangeText={(text) => {
if (!isNaN(Number(text))) {
setMove({ ...move, accuracy: Number(text) });
}
}}
style={styles.input}
keyboardType="numeric"
/>
</View>
<View style={styles.row}>
<Text style={styles.label}>Type: </Text>
<Picker
selectedValue={move.type.name}
style={styles.input}
onValueChange={(itemValue: ItemValue) =>
setMove({ ...move, type: { ...move.type, name: itemValue as TypeName } })
}>
{Object.values(TypeName).map((value) =>
<Picker.Item key={value} label={value} value={value}/>
)}
</Picker>
</View>
<Text>Weak Against: {selectedWeakAgainst.join(', ')}</Text>
<View style={styles.buttonContainer}>
<Button title="Select Weak Against" onPress={() => handleOpenModal('weakAgainst')}/>
</View>
<Text>Effective Against: {selectedEffectiveAgainst.join(', ')}</Text>
<View style={styles.buttonContainer}>
<Button title="Select Effective Against" onPress={() => handleOpenModal('effectiveAgainst')}/>
</View>
<View style={styles.buttonContainer}>
<Button title="Save" onPress={handleSave} color={styles.saveButton.backgroundColor}/>
</View>
</ScrollView>
<Modal
animationType="slide"
transparent={true}
visible={isModalVisible}
onRequestClose={() => {
setModalVisible(!isModalVisible);
}}>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<MultiSelect
items={
Object.values(TypeName).map((value) => ({ id: value, name: value }))
}
uniqueKey="id"
onSelectedItemsChange={
(selectedItems) => handleSelectType(selectedItems, currentMultiSelect === 'weakAgainst' ? setSelectedWeakAgainst : setSelectedEffectiveAgainst, currentMultiSelect === 'weakAgainst' ? selectedEffectiveAgainst : selectedWeakAgainst)
}
selectedItems={currentMultiSelect === 'weakAgainst' ? selectedWeakAgainst : selectedEffectiveAgainst}
displayKey="name"
/>
<Button title="Close" onPress={() => setModalVisible(false)}/>
</View>
</View>
</Modal>
</KeyboardAvoidingView>
);
};
@ -167,14 +208,49 @@ const styles = StyleSheet.create({
flex: 1,
padding: 16,
},
label: {
marginRight: 8,
},
input: {
backgroundColor: '#CEC',
borderRadius: 8,
height: 32,
minHeight: 32,
flex: 1,
},
label: {
row: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
margin: 8,
}
flexWrap: 'wrap',
},
centeredView: {
flex: 1,
justifyContent: "center",
alignItems: "center",
marginTop: 22
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 20,
padding: 35,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5
},
buttonContainer: {
margin: 8,
},
saveButton: {
backgroundColor: '#BADA55',
},
});
export default MoveFormScreen;

@ -1,18 +1,18 @@
// screens/moves/MoveListScreen.tsx
import React from 'react';
import React from 'react';
import { Button, FlatList, StyleSheet, View } from 'react-native';
import { StackNavigationProp } from '@react-navigation/stack';
import { RootStackParamList } from "../../navigation/navigationTypes";
import { useDispatch, useSelector } from 'react-redux';
import { deleteMove, getMoves } from '../../redux/actions/moveActions';
import { MoveState } from "../../redux/reducers/moveReducer";
import { AppDispatch } from "../../redux/store";
import MoveListItem from "../../components/MoveListItem";
import { MOVE_DETAIL, MOVE_FORM, MOVE_LIST } from "../../navigation/constants";
import { RouteProp, useFocusEffect } from "@react-navigation/native";
import AlertModal from "../../components/AlertModal";
import { MOVE_ERROR } from "../../redux/constants";
import { StackNavigationProp } from '@react-navigation/stack';
import { RootStackParamList } from "../../navigation/navigationTypes";
import { useDispatch, useSelector } from 'react-redux';
import { deleteMove, getMoves } from '../../redux/actions/moveActions';
import { MoveState } from "../../redux/reducers/moveReducer";
import { AppDispatch } from "../../redux/store";
import MoveListItem from "../../components/MoveListItem";
import { MOVE_DETAIL, MOVE_FORM, MOVE_LIST } from "../../navigation/constants";
import { RouteProp, useFocusEffect } from "@react-navigation/native";
import AlertModal from "../../components/AlertModal";
import { MOVE_ERROR } from "../../redux/constants";
type MoveListScreenNavigationProp = StackNavigationProp<RootStackParamList, typeof MOVE_LIST>;
type MoveListScreenRouteProp = RouteProp<RootStackParamList, typeof MOVE_LIST>;
@ -49,7 +49,10 @@ const MoveListScreen = ({ navigation }: Props) => {
<FlatList
data={moves}
ListHeaderComponent={
<Button title="Add Move" onPress={() => navigation.navigate(MOVE_FORM, { move: undefined })}/>
<Button
title="Add Move"
color={styles.addButton.backgroundColor}
onPress={() => navigation.navigate(MOVE_FORM, { move: undefined })}/>
}
renderItem={({ item }) => (
<View style={styles.listItemContainer}>
@ -77,6 +80,9 @@ const styles = StyleSheet.create({
deleteButton: {
backgroundColor: '#FF6961',
},
addButton: {
backgroundColor: '#BADA55',
},
listItemContainer: {
flexDirection: 'row',
justifyContent: 'space-between',

Loading…
Cancel
Save