Skip to content

Commit

Permalink
normalize error and warning message objects for iOS and Android (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmilham21 authored Oct 21, 2024
1 parent 6cb0e74 commit 83c1fd2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ private void setupPlayer(ReadableMap prop) {
// Legacy
// This isn't the ideal way to do this on Android. All drawables/colors/themes shoudld
// be targed using styling. See `https://docs.jwplayer.com/players/docs/android-styling-guide`
// for more information on how best to override the JWP styles using XML. If you are unsure of a
// for more information on how best to override the JWP styles using XML. If you are unsure of a
// color/drawable/theme, open an `Ask` issue.
if (mColors != null) {
if (mColors.hasKey("backgroundColor")) {
Expand Down Expand Up @@ -1331,7 +1331,7 @@ public void onAdWarning(AdWarningEvent adWarningEvent) {
event.putString("message", "onPlayerAdWarning");
event.putInt("code", adWarningEvent.getCode());
event.putInt("adErrorCode", adWarningEvent.getAdErrorCode());
event.putString("error", adWarningEvent.getMessage());
event.putString("warning", adWarningEvent.getMessage());
getReactContext().getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topPlayerAdWarning", event);
}

Expand Down Expand Up @@ -1485,6 +1485,7 @@ public void onError(ErrorEvent errorEvent) {
if (ex != null) {
event.putString("error", ex.toString());
event.putString("description", errorEvent.getMessage());
event.putInt("errorCode", errorEvent.getErrorCode());
}
getReactContext().getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topPlayerError", event);

Expand Down Expand Up @@ -1635,6 +1636,8 @@ public void onPlaybackRateChanged(PlaybackRateChangedEvent playbackRateChangedEv
public void onSetupError(SetupErrorEvent setupErrorEvent) {
WritableMap event = Arguments.createMap();
event.putString("message", "onSetupError");
event.putString("errorMessage", setupErrorEvent.getMessage());
event.putInt("errorCode", setupErrorEvent.getCode());
getReactContext().getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topSetupPlayerError", event);

updateWakeLock(false);
Expand Down
23 changes: 17 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,15 @@ declare module "@jwplayer/jwplayer-react-native" {
rate: number;
at: number;
}
interface PlayerSetupErrorProps {
errorMessage?: string;
errorCode?: number;
}
interface PlayerErrorProps {
error?: string;
errorCode?: number;
description?: string; // Android Only
}
interface TimeEventProps {
position: number;
duration: number;
Expand All @@ -504,15 +513,17 @@ declare module "@jwplayer/jwplayer-react-native" {
error: string;
}
interface PlayerWarningEventProps {
code: string;
warning: string;
code?: number;
warning?: string;
adErrorCode?: number; // Android only
}
interface AdEventProps {
client?: string;
reason?: string;
type: number;
}
type NativeError = (event: BaseEvent<PlayerErrorEventProps>) => void;
// Overloaded type to be used in multiple error events
type NativeError = (event: BaseEvent<PlayerErrorEventProps> | BaseEvent<PlayerSetupErrorProps> | BaseEvent<PlayerErrorProps>) => void;
type NativeWarning = (event: BaseEvent<PlayerWarningEventProps>) => void;
interface PropsType {
config: Config | JwConfig;
Expand All @@ -532,9 +543,9 @@ declare module "@jwplayer/jwplayer-react-native" {
onRateChanged?: (event?: BaseEvent<RateChangedEventProps>) => void;
onSetupPlayerError?: NativeError;
onPlayerError?: NativeError;
onPlayerWarning?: NativeWarning;
onPlayerAdError?: NativeError;
onPlayerAdWarning?: NativeWarning;
onPlayerWarning?: NativeWarning;
onPlayerAdError?: NativeError;
onPlayerAdWarning?: NativeWarning;
onAdEvent?: (event: BaseEvent<AdEventProps>) => void;
onAdTime?: (event: BaseEvent<TimeEventProps>) => void;
onBuffer?: () => void;
Expand Down
6 changes: 3 additions & 3 deletions ios/RNJWPlayer/RNJWPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,12 @@ class RNJWPlayerView : UIView, JWPlayerDelegate, JWPlayerStateDelegate, JWAdDele
}

func jwplayer(_ player:JWPlayer, failedWithError code:UInt, message:String) {
self.onPlayerError?(["error": message])
self.onPlayerError?(["error": message, "errorCode": code])
playerFailed = true
}

func jwplayer(_ player:JWPlayer, failedWithSetupError code:UInt, message:String) {
self.onSetupPlayerError?(["error": message])
self.onSetupPlayerError?(["errorMessage": message, "errorCode": code])
playerFailed = true
}

Expand All @@ -979,7 +979,7 @@ class RNJWPlayerView : UIView, JWPlayerDelegate, JWPlayerStateDelegate, JWAdDele


func jwplayer(_ player:JWPlayer, encounteredAdWarning code:UInt, message:String) {
self.onPlayerAdWarning?(["warning": message])
self.onPlayerAdWarning?(["warning": message, "code": code])
}


Expand Down
6 changes: 3 additions & 3 deletions ios/RNJWPlayer/RNJWPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class RNJWPlayerViewController : JWPlayerViewController, JWPlayerViewControllerD

override func jwplayer(_ player:JWPlayer, failedWithError code:UInt, message:String) {
super.jwplayer(player, failedWithError:code, message:message)
parentView?.onPlayerError?(["error": message])
parentView?.onPlayerError?(["error": message, "errorCode": code])
parentView?.playerFailed = true
}

override func jwplayer(_ player:JWPlayer, failedWithSetupError code:UInt, message:String) {
super.jwplayer(player, failedWithSetupError:code, message:message)
parentView?.onSetupPlayerError?(["error": message])
parentView?.onSetupPlayerError?(["errorMessage": message, "errorCode": code])
parentView?.playerFailed = true
}

Expand All @@ -78,7 +78,7 @@ class RNJWPlayerViewController : JWPlayerViewController, JWPlayerViewControllerD

override func jwplayer(_ player:JWPlayer, encounteredAdWarning code:UInt, message:String) {
super.jwplayer(player, encounteredAdWarning:code, message:message)
parentView?.onPlayerAdWarning?(["warning": message])
parentView?.onPlayerAdWarning?(["warning": message, "code": code])
}


Expand Down

0 comments on commit 83c1fd2

Please sign in to comment.