mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
refactor
This commit is contained in:
@@ -33,17 +33,11 @@ const SimpleList = ({
|
||||
placeholder,
|
||||
RenderItem,
|
||||
focused,
|
||||
placeholderText,
|
||||
pinned = null,
|
||||
customRefresh,
|
||||
customRefreshing,
|
||||
isMove,
|
||||
hideMore,
|
||||
noteToMove,
|
||||
isHome = false,
|
||||
}) => {
|
||||
const [state, dispatch] = useTracked();
|
||||
const {colors, selectionMode, user, currentScreen} = state;
|
||||
const {colors, selectionMode, user} = state;
|
||||
const searchResults = {...state.searchResults};
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const [dataProvider, setDataProvider] = useState(null);
|
||||
@@ -54,17 +48,6 @@ const SimpleList = ({
|
||||
eSendEvent(eScrollEvent, y);
|
||||
};
|
||||
|
||||
const NOOP = () => undefined;
|
||||
|
||||
const layoutItemAnimator = {
|
||||
animateDidMount: NOOP,
|
||||
animateShift: NOOP,
|
||||
animateWillMount: NOOP,
|
||||
animateWillUnmount: NOOP,
|
||||
animateWillUpdate: () =>
|
||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut),
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
let mainData =
|
||||
searchResults.type === type && focused && searchResults.results.length > 0
|
||||
@@ -130,24 +113,8 @@ const SimpleList = ({
|
||||
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',
|
||||
@@ -158,12 +125,134 @@ const SimpleList = ({
|
||||
},
|
||||
'Login',
|
||||
);
|
||||
} finally {
|
||||
if (Platform.OS === 'ios') {
|
||||
dispatch({
|
||||
type: ACTIONS.SYNCING,
|
||||
syncing: false,
|
||||
});
|
||||
} else {
|
||||
setRefreshing(false);
|
||||
}
|
||||
dispatch({type: ACTIONS.ALL});
|
||||
}
|
||||
};
|
||||
|
||||
const ListHeaderComponent = () => {
|
||||
return searchResults.type === type && searchResults.results.length > 0 ? (
|
||||
const _ListEmptyComponent = (
|
||||
<View
|
||||
style={{
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
alignItems: 'center',
|
||||
alignSelf: 'center',
|
||||
justifyContent: 'center',
|
||||
opacity: 1,
|
||||
backgroundColor: colors.bg,
|
||||
}}>
|
||||
<>{placeholder}</>
|
||||
</View>
|
||||
);
|
||||
|
||||
const _layoutProvider = new LayoutProvider(
|
||||
(index) => {
|
||||
return dataProvider.getDataForIndex(index).type;
|
||||
},
|
||||
(type, dim) => {
|
||||
switch (type) {
|
||||
case 'note':
|
||||
dim.width = width;
|
||||
dim.height = 100;
|
||||
break;
|
||||
case 'notebook':
|
||||
dim.width = width;
|
||||
dim.height = 110;
|
||||
break;
|
||||
case 'topic':
|
||||
dim.width = width;
|
||||
dim.height = 80;
|
||||
break;
|
||||
case 'tag':
|
||||
dim.width = width;
|
||||
dim.height = 80;
|
||||
break;
|
||||
case 'header':
|
||||
dim.width = width;
|
||||
dim.height = 40;
|
||||
break;
|
||||
case 'MAIN_HEADER':
|
||||
dim.width = width;
|
||||
dim.height = user || !data[0] || selectionMode ? 0 : 40;
|
||||
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 'MAIN_HEADER':
|
||||
return <ListHeaderComponent type={type} />;
|
||||
case 'header':
|
||||
return <RenderSectionHeader item={data} />;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
return !data || data.length === 0 ? (
|
||||
_ListEmptyComponent
|
||||
) : (
|
||||
<RecyclerListView
|
||||
layoutProvider={_layoutProvider}
|
||||
dataProvider={dataProvider}
|
||||
rowRenderer={_renderRow}
|
||||
onScroll={_onScroll}
|
||||
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={{
|
||||
height: '100%',
|
||||
backgroundColor: colors.bg,
|
||||
width: '100%',
|
||||
paddingTop:
|
||||
Platform.OS == 'ios'
|
||||
? data[0] && !selectionMode
|
||||
? 115
|
||||
: 115 - 60
|
||||
: data[0] && !selectionMode
|
||||
? 155 - insets.top
|
||||
: 155 - insets.top - 60,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default SimpleList;
|
||||
|
||||
const SearchHeader = () => {
|
||||
const [state, dispatch] = useTracked();
|
||||
const {colors} = state;
|
||||
const searchResults = {...state.searchResults};
|
||||
|
||||
return (
|
||||
<View
|
||||
style={{
|
||||
flexDirection: 'row',
|
||||
@@ -192,7 +281,14 @@ const SimpleList = ({
|
||||
Clear
|
||||
</Text>
|
||||
</View>
|
||||
) : (
|
||||
);
|
||||
};
|
||||
|
||||
const LoginCard = () => {
|
||||
const [state, dispatch] = useTracked();
|
||||
const {colors, selectionMode, user, currentScreen} = state;
|
||||
|
||||
return (
|
||||
<View>
|
||||
{user || !data[0] || selectionMode ? null : (
|
||||
<PressableButton
|
||||
@@ -269,136 +365,15 @@ const SimpleList = ({
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
};
|
||||
|
||||
const _ListEmptyComponent = (
|
||||
<View
|
||||
style={{
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
alignItems: 'center',
|
||||
alignSelf: 'center',
|
||||
justifyContent: 'center',
|
||||
opacity: 1,
|
||||
backgroundColor: colors.bg,
|
||||
}}>
|
||||
<>{placeholder}</>
|
||||
</View>
|
||||
);
|
||||
|
||||
|
||||
const _layoutProvider = new LayoutProvider(
|
||||
(index) => {
|
||||
|
||||
return dataProvider.getDataForIndex(index).type;
|
||||
},
|
||||
(type, dim) => {
|
||||
switch (type) {
|
||||
case 'note':
|
||||
dim.width = width;
|
||||
dim.height = 100;
|
||||
break;
|
||||
case 'notebook':
|
||||
dim.width = width;
|
||||
dim.height = 110;
|
||||
break;
|
||||
case 'topic':
|
||||
dim.width = width;
|
||||
dim.height = 80;
|
||||
break;
|
||||
case 'tag':
|
||||
dim.width = width;
|
||||
dim.height = 80;
|
||||
break;
|
||||
case 'header':
|
||||
dim.width = width;
|
||||
dim.height = 40;
|
||||
break;
|
||||
case 'MAIN_HEADER':
|
||||
dim.width = width;
|
||||
dim.height =user || !data[0] || selectionMode ? 0: 40;
|
||||
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 'MAIN_HEADER':
|
||||
return <ListHeaderComponent />;
|
||||
case 'header':
|
||||
return <RenderSectionHeader item={data} />;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
return !data || data.length === 0 ? (
|
||||
_ListEmptyComponent
|
||||
) : (
|
||||
<RecyclerListView
|
||||
layoutProvider={_layoutProvider}
|
||||
dataProvider={dataProvider}
|
||||
rowRenderer={_renderRow}
|
||||
onScroll={_onScroll}
|
||||
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%',
|
||||
},
|
||||
}}
|
||||
/* data={
|
||||
searchResults.type === type &&
|
||||
focused &&
|
||||
searchResults.results.length > 0
|
||||
? searchResults.results
|
||||
: data
|
||||
}
|
||||
|
||||
keyExtractor={_listKeyExtractor} */
|
||||
//
|
||||
//onScroll={_onScroll}
|
||||
//
|
||||
//
|
||||
|
||||
style={{
|
||||
height: '100%',
|
||||
backgroundColor: colors.bg,
|
||||
width: '100%',
|
||||
paddingTop:
|
||||
Platform.OS == 'ios'
|
||||
? data[0] && !selectionMode
|
||||
? 115
|
||||
: 115 - 60
|
||||
: data[0] && !selectionMode
|
||||
? 155 - insets.top
|
||||
: 155 - insets.top - 60,
|
||||
}}
|
||||
/* renderItem={({item, index}) =>
|
||||
item.type === 'header' ? (
|
||||
<RenderSectionHeader item={item} />
|
||||
) : (
|
||||
<RenderItem item={item} index={index} />
|
||||
)
|
||||
} */
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default SimpleList;
|
||||
const ListHeaderComponent = (type) => {
|
||||
const [state, dispatch] = useTracked();
|
||||
const searchResults = {...state.searchResults};
|
||||
|
||||
return searchResults.type === type && searchResults.results.length > 0 ? (
|
||||
<SearchHeader />
|
||||
) : (
|
||||
<LoginCard type={type} />
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user