ordering-ui-react-native 0.21.27 → 0.21.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ordering-ui-react-native",
3
- "version": "0.21.27",
3
+ "version": "0.21.28",
4
4
  "description": "Reusable components made in react native",
5
5
  "main": "src/index.tsx",
6
6
  "author": "ordering.inc",
@@ -1,22 +1,33 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import {
3
+ Modal,
4
+ View,
5
+ StyleSheet,
6
+ Dimensions,
7
+ TouchableOpacity
8
+ } from 'react-native';
9
+ import { useTheme } from 'styled-components/native'
1
10
  import moment from 'moment'
2
- import { NewOrderNotification as NewOrderNotificationController, useApi, useEvent, useLanguage, useSession } from 'ordering-components/native'
3
- import React, { useEffect, useState } from 'react'
4
- import { Dimensions, Modal, StyleSheet, TouchableOpacity, View } from 'react-native'
5
- import Sound from 'react-native-sound'
6
11
  import Icon from 'react-native-vector-icons/Feather'
7
- import { useTheme } from 'styled-components/native'
8
12
 
9
- import { useLocation } from '../../hooks/useLocation'
13
+ import {
14
+ NewOrderNotification as NewOrderNotificationController,
15
+ useApi,
16
+ useEvent,
17
+ useLanguage,
18
+ useSession
19
+ } from 'ordering-components/native'
20
+
21
+ import TrackPlayer from 'react-native-track-player';
22
+
10
23
  import { OIcon, OText } from '../shared'
11
24
  import { NotificationContainer } from './styles'
25
+ import { useLocation } from '../../hooks/useLocation'
12
26
 
13
- Sound.setCategory('Playback', true)
14
- Sound.setMode('Default')
15
-
27
+ const SOUND_LOOP_AMOUNT = 3
28
+ const DURATION_BETWEEN_LOOP = 2000 // ms
16
29
  const windowWidth = Dimensions.get('screen').width
17
30
 
