From 885c95cb3f95c1f12cb1cd47fd679e33a96b5cde Mon Sep 17 00:00:00 2001 From: FranciscoOssian Date: Mon, 3 Jun 2024 12:12:20 -0300 Subject: [PATCH] changes --- App.tsx | 13 ++- package.json | 1 + src/components/common/Back/styles.js | 1 + src/components/common/Close/index.tsx | 12 +++ src/components/common/Close/styles.ts | 5 + src/components/pages/Home/NewChalk/index.tsx | 52 ++++++++++ src/components/pages/Home/NewChalk/styles.ts | 17 ++++ src/contexts/realm/{index.js => index.ts} | 0 src/hooks/useChats/index.ts | 7 +- src/hooks/useMatchConfig/index.ts | 30 ------ src/hooks/useMatchConfig/index.tsx | 26 +++++ src/hooks/useRemoteAsset/index.tsx | 32 ++++++ src/pages/Account/editor.tsx | 39 ++------ src/pages/Account/index.tsx | 37 +++++-- src/pages/Account/signIn.tsx | 66 ++++++++++--- src/pages/Account/signUp.tsx | 10 +- src/pages/index.tsx | 99 +++++++++++++++---- src/pages/newChalk.tsx | 35 +------ src/services/chalkSystem/index.ts | 60 +++++++++++ src/services/firebase/create/notification.ts | 25 +++++ src/services/firebase/set/user.ts | 19 ++-- src/services/localStorage/defaults.ts | 6 +- src/services/realm/create/user.ts | 36 +++++++ .../schemas/{chatSchema.js => chatSchema.ts} | 0 ...ssageSchema.js => contentMessageSchema.ts} | 0 .../schemas/{fileSchema.js => fileSchema.ts} | 0 .../realm/schemas/{index.js => index.ts} | 10 +- src/services/realm/schemas/matchingConfig.ts | 13 +++ .../{messageSchema.js => messageSchema.ts} | 0 .../schemas/{userSchema.js => userSchema.ts} | 1 + src/services/realm/update/matchConfig.tsx | 22 +++++ src/services/realm/update/user.tsx | 31 ++++++ src/types/user.ts | 20 ++-- src/utils/backgroundPublishMatch.ts | 52 ++++++++++ src/utils/backgroundTaskMessages.ts | 15 ++- src/utils/translations.json | 11 ++- 36 files changed, 628 insertions(+), 175 deletions(-) create mode 100644 src/components/common/Close/index.tsx create mode 100644 src/components/common/Close/styles.ts create mode 100644 src/components/pages/Home/NewChalk/index.tsx create mode 100644 src/components/pages/Home/NewChalk/styles.ts rename src/contexts/realm/{index.js => index.ts} (100%) delete mode 100644 src/hooks/useMatchConfig/index.ts create mode 100644 src/hooks/useMatchConfig/index.tsx create mode 100644 src/hooks/useRemoteAsset/index.tsx create mode 100644 src/services/chalkSystem/index.ts create mode 100644 src/services/firebase/create/notification.ts create mode 100644 src/services/realm/create/user.ts rename src/services/realm/schemas/{chatSchema.js => chatSchema.ts} (100%) rename src/services/realm/schemas/{contentMessageSchema.js => contentMessageSchema.ts} (100%) rename src/services/realm/schemas/{fileSchema.js => fileSchema.ts} (100%) rename src/services/realm/schemas/{index.js => index.ts} (55%) create mode 100644 src/services/realm/schemas/matchingConfig.ts rename src/services/realm/schemas/{messageSchema.js => messageSchema.ts} (100%) rename src/services/realm/schemas/{userSchema.js => userSchema.ts} (88%) create mode 100644 src/services/realm/update/matchConfig.tsx create mode 100644 src/services/realm/update/user.tsx create mode 100644 src/utils/backgroundPublishMatch.ts diff --git a/App.tsx b/App.tsx index a793cdf..c119d83 100644 --- a/App.tsx +++ b/App.tsx @@ -12,6 +12,8 @@ import { startBackgroundFetch } from '@services/background'; import useAppState from '@src/hooks/useAppState'; import { useNetInfo } from '@react-native-community/netinfo'; import LottieView from 'lottie-react-native'; +import { matchListener } from '@src/services/chalkSystem'; +import { initMatching } from '@src/utils/backgroundPublishMatch'; const { RealmProvider } = realmContext; @@ -42,8 +44,15 @@ function SplashScreenComp() { ); } -const BACKGROUND_FETCH_TASK = 'fetch-new-messages-store-and-notify'; -startBackgroundFetch(BACKGROUND_FETCH_TASK, async () => { +const BACKGROUND_FETCH_TASK_1 = 'publish-account-and-await-to-maitch'; +startBackgroundFetch(BACKGROUND_FETCH_TASK_1, async () => { + initMatching(); +}); + +initMatching(); + +const BACKGROUND_FETCH_TASK_2 = 'fetch-new-messages-store-and-notify'; +startBackgroundFetch(BACKGROUND_FETCH_TASK_2, async () => { iterateOverChats(); }); diff --git a/package.json b/package.json index 55f0db1..ffb72ae 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@realm/react": "^0.4.3", "axios": "^1.4.0", "expo": "~48.0.21", + "expo-asset": "~8.9.1", "expo-background-fetch": "^11.1.1", "expo-camera": "~13.2.1", "expo-constants": "~14.2.1", diff --git a/src/components/common/Back/styles.js b/src/components/common/Back/styles.js index cbfa833..120d9f8 100644 --- a/src/components/common/Back/styles.js +++ b/src/components/common/Back/styles.js @@ -4,6 +4,7 @@ import T from '../Touch'; export const Touch = styled(T)` position: absolute; + padding: 10px; left: 25px; top: 25px; `; diff --git a/src/components/common/Close/index.tsx b/src/components/common/Close/index.tsx new file mode 100644 index 0000000..3e7a1bb --- /dev/null +++ b/src/components/common/Close/index.tsx @@ -0,0 +1,12 @@ +import { AntDesign } from '@expo/vector-icons'; +import { Touch } from './styles'; + +const Close = ({ onPress, size }: { onPress: () => Promise; size: number }) => { + return ( + + + + ); +}; + +export default Close; diff --git a/src/components/common/Close/styles.ts b/src/components/common/Close/styles.ts new file mode 100644 index 0000000..9c35b7d --- /dev/null +++ b/src/components/common/Close/styles.ts @@ -0,0 +1,5 @@ +import styled from 'styled-components/native'; + +import T from '@components/common/Touch'; + +export const Touch = styled(T)``; diff --git a/src/components/pages/Home/NewChalk/index.tsx b/src/components/pages/Home/NewChalk/index.tsx new file mode 100644 index 0000000..03223e2 --- /dev/null +++ b/src/components/pages/Home/NewChalk/index.tsx @@ -0,0 +1,52 @@ +import { useRef } from 'react'; +import { ActivityIndicator, Modal, Text } from 'react-native'; +import LottieView from 'lottie-react-native'; +import { Container, ContainerBox } from './styles'; +import Close from '@src/components/common/Close'; +import useRemoteAsset from '@src/hooks/useRemoteAsset'; + +const NewChalk = ({ + text, + visible, + toggleModal, +}: { + text: string; + visible: boolean; + toggleModal: () => void; +}) => { + const animation = useRef(null); + const { data, loading } = useRemoteAsset( + 'https://firebasestorage.googleapis.com/v0/b/chatalk-96c5b.appspot.com/o/app-assets%2Flottiefiles%2Fradar.json?alt=media&token=4f377968-7661-4911-8868-4de72991e798' + ); + + if (loading) { + return ; + } + + return ( + + + + {data ? ( + + ) : ( + Error loading animation + )} + {text} + toggleModal()} /> + + + + ); +}; + +export default NewChalk; diff --git a/src/components/pages/Home/NewChalk/styles.ts b/src/components/pages/Home/NewChalk/styles.ts new file mode 100644 index 0000000..b423f64 --- /dev/null +++ b/src/components/pages/Home/NewChalk/styles.ts @@ -0,0 +1,17 @@ +import styled from 'styled-components/native'; + +export const Container = styled.View` + flex: 1; + justify-content: center; + align-items: center; +`; + +export const ContainerBox = styled.View` + width: 50%; + height: 50%; + background-color: white; + margin: 10px; + justify-content: space-between; + align-items: center; + border-radius: 100000px; +`; diff --git a/src/contexts/realm/index.js b/src/contexts/realm/index.ts similarity index 100% rename from src/contexts/realm/index.js rename to src/contexts/realm/index.ts diff --git a/src/hooks/useChats/index.ts b/src/hooks/useChats/index.ts index 070ef3e..520356b 100644 --- a/src/hooks/useChats/index.ts +++ b/src/hooks/useChats/index.ts @@ -5,7 +5,12 @@ const useChats = (id: string | undefined = undefined) => { const chats = realmContext.useQuery('Chat'); if (id) return chats.filtered(`id == '${id}'`); - else return chats; + else + return [...chats].sort((a: ChatType, b: ChatType) => { + const dateA = a.lastMessage ? new Date(a.lastMessage.timestamp).getTime() : 0; + const dateB = b.lastMessage ? new Date(b.lastMessage.timestamp).getTime() : 0; + return dateB - dateA; + }); }; export default useChats; diff --git a/src/hooks/useMatchConfig/index.ts b/src/hooks/useMatchConfig/index.ts deleted file mode 100644 index 1d80c9f..0000000 --- a/src/hooks/useMatchConfig/index.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { useEffect, useState } from 'react'; -import MatchConfigType from '@src/types/matchConfig'; -import localStorage from '@src/services/localStorage'; - -type ReturnType = [MatchConfigType, (config: MatchConfigType) => void]; - -const useMatchConfig = (): ReturnType => { - const [matchConfig, setMatchConfig] = useState({ - from: 18, - to: 18, - lang: '', - genders: [], - }); - - useEffect(() => { - (async () => { - const mc = await localStorage('matchingConfig').get(); - setMatchConfig(mc); - })(); - }, []); - - const setMatchConfigHandler = (config: MatchConfigType) => { - localStorage('matchingConfig').set(config); - setMatchConfig(config); - }; - - return [matchConfig, setMatchConfigHandler]; -}; - -export default useMatchConfig; diff --git a/src/hooks/useMatchConfig/index.tsx b/src/hooks/useMatchConfig/index.tsx new file mode 100644 index 0000000..4d1dcab --- /dev/null +++ b/src/hooks/useMatchConfig/index.tsx @@ -0,0 +1,26 @@ +import MatchConfigType from '@src/types/matchConfig'; +import { useCallback } from 'react'; +import realmContext from '@contexts/realm'; +import useUser from '../useUser'; +import updateRealmMatchConfig from '@src/services/realm/update/matchConfig'; +import { matchingConfig } from '@src/services/localStorage/defaults'; + +const useMatchConfig = () => { + const realm = realmContext.useRealm(); + const me = useUser(); + + const state = realmContext + .useQuery('MatchingConfig') + .filtered(`id == '${me?.id}'`)[0]; + + const change = useCallback( + (value: MatchConfigType) => { + updateRealmMatchConfig(realm, { id: me.id, ...value }); + }, + [me] + ); + + return { matchConfig: state ?? matchingConfig, setMatchConfig: change }; +}; + +export default useMatchConfig; diff --git a/src/hooks/useRemoteAsset/index.tsx b/src/hooks/useRemoteAsset/index.tsx new file mode 100644 index 0000000..335a3ef --- /dev/null +++ b/src/hooks/useRemoteAsset/index.tsx @@ -0,0 +1,32 @@ +import { useEffect, useState } from 'react'; +import { Asset } from 'expo-asset'; + +const useRemoteAsset = (url: string) => { + const [data, setData] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + const loadRemote = async () => { + try { + const asset = Asset.fromURI(url); + await asset.downloadAsync(); + const response = await fetch(asset.uri); + const jsonData = await response.json(); + setData(jsonData); + } catch (error) { + console.error('Error loading remote JSON asset:', error); + } finally { + setLoading(false); + } + }; + + loadRemote(); + }, []); + + return { + data, + loading, + }; +}; + +export default useRemoteAsset; diff --git a/src/pages/Account/editor.tsx b/src/pages/Account/editor.tsx index 13f0a2f..d7bdfe3 100644 --- a/src/pages/Account/editor.tsx +++ b/src/pages/Account/editor.tsx @@ -1,5 +1,4 @@ import React, { useEffect, useState } from 'react'; -import * as FileSystem from 'expo-file-system'; import styled from 'styled-components/native'; import { useTranslation } from 'react-i18next'; @@ -17,36 +16,12 @@ import useUser from '@hooks/useUser'; import setFireUser from '@services/firebase/set/user'; import setProfileImage from '@services/firebase/set/profileImage'; -import { fileCache } from '@src/services/realm/fileCache'; +import { classifyImage } from '@src/services/chalkSystem'; import realmContext from '@contexts/realm'; import Done from '@src/components/pages/Account/Done'; import Back from '@src/components/common/Back'; - -const classifyImage = async ( - uri: string -): Promise<{ className: string; probability: number }[]> => { - try { - const response = await FileSystem.uploadAsync( - 'https://nsfw-img-detect-node.onrender.com/classify', - uri, - { - fieldName: 'image', - httpMethod: 'POST', - uploadType: FileSystem.FileSystemUploadType.MULTIPART, - } - ); - - return JSON.parse(response.body); - } catch (error) { - console.error('Error classifying image:', error); - Snackbar.show({ - text: 'Error, Failed to classify image', - duration: Snackbar.LENGTH_SHORT, - }); - return []; - } -}; +import { matchingConfig } from '@src/services/localStorage/defaults'; const isNSFW = async (uri: string) => { const api_classes = await classifyImage(uri); @@ -85,6 +60,7 @@ function MyProfile({ navigation }: any) { profilePicture: defaultFirebaseProfilePicture, authenticated: false, gender: '', + matchingConfig: matchingConfig, }); const realm = realmContext.useRealm(); @@ -96,14 +72,15 @@ function MyProfile({ navigation }: any) { useEffect(() => { if (!me) return; - const { profilePicture, name, age, bio, gender } = me; + const { profilePicture, name, age, bio, gender, matchingConfig } = me; setNewMe({ profilePicture, name, bio, age, gender, - }); + matchingConfig, + } as UserType); }, [me]); const [_, pick] = usePicker({ base64: false }); @@ -117,7 +94,6 @@ function MyProfile({ navigation }: any) { const onHandleDone = async () => { if (!me.id || newMe === me) return; - let cached: string | null = null; try { let imgFire = ''; if (me.profilePicture !== newMe.profilePicture) { @@ -128,7 +104,6 @@ function MyProfile({ navigation }: any) { user: { ...newMe, profilePicture: imgFire }, update: true, }); - cached = (await fileCache(imgFire, realm)).path; } else { const { profilePicture, ...userMe } = me; setFireUser({ @@ -139,7 +114,7 @@ function MyProfile({ navigation }: any) { realm.write(() => { const usr = realm.objectForPrimaryKey('User', me.id || ''); if (!usr) return; - if (cached) usr.profilePicture = cached; + usr.profilePicture = newMe.profilePicture; usr.name = newMe.name; usr.age = newMe.age; usr.name = newMe.name; diff --git a/src/pages/Account/index.tsx b/src/pages/Account/index.tsx index e1f7506..c31b585 100644 --- a/src/pages/Account/index.tsx +++ b/src/pages/Account/index.tsx @@ -9,7 +9,6 @@ import SafeArea from '@components/common/SafeArea'; import { useTranslation } from 'react-i18next'; import Snackbar from 'react-native-snackbar'; import auth from '@react-native-firebase/auth'; -import Constants from 'expo-constants'; import RNAdvertisingId from 'react-native-advertising-id'; import useUser from '@src/hooks/useUser'; @@ -37,6 +36,7 @@ import Row from '@src/components/common/Row'; import removeFriendOfList from '@src/services/firebase/del/friendOfList'; import deleteEntireDatabase from '@src/services/realm/delete/deleteEntireDatabase'; import delUser from '@src/services/firebase/del/user'; +import setUser from '@src/services/firebase/set/user'; function Profile({ navigation, route }: any) { const user = useUser(route.params.id); @@ -50,19 +50,38 @@ function Profile({ navigation, route }: any) { const { t: translation, i18n } = useTranslation(); const t = (s: string) => translation(s); - const [appLanguage, setAppLanguage] = useState(''); - const [matchConfig, setMatchConfig] = useMatchConfig(); - const realm = realmContext.useRealm(); + const [appLanguage, setAppLanguage] = useState(''); + const { matchConfig, setMatchConfig } = useMatchConfig(); + const onHandleSetAppLanguage = async (s: string) => { setAppLanguage(s); await localStorage('appLanguage').set(s); i18n.changeLanguage(s); }; + const me = useUser(); + const onHandleSetMatchConfig = async (config: MatchConfigType) => { - setMatchConfig(config); + config = { ...config, ...matchConfig }; + setMatchConfig({ + from: config.from ?? me.matchingConfig.from, + to: config.to ?? me.matchingConfig.to, + genders: config.genders ?? me.matchingConfig.genders, + lang: config.lang ?? me.matchingConfig.lang, + }); + setUser({ + user: { + matchingConfig: { + from: config.from ?? me.matchingConfig.from, + to: config.to ?? me.matchingConfig.to, + genders: config.genders ?? me.matchingConfig.genders, + lang: config.lang ?? me.matchingConfig.lang, + }, + }, + update: true, + }); }; useEffect(() => { @@ -98,7 +117,7 @@ function Profile({ navigation, route }: any) {