UNPKG

react-native-hud-hybrid

Version:
114 lines (113 loc) 2.92 kB
import { NativeModules, DeviceEventEmitter, Platform } from 'react-native'; const { HudHybrid } = NativeModules; 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 = HudHybrid.create(); if (Platform.OS === 'android') { DeviceEventEmitter.addListener('ON_HUD_DISMISS', this.handleDismission); } } static config(options = {}) { HudHybrid.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 => { HudHybrid.spinner(hudKey, text); }); return this; } text(text) { this.promise.then(hudKey => { HudHybrid.text(hudKey, text); }); return this; } info(text) { this.promise.then(hudKey => { HudHybrid.info(hudKey, text); }); return this; } done(text) { this.promise.then(hudKey => { HudHybrid.done(hudKey, text); }); return this; } error(text) { this.promise.then(hudKey => { HudHybrid.error(hudKey, text); }); return this; } hide() { this.promise.then(hudKey => { HudHybrid.hide(hudKey); }); } hideDelay(delayMs = 0) { this.promise.then(hudKey => { HudHybrid.hideDelay(hudKey, delayMs); }); } hideDelayDefault() { this.promise.then(hudKey => { HudHybrid.hideDelayDefault(hudKey); }); } }