|
| 1 | +# Load calls history with cursor based navigation |
| 2 | + |
| 3 | + |
| 4 | +## Installation: |
| 5 | +Run `npm i react-native-calls-history` |
| 6 | + |
| 7 | + |
| 8 | +### Android |
| 9 | + |
| 10 | +#### React Native 0.60+ |
| 11 | +`auto links the module` |
| 12 | +#### React Native <= 0.59 |
| 13 | +##### Auto |
| 14 | +`react-native link react-native-calls-history` |
| 15 | + |
| 16 | +#### Manual |
| 17 | + |
| 18 | +* Edit your `android/settings.gradle` to look like this (exclude +) |
| 19 | + |
| 20 | +```diff |
| 21 | ++ include ':react-native-calls-history' |
| 22 | ++ project(':react-native-calls-history').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-calls-history/android') |
| 23 | +``` |
| 24 | + |
| 25 | +* Edit your `android/app/build.gradle` (note: **app** folder) to look like this (exclude +) |
| 26 | + |
| 27 | + ```diff |
| 28 | +dependencies { |
| 29 | + + implementation project(':react-native-calls-history') |
| 30 | + } |
| 31 | + ``` |
| 32 | + |
| 33 | +* Edit your `MainApplication.java` from ( `android/app/src/main/java/...`) to look like this (exclude +) |
| 34 | +```diff |
| 35 | ++ import ru.enniel.callshistory.CallsHistoryPackage; |
| 36 | + |
| 37 | +@Override |
| 38 | + protected List<ReactPackage> getPackages() { |
| 39 | + return Arrays.<ReactPackage>asList( |
| 40 | + new MainReactPackage(), |
| 41 | ++ new CallsHistoryPackage() |
| 42 | + ); |
| 43 | + } |
| 44 | +``` |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +```typescript |
| 49 | +import { useState } from 'react'; |
| 50 | +import { PermissionsAndroid } from 'react-native'; |
| 51 | +import CallsHistory, { CallsHistoryItem } from 'react-native-calls-history'; |
| 52 | + |
| 53 | +interface State { |
| 54 | + loadingFirst: boolean; |
| 55 | + loadingBefore: boolean; |
| 56 | + loadingAfter: boolean; |
| 57 | + items: CallsHistoryItem[]; |
| 58 | + pagination: { |
| 59 | + before?: string | null; |
| 60 | + after?: string | null; |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +const useCallsHistory = () => { |
| 65 | + const [state, setState] = useState<State>({ |
| 66 | + loadingFirst: false, |
| 67 | + loadingBefore: false, |
| 68 | + loadingAfter: false, |
| 69 | + items: [], |
| 70 | + pagination: { |
| 71 | + before: null, |
| 72 | + after: null, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + const loadFirst = async () => { |
| 77 | + setState((prev) => ({ |
| 78 | + ...prev, |
| 79 | + loading: true, |
| 80 | + })); |
| 81 | + const { items, pagination } = await CallsHistory.load(10) |
| 82 | + setState((prev) => ({ |
| 83 | + ...prev, |
| 84 | + loading: false, |
| 85 | + items, |
| 86 | + pagination |
| 87 | + })); |
| 88 | + }; |
| 89 | + |
| 90 | + const loadBefore = async () => { |
| 91 | + setState((prev) => ({ |
| 92 | + ...prev, |
| 93 | + loadingBefore: true, |
| 94 | + })); |
| 95 | + const { items, pagination } = await CallsHistory.load(10, state.pagination.before) |
| 96 | + setState((prev) => ({ |
| 97 | + ...prev, |
| 98 | + loadingBefore: false, |
| 99 | + items: [ |
| 100 | + ...items, |
| 101 | + ...state.items, |
| 102 | + ], |
| 103 | + pagination: { |
| 104 | + before: pagination.before, |
| 105 | + after: prev.pagination.after, |
| 106 | + }, |
| 107 | + })); |
| 108 | + }; |
| 109 | + |
| 110 | + const loadAfter = async () => { |
| 111 | + setState((prev) => ({ |
| 112 | + ...prev, |
| 113 | + loadingAfter: true, |
| 114 | + })); |
| 115 | + const { items, pagination } = await CallsHistory.load(10, state.pagination.after) |
| 116 | + setState((prev) => ({ |
| 117 | + ...prev, |
| 118 | + loadingAfter: false, |
| 119 | + items: [ |
| 120 | + ...state.items, |
| 121 | + ...items, |
| 122 | + ], |
| 123 | + pagination: { |
| 124 | + before: prev.pagination.before, |
| 125 | + after: pagination.after, |
| 126 | + }, |
| 127 | + })); |
| 128 | + }; |
| 129 | + |
| 130 | + return { |
| 131 | + state, |
| 132 | + loadFirst, |
| 133 | + loadBefore, |
| 134 | + loadAfter, |
| 135 | + }; |
| 136 | +}; |
| 137 | + |
| 138 | +const CallsHistoryScreen = () => { |
| 139 | + const { state, loadFirst, loadBefore, loadAfter } = useCallsHistory(); |
| 140 | + |
| 141 | + useEffect(() => { |
| 142 | + const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CALL_LOG) |
| 143 | + if (granted === PermissionsAndroid.RESULTS.GRANTED) { |
| 144 | + loadFirst(); |
| 145 | + |
| 146 | + CallsHistory.registerOnChangeListener(); |
| 147 | + const emitter = new NativeEventEmitter(); |
| 148 | + callHistoryChangeDataListener = emitter.addListener('CallsHistoryChangeData', () => { |
| 149 | + loadBefore(); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + return () => { |
| 154 | + CallsHistory.removeOnChangeListener(); |
| 155 | + callHistoryChangeDataListener?.remove(); |
| 156 | + }; |
| 157 | + }, []); |
| 158 | + |
| 159 | + const onLoadMore = () => loadAfter(); |
| 160 | + |
| 161 | + ... |
| 162 | +}; |
| 163 | +``` |
0 commit comments