Skip to content

Commit

Permalink
Merge pull request #1646 from didi/add-api-proxy-v1
Browse files Browse the repository at this point in the history
Add api proxy v1
  • Loading branch information
hiyuki authored Oct 11, 2024
2 parents 16ed4fe + 31a4464 commit e875e7f
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 12 deletions.
6 changes: 5 additions & 1 deletion packages/api-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"expo-clipboard": "~6.0.3",
"react-native-device-info": "^10.13.2",
"react-native-get-location": "^4.0.1",
"react-native-safe-area-context": "^4.10.1"
"react-native-safe-area-context": "^4.10.1",
"react-native-haptic-feedback": "^2.3.3"
},
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
Expand All @@ -74,6 +75,9 @@
},
"expo-brightness": {
"optional": true
},
"react-native-haptic-feedback": {
"optional": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index'
62 changes: 62 additions & 0 deletions packages/api-proxy/src/platform/api/keyboard/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Keyboard } from 'react-native'
import { successHandle, failHandle } from '../../../common/js'
let hasListener = false
const callbacks = []

function keyboardShowListener (e) {
const endCoordinates = e.endCoordinates || {}
// eslint-disable-next-line node/no-callback-literal
callbacks.forEach(cb => cb({
height: endCoordinates.height
}))
}
function keyboardHideListener (e) {
const endCoordinates = e.endCoordinates || {}
let height
if (__mpx_mode__ === 'ios') {
height = 0
} else {
height = endCoordinates.height
}
// eslint-disable-next-line node/no-callback-literal
callbacks.forEach(cb => cb({
height
}))
}
const onKeyboardHeightChange = function (callback) {
if (!hasListener) {
Keyboard.addListener('keyboardDidShow', keyboardShowListener)
Keyboard.addListener('keyboardDidHide', keyboardHideListener)
hasListener = true
}
callbacks.push(callback)
}
const offKeyboardHeightChange = function (callback) {
const index = callbacks.indexOf(callback)
if (index > -1) {
callbacks.splice(index, 1)
}
if (callbacks.length === 0) {
Keyboard.removeAllListeners('keyboardDidShow')
Keyboard.removeAllListeners('keyboardDidHide')
hasListener = false
}
}

const hideKeyboard = function (options = {}) {
const { success, fail, complete } = options
try {
Keyboard.dismiss()
const result = { errMsg: 'hideKeyboard:ok' }
successHandle(result, success, complete)
} catch (err) {
const result = { errMsg: err.message }
failHandle(result, fail, complete)
}
}

export {
onKeyboardHeightChange,
offKeyboardHeightChange,
hideKeyboard
}
13 changes: 13 additions & 0 deletions packages/api-proxy/src/platform/api/keyboard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ENV_OBJ, envError } from '../../../common/js'

const onKeyboardHeightChange = ENV_OBJ.onKeyboardHeightChange || envError('onKeyboardHeightChange')

const offKeyboardHeightChange = ENV_OBJ.offKeyboardHeightChange || envError('offKeyboardHeightChange')

const hideKeyboard = ENV_OBJ.hideKeyboard || envError('hideKeyboard')

export {
onKeyboardHeightChange,
offKeyboardHeightChange,
hideKeyboard
}
8 changes: 6 additions & 2 deletions packages/api-proxy/src/platform/api/modal/rnModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ const showModal = function (options = {}) {
const modalWidth = width - 60
const styles = StyleSheet.create({
modalTask: {
width,
height,
left: 0,
right: 0,
top: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center',
display: 'flex',
backgroundColor: 'rgba(0,0,0,0.6)',
position: 'absolute'
},
Expand Down Expand Up @@ -144,6 +147,7 @@ const showModal = function (options = {}) {
height: 40,
backgroundColor: '#eeeeee',
width: '100%',
keyboardType: 'default',
paddingLeft: 10,
paddingRight: 10
}} onChangeText={text => onChangeText(text)} defaultValue={content}></TextInput></View>)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { successHandle, failHandle, getFocusedNavigation } from '../../../common/js'

import { nextTick } from '../next-tick'
function setNavigationBarTitle (options = {}) {
const { title = '', success, fail, complete } = options
const navigation = getFocusedNavigation()
if (!(navigation && navigation.setOptions)) {
failHandle({ errMsg: 'setNavigationBarTitle:fail' }, fail, complete)
} else {
navigation.setOptions({ headerTitle: title })
successHandle({ errMsg: 'setNavigationBarTitle:ok' }, success, complete)
nextTick(() => {
navigation.setOptions({ headerTitle: title })
successHandle({ errMsg: 'setNavigationBarTitle:ok' }, success, complete)
})
}
}

Expand All @@ -17,13 +19,15 @@ function setNavigationBarColor (options = {}) {
if (!(navigation && navigation.setOptions)) {
failHandle({ errMsg: 'setNavigationBarColor:fail' }, fail, complete)
} else {
navigation.setOptions({
headerStyle: {
backgroundColor: backgroundColor
},
headerTintColor: frontColor
nextTick(() => {
navigation.setOptions({
headerStyle: {
backgroundColor: backgroundColor
},
headerTintColor: frontColor
})
successHandle({ errMsg: 'setNavigationBarColor:ok' }, success, complete)
})
successHandle({ errMsg: 'setNavigationBarColor:ok' }, success, complete)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index.ios'
39 changes: 39 additions & 0 deletions packages/api-proxy/src/platform/api/vibrate/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ReactNativeHapticFeedback from 'react-native-haptic-feedback'
import { Vibration } from 'react-native'
import { successHandle, failHandle } from '../../../common/js'

const getType = function (type = 'light') {
return 'impact' + type[0].toUpperCase() + type.substr(1)
}
const vibrateShort = function (options = {}) {
const { type = 'light', success, fail, complete } = options
try {
ReactNativeHapticFeedback.trigger(getType(type), {
ignoreAndroidSystemSettings: true,
enableVibrateFallback: true
})
const result = {
errMsg: 'vibrateShort:ok'
}
successHandle(result, success, complete)
} catch (e) {
const result = {
errMsg: 'vibrateShort:fail'
}
failHandle(result, fail, complete)
}
}

const vibrateLong = function (options = {}) {
const { success, complete } = options
Vibration.vibrate(400)
const result = {
errMsg: 'vibrateLong:ok'
}
successHandle(result, success, complete)
}

export {
vibrateShort,
vibrateLong
}
10 changes: 10 additions & 0 deletions packages/api-proxy/src/platform/api/vibrate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ENV_OBJ, envError } from '../../../common/js'

const vibrateShort = ENV_OBJ.vibrateShort || envError('vibrateShort')

const vibrateLong = ENV_OBJ.vibrateLong || envError('vibrateLong')

export {
vibrateShort,
vibrateLong
}
6 changes: 6 additions & 0 deletions packages/api-proxy/src/platform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ export * from './api/location'

// getExtConfig, getExtConfigSync
export * from './api/ext'

// vibrateShort, vibrateLong
export * from './api/vibrate'

// onKeyboardHeightChange, offKeyboardHeightChange, hideKeyboard
export * from './api/keyboard'

0 comments on commit e875e7f

Please sign in to comment.