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

390 lines
9.6 KiB
JavaScript
Raw Normal View History

import React, {useCallback, useEffect, useMemo, useState} from 'react';
2020-10-26 11:14:02 +05:00
import {
Platform,
RefreshControl,
StyleSheet,
Text,
useWindowDimensions,
View,
} from 'react-native';
import {initialWindowMetrics} from 'react-native-safe-area-context';
2020-09-09 14:55:59 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-09-18 20:47:52 +05:00
import {DataProvider, LayoutProvider, RecyclerListView} from 'recyclerlistview';
import {useTracked} from '../../provider';
2020-10-13 17:02:14 +05:00
import {Actions} from '../../provider/Actions';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
2020-10-26 11:14:02 +05:00
import {eClearSearch, eOpenLoginDialog, eScrollEvent} from '../../utils/Events';
2020-09-18 20:47:52 +05:00
import {PressableButton} from '../PressableButton';
2020-10-26 11:14:02 +05:00
import {COLORS_NOTE} from '../../utils/Colors';
import {SIZE, WEIGHT} from '../../utils/SizeUtils';
import {db} from '../../utils/DB';
2020-09-18 20:47:52 +05:00
const header = {
2020-10-26 11:14:02 +05:00
type: 'MAIN_HEADER',
2020-09-18 20:47:52 +05:00
};
2020-05-10 22:19:23 +05:00
2020-03-09 20:06:55 +05:00
const SimpleList = ({
2020-10-26 11:14:02 +05:00
data,
type,
placeholder,
RenderItem,
focused,
customRefresh,
customRefreshing,
refreshCallback,
}) => {
const [state, dispatch] = useTracked();
const {colors, selectionMode, user, messageBoardState} = state;
const searchResults = {...state.searchResults};
const [refreshing, setRefreshing] = useState(false);
const [dataProvider, setDataProvider] = useState(
new DataProvider((r1, r2) => {
return r1 !== r2;
}),
);
const {width, fontScale} = useWindowDimensions();
2020-10-26 11:14:02 +05:00
const listData = data;
const dataType = type;
const _onScroll = (event) => {
if (!event) return;
let y = event.nativeEvent.contentOffset.y;
eSendEvent(eScrollEvent, y);
};
2020-03-09 20:06:55 +05:00
2020-10-26 11:14:02 +05:00
useEffect(() => {
loadData();
}, [listData]);
2020-09-18 20:47:52 +05:00
2020-10-26 11:14:02 +05:00
const loadData = () => {
let mainData =
searchResults.type === type &&
focused() &&
searchResults.results.length > 0
? searchResults.results
: listData;
2020-09-18 20:47:52 +05:00
2020-10-26 11:14:02 +05:00
let d = [header, ...mainData];
setDataProvider(dataProvider.cloneWithRows(d));
};
2020-10-26 11:14:02 +05:00
const RenderSectionHeader = ({item}) => (
<Text
style={[
{
color: colors.accent,
},
styles.sectionHeader,
]}>
{item.title}
</Text>
);
2020-03-09 20:06:55 +05:00
2020-10-26 11:14:02 +05:00
const _onRefresh = useCallback(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();
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
ToastEvent.show(
'You must login to sync.',
'error',
'global',
5000,
() => {
eSendEvent(eOpenLoginDialog);
2020-09-27 10:15:19 +05:00
},
2020-10-26 11:14:02 +05:00
'Login',
);
} finally {
if (Platform.OS === 'ios') {
dispatch({
type: Actions.SYNCING,
syncing: false,
});
} else {
setRefreshing(false);
}
if (refreshCallback) {
refreshCallback();
}
}
dispatch({type: Actions.ALL});
}, []);
2020-10-18 13:15:24 +05:00
2020-10-26 11:14:02 +05:00
const _ListEmptyComponent = (
<View
style={[
{
backgroundColor: colors.bg,
2020-10-13 10:29:16 +05:00
},
2020-10-26 11:14:02 +05:00
styles.emptyList,
]}>
<>{placeholder}</>
</View>
);
2020-09-18 20:47:52 +05:00
2020-10-26 11:14:02 +05:00
const _layoutProvider = new LayoutProvider(
(index) => {
return dataProvider.getDataForIndex(index).type;
},
(type, dim) => {
switch (type) {
case 'note':
dim.width = width;
dim.height = 100 * fontScale;
break;
case 'notebook':
dim.width = width;
dim.height = 110 * fontScale;
break;
case 'topic':
dim.width = width;
dim.height = 80 * fontScale;
break;
case 'tag':
dim.width = width;
dim.height = 80 * fontScale;
break;
case 'header':
dim.width = width;
dim.height = 30 * fontScale;
break;
case 'MAIN_HEADER':
dim.width = width;
dim.height =
!messageBoardState.visible || !listData[0] || selectionMode
? 0
: 40 * fontScale;
break;
default:
dim.width = width;
dim.height = 0;
}
},
);
const _renderRow = (type, data, index) => {
switch (type) {
case 'note':
return <RenderItem item={data} pinned={data.pinned} index={index} />;
case 'notebook':
return <RenderItem item={data} pinned={data.pinned} index={index} />;
case 'MAIN_HEADER':
return <ListHeaderComponent type={dataType} data={listData} />;
case 'header':
return <RenderSectionHeader item={data} />;
default:
return <RenderItem item={data} index={index} />;
}
};
2020-09-27 10:15:19 +05:00
2020-10-26 11:14:02 +05:00
const listStyle = useMemo(() => {
return {
height: '100%',
backgroundColor: colors.bg,
width: '100%',
paddingTop:
Platform.OS === 'ios'
? listData[0] && !selectionMode
? 130
: 130 - 60
: listData[0] && !selectionMode
2020-10-26 12:01:05 +05:00
? 155 - initialWindowMetrics.insets.top
: (155 - initialWindowMetrics.insets.top) - 60,
2020-10-26 11:14:02 +05:00
};
}, [selectionMode, listData, colors]);
2020-10-13 10:29:16 +05:00
2020-10-26 11:14:02 +05:00
return !listData || listData.length === 0 || !dataProvider ? (
_ListEmptyComponent
) : (
<RecyclerListView
layoutProvider={_layoutProvider}
dataProvider={dataProvider}
rowRenderer={_renderRow}
onScroll={_onScroll}
renderFooter={() => <View style={{height: 400}} />}
scrollViewProps={{
refreshControl: (
<RefreshControl
tintColor={colors.accent}
colors={[colors.accent]}
progressViewOffset={150}
onRefresh={customRefresh ? customRefresh : _onRefresh}
refreshing={customRefresh ? customRefreshing : refreshing}
/>
),
contentContainerStyle: {
width: '100%',
alignSelf: 'center',
minHeight: '100%',
},
}}
style={listStyle}
/>
);
2020-03-09 20:06:55 +05:00
};
export default SimpleList;
2020-09-21 13:13:18 +05:00
const SearchHeader = () => {
2020-10-26 11:14:02 +05:00
const [state] = useTracked();
const {colors} = state;
const searchResults = {...state.searchResults};
return (
<View style={styles.searchHeader}>
<Text
style={{
fontFamily: WEIGHT.bold,
color: colors.accent,
fontSize: SIZE.xs,
}}>
Showing Results for {searchResults.keyword}
</Text>
<Text
onPress={() => {
eSendEvent(eClearSearch);
}}
style={{
fontFamily: WEIGHT.regular,
color: colors.errorText,
fontSize: SIZE.xs,
}}>
Clear
</Text>
</View>
);
};
2020-09-21 13:13:18 +05:00
2020-10-26 11:14:02 +05:00
const MessageCard = ({data}) => {
const [state] = useTracked();
const {colors, selectionMode, currentScreen, messageBoardState} = state;
return (
<View>
{!messageBoardState.visible || !data[0] || selectionMode ? null : (
<PressableButton
onPress={messageBoardState.onPress}
color={
COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.shade
}
selectedColor={
COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.accent
}
alpha={!colors.night ? -0.02 : 0.1}
opacity={0.12}
customStyle={styles.loginCard}>
<View
style={{
width: 25,
backgroundColor: COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.accent,
height: 25,
borderRadius: 100,
alignItems: 'center',
justifyContent: 'center',
}}>
<Icon
style={styles.loginIcon}
name={messageBoardState.icon}
color="white"
size={SIZE.xs}
/>
</View>
<View
style={{
marginLeft: 10,
}}>
2020-09-21 13:13:18 +05:00
<Text
2020-10-26 11:14:02 +05:00
style={{
fontFamily: WEIGHT.regular,
color: colors.icon,
fontSize: SIZE.xxs - 1,
}}>
{messageBoardState.message}
2020-09-21 13:13:18 +05:00
</Text>
<Text
2020-10-26 11:14:02 +05:00
style={{
color: COLORS_NOTE[currentScreen]
? COLORS_NOTE[currentScreen]
: colors.accent,
fontSize: SIZE.xxs,
}}>
2020-10-26 11:35:42 +05:00
{messageBoardState.actionText}
2020-09-21 13:13:18 +05:00
</Text>
2020-10-26 11:14:02 +05:00
</View>
</PressableButton>
)}
</View>
);
2020-09-21 13:13:18 +05:00
};
2020-09-24 09:51:15 +05:00
const ListHeaderComponent = ({type, data}) => {
2020-10-26 11:14:02 +05:00
const [state] = useTracked();
const searchResults = {...state.searchResults};
2020-09-21 13:13:18 +05:00
2020-10-26 11:14:02 +05:00
return searchResults.type === type && searchResults.results.length > 0 ? (
<SearchHeader />
) : (
<MessageCard type={type} data={data} />
);
2020-09-21 13:13:18 +05:00
};
2020-09-27 10:15:19 +05:00
const styles = StyleSheet.create({
2020-10-26 11:14:02 +05:00
loginCard: {
width: '100%',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
paddingHorizontal: 12,
alignSelf: 'center',
height: 40,
borderRadius: 0,
position: 'relative',
},
loginIcon: {
textAlign: 'center',
textAlignVertical: 'center',
},
searchHeader: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 12,
height: 40,
},
sectionHeader: {
fontFamily: WEIGHT.bold,
fontSize: SIZE.xs + 1,
paddingHorizontal: 12,
width: '100%',
alignSelf: 'center',
marginTop: 10,
height: 25,
textAlignVertical: 'center',
},
emptyList: {
height: '100%',
width: '100%',
alignItems: 'center',
alignSelf: 'center',
justifyContent: 'center',
opacity: 1,
},
2020-09-27 10:15:19 +05:00
});