Skip to content

Commit

Permalink
feat(rn): 新增deviceMotion,accelerometer两个API
Browse files Browse the repository at this point in the history
  • Loading branch information
Bless-L committed Jan 22, 2019
1 parent d690ac1 commit 6bef8f0
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 34 deletions.
3 changes: 2 additions & 1 deletion packages/taro-rn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
"babel-jest": "^23.0.1",
"babel-preset-env": "^1.7.0",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.6.0",
"enzyme-adapter-react-16": "^1.7.1",
"enzyme-to-json": "^3.3.4",
"jest": "^23.1.0",
"jest-environment-enzyme": "^7.0.0",
"jest-enzyme": "^7.0.0",
"jest-react-native": "^18.0.0",
"mock-socket": "^7.1.0",
"react": "^16.4.1",
"react-dom": "^16.7.0",
"react-native": "^0.55.4",
"react-test-renderer": "^16.5.2",
"redux": "^4.0.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/clipboard.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Clipboard } from 'react-native'
import MockClipboard from './__mock__/mockClipboard'
import Taro from '../index.js'
import clipboard from '../api/device/clipboard'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, clipboard)

describe('clipboard', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/network.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NetInfo } from 'react-native'
import MockNetInfo from './__mock__/mockNetwork'
import Taro from '../index.js'
import network from '../api/device/network'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, network)

describe('network', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/others.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Taro from '../index.js'
import others from '../api/others'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, others)

describe('base64 and arrayBuffer', () => {
describe('arrayBufferToBase64', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/phone.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Taro from '../index.js'
import phone from '../api/device/phone'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, phone)

describe('phone', () => {
describe('makePhoneCall', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/request.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Taro from '../index.js'
import request from '../api/request'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, request)

describe('request', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/storage.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AsyncStorage } from 'react-native'
import MockStorage from './__mock__/mockAsyncStorage'
import Taro from '../index.js'
import storage from '../api/storage'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, storage)

describe('storage', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/system.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Taro from '../index.js'
import system from '../api/system'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, system)

describe('system', () => {
describe('getSystemInfoSync', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/vibrate.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vibration from './__mock__/mockVibrate'
import Taro from '../index.js'
import vibrate from '../api/device/vibrate'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, vibrate)

describe('vibrate', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-rn/src/__tests__/websocket.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WebSocket, Server } from 'mock-socket'
import Taro from '../index.js'
import webSocket from '../api/webSocket'

Taro.initNativeApi(Taro)
const Taro = Object.assign({}, webSocket)

describe('websocket', () => {
beforeEach(() => {
Expand Down
45 changes: 45 additions & 0 deletions packages/taro-rn/src/api/device/accelerometer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Accelerometer } from 'expo'

const accCase = {}
const intervalMap = {
game: 29,
ui: 60,
normal: 200
}

function onAccelerometerChange (fnc) {
accCase.callback = fnc
}

function startAccelerometer (object) {
const { interval, success, fail, complete } = object
accCase.interval = interval
try {
accCase.listener = Accelerometer.addListener(accCase.callback)
success && success()
complete && complete()
} catch (error) {
fail && fail()
complete && complete()
}
Accelerometer.setUpdateInterval(intervalMap[interval])
}

function stopAccelerometer (object) {
const { success, fail, complete } = object
try {
accCase.listener.remove()
accCase.listener = null
success && success()
complete && complete()
} catch (error) {
fail && fail()
complete && complete()
}
}

export default {
onAccelerometerChange,
startAccelerometer,
stopAccelerometer
}
File renamed without changes.
48 changes: 48 additions & 0 deletions packages/taro-rn/src/api/device/deviceMotion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DangerZone } from 'expo'

const devMotionCase = {}
const intervalMap = {
game: 20,
ui: 60,
normal: 200
}

function onDeviceMotionChange (fnc) {
devMotionCase.callback = fnc
}

function startDeviceMotionListening (object = {}) {
const { interval = 'normal', success, fail, complete } = object
devMotionCase.interval = interval
try {
devMotionCase.listener = DangerZone.DeviceMotion.addListener((res) => {
const { rotation } = res
devMotionCase.callback && devMotionCase.callback(rotation)
})
success && success()
complete && complete()
} catch (error) {
fail && fail()
complete && complete()
}
DangerZone.DeviceMotion.setUpdateInterval(intervalMap[interval] || intervalMap.normal)
}

function stopDeviceMotionListening (object = {}) {
const { success, fail, complete } = object
try {
devMotionCase.listener.remove()
devMotionCase.listener = null
success && success()
complete && complete()
} catch (error) {
fail && fail()
complete && complete()
}
}

export default {
onDeviceMotionChange,
startDeviceMotionListening,
stopDeviceMotionListening
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 10 additions & 1 deletion packages/taro-rn/src/api/storage/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { AsyncStorage } from 'react-native'
import { generateUnSupportApi } from '../utils'

function generateUnSupportApi (errText, fnNames) {
const res = {}
fnNames.forEach((fnName) => {
res[fnName] = function () {
throw new Error(`${errText} ## ${JSON.stringify(arguments)}`)
}
})
return res
}

export function setStorage (opts = {}) {
const { key, data, success, fail, complete } = opts
Expand Down
10 changes: 0 additions & 10 deletions packages/taro-rn/src/api/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { Permissions } from 'expo'

export function generateUnSupportApi (errText, fnNames) {
const res = {}
fnNames.forEach((fnName) => {
res[fnName] = function () {
throw new Error(`${errText} ## ${JSON.stringify(arguments)}`)
}
})
return res
}

export async function askAsyncPermissions (PermissionsType) {
const { status } = await Permissions.askAsync(PermissionsType)
return status
Expand Down
12 changes: 8 additions & 4 deletions packages/taro-rn/src/native-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { onAndSyncApis, noPromiseApis, otherApis, initPxTransform } from '@taroj
import request from './api/request'
import storage from './api/storage'
import system from './api/system'
import network from './api/network'
import clipboard from './api/clipboard'
import phone from './api/phone'
import vibrate from './api/vibrate'
import network from './api/device/network'
import clipboard from './api/device/clipboard'
import phone from './api/device/phone'
import vibrate from './api/device/vibrate'
import accelerometer from './api/device/accelerometer'
import deviceMotion from './api/device/deviceMotion'
import others from './api/others'
import media from './api/media'
import file from './api/file'
Expand Down Expand Up @@ -48,6 +50,8 @@ export default function initNativeApi (taro) {
phone,
web,
vibrate,
accelerometer,
deviceMotion,
media,
file,
webSocket,
Expand Down

0 comments on commit 6bef8f0

Please sign in to comment.