mirror of
https://github.com/tapframe/NuvioStreaming.git
synced 2026-05-04 09:19:06 +00:00
SDUI prod url init
This commit is contained in:
parent
8b3a1b57bf
commit
0165b1f987
1 changed files with 5 additions and 34 deletions
|
|
@ -1,19 +1,13 @@
|
||||||
import { mmkvStorage } from './mmkvStorage';
|
import { mmkvStorage } from './mmkvStorage';
|
||||||
import { Platform } from 'react-native';
|
import { Platform } from 'react-native';
|
||||||
import Constants from 'expo-constants';
|
|
||||||
|
|
||||||
// --- Configuration ---
|
const DEV_URL = 'http://192.168.1.5:3000';
|
||||||
// Dev: Uses Mac's LAN IP for physical device testing (run: ipconfig getifaddr en0)
|
const PROD_URL = process.env.EXPO_PUBLIC_CAMPAIGN_API_URL || '';
|
||||||
// Prod: Uses EXPO_PUBLIC_CAMPAIGN_API_URL from .env
|
const CAMPAIGN_API_URL = __DEV__ ? DEV_URL : PROD_URL;
|
||||||
const CAMPAIGN_API_URL = __DEV__
|
|
||||||
? 'http://192.168.1.5:3000'
|
|
||||||
: Constants.expoConfig?.extra?.CAMPAIGN_API_URL || process.env.EXPO_PUBLIC_CAMPAIGN_API_URL || '';
|
|
||||||
|
|
||||||
// --- Types ---
|
|
||||||
|
|
||||||
export type CampaignAction = {
|
export type CampaignAction = {
|
||||||
type: 'link' | 'navigate' | 'dismiss';
|
type: 'link' | 'navigate' | 'dismiss';
|
||||||
value?: string; // URL or Route Name
|
value?: string;
|
||||||
label: string;
|
label: string;
|
||||||
style?: 'primary' | 'secondary' | 'outline';
|
style?: 'primary' | 'secondary' | 'outline';
|
||||||
};
|
};
|
||||||
|
|
@ -49,32 +43,25 @@ export type Campaign = {
|
||||||
rules: CampaignRules;
|
rules: CampaignRules;
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Service ---
|
|
||||||
|
|
||||||
class CampaignService {
|
class CampaignService {
|
||||||
private sessionImpressions: Set<string>;
|
private sessionImpressions: Set<string>;
|
||||||
private campaignQueue: Campaign[] = [];
|
private campaignQueue: Campaign[] = [];
|
||||||
private currentIndex: number = 0;
|
private currentIndex: number = 0;
|
||||||
private lastFetch: number = 0;
|
private lastFetch: number = 0;
|
||||||
private readonly CACHE_TTL = 5 * 60 * 1000; // 5 minutes
|
private readonly CACHE_TTL = 5 * 60 * 1000;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.sessionImpressions = new Set();
|
this.sessionImpressions = new Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches all active campaigns and returns the next valid one in the queue.
|
|
||||||
*/
|
|
||||||
async getActiveCampaign(): Promise<Campaign | null> {
|
async getActiveCampaign(): Promise<Campaign | null> {
|
||||||
try {
|
try {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
// If we have campaigns in queue and cache is still valid, get next valid one
|
|
||||||
if (this.campaignQueue.length > 0 && (now - this.lastFetch) < this.CACHE_TTL) {
|
if (this.campaignQueue.length > 0 && (now - this.lastFetch) < this.CACHE_TTL) {
|
||||||
return this.getNextValidCampaign();
|
return this.getNextValidCampaign();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all campaigns from server
|
|
||||||
const platform = Platform.OS;
|
const platform = Platform.OS;
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${CAMPAIGN_API_URL}/api/campaigns/queue?platform=${platform}`,
|
`${CAMPAIGN_API_URL}/api/campaigns/queue?platform=${platform}`,
|
||||||
|
|
@ -98,7 +85,6 @@ class CampaignService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve relative image URLs
|
|
||||||
campaigns.forEach((campaign: Campaign) => {
|
campaigns.forEach((campaign: Campaign) => {
|
||||||
if (campaign.content?.imageUrl && campaign.content.imageUrl.startsWith('/')) {
|
if (campaign.content?.imageUrl && campaign.content.imageUrl.startsWith('/')) {
|
||||||
campaign.content.imageUrl = `${CAMPAIGN_API_URL}${campaign.content.imageUrl}`;
|
campaign.content.imageUrl = `${CAMPAIGN_API_URL}${campaign.content.imageUrl}`;
|
||||||
|
|
@ -116,9 +102,6 @@ class CampaignService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the next valid campaign from the queue.
|
|
||||||
*/
|
|
||||||
private getNextValidCampaign(): Campaign | null {
|
private getNextValidCampaign(): Campaign | null {
|
||||||
while (this.currentIndex < this.campaignQueue.length) {
|
while (this.currentIndex < this.campaignQueue.length) {
|
||||||
const campaign = this.campaignQueue[this.currentIndex];
|
const campaign = this.campaignQueue[this.currentIndex];
|
||||||
|
|
@ -130,26 +113,18 @@ class CampaignService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Moves to the next campaign in the queue and returns it.
|
|
||||||
*/
|
|
||||||
getNextCampaign(): Campaign | null {
|
getNextCampaign(): Campaign | null {
|
||||||
this.currentIndex++;
|
this.currentIndex++;
|
||||||
return this.getNextValidCampaign();
|
return this.getNextValidCampaign();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates campaign against local-only rules.
|
|
||||||
*/
|
|
||||||
private isLocallyValid(campaign: Campaign): boolean {
|
private isLocallyValid(campaign: Campaign): boolean {
|
||||||
const { rules } = campaign;
|
const { rules } = campaign;
|
||||||
|
|
||||||
// Show once per user (persisted forever)
|
|
||||||
if (rules.showOncePerUser && this.hasSeenCampaign(campaign.id)) {
|
if (rules.showOncePerUser && this.hasSeenCampaign(campaign.id)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Impression limit check
|
|
||||||
if (rules.maxImpressions) {
|
if (rules.maxImpressions) {
|
||||||
const impressionCount = this.getImpressionCount(campaign.id);
|
const impressionCount = this.getImpressionCount(campaign.id);
|
||||||
if (impressionCount >= rules.maxImpressions) {
|
if (impressionCount >= rules.maxImpressions) {
|
||||||
|
|
@ -157,7 +132,6 @@ class CampaignService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Session check
|
|
||||||
if (rules.showOncePerSession && this.sessionImpressions.has(campaign.id)) {
|
if (rules.showOncePerSession && this.sessionImpressions.has(campaign.id)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -200,9 +174,6 @@ class CampaignService {
|
||||||
this.lastFetch = 0;
|
this.lastFetch = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns remaining campaigns in queue count.
|
|
||||||
*/
|
|
||||||
getRemainingCount(): number {
|
getRemainingCount(): number {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
for (let i = this.currentIndex; i < this.campaignQueue.length; i++) {
|
for (let i = this.currentIndex; i < this.campaignQueue.length; i++) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue