Skip to content

Commit d62935c

Browse files
committed
init
0 parents  commit d62935c

13 files changed

+806
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
package-lock.json
3+
.gradle
4+
.idea
5+
local.properties
6+
build
7+
.DS_Store

README.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
```

android/build.gradle

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
def safeExtGet(prop, fallback) {
2+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
3+
}
4+
5+
def kotlinVersion = safeExtGet('kotlinVersion', '1.4.10')
6+
def reactNativeVersion = safeExtGet('reactNativeVersion', '+')
7+
8+
buildscript {
9+
repositories {
10+
google()
11+
jcenter()
12+
}
13+
14+
dependencies {
15+
classpath("com.android.tools.build:gradle:4.1.3")
16+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
17+
}
18+
}
19+
20+
apply plugin: 'com.android.library'
21+
apply plugin: 'kotlin-android'
22+
apply plugin: 'kotlin-kapt'
23+
24+
android {
25+
compileSdkVersion safeExtGet('compileSdkVersion', 28)
26+
27+
kotlinOptions {
28+
jvmTarget = JavaVersion.VERSION_1_8.toString()
29+
}
30+
31+
defaultConfig {
32+
minSdkVersion safeExtGet('minSdkVersion', 16)
33+
targetSdkVersion safeExtGet('targetSdkVersion', 28)
34+
versionCode 1
35+
versionName "1.0"
36+
}
37+
38+
lintOptions {
39+
abortOnError false
40+
}
41+
42+
sourceSets {
43+
main.java.srcDirs += 'src/main/kotlin'
44+
}
45+
}
46+
47+
repositories {
48+
google()
49+
maven {
50+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
51+
url "$rootDir/../node_modules/react-native/android"
52+
}
53+
jcenter()
54+
}
55+
56+
dependencies {
57+
compileOnly "com.facebook.react:react-native:$reactNativeVersion"
58+
59+
implementation "androidx.core:core-ktx:1.6.0"
60+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
61+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1"
62+
}

android/gradle.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# AndroidX package structure to make it clearer which packages are bundled with the
2+
# Android operating system, and which are packaged with your app's APK
3+
# https://developer.android.com/topic/libraries/support-library/androidx-rn
4+
android.useAndroidX=true
54.9 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)