UNPKG

react-native-repro

Version:

Repro is a mobile analytics tool that lets you have much deeper understanding of mobile app users.

252 lines (200 loc) 6.71 kB
// Import example: // // import Repro from 'react-native-repro'; import { NativeModules, NativeEventEmitter } from 'react-native'; const safeString = (str) => { if (str && str === 'null') { return null }; return str; } class NewsFeedEntry { constructor(value) { this.id = value["newsfeed_id"]; this.deviceID = value["device_id"]; this.title = value["title"]; this.summary = value["summary"]; this.body = safeString(value["body"]); this.campaignType = value["campaign_type"]; this.shown = value["shown"]; this.read = value["read"]; this.deliveredAt = value["delivered_at"]; this.linkUrl = safeString(value["link_url"]); this.linkUrlString = safeString(value["link_url_string"]); this.imageUrl = safeString(value["image_url"]); this.imageUrlString = safeString(value["image_url_string"]); } toJson() { return { "newsfeed_id": this.id, "device_id": this.deviceID, "title": this.title, "summary": this.summary, "body": this.body, "campaign_type": this.campaignType, "shown": this.shown, "read": this.read, "delivered_at": this.deliveredAt, "link_url": this.linkUrl, "link_url_string": this.linkUrlString, "image_url": this.imageUrl, "image_url_string": this.imageUrlString }; } } const NewsFeedModule = { getNewsFeeds: (limit, callback) => { NativeModules.Repro.getNewsFeeds(limit, NativeModules.Repro.CAMPAIGN_TYPE_PUSH_NOTIFICATION, (error, items) => { if (!callback) return; if (!items) { callback(null, []); return; } const newsFeeds = []; for (const item of items) { newsFeeds.push(new NewsFeedEntry(item)); } callback(error, newsFeeds); }) }, getNewsFeedsWithCampaignType: (limit, campaignType, callback) => { NativeModules.Repro.getNewsFeeds(limit, campaignType, (error, items) => { if (!callback) return; if (!items) { callback(null, []); return; } const newsFeeds = []; for (const item of items) { newsFeeds.push(new NewsFeedEntry(item)); } callback(error, newsFeeds); }) }, getNewsFeedsFor: (limit, offsetID, callback) => { NativeModules.Repro.getNewsFeedsFor(limit, offsetID, NativeModules.Repro.CAMPAIGN_TYPE_PUSH_NOTIFICATION, (error, items) => { if (!callback) return; if (!items) { callback(null, []); return; } const newsFeeds = []; for (const item of items) { newsFeeds.push(new NewsFeedEntry(item)); } callback(error, newsFeeds); }) }, getNewsFeedsWithCampaignTypeFor: (limit, offsetID, campaignType, callback) => { NativeModules.Repro.getNewsFeedsFor(limit, offsetID, campaignType, (error, items) => { if (!callback) return; if (!items) { callback(null, []); return; } const newsFeeds = []; for (const item of items) { newsFeeds.push(new NewsFeedEntry(item)); } callback(error, newsFeeds); }) }, updateNewsFeeds: (newsFeeds, callback) => { const items = []; for(const entry of newsFeeds) { if (entry && entry instanceof NewsFeedEntry) { items.push(entry.toJson()); } } NativeModules.Repro.updateNewsFeeds(items, (error) => { callback(error); }); }, } class RemoteConfigValue { constructor(value) { this.value = value; } asString() { if (typeof this.value === 'string') { return this.value; } return null; } toString() { return this.value ?? ""; } } class RemoteConfigModule { static FETCH_STATUS = NativeModules.RemoteConfig.getConstants(); static fetch(timeout, callback) { NativeModules.RemoteConfig.fetch(timeout, callback); } static activateFetched() { NativeModules.RemoteConfig.activateFetched() } static setDefaultsFromDictionary(dict) { NativeModules.RemoteConfig.setDefaultsFromDictionary(dict); } static setDefaultsFromJsonString(str) { NativeModules.RemoteConfig.setDefaultsFromJsonString(str); } static getAllValues(callback) { NativeModules.RemoteConfig.getAllValues((values) => { if (!callback) return; var retValue = {} for(k in values) { retValue[k] = new RemoteConfigValue(values[k]); } callback(retValue); }); } static getAllValuesWithPrefix(prefix, callback) { NativeModules.RemoteConfig.getAllValuesWithPrefix(prefix, (values) => { if (!callback) return; var retValue = {} for(k in values) { retValue[k] = new RemoteConfigValue(values[k]); } callback(retValue); }); } static getValue(key, callback) { NativeModules.RemoteConfig.getValue(key, (value) => { if (!callback) return; callback(new RemoteConfigValue(value)) }) } static getLocalDefaultValue(key, callback) { NativeModules.RemoteConfig.getLocalDefaultValue(key, (value) => { if (!callback) return; callback(new RemoteConfigValue(value)) }) } static forceReset() { NativeModules.RemoteConfig.forceReset() } } class EventEmitterOpenUrl { static setCallback(callback) { if (!this.emitter) { this.emitter = new NativeEventEmitter(NativeModules.RPREventEmitterOpenUrl); } if (this.subscription) { this.subscription.remove() } if (callback) { this.subscription = this.emitter.addListener('__RPR__openUrlCallback', url => callback(url)); } } } const Repro = { ...NativeModules.Repro, ...NewsFeedModule, ...{ remoteConfig: RemoteConfigModule }, setOpenUrlCallback: EventEmitterOpenUrl.setCallback, handleWebViewUrl: (url) => { NativeModules.Repro._handleWebViewUrl(url); return url.startsWith('repro://'); } } export default Repro;