ordering-ui-react-native 0.21.26 → 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.26",
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,36 +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
- return
76
- }
77
- }, 2500)
80
+ playSoundNotification()
78
81
  }
79
82
 
80
83
  const handleEventNotification = async (evtType: number, value: any) => {
@@ -96,10 +99,14 @@ const NewOrderNotificationUI = (props: any) => {
96
99
  }
97
100
  }
98
101
  if (evtType === 3 || value?.author_id === user.id) return
99
- setTimeout(() => handlePlayNotificationSound({
102
+ handlePlayNotificationSound({
100
103
  evt: evtType,
101
- orderId: value?.driver ? value?.order_id : evtList[evtType].event === 'messages' ? value?.order?.id : value?.order_id ?? value?.id
102
- }), 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
+ })
103
110
  }
104
111
 
105
112
  useEffect(() => {
@@ -121,48 +128,44 @@ const NewOrderNotificationUI = (props: any) => {
121
128
  useEffect(() => {
122
129
  return () => {
123
130
  handleCloseEvents()
124
- notificationSound.stop()
125
- notificationSound.release()
126
131
  }
127
132
  }, [])
128
133
 
129
134
  return (
130
- <>
131
- <Modal
132
- animationType='slide'
133
- transparent={true}
134
- visible={!!currentEvent?.orderId}
135
- >
136
- <NotificationContainer>
137
- <View style={styles.modalView}>
138
- <TouchableOpacity
139
- style={styles.wrapperIcon}
140
- onPress={() => handleCloseEvents()}
141
- >
142
- <Icon name="x" size={30} />
143
- </TouchableOpacity>
144
- <OText
145
- size={18}
146
- color={theme.colors.textGray}
147
- weight={600}
148
- >
149
- {evtList[currentEvent?.evt]?.message}
150
- </OText>
151
- <OIcon
152
- src={theme.images.general.newOrder}
153
- width={250}
154
- height={200}
155
- />
156
- <OText
157
- color={theme.colors.textGray}
158
- mBottom={15}
159
- >
160
- {evtList[currentEvent?.evt]?.message2}
161
- </OText>
162
- </View>
163
- </NotificationContainer>
164
- </Modal>
165
- </>
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>
166
169
  )
167
170
  }
168
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) && (