Skip to content

Commit

Permalink
Merge pull request #52 from fga-eps-mds/hotfix/#223-editarRotina-ativ…
Browse files Browse the repository at this point in the history
…ar-notificacao

(#223) Adiciona notificacao no editarRotina
  • Loading branch information
EduardoGurgel authored Dec 10, 2023
2 parents 2d050ac + ed2f914 commit b95d0c5
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/app/private/pages/editarRotina.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Text,
View,
TextInput,
Platform,
Switch,
} from "react-native";
import React, { useEffect, useState } from "react";
import { ScrollView } from "react-native-gesture-handler";
Expand All @@ -23,6 +25,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
import ErrorMessage from "../../components/ErrorMessage";
import ModalConfirmation from "../../components/ModalConfirmation";
import { IIdoso } from "../../interfaces/idoso.interface";
import * as Notifications from "expo-notifications";

interface IErrors {
titulo?: string;
Expand Down Expand Up @@ -51,6 +54,10 @@ export default function EditarRotina() {
const [hora, setHora] = useState("");
const [showLoadingApagar, setShowLoadingApagar] = useState(false);
const [modalVisible, setModalVisible] = useState(false);
const [notificacao, setNotificacao] = useState(

Check warning on line 57 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L49-L57

Added lines #L49 - L57 were not covered by tests
String(params.notificacao) === "true",
);
const [expoToken, setExpoToken] = useState(params.token);

Check warning on line 60 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L60

Added line #L60 was not covered by tests

const getIdoso = () => {
AsyncStorage.getItem("idoso").then((idosoString) => {

Check warning on line 63 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L62-L63

Added lines #L62 - L63 were not covered by tests
Expand Down Expand Up @@ -139,6 +146,8 @@ export default function EditarRotina() {
dataHora: getDateIsoString(data, hora),
categoria: categoria as ECategoriaRotina,
dias,
token: expoToken,
notificacao,
descricao,
};

Expand Down Expand Up @@ -188,10 +197,48 @@ export default function EditarRotina() {
}
};

const handleNotificacao = async () => {

Check warning on line 200 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L200

Added line #L200 was not covered by tests
if (!notificacao) return;

if (Platform.OS === "android") {
Notifications.setNotificationChannelAsync("default", {

Check warning on line 204 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L204

Added line #L204 was not covered by tests
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}

const { status: existingStatus } =
await Notifications.getPermissionsAsync();

Check warning on line 213 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L213

Added line #L213 was not covered by tests

let finalStatus = existingStatus;

Check warning on line 215 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L215

Added line #L215 was not covered by tests

if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;

Check warning on line 219 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L218-L219

Added lines #L218 - L219 were not covered by tests
}

if (finalStatus !== "granted") {
alert("É necessário permitir as notificações!");
setNotificacao(false);
return;

Check warning on line 225 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L223-L225

Added lines #L223 - L225 were not covered by tests
}

const response = await Notifications.getExpoPushTokenAsync({

Check warning on line 228 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L228

Added line #L228 was not covered by tests
projectId: "7028a81c-adee-41de-91a7-b7e80535a448",
});

setExpoToken(response.data);

Check warning on line 232 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L232

Added line #L232 was not covered by tests
};

useEffect(() => getIdoso(), []);
useEffect(() => getToken(), []);
useEffect(() => handleErrors(), [titulo, data, hora, categoria, descricao]);
useEffect(() => handleDataHora(), []);
useEffect(() => {
handleNotificacao();

Check warning on line 240 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L235-L240

Added lines #L235 - L240 were not covered by tests
}, [notificacao]);

const confirmation = () => {
setModalVisible(!modalVisible);

Check warning on line 244 in src/app/private/pages/editarRotina.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/private/pages/editarRotina.tsx#L243-L244

Added lines #L243 - L244 were not covered by tests
Expand Down Expand Up @@ -294,8 +341,16 @@ export default function EditarRotina() {
<WeekDays dias={dias} callbackFn={setDias} />
</View>

<View style={styles.notificacaoContainer}>
<Switch
trackColor={{ false: "#767577", true: "#2CCDB5" }}
onValueChange={setNotificacao}
value={notificacao}
/>
<Text style={styles.notificacaoText}>Ativar notificação</Text>
</View>

<View style={styles.descricao}>
{/* <Fontisto style={styles.iconDesciption} name="left-align" size={15} /> */}
<TextInput
onChangeText={setDescricao}
value={descricao}
Expand Down Expand Up @@ -464,4 +519,17 @@ const styles = StyleSheet.create({
marginBottom: 25,
alignItems: "center",
},
notificacaoContainer: {
flexDirection: "row",
alignItems: "center",
width: "100%",
fontWeight: "700",
marginBottom: 25,
},
notificacaoText: {
fontWeight: "600",
marginLeft: 7,
fontSize: 16,
color: "#616161",
},
});

0 comments on commit b95d0c5

Please sign in to comment.