Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
Fix android
Generalize android permissions (might support most permissions in the future)
  • Loading branch information
JesperLekland committed Nov 16, 2017
1 parent b9761b7 commit 9e02e39
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 185 deletions.
57 changes: 0 additions & 57 deletions App.js

This file was deleted.

2 changes: 1 addition & 1 deletion __tests__/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'react-native';
import React from 'react';
import App from '../App';
import App from '../example';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
Expand Down
6 changes: 3 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
applicationId "com.rnpermissions"
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk {
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-sdk
android:minSdkVersion="16"
Expand Down
43 changes: 43 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'
import { Button, StyleSheet, Text, View } from 'react-native'
import { Location } from './src'

class LocationExample extends React.PureComponent {

state = {
isLoading: false,
}

render() {

const { test, locationPermission, requestLocationPermission } = this.props
const { isLoading } = this.state

return (
<View style={ styles.container }>
<Text>
{ 'havePermission: ' + locationPermission }
</Text>
<Text>
{ test }
</Text>
<Button
title={ locationPermission === Location.Permission.RESULT.GRANTED ? 'continue' : 'request permisssion' }
style={ { alignSelf: 'center', marginTop: 50 } }
onPress={ requestLocationPermission }
/>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-between',
alignItems: 'center',
marginVertical: 150,
}
})

export default Location.WithPermission(LocationExample)
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppRegistry } from 'react-native';
import App from './App';
import App from './example';

AppRegistry.registerComponent('rnPermissions', () => App);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"notification"
],
"dependencies": {
"hoist-non-react-statics": "^2.3.1",
"react": "16.0.0",
"react-native": "0.50.3"
},
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import LocationPermission, { WithLocationPermission } from './location'
import NotificationPermission, { WithNotificationPermission } from './notification'
import RESULT from './util/results'


export const Location = {
Permission: LocationPermission,
Permission: {
...LocationPermission,
RESULT,
},
WithPermission: WithLocationPermission,
}

export const Notification = {
Permission: NotificationPermission,
Permission: {
...NotificationPermission,
RESULT,
},
WithPermission: WithNotificationPermission,
}
6 changes: 4 additions & 2 deletions src/location/location-hoc/index.android.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react'
import { AppState } from 'react-native'
import { Permissions } from '../../common/location'
import Permissions from '../permission'
import hoistNonReactStatic from 'hoist-non-react-statics'
import { getDisplayName } from '../../util'
import RESULT from '../../util/results'

