fix refreshControl

This commit is contained in:
ammarahm-ed
2020-04-26 16:15:59 +05:00
parent 1c0e3714af
commit 2b3ce8d40b
2 changed files with 44 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
import React, {createRef} from 'react';
import React, {createRef, useState} from 'react';
import {
FlatList,
Platform,
@@ -12,7 +12,7 @@ import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
import {eSendEvent} from '../../services/eventManager';
import {eClearSearch, eScrollEvent} from '../../services/events';
import {hexToRGBA} from '../../utils/utils';
import {hexToRGBA, ToastEvent, db} from '../../utils/utils';
import {NotebookItem} from '../NotebookItem';
import SelectionWrapper from '../SelectionWrapper';
import {useSafeArea} from 'react-native-safe-area-context';
@@ -25,7 +25,6 @@ const SimpleList = ({
onRefresh,
renderItem,
focused,
refreshing,
placeholderText,
pinned = null,
isMove,
@@ -36,6 +35,7 @@ const SimpleList = ({
const [state, dispatch] = useTracked();
const {colors, selectionMode, syncing} = state;
const searchResults = {...state.searchResults};
const [refreshing, setRefreshing] = useState(false);
const insets = useSafeArea();
const _onScroll = event => {
if (!event) return;
@@ -77,6 +77,44 @@ const SimpleList = ({
</Text>
);
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});
};
const _ListHeaderComponent_S =
searchResults.type === type && searchResults.results.length > 0 ? (
<View
@@ -258,7 +296,7 @@ const SimpleList = ({
tintColor={colors.accent}
colors={[colors.accent]}
progressViewOffset={150}
onRefresh={onRefresh}
onRefresh={_onRefresh}
refreshing={syncing}
/>
}

View File

@@ -13,11 +13,12 @@ import SimpleList from '../../components/SimpleList';
import {NotesPlaceHolder} from '../../components/ListPlaceholders';
import SelectionWrapper from '../../components/SelectionWrapper';
import NoteItem from '../../components/NoteItem';
import {Platform} from 'react-native';
let count = 0;
export const Home = ({navigation}) => {
const [state, dispatch] = useTracked();
const [refreshing, setRefreshing] = useState(false);
const {
colors,
selectionMode,
@@ -39,34 +40,6 @@ export const Home = ({navigation}) => {
dispatch({type: ACTIONS.NOTES});
}
}, [isFocused]);
const _onRefresh = async () => {
dispatch({
type: ACTIONS.SYNCING,
syncing: true,
});
try {
await db.sync();
dispatch({
type: ACTIONS.SYNCING,
syncing: false,
});
ToastEvent.show('Sync Complete', 'success');
} catch (e) {
dispatch({
type: ACTIONS.SYNCING,
syncing: false,
});
ToastEvent.show(e.message, 'error');
}
dispatch({type: ACTIONS.NOTES});
dispatch({type: ACTIONS.PINNED});
let user = await db.user.get();
dispatch({type: ACTIONS.USER, user: user});
};
const _renderItem = ({item, index}) => (
<SelectionWrapper
index={index}
@@ -127,9 +100,7 @@ export const Home = ({navigation}) => {
type="notes"
isHome={true}
pinned={pinned.notes}
refreshing={refreshing}
focused={isFocused}
onRefresh={_onRefresh}
renderItem={_renderItem}
placeholder={<NotesPlaceHolder colors={colors} />}
placeholderText={`Notes you write appear here`}