18
- const SOUND_LOOP = 3
19
-
20
31
  const NewOrderNotificationUI = (props: any) => {
21
32
  const { isBusinessApp } = props
22
33
  const [events] = useEvent()
@@ -24,7 +35,7 @@ const NewOrderNotificationUI = (props: any) => {
24
35
  const [, t] = useLanguage()
25
36
  const [{ user, token }] = useSession()
26
37
  const [ordering] = useApi()
27
- const { getCurrentLocation } = useLocation();
38
+ const { getCurrentLocation } = useLocation()
28
39
  const [currentEvent, setCurrentEvent] = useState<any>(null)
29
40
 
30
41
  const evtList: any = {
@@ -45,38 +56,28 @@ const NewOrderNotificationUI = (props: any) => {
45
56
  },
46
57
  }
47
58
 
48
- const soundSrc = 'https://d33aymufw4jvwf.cloudfront.net/notification.mp3' ?? theme.sounds.notification
49
-
50
- const notificationSound = new Sound(soundSrc, '', () => { });
51
-
52
- let _timeout: any = null
53
- let times = 0
54
-
55
59
  const handleCloseEvents = () => {
56
60
  setCurrentEvent(null)
57
- clearInterval(_timeout)
61
+ }
62
+
63
+ const playSoundNotification = () => {
64
+ let count = 0;
65
+ const intervalId = setInterval(() => {
66
+ TrackPlayer.seekTo(0)
67
+ TrackPlayer.play()
68
+ setTimeout(() => {
69
+ TrackPlayer.pause()
70
+ count++
71
+ if (count === SOUND_LOOP_AMOUNT) {
72
+ clearInterval(intervalId);
73
+ }
74
+ }, 500) // sound duration divide by 2
75
+ }, DURATION_BETWEEN_LOOP) // notification duration
58
76
  }
59
77
 
60
78
  const handlePlayNotificationSound = (eventObj: any = null) => {
61
79
  setCurrentEvent(eventObj)
62
- if (times > 0) {
63
- if (times === 3) {
64
- times = 0
65
- return
66
- }
67
- return
68
- }
69
- _timeout = setInterval(() => {
70
- if (times < SOUND_LOOP) {
71
- notificationSound.play()
72
- times++
73
- } else {
74
- clearInterval(_timeout)
75
- notificationSound.stop()
76
- notificationSound.release()
77
- return
78
- }
79
- }, 2500)
80
+ playSoundNotification()
80
81
  }
81
82
 
82
83
  const handleEventNotification = async (evtType: number, value: any) => {
@@ -98,10 +99,14 @@ const NewOrderNotificationUI = (props: any) => {
98
99
  }
99
100
  }
100
101
  if (evtType === 3 || value?.author_id === user.id) return
101
- setTimeout(() => handlePlayNotificationSound({
102
+ handlePlayNotificationSound({
102
103
  evt: evtType,
103
- orderId: value?.driver ? value?.order_id : evtList[evtType].event === 'messages' ? value?.order?.id : value?.order_id ?? value?.id
104
- }), 1000)
104
+ orderId: value?.driver
105
+ ? value?.order_id
106
+ : evtList[evtType].event === 'messages'
107
+ ? value?.order?.id
108
+ : value?.order_id ?? value?.id
109
+ })
105
110
  }
106
111
 
107
112
  useEffect(() => {
@@ -123,48 +128,44 @@ const NewOrderNotificationUI = (props: any) => {
123
128
  useEffect(() => {
124
129
  return () => {
125
130
  handleCloseEvents()
126
- notificationSound.stop()
127
- notificationSound.release()
128
131
  }
129
132
  }, [])
130
133
 
131
134
  return (
132
- <>
133
- <Modal
134
- animationType='slide'
135
- transparent={true}
136
- visible={!!currentEvent?.orderId}
137
- >
138
- <NotificationContainer>
139
- <View style={styles.modalView}>
140
- <TouchableOpacity
141
- style={styles.wrapperIcon}
142
- onPress={() => handleCloseEvents()}
143
- >
144
- <Icon name="x" size={30} />
145
- </TouchableOpacity>
146
- <OText
147
- size={18}
148
- color={theme.colors.textGray}
149
- weight={600}
150
- >
151
- {evtList[currentEvent?.evt]?.message}
152
- </OText>
153
- <OIcon
154
- src={theme.images.general.newOrder}
155
- width={250}
156
- height={200}
157
- />
158
- <OText
159
- color={theme.colors.textGray}
160
- mBottom={15}
161
- >
162
- {evtList[currentEvent?.evt]?.message2}
163
- </OText>
164
- </View>
165
- </NotificationContainer>
166
- </Modal>
167
- </>
135
+ <Modal
136
+ animationType='slide'
137
+ transparent={true}
138
+ visible={!!currentEvent?.orderId}
139
+ >
140
+ <NotificationContainer>
141
+ <View style={styles.modalView}>
142
+ <TouchableOpacity
143
+ style={styles.wrapperIcon}
144
+ onPress={() => handleCloseEvents()}
145
+ >
146
+ <Icon name="x" size={30} />
147
+ </TouchableOpacity>
148
+ <OText
149
+ size={18}
150
+ color={theme.colors.textGray}
151
+ weight={600}
152
+ >
153
+ {evtList[currentEvent?.evt]?.message}
154
+ </OText>
155
+ <OIcon
156
+ src={theme.images.general.newOrder}
157
+ width={250}
158
+ height={200}
159
+ />
160
+ <OText
161
+ color={theme.colors.textGray}
162
+ mBottom={15}
163
+ >
164
+ {evtList[currentEvent?.evt]?.message2}
165
+ </OText>
166
+ </View>
167
+ </NotificationContainer>
168
+ </Modal>
168
169
  )
169
170
  }
170
171
 
@@ -6,6 +6,12 @@ import { Placeholder, PlaceholderLine, Fade } from 'rn-placeholder';
6
6
  import FeatherIcon from 'react-native-vector-icons/Feather';
7
7
  import FontistoIcon from 'react-native-vector-icons/Fontisto'
8
8
  import AntDesignIcon from 'react-native-vector-icons/AntDesign'
9
+ import TrackPlayer, {
10
+ RepeatMode,
11
+ Capability,
12
+ AppKilledPlaybackBehavior
13
+ } from 'react-native-track-player';
14
+
9
15
  import { useTheme } from 'styled-components/native';
10
16
  import { DeviceOrientationMethods } from '../../../../../src/hooks/DeviceOrientation'
11
17
  import { NotificationSetting } from '../../../../../src/components/NotificationSetting'
@@ -106,6 +112,79 @@ const OrdersOptionUI = (props: OrdersOptionParams) => {
106
112
  const HEIGHT_SCREEN = orientationState?.dimensions?.height
107
113
  const IS_PORTRAIT = orientationState.orientation === PORTRAIT
108
114
 
115
+ const URL_SOUND = 'https://d33aymufw4jvwf.cloudfront.net/notification.mp3' ?? theme.sounds.notification
116
+
117
+ const setupPlayer = async (options: any) => {
118
+ const setup = async () => {
119
+ try {
120
+ await TrackPlayer.setupPlayer(options);
121
+ } catch (error) {
122
+ return (error as Error & { code?: string }).code;
123
+ }
124
+ }
125
+ while ((await setup()) === 'android_cannot_setup_player_in_background') {
126
+ await new Promise<void>((resolve) => setTimeout(resolve, 1));
127
+ }
128
+ };
129
+
130
+ const SetupService = async () => {
131
+ await setupPlayer({ autoHandleInterruptions: true });
132
+ await TrackPlayer.updateOptions({
133
+ android: {
134
+ appKilledPlaybackBehavior:
135
+ AppKilledPlaybackBehavior.StopPlaybackAndRemoveNotification,
136
+ },
137
+ capabilities: [
138
+ Capability.Play,
139
+ Capability.Pause,
140
+ Capability.SkipToNext,
141
+ Capability.SkipToPrevious,
142
+ Capability.SeekTo,
143
+ ],
144
+ compactCapabilities: [
145
+ Capability.Play,
146
+ Capability.Pause,
147
+ Capability.SkipToNext,
148
+ ],
149
+ progressUpdateEventInterval: 2,
150
+ });
151
+ await TrackPlayer.setRepeatMode(RepeatMode.Track);
152
+ };
153
+
154
+ const QueueInitialTracksService = async () => {
155
+ await TrackPlayer.add([
156
+ {
157
+ id: 'notification',
158
+ url: URL_SOUND,
159
+ title: 'notification'
160
+ },
161
+ ]);
162
+ }
163
+
164
+ function useSetupPlayer() {
165
+ const [playerReady, setPlayerReady] = useState<boolean>(false)
166
+
167
+ useEffect(() => {
168
+ let unmounted = false;
169
+ (async () => {
170
+ await SetupService()
171
+ if (unmounted) return
172
+ setPlayerReady(true)
173
+ const queue = await TrackPlayer.getQueue()
174
+ if (unmounted) return
175
+ if (queue.length <= 0) {
176
+ await QueueInitialTracksService()
177
+ }
178
+ })();
179
+ return () => {
180
+ unmounted = true;
181
+ };
182
+ }, []);
183
+ return playerReady;
184
+ }
185
+
186
+ const isPlayerReady = useSetupPlayer()
187
+
109
188
  const preorderTypeList = [
110
189
  { key: null, name: t('SLA', 'SLA\'s') },
111
190
  { key: 'in_time', name: t('OK', 'Ok') },
@@ -701,7 +780,10 @@ const OrdersOptionUI = (props: OrdersOptionParams) => {
701
780
  </View>
702
781
 
703
782
  {isBusinessApp && (
704
- <NewOrderNotification isBusinessApp={isBusinessApp} />
783
+ <NewOrderNotification
784
+ isBusinessApp={isBusinessApp}
785
+ isPlayerReady={isPlayerReady}
786
+ />
705
787
  )}
706
788
 
707
789
  {(openSearchModal || openSLASettingModal) && (