react-native-hud-hybrid
Version:
progress hud for react native
114 lines (113 loc) • 2.92 kB
JavaScript
import { NativeModules, DeviceEventEmitter, Platform } from "react-native";
const HUDModule = NativeModules.HUD;
export class LoadingHUD {
constructor() {
this.hud = null;
this.loadingCount = 0;
}
show(text) {
if (this.loadingCount <= 0) {
this.hud = new HUD();
this.hud.onDismiss = () => {
this.hideAll();
};
this.loadingCount = 0;
}
this.hud.spinner(text);
this.loadingCount++;
}
hide() {
this.loadingCount--;
if (this.loadingCount <= 0) {
this.hideAll();
}
}
hideAll() {
if (this.hud) {
this.hud.hide();
this.hud = null;
this.loadingCount = 0;
}
}
}
export default class HUD {
constructor() {
this.handleDismission = (event) => {
this.promise.then(hudKey => {
if (event.hudKey === hudKey) {
DeviceEventEmitter.removeListener("ON_HUD_DISMISS", this.handleDismission);
if (this.onDismiss) {
this.onDismiss();
}
}
});
};
this.promise = HUDModule.create();
if (Platform.OS === "android") {
DeviceEventEmitter.addListener("ON_HUD_DISMISS", this.handleDismission);
}
}
static config(options = {}) {
HUDModule.config(options);
}
static text(text) {
new HUD().text(text).hideDelayDefault();
}
static info(text) {
new HUD().info(text).hideDelayDefault();
}
static done(text) {
new HUD().done(text).hideDelayDefault();
}
static error(text) {
new HUD().error(text).hideDelayDefault();
}
static spinner(text) {
return new HUD().spinner(text);
}
spinner(text) {
this.promise.then(hudKey => {
HUDModule.spinner(hudKey, text);
});
return this;
}
text(text) {
this.promise.then(hudKey => {
HUDModule.text(hudKey, text);
});
return this;
}
info(text) {
this.promise.then(hudKey => {
HUDModule.info(hudKey, text);
});
return this;
}
done(text) {
this.promise.then(hudKey => {
HUDModule.done(hudKey, text);
});
return this;
}
error(text) {
this.promise.then(hudKey => {
HUDModule.error(hudKey, text);
});
return this;
}
hide() {
this.promise.then(hudKey => {
HUDModule.hide(hudKey);
});
}
hideDelay(delayMs = 0) {
this.promise.then(hudKey => {
HUDModule.hideDelay(hudKey, delayMs);
});
}
hideDelayDefault() {
this.promise.then(hudKey => {
HUDModule.hideDelayDefault(hudKey);
});
}
}