Skip to content

Commit a185bf0

Browse files
committed
fix: reject promise if not player in the map
1 parent 6d9ebe6 commit a185bf0

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

android/src/main/java/com/audiowaveform/AudioWaveformModule.kt

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,10 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
159159
@ReactMethod
160160
fun startPlayer(obj: ReadableMap, promise: Promise) {
161161
val finishMode = obj.getInt(Constants.finishMode)
162-
val key = obj.getString(Constants.playerKey)
163162
val speed = obj.getDouble(Constants.speed)
164-
if (key != null) {
165-
audioPlayers[key]?.start(finishMode ?: 2, speed.toFloat(),promise)
166-
} else {
167-
promise.reject("startPlayer Error", "Player key can't be null")
168-
}
163+
164+
val player = getPlayerOrReject(obj, promise, "startPlayer Error");
165+
player?.start(finishMode ?: 2, speed.toFloat(),promise)
169166
}
170167

171168
@ReactMethod
@@ -182,25 +179,18 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
182179

183180
@ReactMethod
184181
fun pausePlayer(obj: ReadableMap, promise: Promise) {
185-
val key = obj.getString(Constants.playerKey)
186-
if (key != null) {
187-
audioPlayers[key]?.pause(promise)
188-
} else {
189-
promise.reject("pausePlayer Error", "Player key can't be null")
190-
}
182+
val player = getPlayerOrReject(obj, promise, "pausePlayer Error");
183+
player?.pause(promise);
191184
}
192185

193186
@ReactMethod
194187
fun seekToPlayer(obj: ReadableMap, promise: Promise) {
195188
try {
196189
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
197190
val progress = obj.getInt(Constants.progress)
198-
val key = obj.getString(Constants.playerKey)
199-
if (key != null) {
200-
audioPlayers[key]?.seekToPosition(progress.toLong(), promise)
201-
} else {
202-
promise.reject("seekTo Error", "Player key can't be null")
203-
}
191+
192+
val player = getPlayerOrReject(obj, promise, "seekTo Error");
193+
player?.seekToPosition(progress.toLong(), promise)
204194
} else {
205195
Log.e(
206196
Constants.LOG_TAG,
@@ -216,24 +206,18 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
216206
@ReactMethod
217207
fun setVolume(obj: ReadableMap, promise: Promise) {
218208
val volume = obj.getInt(Constants.volume)
219-
val key = obj.getString(Constants.playerKey)
220-
if (key != null) {
221-
audioPlayers[key]?.setVolume(volume.toFloat(), promise)
222-
} else {
223-
promise.reject("setVolume error", "Player key can't be null")
224-
}
209+
210+
val player = getPlayerOrReject(obj, promise, "setVolume Error");
211+
player?.setVolume(volume.toFloat(), promise)
225212
}
226213

227214
@ReactMethod
228215
fun getDuration(obj: ReadableMap, promise: Promise) {
229-
val key = obj.getString(Constants.playerKey)
230216
val duration = obj.getInt(Constants.durationType)
231217
val type = if (duration == 0) DurationType.Current else DurationType.Max
232-
if (key != null) {
233-
audioPlayers[key]?.getDuration(type, promise)
234-
} else {
235-
promise.reject("getDuration Error", "Player key can't be null")
236-
}
218+
219+
val player = getPlayerOrReject(obj, promise, "getDuration Error");
220+
player?.getDuration(type, promise)
237221
}
238222

239223
@ReactMethod
@@ -429,4 +413,18 @@ class AudioWaveformModule(context: ReactApplicationContext): ReactContextBaseJav
429413
handler.removeCallbacks(emitLiveRecordValue)
430414
}
431415

416+
private fun getPlayerOrReject(arguments: ReadableMap, promise: Promise, errorCode: String): AudioPlayer? {
417+
val key = getPlayerKeyOrReject(arguments, promise, errorCode)
418+
return audioPlayers[key] ?: run {
419+
promise.reject(errorCode, "$errorCode: Player not in the list")
420+
null
421+
}
422+
}
423+
424+
private fun getPlayerKeyOrReject(arguments: ReadableMap, promise: Promise, errorCode: String): String? {
425+
return arguments.getString(Constants.playerKey) ?: run {
426+
promise.reject(errorCode, "$errorCode: Player key can't be null")
427+
null
428+
}
429+
}
432430
}

0 commit comments

Comments
 (0)