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

View File

@@ -13,11 +13,12 @@ import SimpleList from '../../components/SimpleList';
import {NotesPlaceHolder} from '../../components/ListPlaceholders'; import {NotesPlaceHolder} from '../../components/ListPlaceholders';
import SelectionWrapper from '../../components/SelectionWrapper'; import SelectionWrapper from '../../components/SelectionWrapper';
import NoteItem from '../../components/NoteItem'; import NoteItem from '../../components/NoteItem';
import {Platform} from 'react-native';
let count = 0; let count = 0;
export const Home = ({navigation}) => { export const Home = ({navigation}) => {
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
const [refreshing, setRefreshing] = useState(false);
const { const {
colors, colors,
selectionMode, selectionMode,
@@ -39,34 +40,6 @@ export const Home = ({navigation}) => {
dispatch({type: ACTIONS.NOTES}); dispatch({type: ACTIONS.NOTES});
} }
}, [isFocused]); }, [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}) => ( const _renderItem = ({item, index}) => (
<SelectionWrapper <SelectionWrapper
index={index} index={index}
@@ -127,9 +100,7 @@ export const Home = ({navigation}) => {
type="notes" type="notes"
isHome={true} isHome={true}
pinned={pinned.notes} pinned={pinned.notes}
refreshing={refreshing}
focused={isFocused} focused={isFocused}
onRefresh={_onRefresh}
renderItem={_renderItem} renderItem={_renderItem}
placeholder={<NotesPlaceHolder colors={colors} />} placeholder={<NotesPlaceHolder colors={colors} />}
placeholderText={`Notes you write appear here`} placeholderText={`Notes you write appear here`}