Files
notesnook/apps/mobile/src/components/SimpleList/index.js

355 lines
9.8 KiB
JavaScript
Raw Normal View History

2020-04-26 16:15:59 +05:00
import React, {createRef, useState} from 'react';
2020-04-20 10:54:58 +05:00
import {
FlatList,
Platform,
RefreshControl,
Text,
View,
SectionList,
} from 'react-native';
2020-04-20 09:16:01 +05:00
import {SIZE, WEIGHT} from '../../common/common';
import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
import {eSendEvent} from '../../services/eventManager';
import {eClearSearch, eScrollEvent} from '../../services/events';
2020-04-26 16:15:59 +05:00
import {hexToRGBA, ToastEvent, db} from '../../utils/utils';
2020-04-20 09:16:01 +05:00
import {NotebookItem} from '../NotebookItem';
2020-03-18 12:31:06 +05:00
import SelectionWrapper from '../SelectionWrapper';
2020-04-20 09:16:01 +05:00
import {useSafeArea} from 'react-native-safe-area-context';
2020-04-20 10:54:58 +05:00
import NoteItem from '../NoteItem';
const sectionListRef = createRef();
2020-03-09 20:06:55 +05:00
const SimpleList = ({
data,
type,
placeholder,
renderItem,
focused,
placeholderText,
2020-03-09 20:15:00 +05:00
pinned = null,
2020-03-17 10:02:34 +05:00
isMove,
hideMore,
noteToMove,
2020-04-20 10:54:58 +05:00
isHome = false,
2020-03-09 20:06:55 +05:00
}) => {
const [state, dispatch] = useTracked();
2020-04-26 16:08:50 +05:00
const {colors, selectionMode, syncing} = state;
2020-04-20 09:16:01 +05:00
const searchResults = {...state.searchResults};
2020-04-26 16:15:59 +05:00
const [refreshing, setRefreshing] = useState(false);
2020-04-16 13:57:51 +05:00
const insets = useSafeArea();
2020-03-09 20:06:55 +05:00
const _onScroll = event => {
if (!event) return;
let y = event.nativeEvent.contentOffset.y;
eSendEvent(eScrollEvent, y);
};
const _ListFooterComponent = data[0] ? (
<View
style={{
height: 150,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
color: colors.navbg,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
}}>
- End -
</Text>
</View>
) : null;
2020-04-20 10:54:58 +05:00
const _renderSectionHeader = ({section: {title}}) => (
<Text
style={{
fontFamily: WEIGHT.bold,
fontSize: SIZE.xs + 1,
color: colors.accent,
paddingHorizontal: 12,
width: '100%',
alignSelf: 'center',
marginTop: 15,
paddingBottom: 5,
}}>
{title}
</Text>
);
2020-04-26 16:15:59 +05:00
const _onRefresh = async () => {
if (Platform.OS === 'ios') {
dispatch({
type: ACTIONS.SYNCING,
syncing: true,
});
} else {
setRefreshing(true);
}
try {
let user = await db.user.get();
dispatch({type: ACTIONS.USER, user: user});
await db.sync();
if (Platform.OS === 'ios') {
dispatch({
type: ACTIONS.SYNCING,
syncing: false,
});
} else {
setRefreshing(false);
}
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
if (Platform.OS === 'ios') {
dispatch({
type: ACTIONS.SYNCING,
syncing: false,
});
} else {
setRefreshing(false);
}
ToastEvent.show(e.message, 'error');
}
dispatch({type: ACTIONS.ALL});
};
2020-03-09 20:06:55 +05:00
const _ListHeaderComponent_S =
searchResults.type === type && searchResults.results.length > 0 ? (
<View
style={{
marginTop:
Platform.OS == 'ios'
? data[0] && !selectionMode
2020-04-26 16:08:50 +05:00
? 115
: 115 - 60
2020-03-09 20:06:55 +05:00
: data[0] && !selectionMode
2020-04-20 10:54:58 +05:00
? 155 - insets.top
: 155 - insets.top - 60,
2020-03-09 20:06:55 +05:00
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 12,
}}>
<Text
style={{
fontFamily: WEIGHT.bold,
color: colors.accent,
fontSize: SIZE.xs,
}}>
2020-04-25 11:28:20 +05:00
Showing Results for {searchResults.keyword}
2020-03-09 20:06:55 +05:00
</Text>
<Text
onPress={() => {
eSendEvent(eClearSearch);
2020-03-09 20:06:55 +05:00
}}
style={{
fontFamily: WEIGHT.regular,
color: colors.errorText,
fontSize: SIZE.xs,
}}>
Clear
</Text>
</View>
) : (
2020-04-20 09:16:01 +05:00
<View
style={{
marginTop:
Platform.OS == 'ios'
? data[0] && !selectionMode
2020-04-26 16:08:50 +05:00
? 115
: 115 - 60
2020-04-20 09:16:01 +05:00
: data[0] && !selectionMode
2020-04-20 10:54:58 +05:00
? 155 - insets.top
: 155 - 60 - insets.top,
2020-04-20 09:16:01 +05:00
}}>
2020-04-22 11:22:20 +05:00
{pinned && pinned.length > 0 ? (
2020-04-20 09:16:01 +05:00
<>
<FlatList
2020-04-22 11:22:20 +05:00
data={pinned}
2020-04-20 09:16:01 +05:00
keyExtractor={(item, index) => item.id.toString()}
renderItem={({item, index}) =>
item.type === 'notebook' ? (
<SelectionWrapper
index={index}
currentEditingNote={false}
pinned={true}
background={
Platform.ios
? hexToRGBA(colors.accent + '19')
: hexToRGBA(colors.shade)
}
item={item}>
<NotebookItem
hideMore={hideMore}
customStyle={{
width: '100%',
paddingTop: 15,
paddingRight: 18,
marginBottom: 10,
marginTop: 15,
borderBottomWidth: 0,
marginHorizontal: 0,
}}
isMove={isMove}
selectionMode={selectionMode}
onLongPress={() => {
if (!selectionMode) {
dispatch({
type: ACTIONS.SELECTION_MODE,
enabled: true,
});
}
dispatch({type: ACTIONS.SELECTED_ITEMS, item: item});
}}
noteToMove={noteToMove}
item={item}
2020-04-16 13:57:51 +05:00
pinned={true}
2020-04-20 09:16:01 +05:00
index={index}
colors={colors}
/>
</SelectionWrapper>
2020-04-20 10:54:58 +05:00
) : (
<SelectionWrapper
index={index}
currentEditingNote={false}
pinned={true}
background={
Platform.ios
? hexToRGBA(colors.accent + '19')
: hexToRGBA(colors.shade)
}
item={item}>
<NoteItem
colors={colors}
customStyle={{
width: selectionMode ? '90%' : '100%',
marginHorizontal: 0,
paddingTop: 15,
paddingRight: 18,
marginBottom: 10,
marginTop: 15,
borderBottomWidth: 0,
}}
currentEditingNote={false}
pinned={true}
selectionMode={selectionMode}
onLongPress={() => {
if (!selectionMode) {
dispatch({
type: ACTIONS.SELECTION_MODE,
enabled: true,
});
}
dispatch({type: ACTIONS.SELECTED_ITEMS, item: item});
}}
update={() => {
dispatch({type: ACTIONS.NOTES});
}}
item={item}
index={index}
/>
</SelectionWrapper>
)
2020-04-20 09:16:01 +05:00
}
/>
</>
) : null}
</View>
);
2020-03-09 20:06:55 +05:00
const _ListEmptyComponent = (
<View
style={{
height: '80%',
width: '100%',
alignItems: 'center',
alignSelf: 'center',
justifyContent: 'center',
2020-03-19 11:04:47 +05:00
opacity: 1,
2020-03-09 20:06:55 +05:00
}}>
<>
{placeholder}
<Text
style={{
color: colors.icon,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
marginTop: 35,
}}>
{placeholderText}
</Text>
</>
</View>
);
const _listKeyExtractor = (item, index) =>
2020-03-22 12:39:35 +05:00
item.id.toString() + index.toString();
2020-03-09 20:06:55 +05:00
2020-04-20 10:54:58 +05:00
return isHome && searchResults.type !== 'notes' ? (
<SectionList
ref={sectionListRef}
sections={data}
refreshControl={
<RefreshControl
tintColor={colors.accent}
colors={[colors.accent]}
2020-04-26 16:08:50 +05:00
progressViewOffset={150}
2020-04-26 16:15:59 +05:00
onRefresh={_onRefresh}
2020-04-26 16:27:54 +05:00
refreshing={refreshing}
2020-04-20 10:54:58 +05:00
/>
}
keyExtractor={_listKeyExtractor}
renderSectionHeader={_renderSectionHeader}
onScroll={_onScroll}
2020-04-26 16:08:50 +05:00
stickySectionHeadersEnabled={false}
2020-04-20 10:54:58 +05:00
ListEmptyComponent={_ListEmptyComponent}
ListHeaderComponent={_ListHeaderComponent_S}
contentContainerStyle={{
width: '100%',
alignSelf: 'center',
minHeight: '100%',
}}
style={{
height: '100%',
}}
removeClippedSubviews={true}
ListFooterComponent={_ListFooterComponent}
renderItem={renderItem}
/>
) : (
2020-03-09 20:06:55 +05:00
<FlatList
data={
searchResults.type === type &&
2020-04-20 09:16:01 +05:00
focused &&
searchResults.results.length > 0
2020-03-09 20:06:55 +05:00
? searchResults.results
: data
}
refreshControl={
<RefreshControl
tintColor={colors.accent}
colors={[colors.accent]}
2020-04-26 16:08:50 +05:00
progressViewOffset={-200}
2020-04-26 16:27:54 +05:00
onRefresh={_onRefresh}
2020-03-09 20:06:55 +05:00
refreshing={refreshing}
/>
}
keyExtractor={_listKeyExtractor}
ListFooterComponent={_ListFooterComponent}
onScroll={_onScroll}
ListHeaderComponent={_ListHeaderComponent_S}
ListEmptyComponent={_ListEmptyComponent}
contentContainerStyle={{
width: '100%',
alignSelf: 'center',
minHeight: '100%',
}}
style={{
height: '100%',
}}
renderItem={renderItem}
/>
);
};
export default SimpleList;