Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
fix: DrawerLayout warnings (#430)
Browse files Browse the repository at this point in the history
* fix: use FlatList in NoteSideMenu

* fix: use FlatList in MainSideMenu
  • Loading branch information
arielsvg committed May 5, 2021
1 parent fb58bfc commit 7565a36
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 71 deletions.
25 changes: 18 additions & 7 deletions src/screens/SideMenu/MainSideMenu.styled.ts
@@ -1,5 +1,6 @@
import { Platform, SafeAreaView, ScrollView, StatusBar } from 'react-native';
import styled, { css } from 'styled-components/native';
import { useMemo } from 'react';
import { Platform, SafeAreaView, StatusBar, StyleSheet } from 'react-native';
import styled, { css, DefaultTheme } from 'styled-components/native';

// We want top color to be different from bottom color of safe area.
// See https://stackoverflow.com/questions/47725607/react-native-safeareaview-background-color-how-to-assign-two-different-backgro
Expand All @@ -16,8 +17,18 @@ export const MainSafeAreaView = styled(SafeAreaView)`
background-color: ${({ theme }) => theme.stylekitBackgroundColor};
color: ${({ theme }) => theme.stylekitForegroundColor};
`;
export const SideMenuSectionContainer = styled(ScrollView)`
padding: 15px;
flex: 1;
background-color: ${({ theme }) => theme.stylekitBackgroundColor};
`;

/** Styled doesn't support FlatList types */
export const useStyles = (theme: DefaultTheme) => {
return useMemo(
() =>
StyleSheet.create({
sections: {
padding: 15,
flex: 1,
backgroundColor: theme.stylekitBackgroundColor,
},
}),
[theme.stylekitBackgroundColor]
);
};
96 changes: 59 additions & 37 deletions src/screens/SideMenu/MainSideMenu.tsx
Expand Up @@ -23,13 +23,14 @@ import React, {
} from 'react';
import { Platform } from 'react-native';
import FAB from 'react-native-fab';
import { FlatList } from 'react-native-gesture-handler';
import DrawerLayout from 'react-native-gesture-handler/DrawerLayout';
import Icon from 'react-native-vector-icons/Ionicons';
import { ThemeContext } from 'styled-components/native';
import {
FirstSafeAreaView,
MainSafeAreaView,
SideMenuSectionContainer,
useStyles,
} from './MainSideMenu.styled';
import { SideMenuHero } from './SideMenuHero';
import { SideMenuOption, SideMenuSection } from './SideMenuSection';
Expand All @@ -51,6 +52,7 @@ export const MainSideMenu = React.memo(({ drawerRef }: Props) => {
application!.getAppState().getSelectedTag()
);
const [themes, setThemes] = useState<SNTheme[]>([]);
const styles = useStyles(theme);

useEffect(() => {
const removeTagChangeObserver = application!
Expand Down Expand Up @@ -239,15 +241,18 @@ export const MainSideMenu = React.memo(({ drawerRef }: Props) => {
onThemeSelect,
]);

const onTagSelect = async (tag: SNTag) => {
if (tag.conflictOf) {
application!.changeAndSaveItem(tag.uuid, mutator => {
mutator.conflictOf = undefined;
});
}
application?.getAppState().setSelectedTag(tag, true);
drawerRef?.closeDrawer();
};
const onTagSelect = useCallback(
async (tag: SNTag) => {
if (tag.conflictOf) {
application!.changeAndSaveItem(tag.uuid, mutator => {
mutator.conflictOf = undefined;
});
}
application?.getAppState().setSelectedTag(tag, true);
drawerRef?.closeDrawer();
},
[application, drawerRef]
);

const openSettings = () => {
drawerRef?.closeDrawer();
Expand All @@ -266,6 +271,10 @@ export const MainSideMenu = React.memo(({ drawerRef }: Props) => {
}
};

const selectedTags = useMemo(() => (selectedTag ? [selectedTag] : []), [
selectedTag,
]);

return (
<Fragment>
<FirstSafeAreaView />
Expand All @@ -275,33 +284,46 @@ export const MainSideMenu = React.memo(({ drawerRef }: Props) => {
onPress={openSettings}
onOutOfSyncPress={outOfSyncPressed}
/>
<SideMenuSectionContainer>
<SideMenuSection
title="Themes"
key="themes-section"
options={themeOptions}
collapsed={true}
/>
<SideMenuSection title="Views" key="views-section">
<TagSelectionList
key="views-section-list"
contentType={ContentType.SmartTag}
onTagSelect={onTagSelect}
selectedTags={selectedTag ? [selectedTag] : []}
/>
</SideMenuSection>

<SideMenuSection title="Tags" key="tags-section">
<TagSelectionList
key="tags-section-list"
hasBottomPadding={Platform.OS === 'android'}
emptyPlaceholder={'No tags. Create one from the note composer.'}
contentType={ContentType.Tag}
onTagSelect={onTagSelect}
selectedTags={selectedTag ? [selectedTag] : []}
/>
</SideMenuSection>
</SideMenuSectionContainer>
<FlatList
style={styles.sections}
data={['themes-section', 'views-section', 'tags-section'].map(
key => ({
key,
themeOptions,
onTagSelect,
selectedTags,
})
)}
renderItem={({ item, index }) => {
return index === 0 ? (
<SideMenuSection
title="Themes"
options={item.themeOptions}
collapsed={true}
/>
) : index === 1 ? (
<SideMenuSection title="Views">
<TagSelectionList
contentType={ContentType.SmartTag}
onTagSelect={item.onTagSelect}
selectedTags={item.selectedTags}
/>
</SideMenuSection>
) : index === 2 ? (
<SideMenuSection title="Tags">
<TagSelectionList
hasBottomPadding={Platform.OS === 'android'}
emptyPlaceholder={
'No tags. Create one from the note composer.'
}
contentType={ContentType.Tag}
onTagSelect={item.onTagSelect}
selectedTags={item.selectedTags}
/>
</SideMenuSection>
) : null;
}}
/>
<FAB
buttonColor={theme.stylekitInfoColor}
iconTextColor={theme.stylekitInfoContrastColor}
Expand Down
17 changes: 16 additions & 1 deletion src/screens/SideMenu/NoteSideMenu.styled.ts
@@ -1,6 +1,8 @@
import { useMemo } from 'react';
import { StyleSheet } from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';
import { SafeAreaView } from 'react-native-safe-area-context';
import styled from 'styled-components/native';
import styled, { DefaultTheme } from 'styled-components/native';

export const SafeAreaContainer = styled(SafeAreaView)`
flex: 1;
Expand All @@ -12,3 +14,16 @@ export const StyledList = styled(ScrollView)`
padding: 15px;
background-color: ${({ theme }) => theme.stylekitBackgroundColor};
`;

export const useStyles = (theme: DefaultTheme) => {
return useMemo(
() =>
StyleSheet.create({
sections: {
padding: 15,
backgroundColor: theme.stylekitBackgroundColor,
},
}),
[theme.stylekitBackgroundColor]
);
};
64 changes: 38 additions & 26 deletions src/screens/SideMenu/NoteSideMenu.tsx
Expand Up @@ -47,10 +47,11 @@ import React, {
} from 'react';
import { Platform, Share } from 'react-native';
import FAB from 'react-native-fab';
import { FlatList } from 'react-native-gesture-handler';
import DrawerLayout from 'react-native-gesture-handler/DrawerLayout';
import Icon from 'react-native-vector-icons/Ionicons';
import { ThemeContext } from 'styled-components/native';
import { SafeAreaContainer, StyledList } from './NoteSideMenu.styled';
import { SafeAreaContainer, useStyles } from './NoteSideMenu.styled';
import { SideMenuOption, SideMenuSection } from './SideMenuSection';
import { TagSelectionList } from './TagSelectionList';

Expand All @@ -73,6 +74,7 @@ export const NoteSideMenu = React.memo((props: Props) => {
AppStackNavigationProp<typeof SCREEN_COMPOSE>['navigation']
>();
const { showActionSheet } = useCustomActionSheet();
const styles = useStyles(theme);

// State
const [editor, setEditor] = useState<Editor | undefined>(undefined);
Expand Down Expand Up @@ -594,31 +596,41 @@ export const NoteSideMenu = React.memo((props: Props) => {

return (
<SafeAreaContainer edges={['top', 'bottom', 'right']}>
<StyledList>
<SideMenuSection
title="Options"
key="options-section"
options={noteOptions}
/>
<SideMenuSection
title="Editors"
key="editors-section"
options={editorComponents}
collapsed={true}
/>
<SideMenuSection title="Tags" key="tags-section">
<TagSelectionList
key="tags-section-list"
hasBottomPadding={Platform.OS === 'android'}
contentType={ContentType.Tag}
onTagSelect={onTagSelect}
selectedTags={selectedTags}
emptyPlaceholder={
'Create a new tag using the tag button in the bottom right corner.'
}
/>
</SideMenuSection>
</StyledList>
<FlatList
style={styles.sections}
data={['options-section', 'editors-section', 'tags-section'].map(
key => ({
key,
noteOptions,
editorComponents,
onTagSelect,
selectedTags,
})
)}
renderItem={({ item, index }) =>
index === 0 ? (
<SideMenuSection title="Options" options={item.noteOptions} />
) : index === 1 ? (
<SideMenuSection
title="Editors"
options={item.editorComponents}
collapsed={true}
/>
) : index === 2 ? (
<SideMenuSection title="Tags">
<TagSelectionList
hasBottomPadding={Platform.OS === 'android'}
contentType={ContentType.Tag}
onTagSelect={item.onTagSelect}
selectedTags={item.selectedTags}
emptyPlaceholder={
'Create a new tag using the tag button in the bottom right corner.'
}
/>
</SideMenuSection>
) : null
}
/>

<FAB
buttonColor={theme.stylekitInfoColor}
Expand Down

0 comments on commit 7565a36

Please sign in to comment.