From f52f6eaa57250a3eb5cf50dab393c5ec29f4d688 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Mon, 7 Jun 2021 15:57:32 -0400 Subject: Resolve warning --- src/screens/onboarding/BasicInfoOnboarding.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/screens') diff --git a/src/screens/onboarding/BasicInfoOnboarding.tsx b/src/screens/onboarding/BasicInfoOnboarding.tsx index e5e6f59b..d5998ac1 100644 --- a/src/screens/onboarding/BasicInfoOnboarding.tsx +++ b/src/screens/onboarding/BasicInfoOnboarding.tsx @@ -13,7 +13,7 @@ import { TouchableOpacity, } from 'react-native'; import {normalize} from 'react-native-elements'; -import Animated, {Easing, useValue} from 'react-native-reanimated'; +import Animated, {EasingNode, useValue} from 'react-native-reanimated'; import { ArrowButton, Background, @@ -99,7 +99,7 @@ const BasicInfoOnboarding: React.FC = ({route}) => { Animated.timing(fadeButtonValue, { toValue: target, duration: 100, - easing: Easing.linear, + easing: EasingNode.linear, }).start(); }; @@ -108,7 +108,7 @@ const BasicInfoOnboarding: React.FC = ({route}) => { Animated.timing(fadeValue, { toValue: 1, duration: 1000, - easing: Easing.linear, + easing: EasingNode.linear, }).start(); }; fade(); -- cgit v1.2.3-70-g09d2 From d71a4d178715d34570d949e0cd0c1c58186715fc Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Mon, 7 Jun 2021 16:07:56 -0400 Subject: Adjust header height --- .../moments/IndividualMomentTitleBar.tsx | 1 - src/screens/profile/IndividualMoment.tsx | 26 ++++++++-------------- 2 files changed, 9 insertions(+), 18 deletions(-) (limited to 'src/screens') diff --git a/src/components/moments/IndividualMomentTitleBar.tsx b/src/components/moments/IndividualMomentTitleBar.tsx index 79453ade..2bebafa7 100644 --- a/src/components/moments/IndividualMomentTitleBar.tsx +++ b/src/components/moments/IndividualMomentTitleBar.tsx @@ -30,7 +30,6 @@ const styles = StyleSheet.create({ flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-start', - height: '5%', }, headerContainer: { width: '80%', diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index 4ad4515d..f8015720 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -8,7 +8,12 @@ import {IndividualMomentTitleBar, MomentPost} from '../../components'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; import {MomentType} from '../../types'; -import {SCREEN_HEIGHT, SCREEN_WIDTH, StatusBarHeight} from '../../utils'; +import { + normalize, + SCREEN_HEIGHT, + SCREEN_WIDTH, + StatusBarHeight, +} from '../../utils'; /** * Individual moment view opened when user clicks on a moment tile @@ -23,8 +28,6 @@ interface IndividualMomentProps { navigation: IndividualMomentNavigationProp; } -const ITEM_HEIGHT = SCREEN_HEIGHT * 0.9; - const IndividualMoment: React.FC = ({ route, navigation, @@ -50,7 +53,7 @@ const IndividualMoment: React.FC = ({ navigation.pop()} - {...{title: moment_category}} + title={moment_category} /> = ({ renderItem={({item}: {item: MomentType}) => ( )} - keyExtractor={(item, index) => index.toString()} + keyExtractor={(_, index) => index.toString()} showsVerticalScrollIndicator={false} - snapToAlignment={'start'} - snapToInterval={ITEM_HEIGHT} - decelerationRate={'fast'} initialScrollIndex={initialIndex} - getItemLayout={(data, index) => ({ - length: ITEM_HEIGHT, - offset: ITEM_HEIGHT * index, - index, - })} - pagingEnabled /> @@ -87,11 +81,9 @@ const styles = StyleSheet.create({ flex: 9, }, header: { - flex: 1, + height: normalize(70), }, postContainer: { - height: ITEM_HEIGHT, - width: SCREEN_WIDTH, flex: 1, }, postHeader: { -- cgit v1.2.3-70-g09d2 From 960b1516bc06c1fa16ba47a4aab62c94c3b10beb Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Mon, 7 Jun 2021 16:11:09 -0400 Subject: Cleanup code --- src/screens/profile/IndividualMoment.tsx | 42 +++++++++----------------------- 1 file changed, 11 insertions(+), 31 deletions(-) (limited to 'src/screens') diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index f8015720..95428c39 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -2,18 +2,13 @@ import {BlurView} from '@react-native-community/blur'; import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import React from 'react'; -import {FlatList, StyleSheet, View} from 'react-native'; +import {FlatList, StyleSheet} from 'react-native'; import {useSelector} from 'react-redux'; import {IndividualMomentTitleBar, MomentPost} from '../../components'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; import {MomentType} from '../../types'; -import { - normalize, - SCREEN_HEIGHT, - SCREEN_WIDTH, - StatusBarHeight, -} from '../../utils'; +import {normalize, StatusBarHeight} from '../../utils'; /** * Individual moment view opened when user clicks on a moment tile @@ -55,41 +50,26 @@ const IndividualMoment: React.FC = ({ close={() => navigation.pop()} title={moment_category} /> - - ( - - )} - keyExtractor={(_, index) => index.toString()} - showsVerticalScrollIndicator={false} - initialScrollIndex={initialIndex} - /> - + ( + + )} + keyExtractor={(_, index) => index.toString()} + showsVerticalScrollIndicator={false} + initialScrollIndex={initialIndex} + /> ); }; const styles = StyleSheet.create({ contentContainer: { - width: SCREEN_WIDTH, - height: SCREEN_HEIGHT, paddingTop: StatusBarHeight, flex: 1, - paddingBottom: 0, - }, - content: { - flex: 9, }, header: { height: normalize(70), }, - postContainer: { - flex: 1, - }, - postHeader: { - flex: 1, - }, - postContent: {flex: 9}, }); export default IndividualMoment; -- cgit v1.2.3-70-g09d2 From f8dd6b8ef033039278e2931876488c307f5d5adc Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 8 Jun 2021 14:20:57 -0400 Subject: Create MomentPostType --- src/components/moments/MomentPost.tsx | 22 +++++++++++++--------- src/screens/profile/IndividualMoment.tsx | 6 +++--- src/services/MomentService.ts | 21 ++++++--------------- src/store/initialStates.ts | 11 +++++++---- src/types/types.ts | 10 +++++++++- 5 files changed, 38 insertions(+), 32 deletions(-) (limited to 'src/screens') diff --git a/src/components/moments/MomentPost.tsx b/src/components/moments/MomentPost.tsx index 1f363052..4f8bb63f 100644 --- a/src/components/moments/MomentPost.tsx +++ b/src/components/moments/MomentPost.tsx @@ -4,16 +4,20 @@ import {useSelector} from 'react-redux'; import {MomentPostContent, MomentPostHeader} from '.'; import {deleteMomentTag, loadMomentTags} from '../../services'; import {RootState} from '../../store/rootReducer'; -import {MomentTagType, MomentType, ScreenType} from '../../types'; +import {MomentPostType, MomentTagType, ScreenType} from '../../types'; import {SCREEN_HEIGHT} from '../../utils'; interface MomentPostProps { - item: MomentType; + moment: MomentPostType; userXId: string | undefined; screenType: ScreenType; } -const MomentPost: React.FC = ({item, userXId, screenType}) => { +const MomentPost: React.FC = ({ + moment, + userXId, + screenType, +}) => { const {userId: loggedInUserId, username: loggedInUsername} = useSelector( (state: RootState) => state.user.user, ); @@ -29,7 +33,7 @@ const MomentPost: React.FC = ({item, userXId, screenType}) => { const isOwnProfile = username === loggedInUsername; const loadTags = async () => { - const response = await loadMomentTags(item.moment_id); + const response = await loadMomentTags(moment.moment_id); setTags(response ? response : []); }; @@ -74,17 +78,17 @@ const MomentPost: React.FC = ({item, userXId, screenType}) => { userXId={userXId} screenType={screenType} username={isOwnProfile ? loggedInUsername : username} - momentId={item.moment_id} + momentId={moment.moment_id} style={styles.postHeader} momentTagId={momentTagId} removeTag={removeTag} /> diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index 95428c39..ba6a731c 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -7,7 +7,7 @@ import {useSelector} from 'react-redux'; import {IndividualMomentTitleBar, MomentPost} from '../../components'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; -import {MomentType} from '../../types'; +import {MomentPostType, MomentType} from '../../types'; import {normalize, StatusBarHeight} from '../../utils'; /** @@ -52,8 +52,8 @@ const IndividualMoment: React.FC = ({ /> ( - + renderItem={({item}: {item: MomentPostType}) => ( + )} keyExtractor={(_, index) => index.toString()} showsVerticalScrollIndicator={false} diff --git a/src/services/MomentService.ts b/src/services/MomentService.ts index af602dc7..e227bc9e 100644 --- a/src/services/MomentService.ts +++ b/src/services/MomentService.ts @@ -6,7 +6,7 @@ import { MOMENT_TAGS_ENDPOINT, MOMENT_THUMBNAIL_ENDPOINT, } from '../constants'; -import {MomentTagType, MomentType} from '../types'; +import {MomentPostType, MomentTagType, MomentType} from '../types'; import {checkImageUploadStatus} from '../utils'; export const postMoment = async ( @@ -54,11 +54,7 @@ export const postMoment = async ( return undefined; }; -export const loadMoments: ( - userId: string, - token: string, -) => Promise = async (userId, token) => { - let moments: MomentType[] = []; +export const loadMoments = async (userId: string, token: string) => { try { const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, { method: 'GET', @@ -66,19 +62,14 @@ export const loadMoments: ( Authorization: 'Token ' + token, }, }); - const status = response.status; - if (status === 200) { - const data = await response.json(); - moments = data; - } else { - console.log('Could not load moments!'); - return []; + if (response.status === 200) { + const typedData: MomentPostType[] = await response.json(); + return typedData; } } catch (err) { console.log(err); - return []; } - return moments; + return []; }; export const deleteMoment = async (momentId: string) => { diff --git a/src/store/initialStates.ts b/src/store/initialStates.ts index e2902a2d..92a1e456 100644 --- a/src/store/initialStates.ts +++ b/src/store/initialStates.ts @@ -1,14 +1,17 @@ -import {CommentThreadType, UniversityType} from './../types/types'; import { - MomentType, NotificationType, - ProfilePreviewType, ProfileInfoType, + ProfilePreviewType, ScreenType, SocialAccountType, UserType, UserXType, } from '../types'; +import { + CommentThreadType, + MomentPostType, + UniversityType, +} from './../types/types'; export const NO_PROFILE: ProfileInfoType = { biography: '', @@ -29,7 +32,7 @@ export const NO_PROFILE: ProfileInfoType = { is_private: true, }; -export const EMPTY_MOMENTS_LIST = []; +export const EMPTY_MOMENTS_LIST = []; export const EMPTY_NOTIFICATIONS_LIST = []; diff --git a/src/types/types.ts b/src/types/types.ts index e54c2201..1e50eba8 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -119,6 +119,14 @@ export interface MomentType { thumbnail_url: string; } +export interface MomentPostType extends MomentType { + comments_count: number; + comment_preview: { + user: ProfilePreviewType; + comment: string; + }; +} + export interface MomentTagType { id: string; user: ProfilePreviewType; @@ -172,7 +180,7 @@ export enum ScreenType { */ export interface UserXType { friends: ProfilePreviewType[]; - moments: MomentType[]; + moments: MomentPostType[]; socialAccounts: Record; momentCategories: string[]; user: UserType; -- cgit v1.2.3-70-g09d2 From 7de499af625b28074e86854b997e66257ffab8c8 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 8 Jun 2021 16:18:30 -0400 Subject: Adjust styling --- src/components/comments/AddComment.tsx | 34 ++++++++++++++++++++-------- src/components/moments/MomentPostContent.tsx | 30 ++++++++++++++++-------- src/screens/profile/IndividualMoment.tsx | 4 ++-- src/services/MomentService.ts | 2 +- src/types/types.ts | 2 +- 5 files changed, 48 insertions(+), 24 deletions(-) (limited to 'src/screens') diff --git a/src/components/comments/AddComment.tsx b/src/components/comments/AddComment.tsx index b229d010..24ff830a 100644 --- a/src/components/comments/AddComment.tsx +++ b/src/components/comments/AddComment.tsx @@ -24,10 +24,15 @@ import {MentionInputControlled} from './MentionInputControlled'; export interface AddCommentProps { momentId: string; placeholderText: string; + theme?: 'dark' | 'white'; } -const AddComment: React.FC = ({momentId, placeholderText}) => { - const {setShouldUpdateAllComments, commentTapped} = +const AddComment: React.FC = ({ + momentId, + placeholderText, + theme = 'white', +}) => { + const {setShouldUpdateAllComments = () => null, commentTapped} = useContext(CommentContext); const [inReplyToMention, setInReplyToMention] = useState(''); const [comment, setComment] = useState(''); @@ -106,13 +111,14 @@ const AddComment: React.FC = ({momentId, placeholderText}) => { keyboardVerticalOffset={SCREEN_HEIGHT * 0.1}> { @@ -124,11 +130,15 @@ const AddComment: React.FC = ({momentId, placeholderText}) => { inputRef={ref} partTypes={mentionPartTypes('blue')} /> - - - - - + {(theme === 'white' || (theme === 'dark' && keyboardVisible)) && ( + + + + + + )} @@ -136,7 +146,11 @@ const AddComment: React.FC = ({momentId, placeholderText}) => { }; const styles = StyleSheet.create({ - container: { + containerDark: { + alignItems: 'center', + width: SCREEN_WIDTH, + }, + containerWhite: { backgroundColor: '#f7f7f7', alignItems: 'center', width: SCREEN_WIDTH, diff --git a/src/components/moments/MomentPostContent.tsx b/src/components/moments/MomentPostContent.tsx index c59a9c39..e76a8ceb 100644 --- a/src/components/moments/MomentPostContent.tsx +++ b/src/components/moments/MomentPostContent.tsx @@ -4,6 +4,7 @@ import {Image, StyleSheet, Text, View, ViewProps} from 'react-native'; import {TouchableWithoutFeedback} from 'react-native-gesture-handler'; import Animated, {EasingNode} from 'react-native-reanimated'; import {useDispatch, useStore} from 'react-redux'; +import {ADD_COMMENT_TEXT} from '../../constants/strings'; import {RootState} from '../../store/rootReducer'; import {MomentPostType, MomentTagType, ScreenType, UserType} from '../../types'; import { @@ -13,7 +14,7 @@ import { SCREEN_WIDTH, } from '../../utils'; import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments'; -import {CommentsCount} from '../comments'; +import {AddComment, CommentsCount} from '../comments'; import {MomentTags} from '../common'; interface MomentPostContentProps extends ViewProps { @@ -38,7 +39,6 @@ const MomentPostContent: React.FC = ({ const [fadeValue, setFadeValue] = useState>( new Animated.Value(0), ); - useEffect(() => { setTags(momentTags); }, [momentTags]); @@ -96,19 +96,27 @@ const MomentPostContent: React.FC = ({ /> {elapsedTime} - {renderTextWithMentions({ - value: moment.caption, - styles: styles.captionText, - partTypes: mentionPartTypes('white'), - onPress: (user: UserType) => - navigateToProfile(state, dispatch, navigation, screenType, user), - })} + {moment.caption !== '' && + renderTextWithMentions({ + value: moment.caption, + styles: styles.captionText, + partTypes: mentionPartTypes('white'), + onPress: (user: UserType) => + navigateToProfile(state, dispatch, navigation, screenType, user), + })} + ); }; const styles = StyleSheet.create({ - container: {}, + container: { + borderWidth: 1, + }, image: { width: SCREEN_WIDTH, aspectRatio: 1, @@ -117,6 +125,7 @@ const styles = StyleSheet.create({ footerContainer: { flexDirection: 'row', justifyContent: 'space-between', + borderWidth: 1, marginLeft: '7%', marginRight: '5%', marginBottom: '2%', @@ -140,6 +149,7 @@ const styles = StyleSheet.create({ fontSize: normalize(13), lineHeight: normalize(15.51), letterSpacing: normalize(0.6), + borderWidth: 1, }, tapTag: { position: 'absolute', diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index ba6a731c..5ac8aeab 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -7,7 +7,7 @@ import {useSelector} from 'react-redux'; import {IndividualMomentTitleBar, MomentPost} from '../../components'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; -import {MomentPostType, MomentType} from '../../types'; +import {MomentPostType} from '../../types'; import {normalize, StatusBarHeight} from '../../utils'; /** @@ -47,7 +47,7 @@ const IndividualMoment: React.FC = ({ style={styles.contentContainer}> navigation.pop()} + close={() => navigation.goBack()} title={moment_category} /> Date: Tue, 8 Jun 2021 17:17:11 -0400 Subject: Remove CommentCount, Add new comments component --- src/components/comments/CommentsCount.tsx | 58 ------------------------- src/components/comments/index.ts | 1 - src/components/moments/MomentPostContent.tsx | 63 +++++++++++++++------------- src/screens/profile/IndividualMoment.tsx | 2 +- 4 files changed, 36 insertions(+), 88 deletions(-) delete mode 100644 src/components/comments/CommentsCount.tsx (limited to 'src/screens') diff --git a/src/components/comments/CommentsCount.tsx b/src/components/comments/CommentsCount.tsx deleted file mode 100644 index f4f8197d..00000000 --- a/src/components/comments/CommentsCount.tsx +++ /dev/null @@ -1,58 +0,0 @@ -import {useNavigation} from '@react-navigation/native'; -import * as React from 'react'; -import {StyleSheet, TouchableOpacity} from 'react-native'; -import {Text} from 'react-native-animatable'; -import CommentIcon from '../../assets/icons/moment-comment-icon.svg'; -import {ScreenType} from '../../types'; - -/** - * Provides a view for the comment icon and the comment count. - * When the user clicks on this view, a new screen opens to display all the comments. - */ - -type CommentsCountProps = { - commentsCount: string; - momentId: string; - screenType: ScreenType; -}; - -const CommentsCount: React.FC = ({ - commentsCount, - momentId, - screenType, -}) => { - const navigation = useNavigation(); - const navigateToCommentsScreen = async () => { - navigation.push('MomentCommentsScreen', { - moment_id: momentId, - screenType, - }); - }; - return ( - <> - - - - {commentsCount !== '0' ? commentsCount : ''} - - - - ); -}; - -const styles = StyleSheet.create({ - image: { - position: 'relative', - width: 21, - height: 21, - }, - count: { - position: 'relative', - fontWeight: 'bold', - color: 'white', - paddingTop: '3%', - textAlign: 'center', - }, -}); - -export default CommentsCount; diff --git a/src/components/comments/index.ts b/src/components/comments/index.ts index 6293f799..ebd93844 100644 --- a/src/components/comments/index.ts +++ b/src/components/comments/index.ts @@ -1,3 +1,2 @@ -export {default as CommentsCount} from '../comments/CommentsCount'; export {default as CommentTile} from './CommentTile'; export {default as AddComment} from './AddComment'; diff --git a/src/components/moments/MomentPostContent.tsx b/src/components/moments/MomentPostContent.tsx index 582cba25..34503bf4 100644 --- a/src/components/moments/MomentPostContent.tsx +++ b/src/components/moments/MomentPostContent.tsx @@ -1,10 +1,12 @@ import {useNavigation} from '@react-navigation/native'; import React, {useEffect, useRef, useState} from 'react'; import {Image, StyleSheet, Text, View, ViewProps} from 'react-native'; -import {TouchableWithoutFeedback} from 'react-native-gesture-handler'; +import { + TouchableOpacity, + TouchableWithoutFeedback, +} from 'react-native-gesture-handler'; import Animated, {EasingNode} from 'react-native-reanimated'; import {useDispatch, useStore} from 'react-redux'; -import {ADD_COMMENT_TEXT} from '../../constants/strings'; import {RootState} from '../../store/rootReducer'; import {MomentPostType, MomentTagType, ScreenType, UserType} from '../../types'; import { @@ -14,7 +16,7 @@ import { SCREEN_WIDTH, } from '../../utils'; import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments'; -import {AddComment, CommentsCount} from '../comments'; +import {AddComment} from '../comments'; import {MomentTags} from '../common'; interface MomentPostContentProps extends ViewProps { @@ -31,22 +33,21 @@ const MomentPostContent: React.FC = ({ const state: RootState = useStore().getState(); const navigation = useNavigation(); const dispatch = useDispatch(); - const [elapsedTime, setElapsedTime] = useState(''); const [tags, setTags] = useState(momentTags); const imageRef = useRef(null); const [visible, setVisible] = useState(false); - const [fadeValue, setFadeValue] = useState>( new Animated.Value(0), ); + const commentCountText = + moment.comments_count === 0 + ? 'No Comments' + : moment.comments_count + ' comments'; + useEffect(() => { setTags(momentTags); }, [momentTags]); - useEffect(() => { - setElapsedTime(getTimePosted(moment.date_created)); - }, [moment.date_created]); - useEffect(() => { const fade = async () => { Animated.timing(fadeValue, { @@ -96,19 +97,22 @@ const MomentPostContent: React.FC = ({ onPress: (user: UserType) => navigateToProfile(state, dispatch, navigation, screenType, user), })} - - - {elapsedTime} - + + navigation.push('MomentCommentsScreen', { + moment_id: moment.moment_id, + screenType, + }) + }> + {commentCountText} + + {getTimePosted(moment.date_created)} ); }; @@ -122,22 +126,24 @@ const styles = StyleSheet.create({ aspectRatio: 1, marginBottom: '3%', }, - footerContainer: { + commentsPreviewContainer: { flexDirection: 'row', justifyContent: 'space-between', - borderWidth: 1, - marginLeft: '7%', - marginRight: '5%', + marginHorizontal: '5%', marginBottom: '2%', + borderWidth: 1, + }, + commentCount: { + fontWeight: '700', + color: 'white', + fontSize: normalize(12), }, text: { - position: 'relative', - paddingBottom: '1%', - paddingTop: '1%', - marginLeft: '7%', - marginRight: '2%', - color: '#ffffff', - fontWeight: 'bold', + marginHorizontal: '5%', + color: 'white', + fontWeight: '500', + textAlign: 'right', + marginTop: 5, }, captionText: { position: 'relative', @@ -150,6 +156,7 @@ const styles = StyleSheet.create({ lineHeight: normalize(15.51), letterSpacing: normalize(0.6), borderWidth: 1, + marginBottom: normalize(18), }, tapTag: { position: 'absolute', diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index 5ac8aeab..447ba2a9 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -55,7 +55,7 @@ const IndividualMoment: React.FC = ({ renderItem={({item}: {item: MomentPostType}) => ( )} - keyExtractor={(_, index) => index.toString()} + keyExtractor={(item, _) => item.moment_id} showsVerticalScrollIndicator={false} initialScrollIndex={initialIndex} /> -- cgit v1.2.3-70-g09d2 From 2f9ecfb9bc89c7c6e7ac09952b97d3f3813075bc Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Thu, 10 Jun 2021 19:20:19 -0400 Subject: Add logic for keyboard focus --- src/App.tsx | 19 ++-- src/components/comments/AddComment.tsx | 5 +- .../moments/IndividualMomentTitleBar.tsx | 11 ++- src/components/moments/MomentPost.tsx | 3 + src/components/moments/MomentPostContent.tsx | 52 +++++++---- src/screens/profile/IndividualMoment.tsx | 102 +++++++++++++++------ src/screens/profile/MomentCommentsScreen.tsx | 5 +- 7 files changed, 135 insertions(+), 62 deletions(-) (limited to 'src/screens') diff --git a/src/App.tsx b/src/App.tsx index 92e7abee..64f40bae 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -27,16 +27,15 @@ export const ChatContext = React.createContext({} as ChatContextType); const App = () => { const routeNameRef = useRef(); const [channel, setChannel] = useState(); - const chatClient = - StreamChat.getInstance< - LocalAttachmentType, - LocalChannelType, - LocalCommandType, - LocalEventType, - LocalMessageType, - LocalResponseType, - LocalUserType - >(STREAM_CHAT_API); + const chatClient = StreamChat.getInstance< + LocalAttachmentType, + LocalChannelType, + LocalCommandType, + LocalEventType, + LocalMessageType, + LocalResponseType, + LocalUserType + >(STREAM_CHAT_API); return ( void; + onFocus?: () => void; theme?: 'dark' | 'white'; } const AddComment: React.FC = ({ momentId, placeholderText, - callback = (msg) => null, + callback = (_) => null, + onFocus = () => null, theme = 'white', }) => { const {setShouldUpdateAllComments = () => null, commentTapped} = @@ -125,6 +127,7 @@ const AddComment: React.FC = ({ placeholderTextColor={theme === 'dark' ? '#828282' : undefined} placeholder={placeholderText} value={inReplyToMention + comment} + onFocus={onFocus} onChange={(newText: string) => { // skipping the `inReplyToMention` text setComment( diff --git a/src/components/moments/IndividualMomentTitleBar.tsx b/src/components/moments/IndividualMomentTitleBar.tsx index 2bebafa7..4ae9471f 100644 --- a/src/components/moments/IndividualMomentTitleBar.tsx +++ b/src/components/moments/IndividualMomentTitleBar.tsx @@ -1,8 +1,13 @@ import React from 'react'; -import {TouchableOpacity} from 'react-native'; -import {Text, View, StyleSheet, ViewProps} from 'react-native'; -import {normalize} from '../../utils'; +import { + StyleSheet, + Text, + TouchableOpacity, + View, + ViewProps, +} from 'react-native'; import CloseIcon from '../../assets/ionicons/close-outline.svg'; +import {normalize} from '../../utils'; interface IndividualMomentTitleBarProps extends ViewProps { title: string; diff --git a/src/components/moments/MomentPost.tsx b/src/components/moments/MomentPost.tsx index 02dbffa3..7a588325 100644 --- a/src/components/moments/MomentPost.tsx +++ b/src/components/moments/MomentPost.tsx @@ -11,12 +11,14 @@ interface MomentPostProps { moment: MomentPostType; userXId: string | undefined; screenType: ScreenType; + index: number; } const MomentPost: React.FC = ({ moment, userXId, screenType, + index, }) => { const {userId: loggedInUserId, username: loggedInUsername} = useSelector( (state: RootState) => state.user.user, @@ -88,6 +90,7 @@ const MomentPost: React.FC = ({ moment={moment} screenType={screenType} momentTags={tags} + index={index} /> ); diff --git a/src/components/moments/MomentPostContent.tsx b/src/components/moments/MomentPostContent.tsx index 5192fdf0..eb89fd03 100644 --- a/src/components/moments/MomentPostContent.tsx +++ b/src/components/moments/MomentPostContent.tsx @@ -1,9 +1,10 @@ import {useNavigation} from '@react-navigation/native'; -import React, {useEffect, useRef, useState} from 'react'; +import React, {useContext, useEffect, useRef, useState} from 'react'; import {Image, StyleSheet, Text, View, ViewProps} from 'react-native'; import {TouchableWithoutFeedback} from 'react-native-gesture-handler'; import Animated, {EasingNode} from 'react-native-reanimated'; import {useDispatch, useStore} from 'react-redux'; +import {MomentContext} from '../../screens/profile/IndividualMoment'; import {RootState} from '../../store/rootReducer'; import { MomentCommentPreviewType, @@ -13,11 +14,11 @@ import { UserType, } from '../../types'; import { + getLoggedInUserAsProfilePreview, getTimePosted, navigateToProfile, normalize, SCREEN_WIDTH, - getLoggedInUserAsProfilePreview, } from '../../utils'; import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments'; import {AddComment} from '../comments'; @@ -28,12 +29,14 @@ interface MomentPostContentProps extends ViewProps { screenType: ScreenType; moment: MomentPostType; momentTags: MomentTagType[]; + index: number; } const MomentPostContent: React.FC = ({ screenType, moment, style, momentTags, + index, }) => { const state: RootState = useStore().getState(); const navigation = useNavigation(); @@ -46,6 +49,7 @@ const MomentPostContent: React.FC = ({ ); const [commentPreview, setCommentPreview] = useState(moment.comment_preview); + const {keyboardVisible, setScrollToTargetIndex} = useContext(MomentContext); useEffect(() => { setTags(momentTags); @@ -92,20 +96,30 @@ const MomentPostContent: React.FC = ({ /> )} - {moment.caption !== '' && - renderTextWithMentions({ - value: moment.caption, - styles: styles.captionText, - partTypes: mentionPartTypes('momentCaption'), - onPress: (user: UserType) => - navigateToProfile(state, dispatch, navigation, screenType, user), - })} - + {!keyboardVisible && ( + <> + {moment.caption !== '' && + renderTextWithMentions({ + value: moment.caption, + styles: styles.captionText, + partTypes: mentionPartTypes('momentCaption'), + onPress: (user: UserType) => + navigateToProfile( + state, + dispatch, + navigation, + screenType, + user, + ), + })} + + + )} = ({ comment: message, }) } + onFocus={() => setScrollToTargetIndex(index)} theme={'dark'} /> {getTimePosted(moment.date_created)} @@ -123,9 +138,7 @@ const MomentPostContent: React.FC = ({ }; const styles = StyleSheet.create({ - container: { - borderWidth: 1, - }, + container: {}, image: { width: SCREEN_WIDTH, aspectRatio: 1, @@ -147,7 +160,6 @@ const styles = StyleSheet.create({ lineHeight: normalize(15.51), letterSpacing: normalize(0.6), marginBottom: normalize(18), - borderWidth: 1, }, tapTag: { position: 'absolute', diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index 447ba2a9..a91f1913 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -1,10 +1,11 @@ import {BlurView} from '@react-native-community/blur'; import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; -import React from 'react'; -import {FlatList, StyleSheet} from 'react-native'; +import React, {useEffect, useRef, useState} from 'react'; +import {FlatList, Keyboard, StyleSheet} from 'react-native'; import {useSelector} from 'react-redux'; import {IndividualMomentTitleBar, MomentPost} from '../../components'; +import {AVATAR_DIM} from '../../constants'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; import {MomentPostType} from '../../types'; @@ -13,11 +14,21 @@ import {normalize, StatusBarHeight} from '../../utils'; /** * Individual moment view opened when user clicks on a moment tile */ + +type MomentContextType = { + keyboardVisible: boolean; + setScrollToTargetIndex: (index: number) => void; +}; + +export const MomentContext = React.createContext({} as MomentContextType); + type IndividualMomentRouteProp = RouteProp; + type IndividualMomentNavigationProp = StackNavigationProp< MainStackParams, 'IndividualMoment' >; + interface IndividualMomentProps { route: IndividualMomentRouteProp; navigation: IndividualMomentNavigationProp; @@ -27,39 +38,78 @@ const IndividualMoment: React.FC = ({ route, navigation, }) => { - const {moment_category, moment_id} = route.params.moment; - const {userXId, screenType} = route.params; - + const { + userXId, + screenType, + moment: {moment_category, moment_id}, + } = route.params; const {moments} = useSelector((state: RootState) => userXId ? state.userX[screenType][userXId] : state.moments, ); - + const scrollRef = useRef>(null); const momentData = moments.filter( (m) => m.moment_category === moment_category, ); const initialIndex = momentData.findIndex((m) => m.moment_id === moment_id); + const [scrollToTargetIndex, setScrollToTargetIndex] = + useState(initialIndex); + const [keyboardVisible, setKeyboardVisible] = React.useState(false); + + useEffect(() => { + const showKeyboard = () => setKeyboardVisible(true); + Keyboard.addListener('keyboardWillShow', showKeyboard); + return () => Keyboard.removeListener('keyboardWillShow', showKeyboard); + }, []); + + useEffect(() => { + const hideKeyboard = () => setKeyboardVisible(false); + Keyboard.addListener('keyboardWillHide', hideKeyboard); + return () => Keyboard.removeListener('keyboardWillHide', hideKeyboard); + }, []); + + useEffect(() => { + if (keyboardVisible) { + scrollRef.current?.scrollToIndex({ + index: scrollToTargetIndex, + // viewOffset: -(AVATAR_DIM + normalize(120)), + viewOffset: -(AVATAR_DIM + normalize(90)), + }); + } + }, [scrollToTargetIndex, keyboardVisible]); return ( - - navigation.goBack()} - title={moment_category} - /> - ( - - )} - keyExtractor={(item, _) => item.moment_id} - showsVerticalScrollIndicator={false} - initialScrollIndex={initialIndex} - /> - + + + navigation.goBack()} + title={moment_category} + /> + ( + + )} + keyExtractor={(item, _) => item.moment_id} + showsVerticalScrollIndicator={false} + initialScrollIndex={initialIndex} + /> + + ); }; const styles = StyleSheet.create({ diff --git a/src/screens/profile/MomentCommentsScreen.tsx b/src/screens/profile/MomentCommentsScreen.tsx index 402e5f44..7dfe8ae9 100644 --- a/src/screens/profile/MomentCommentsScreen.tsx +++ b/src/screens/profile/MomentCommentsScreen.tsx @@ -48,8 +48,9 @@ const MomentCommentsScreen: React.FC = ({route}) => { React.useState(true); //Keeps track of the current comments object in focus so that the application knows which comment to post a reply to - const [commentTapped, setCommentTapped] = - useState(); + const [commentTapped, setCommentTapped] = useState< + CommentType | CommentThreadType | undefined + >(); useEffect(() => { navigation.setOptions({ -- cgit v1.2.3-70-g09d2 From eb672872d85f203085c96005758314d5dba359f2 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Fri, 11 Jun 2021 15:30:56 -0400 Subject: Simplify scroll logic, Cleanup code --- src/components/moments/MomentPostContent.tsx | 7 +++--- src/screens/profile/IndividualMoment.tsx | 33 +++++++++++++++------------- 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'src/screens') diff --git a/src/components/moments/MomentPostContent.tsx b/src/components/moments/MomentPostContent.tsx index eb89fd03..da04211f 100644 --- a/src/components/moments/MomentPostContent.tsx +++ b/src/components/moments/MomentPostContent.tsx @@ -49,7 +49,8 @@ const MomentPostContent: React.FC = ({ ); const [commentPreview, setCommentPreview] = useState(moment.comment_preview); - const {keyboardVisible, setScrollToTargetIndex} = useContext(MomentContext); + const {keyboardVisible, currentScrollToIndex, scrollTo} = + useContext(MomentContext); useEffect(() => { setTags(momentTags); @@ -96,7 +97,7 @@ const MomentPostContent: React.FC = ({ /> )} - {!keyboardVisible && ( + {(!keyboardVisible || currentScrollToIndex !== index) && ( <> {moment.caption !== '' && renderTextWithMentions({ @@ -129,7 +130,7 @@ const MomentPostContent: React.FC = ({ comment: message, }) } - onFocus={() => setScrollToTargetIndex(index)} + onFocus={() => scrollTo(index)} theme={'dark'} /> {getTimePosted(moment.date_created)} diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index a91f1913..ddf4c478 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -17,7 +17,8 @@ import {normalize, StatusBarHeight} from '../../utils'; type MomentContextType = { keyboardVisible: boolean; - setScrollToTargetIndex: (index: number) => void; + currentScrollToIndex: number; + scrollTo: (index: number) => void; }; export const MomentContext = React.createContext({} as MomentContextType); @@ -51,37 +52,39 @@ const IndividualMoment: React.FC = ({ (m) => m.moment_category === moment_category, ); const initialIndex = momentData.findIndex((m) => m.moment_id === moment_id); - const [scrollToTargetIndex, setScrollToTargetIndex] = + const [currentScrollToIndex, setCurrentScrollToIndex] = useState(initialIndex); - const [keyboardVisible, setKeyboardVisible] = React.useState(false); + const [keyboardVisible, setKeyboardVisible] = useState(false); useEffect(() => { const showKeyboard = () => setKeyboardVisible(true); - Keyboard.addListener('keyboardWillShow', showKeyboard); - return () => Keyboard.removeListener('keyboardWillShow', showKeyboard); - }, []); - - useEffect(() => { const hideKeyboard = () => setKeyboardVisible(false); + Keyboard.addListener('keyboardWillShow', showKeyboard); Keyboard.addListener('keyboardWillHide', hideKeyboard); - return () => Keyboard.removeListener('keyboardWillHide', hideKeyboard); + return () => { + Keyboard.removeListener('keyboardWillShow', showKeyboard); + Keyboard.removeListener('keyboardWillHide', hideKeyboard); + }; }, []); - useEffect(() => { - if (keyboardVisible) { + const scrollTo = (index: number) => { + setCurrentScrollToIndex(index); + setTimeout(() => { + console.log('scrolling to', index); scrollRef.current?.scrollToIndex({ - index: scrollToTargetIndex, + index: index, // viewOffset: -(AVATAR_DIM + normalize(120)), viewOffset: -(AVATAR_DIM + normalize(90)), }); - } - }, [scrollToTargetIndex, keyboardVisible]); + }, 100); + }; return ( Date: Fri, 11 Jun 2021 15:41:50 -0400 Subject: Cleanup scrolling logic, Fix scrollTo logic --- src/components/comments/AddComment.tsx | 85 +++++++++++++++------------- src/components/moments/MomentPostContent.tsx | 18 ++++-- src/screens/profile/IndividualMoment.tsx | 17 ++---- 3 files changed, 64 insertions(+), 56 deletions(-) (limited to 'src/screens') diff --git a/src/components/comments/AddComment.tsx b/src/components/comments/AddComment.tsx index 9d9824db..9667046c 100644 --- a/src/components/comments/AddComment.tsx +++ b/src/components/comments/AddComment.tsx @@ -26,6 +26,7 @@ export interface AddCommentProps { placeholderText: string; callback?: (message: string) => void; onFocus?: () => void; + isKeyboardAvoiding?: boolean; theme?: 'dark' | 'white'; } @@ -34,6 +35,7 @@ const AddComment: React.FC = ({ placeholderText, callback = (_) => null, onFocus = () => null, + isKeyboardAvoiding = true, theme = 'white', }) => { const {setShouldUpdateAllComments = () => null, commentTapped} = @@ -111,49 +113,54 @@ const AddComment: React.FC = ({ } }, [isReplyingToComment, isReplyingToReply, commentTapped]); - return ( + const mainContent = () => ( + + + + { + // skipping the `inReplyToMention` text + setComment( + newText.substring(inReplyToMention.length, newText.length), + ); + }} + inputRef={ref} + partTypes={mentionPartTypes('blue')} + /> + {(theme === 'white' || (theme === 'dark' && keyboardVisible)) && ( + + + + + + )} + + + ); + return isKeyboardAvoiding ? ( - - - - { - // skipping the `inReplyToMention` text - setComment( - newText.substring(inReplyToMention.length, newText.length), - ); - }} - inputRef={ref} - partTypes={mentionPartTypes('blue')} - /> - {(theme === 'white' || (theme === 'dark' && keyboardVisible)) && ( - - - - - - )} - - + {mainContent()} + ) : ( + mainContent() ); }; diff --git a/src/components/moments/MomentPostContent.tsx b/src/components/moments/MomentPostContent.tsx index da04211f..89f2a281 100644 --- a/src/components/moments/MomentPostContent.tsx +++ b/src/components/moments/MomentPostContent.tsx @@ -49,8 +49,8 @@ const MomentPostContent: React.FC = ({ ); const [commentPreview, setCommentPreview] = useState(moment.comment_preview); - const {keyboardVisible, currentScrollToIndex, scrollTo} = - useContext(MomentContext); + const {keyboardVisible, scrollTo} = useContext(MomentContext); + const [hideText, setHideText] = useState(false); useEffect(() => { setTags(momentTags); @@ -67,6 +67,12 @@ const MomentPostContent: React.FC = ({ fade(); }, [fadeValue]); + useEffect(() => { + if (!keyboardVisible && hideText) { + setHideText(false); + } + }, [keyboardVisible, hideText]); + return ( = ({ /> )} - {(!keyboardVisible || currentScrollToIndex !== index) && ( + {!hideText && ( <> {moment.caption !== '' && renderTextWithMentions({ @@ -130,7 +136,11 @@ const MomentPostContent: React.FC = ({ comment: message, }) } - onFocus={() => scrollTo(index)} + onFocus={() => { + setHideText(true); + scrollTo(index); + }} + isKeyboardAvoiding={false} theme={'dark'} /> {getTimePosted(moment.date_created)} diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index ddf4c478..b14b31fa 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -17,7 +17,6 @@ import {normalize, StatusBarHeight} from '../../utils'; type MomentContextType = { keyboardVisible: boolean; - currentScrollToIndex: number; scrollTo: (index: number) => void; }; @@ -52,8 +51,6 @@ const IndividualMoment: React.FC = ({ (m) => m.moment_category === moment_category, ); const initialIndex = momentData.findIndex((m) => m.moment_id === moment_id); - const [currentScrollToIndex, setCurrentScrollToIndex] = - useState(initialIndex); const [keyboardVisible, setKeyboardVisible] = useState(false); useEffect(() => { @@ -68,22 +65,16 @@ const IndividualMoment: React.FC = ({ }, []); const scrollTo = (index: number) => { - setCurrentScrollToIndex(index); - setTimeout(() => { - console.log('scrolling to', index); - scrollRef.current?.scrollToIndex({ - index: index, - // viewOffset: -(AVATAR_DIM + normalize(120)), - viewOffset: -(AVATAR_DIM + normalize(90)), - }); - }, 100); + scrollRef.current?.scrollToIndex({ + index: index, + viewOffset: -(AVATAR_DIM + normalize(90)), + }); }; return ( Date: Fri, 11 Jun 2021 16:35:31 -0400 Subject: Fix viewoffset --- src/screens/profile/IndividualMoment.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/screens') diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index b14b31fa..80438bcd 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -9,7 +9,7 @@ import {AVATAR_DIM} from '../../constants'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; import {MomentPostType} from '../../types'; -import {normalize, StatusBarHeight} from '../../utils'; +import {isIPhoneX, normalize, StatusBarHeight} from '../../utils'; /** * Individual moment view opened when user clicks on a moment tile @@ -65,9 +65,11 @@ const IndividualMoment: React.FC = ({ }, []); const scrollTo = (index: number) => { + // TODO: make this dynamic + const offset = isIPhoneX() ? -(AVATAR_DIM + 100) : -(AVATAR_DIM + 160); scrollRef.current?.scrollToIndex({ index: index, - viewOffset: -(AVATAR_DIM + normalize(90)), + viewOffset: offset, }); }; -- cgit v1.2.3-70-g09d2 From 24f79b9cb3456b3901155ed2e4c8fc66710b97b2 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Fri, 11 Jun 2021 18:09:57 -0400 Subject: Fix keyboard avoiding for last item, Add comment for scroll to index fail --- src/screens/profile/IndividualMoment.tsx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/screens') diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index 80438bcd..f8113aba 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -9,7 +9,12 @@ import {AVATAR_DIM} from '../../constants'; import {MainStackParams} from '../../routes'; import {RootState} from '../../store/rootreducer'; import {MomentPostType} from '../../types'; -import {isIPhoneX, normalize, StatusBarHeight} from '../../utils'; +import { + isIPhoneX, + normalize, + SCREEN_HEIGHT, + StatusBarHeight, +} from '../../utils'; /** * Individual moment view opened when user clicks on a moment tile @@ -92,6 +97,7 @@ const IndividualMoment: React.FC = ({ ( = ({ keyExtractor={(item, _) => item.moment_id} showsVerticalScrollIndicator={false} initialScrollIndex={initialIndex} + onScrollToIndexFailed={() => { + // TODO: code below does not work, index resets to 0 + // const wait = new Promise((resolve) => setTimeout(resolve, 500)); + // wait.then(() => { + // console.log('scrolling to ', initialIndex); + // scrollRef.current?.scrollToIndex({index: initialIndex}); + // }); + }} /> @@ -116,6 +130,9 @@ const styles = StyleSheet.create({ header: { height: normalize(70), }, + listContentContainer: { + paddingBottom: SCREEN_HEIGHT * 0.2, + }, }); export default IndividualMoment; -- cgit v1.2.3-70-g09d2