Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rt2zz committed Oct 16, 2015
0 parents commit feb100c
Show file tree
Hide file tree
Showing 14 changed files with 923 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
ios/RCTContacts.xcodeproj/xcuserdata
ios/RCTContacts.xcodeproj/project.xcworkspace

# Android/IJ
.idea/workspace.xml
.idea/libraries
.gradle
local.properties
*.iml
build

.npm-debug.log
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# React Native Contacts
Work in progress successor to react-native-addressbook

## API
`getContacts` (callback) - returns *all* contacts as an array of objects
`addContact` (contact, callback) - adds a contact to the AddressBook.
`updateContact` (contact, callback) - where contact is an object with a valid recordID
`deleteContact` (contact, callback) - where contact is an object with a valid recordID

####Permissions Methods (optional)
`checkPermission` (callback) - checks permission to use AddressBook.
`requestPermission` (callback) - request permission to use AddressBook.

## Usage Example
```js
var AddressBook = require('react-native-addressbook')

AddressBook.getContacts( (err, contacts) => {
if(err && err.type === 'permissionDenied'){
// x.x
}
else{
console.log(contacts)
}
})
```

## Example Contact Record
```js
{
recordID: 1,
lastName: "Jung",
firstName: "Carl",
middleName: "",
emailAddresses: [{
label: "work",
email: "[email protected]",
}],
phoneNumbers: [{
label: "mobile",
number: "(555) 555-5555",
}],
thumbnailPath: "",
}
```

## Adding Contacts
Currently all fields from the contact record except for thumbnailPath are supported for writing
```js
var newPerson = {
lastName: "Nietzsche",
firstName: "Friedrich",
emailAddresses: [{
label: "work",
email: "[email protected]",
}],
}

AddressBook.addContact(newPerson, (err) => { /*...*/ })
```

## Updating and Deleting Contacts
```js
//contrived example
AddressBook.getContacts( (err, contacts) => {
//update the first record
let someRecord = contacts[0]
someRecord.emailAddresses.push({
label: "junk",
email: "[email protected]",
})
AddressBook.updateContact(someRecord, (err) => { /*...*/ })

//delete the second record
AddressBook.deleteContact(contacts[1], (err) => { /*...*/ })
})
```
Update and delete reference contacts by their recordID (as returned by the OS in getContacts). Apple does not guarantee the recordID will not change, e.g. it may be reassigned during a phone migration. Consequently you should always grab a fresh contact list with `getContacts` before performing update and delete operations.

You can also delete a record using only it's recordID like follows: `AddressBook.deleteContact({recordID: 1}, (err) => {})}`

##Permissions
Permissions will automatically be checked and if needed requested upon calling getContacts. If you need more granular control you can using the checkPermission and requestPermission methods as follows:
```js
AddressBook.checkPermission( (err, permission) => {
// AddressBook.PERMISSION_AUTHORIZED || AddressBook.PERMISSION_UNDEFINED || AddressBook.PERMISSION_DENIED
if(permission === 'undefined'){
AddressBook.requestPermission( (err, permission) => {
// ...
})
}
if(permission === 'authorized'){
// yay!
}
if(permission === 'denied'){
// x.x
}
})
```

## Getting started
1. `npm install react-native-addressbook`
2. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
3. add `./node_modules/react-native-addressbook/RCTAddressBook.xcodeproj`
4. In the XCode project navigator, select your project, select the `Build Phases` tab and in the `Link Binary With Libraries` section add **libRCTAddressBook.a**

## Todo
- [x] `checkPermission` & `requestPermission`
- [x] `getContacts` (get all contacts)
- [x] `addContact`
- [x] Update and Delete methods
- [ ] `getContacts` options (a la camera roll's getPhotos)
- [x] Automatic permission check & request in `getContacts`
- [ ] Contact Groups support

## Credits
Thanks to @mattotodd whose [RCTAddressBook](https://github.com/mattotodd/react-native-addressbook-ios) this is largely derived from.
34 changes: 34 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
}

repositories {
mavenCentral()
}

dependencies {
compile 'com.facebook.react:react-native:0.12.+'
}
5 changes: 5 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rt2zz.reactnativecontacts" >
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.rt2zz.reactnativecontacts;

import android.provider.ContactsContract;
import android.content.ContentResolver;
import android.content.Context;

import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.widget.AdapterView;
import android.database.Cursor;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.util.Map;

public class ContactsManager extends ReactContextBaseJavaModule {

public ContactsManager(ReactApplicationContext reactContext) {
super(reactContext);
}

@ReactMethod
public void getContacts(Callback callback) {
callback.invoke("@TODO");
}

@Override
public String getName() {
return "ReactNativeContacts";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.rt2zz.reactnativecontacts;

import java.util.*;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

public class ReactNativeContacts implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();

modules.add(new ContactsManager(reactContext));
return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList();
}
}


// package com.rt2zz.reactnativecontacts;
//
// import com.facebook.react.bridge.NativeModule;
// import com.facebook.react.bridge.ReactApplicationContext;
// import com.facebook.react.bridge.Callback;
// import com.facebook.react.bridge.ReactContext;
// import com.facebook.react.bridge.ReactContextBaseJavaModule;
// import com.facebook.react.bridge.ReactMethod;
//
// import java.util.Map;
//
// public class ReactNativeContacts extends ReactContextBaseJavaModule {
//
// public ReactNativeContacts(ReactApplicationContext reactContext) {
// super(reactContext);
// }
//
// @ReactMethod
// public void getContacts(Callback callback, int duration) {
// callback.invoke("hi");
// }
//
// @Override
// public String getName() {
// return "ReactNativeContacts";
// }
// }

// package com.rt2zz.reactnativecontacts;
//
// import com.facebook.react.ReactPackage;
// import com.facebook.react.bridge.JavaScriptModule;
// import com.facebook.react.bridge.NativeModule;
// import com.facebook.react.bridge.Callback;
// import com.facebook.react.bridge.ReactApplicationContext;
// import com.facebook.react.uimanager.ViewManager;
//
// import java.util.ArrayList;
// import java.util.Arrays;
// import java.util.Collections;
// import java.util.List;
//
// public class ReactNativeContacts implements ReactPackage {
//
// public ReactNativeContacts() {
// }
//
// @Override
// public List<NativeModule> createNativeModules(
// ReactApplicationContext reactContext) {
// return new ArrayList<>();
// }
//
// @Override
// public List<Class<? extends JavaScriptModule>> createJSModules() {
// return Collections.emptyList();
// }
//
// @Override
// public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
// return Arrays.<ViewManager>asList(
// new IconManager(mAllIconFonts)
// );
// }
// }
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var ReactNative = require('react-native')
console.log('rnn', ReactNative.NativeModules)
module.exports = ReactNative.NativeModules.ReactNativeContacts
Loading

0 comments on commit feb100c

Please sign in to comment.