UNPKG

react-native-repro

Version:

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

149 lines (122 loc) 4.37 kB
import { TurboModuleRegistry } from 'react-native'; const NativeReproReact = TurboModuleRegistry.get('ReproReactBridgeModule'); const isNewArchitecture = global.nativeFabricUIManager != null; const constants = (isNewArchitecture && NativeReproReact) ? NativeReproReact.getConstants() : {}; const newsFeedConstants = new Proxy(NativeReproReact || {}, { get: function(target, prop) { if (constants.hasOwnProperty(prop)) { return constants[prop]; } return target[prop]; } }); 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) => { NativeReproReact.oldArchGetNewsFeeds(limit, newsFeedConstants.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) => { NativeReproReact.oldArchGetNewsFeeds(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) => { NativeReproReact.oldArchGetNewsFeedsFor(limit, offsetID, newsFeedConstants.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) => { NativeReproReact.oldArchGetNewsFeedsFor(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()); } } NativeReproReact.oldArchUpdateNewsFeeds(items, (error) => { callback(error); }); }, } export const newsFeedManager = NewsFeedModule;