From 03f65b72dbc8700ab8fae0179b4e09a1b94b012e Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 14 Nov 2022 11:37:50 -0800 Subject: [PATCH] adding account noification option --- .../ios/Databag.xcodeproj/project.pbxproj | 4 ++++ app/mobile/src/api/setAccountNotifications.js | 7 +++++++ .../src/context/useAccountContext.hook.js | 7 ++++++- .../profile/profileBody/ProfileBody.jsx | 19 +++++++++++++++++++ .../profile/profileBody/ProfileBody.styled.js | 11 +++++++++++ .../profileBody/useProfileBody.hook.js | 8 +++++++- 6 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/mobile/src/api/setAccountNotifications.js diff --git a/app/mobile/ios/Databag.xcodeproj/project.pbxproj b/app/mobile/ios/Databag.xcodeproj/project.pbxproj index bfbadc91..2597c552 100644 --- a/app/mobile/ios/Databag.xcodeproj/project.pbxproj +++ b/app/mobile/ios/Databag.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 821572F657508372661B174B /* Pods_Databag.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B345113B5CABA2FED4B91FCF /* Pods_Databag.framework */; }; B18059E884C0ABDD17F3DC3D /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC715A2D49A985799AEE119 /* ExpoModulesProvider.swift */; }; BB2F792D24A3F905000567C9 /* Expo.plist in Resources */ = {isa = PBXBuildFile; fileRef = BB2F792C24A3F905000567C9 /* Expo.plist */; }; + F0BE99122922C2FB007E89B0 /* avatar.png in Resources */ = {isa = PBXBuildFile; fileRef = F0BE99112922C2FA007E89B0 /* avatar.png */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -37,6 +38,7 @@ B345113B5CABA2FED4B91FCF /* Pods_Databag.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Databag.framework; sourceTree = BUILT_PRODUCTS_DIR; }; BB2F792C24A3F905000567C9 /* Expo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Expo.plist; sourceTree = ""; }; ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; + F0BE99112922C2FA007E89B0 /* avatar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = avatar.png; path = ../images/avatar.png; sourceTree = ""; }; FAC715A2D49A985799AEE119 /* ExpoModulesProvider.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ExpoModulesProvider.swift; path = "Pods/Target Support Files/Pods-Databag/ExpoModulesProvider.swift"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -55,6 +57,7 @@ 13B07FAE1A68108700A75B9A /* Databag */ = { isa = PBXGroup; children = ( + F0BE99112922C2FA007E89B0 /* avatar.png */, 7B2C51FE2918CC6E000E7B18 /* Databag.entitlements */, 7B0AD126291073C4002BB72A /* login.png */, BB2F792B24A3F905000567C9 /* Supporting */, @@ -209,6 +212,7 @@ buildActionMask = 2147483647; files = ( BB2F792D24A3F905000567C9 /* Expo.plist in Resources */, + F0BE99122922C2FB007E89B0 /* avatar.png in Resources */, 7B0AD127291073C4002BB72A /* login.png in Resources */, 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */, 3E461D99554A48A4959DE609 /* SplashScreen.storyboard in Resources */, diff --git a/app/mobile/src/api/setAccountNotifications.js b/app/mobile/src/api/setAccountNotifications.js new file mode 100644 index 00000000..c3410846 --- /dev/null +++ b/app/mobile/src/api/setAccountNotifications.js @@ -0,0 +1,7 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function setAccountNotifications(server, token, flag) { + let res = await fetchWithTimeout(`https://${server}/account/notification?agent=${token}`, { method: 'PUT', body: JSON.stringify(flag) }) + checkResponse(res); +} + diff --git a/app/mobile/src/context/useAccountContext.hook.js b/app/mobile/src/context/useAccountContext.hook.js index 6ccf944c..c4eded9a 100644 --- a/app/mobile/src/context/useAccountContext.hook.js +++ b/app/mobile/src/context/useAccountContext.hook.js @@ -1,6 +1,7 @@ import { useState, useRef, useContext } from 'react'; import { StoreContext } from 'context/StoreContext'; import { setAccountSearchable } from 'api/setAccountSearchable'; +import { setAccountNotifications } from 'api/setAccountNotifications'; import { getAccountStatus } from 'api/getAccountStatus'; import { setAccountLogin } from 'api/setAccountLogin'; @@ -14,7 +15,7 @@ export function useAccountContext() { const curRevision = useRef(null); const setRevision = useRef(null); const syncing = useRef(false); - + const updateState = (value) => { setState((s) => ({ ...s, ...value })) } @@ -61,6 +62,10 @@ export function useAccountContext() { curRevision.current = rev; sync(); }, + setNotifications: async (flag) => { + const { server, appToken } = session.current; + await setAccountNotifications(server, appToken, flag); + }, setSearchable: async (flag) => { const { server, appToken } = session.current; await setAccountSearchable(server, appToken, flag); diff --git a/app/mobile/src/session/profile/profileBody/ProfileBody.jsx b/app/mobile/src/session/profile/profileBody/ProfileBody.jsx index 48739403..0a5b44f4 100644 --- a/app/mobile/src/session/profile/profileBody/ProfileBody.jsx +++ b/app/mobile/src/session/profile/profileBody/ProfileBody.jsx @@ -15,6 +15,19 @@ export function ProfileBody({ navigation }) { const { state, actions } = useProfileBody(); + const setNotifications = async (notify) => { + try { + await actions.setNotifications(notify); + } + catch (err) { + console.log(err); + Alert.alert( + 'Account Update Failed', + 'Please try again.', + ); + } + } + const setVisible = async (visible) => { try { await actions.setVisible(visible); @@ -124,6 +137,12 @@ export function ProfileBody({ navigation }) { + + setNotifications(!state.pushEnabled)} activeOpacity={1}> + Enable Notifications + + + Change Login diff --git a/app/mobile/src/session/profile/profileBody/ProfileBody.styled.js b/app/mobile/src/session/profile/profileBody/ProfileBody.styled.js index 2acb2478..b5693435 100644 --- a/app/mobile/src/session/profile/profileBody/ProfileBody.styled.js +++ b/app/mobile/src/session/profile/profileBody/ProfileBody.styled.js @@ -122,6 +122,17 @@ export const styles = StyleSheet.create({ paddingLeft: 8, color: Colors.grey, }, + notify: { + display: 'flex', + flexDirection: 'row', + justifyContent: 'center', + alignItems: 'center', + paddingTop: 16, + }, + notifyText: { + fontSize: 16, + color: Colors.text, + }, visible: { display: 'flex', flexDirection: 'row', diff --git a/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js b/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js index 148c1c7c..9f8a4565 100644 --- a/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js +++ b/app/mobile/src/session/profile/profileBody/useProfileBody.hook.js @@ -16,6 +16,7 @@ export function useProfileBody() { node: null, imageSource: null, searchable: null, + notifications: null, showDetailEdit: false, showLoginEdit: false, editName: null, @@ -63,7 +64,8 @@ export function useProfileBody() { }, [profile]); useEffect(() => { - updateState({ searchable: account.state.status.searchable }); + const { searchable, pushEnabled } = account.state.status; + updateState({ searchable, pushEnabled }); }, [account]); useEffect(() => { @@ -85,6 +87,10 @@ export function useProfileBody() { updateState({ searchable }); await account.actions.setSearchable(searchable); }, + setNotifications: async (pushEnabled) => { + updateState({ pushEnabled }); + await account.actions.setNotifications(pushEnabled); + }, setProfileImage: async (data) => { await profile.actions.setProfileImage(data); },