const LocationHoc = (WrappedComponent) => {
/**
Expand All @@ -11,7 +13,7 @@ const LocationHoc = (WrappedComponent) => {
class WithLocationPermission extends React.PureComponent {

state = {
permission: Permissions.RESULTS.UNDEFINED,
permission: RESULT.UNDEFINED,
}

async componentDidMount() {
Expand Down
11 changes: 6 additions & 5 deletions src/location/location-hoc/index.ios.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react'
import { AppState } from 'react-native'
import { Permissions } from '../../common/location'
import Permissions from '../permission'
import hoistNonReactStatic from 'hoist-non-react-statics'
import { getDisplayName } from '../../util'
import RESULT from '../../util/results'

const LocationHoc = (WrappedComponent) => {

Expand All @@ -15,7 +16,7 @@ const LocationHoc = (WrappedComponent) => {
resolveOngoingRequest

state = {
permission: Permissions.RESULTS.UNDEFINED,
permission: RESULT.UNDEFINED,
}

async componentDidMount() {
Expand All @@ -29,7 +30,7 @@ const LocationHoc = (WrappedComponent) => {
const permission = await Permissions.check()
this.setState({ permission })

if (permission === Permissions.RESULTS.GRANTED) {
if (permission === RESULT.GRANTED) {
this.resolveOngoingRequest && this.resolveOngoingRequest(permission)
}

Expand Down Expand Up @@ -58,9 +59,9 @@ const LocationHoc = (WrappedComponent) => {
return new Promise((resolve) => {
this.resolveOngoingRequest = resolve

if (permission === Permissions.RESULTS.UNDEFINED) {
if (permission === RESULT.UNDEFINED) {
Permissions.request()
} else if (permission === Permissions.RESULTS.DENIED) {
} else if (permission === RESULT.DENIED) {
Permissions.openSettings()
}
})
Expand Down
69 changes: 7 additions & 62 deletions src/location/permission.android.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,14 @@
import { PermissionsAndroid } from 'react-native'
import AsyncStorage, { ASYNC_STORAGE_KEYS } from '../../AsyncStorage'
import RNOpenSettings from 'react-native-open-settings'
import Constants from './constants'

let ongoingRequest
import PermissionsAndroid from '../permissions-android'

const LocationPermissions = {

...Constants,

async request() {

const permission = await
LocationPermissions.check()

if (permission === Constants.RESULTS.GRANTED) {
return
}

if (permission === Constants.RESULTS.DENIED) {
return RNOpenSettings.openSettings()
}

const response = await
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
)

await
AsyncStorage.setItem(
ASYNC_STORAGE_KEYS.LOCATION_PERMISSION_NEVER_ASK_AGAIN,
response === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN,
)

switch (response) {
case PermissionsAndroid.RESULTS.GRANTED:
return Constants.RESULTS.GRANTED
case PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN:
return Constants.RESULTS.DENIED
default:
return Constants.RESULTS.UNDEFINED
}
},

/**
* Returns a promise that resolve to either "true" or "false"
*/
async check() {
const haveClickedNeverAskAgain = await AsyncStorage.getItem(
ASYNC_STORAGE_KEYS.LOCATION_PERMISSION_NEVER_ASK_AGAIN,
false
)

const permission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)

if (permission) {
return Constants.RESULTS.GRANTED
}

if (haveClickedNeverAskAgain) {
return Constants.RESULTS.DENIED
}
async request() {
return PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
},

return Constants.RESULTS.UNDEFINED
},
async check() {
return PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
},
}

export default LocationPermissions
70 changes: 35 additions & 35 deletions src/location/permission.ios.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { Linking } from 'react-native'
import AsyncStorage, { ASYNC_STORAGE_KEYS } from '../../AsyncStorage'
import Constants from './constants'
import AsyncStorage, { ASYNC_STORAGE_KEYS } from '../util/async-storage'
import RESULT from '../util/results'

const LocationPermissions = {

...Constants,
RESULT,

async request() {
const permission = await LocationPermissions.check()
async request() {
const permission = await LocationPermissions.check()

if (permission === Constants.RESULTS.GRANTED) {
return
}
if (permission === RESULT.GRANTED) {
return
}

AsyncStorage.setItem(ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION, true)
AsyncStorage.setItem(ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION, true)

navigator.geolocation.requestAuthorization()
},
navigator.geolocation.requestAuthorization()
},

openSettings() {
Linking.openURL('app-settings:')
},
openSettings() {
Linking.openURL('app-settings:')
},

/**
* Returns a promise that resolve to either "true", "false" or
* PERMISSION_UNDEFINED if we haven't asked yet
*/
async check() {
const haveAsked = await AsyncStorage.getItem(
ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION,
false,
)
/**
* Returns a promise that resolve to either "true", "false" or
* PERMISSION_UNDEFINED if we haven't asked yet
*/
async check() {
const haveAsked = await AsyncStorage.getItem(
ASYNC_STORAGE_KEYS.HAVE_ASKED_FOR_LOCATION_PERMISSION,
false,
)

if (!haveAsked) {
return Constants.RESULTS.UNDEFINED
}
if (!haveAsked) {
return RESULT.UNDEFINED
}

const permission = await new Promise((resolve) => {
navigator.geolocation.getCurrentPosition(
position => resolve(true),
error => resolve(error.code !== error.PERMISSION_DENIED),
{ timeout: 100 }
)
})
const permission = await new Promise((resolve) => {
navigator.geolocation.getCurrentPosition(
position => resolve(true),
error => resolve(error.code !== error.PERMISSION_DENIED),
{ timeout: 100 }
)
})

return permission ? Constants.RESULTS.GRANTED : Constants.RESULTS.DENIED
}
return permission ? RESULT.GRANTED : RESULT.DENIED
}
}

export default LocationPermissions
Loading

0 comments on commit 9e02e39

Please sign in to comment.