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,
|
placeholder,
|
||||||
RenderItem,
|
RenderItem,
|
||||||
focused,
|
focused,
|
||||||
placeholderText,
|
|
||||||
pinned = null,
|
|
||||||
customRefresh,
|
customRefresh,
|
||||||
customRefreshing,
|
customRefreshing,
|
||||||
isMove,
|
|
||||||
hideMore,
|
|
||||||
noteToMove,
|
|
||||||
isHome = false,
|
|
||||||
}) => {
|
}) => {
|
||||||
const [state, dispatch] = useTracked();
|
const [state, dispatch] = useTracked();
|
||||||
const {colors, selectionMode, user, currentScreen} = state;
|
const {colors, selectionMode, user} = state;
|
||||||
const searchResults = {...state.searchResults};
|
const searchResults = {...state.searchResults};
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
const [refreshing, setRefreshing] = useState(false);
|
||||||
const [dataProvider, setDataProvider] = useState(null);
|
const [dataProvider, setDataProvider] = useState(null);
|
||||||
@@ -54,17 +48,6 @@ const SimpleList = ({
|
|||||||
eSendEvent(eScrollEvent, y);
|
eSendEvent(eScrollEvent, y);
|
||||||
};
|
};
|
||||||
|
|
||||||
const NOOP = () => undefined;
|
|
||||||
|
|
||||||
const layoutItemAnimator = {
|
|
||||||
animateDidMount: NOOP,
|
|
||||||
animateShift: NOOP,
|
|
||||||
animateWillMount: NOOP,
|
|
||||||
animateWillUnmount: NOOP,
|
|
||||||
animateWillUpdate: () =>
|
|
||||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut),
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let mainData =
|
let mainData =
|
||||||
searchResults.type === type && focused && searchResults.results.length > 0
|
searchResults.type === type && focused && searchResults.results.length > 0
|
||||||
@@ -130,24 +113,8 @@ const SimpleList = ({
|
|||||||
let user = await db.user.get();
|
let user = await db.user.get();
|
||||||
dispatch({type: ACTIONS.USER, user: user});
|
dispatch({type: ACTIONS.USER, user: user});
|
||||||
await db.sync();
|
await db.sync();
|
||||||
if (Platform.OS === 'ios') {
|
|
||||||
dispatch({
|
|
||||||
type: ACTIONS.SYNCING,
|
|
||||||
syncing: false,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setRefreshing(false);
|
|
||||||
}
|
|
||||||
ToastEvent.show('Sync Complete', 'success');
|
ToastEvent.show('Sync Complete', 'success');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (Platform.OS === 'ios') {
|
|
||||||
dispatch({
|
|
||||||
type: ACTIONS.SYNCING,
|
|
||||||
syncing: false,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
setRefreshing(false);
|
|
||||||
}
|
|
||||||
ToastEvent.show(
|
ToastEvent.show(
|
||||||
e.message,
|
e.message,
|
||||||
'error',
|
'error',
|
||||||
@@ -158,12 +125,134 @@ const SimpleList = ({
|
|||||||
},
|
},
|
||||||
'Login',
|
'Login',
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
if (Platform.OS === 'ios') {
|
||||||
|
dispatch({
|
||||||
|
type: ACTIONS.SYNCING,
|
||||||
|
syncing: false,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setRefreshing(false);
|
||||||
}
|
}
|
||||||
dispatch({type: ACTIONS.ALL});
|
dispatch({type: ACTIONS.ALL});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const ListHeaderComponent = () => {
|
const _ListEmptyComponent = (
|
||||||
return searchResults.type === type && searchResults.results.length > 0 ? (
|
<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
|
<View
|
||||||
style={{
|
style={{
|
||||||
flexDirection: 'row',
|
flexDirection: 'row',
|
||||||
@@ -192,7 +281,14 @@ const SimpleList = ({
|
|||||||
Clear
|
Clear
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
) : (
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const LoginCard = () => {
|
||||||
|
const [state, dispatch] = useTracked();
|
||||||
|
const {colors, selectionMode, user, currentScreen} = state;
|
||||||
|
|
||||||
|
return (
|
||||||
<View>
|
<View>
|
||||||
{user || !data[0] || selectionMode ? null : (
|
{user || !data[0] || selectionMode ? null : (
|
||||||
<PressableButton
|
<PressableButton
|
||||||
@@ -271,134 +367,13 @@ const SimpleList = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const _ListEmptyComponent = (
|
const ListHeaderComponent = (type) => {
|
||||||
<View
|
const [state, dispatch] = useTracked();
|
||||||
style={{
|
const searchResults = {...state.searchResults};
|
||||||
height: '100%',
|
|
||||||
width: '100%',
|
|
||||||
alignItems: 'center',
|
|
||||||
alignSelf: 'center',
|
|
||||||
justifyContent: 'center',
|
|
||||||
opacity: 1,
|
|
||||||
backgroundColor: colors.bg,
|
|
||||||
}}>
|
|
||||||
<>{placeholder}</>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
|
|
||||||
|
return searchResults.type === type && searchResults.results.length > 0 ? (
|
||||||
const _layoutProvider = new LayoutProvider(
|
<SearchHeader />
|
||||||
(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
|
<LoginCard type={type} />
|
||||||
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;
|
|
||||||
|
|||||||
Reference in New Issue
Block a user