From 319705c597d59119c43aa320d4b1a837935e71c9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Oct 2026 09:31:01 +0200 Subject: [PATCH] After adding the fonctionality for apk publish8 --- .../controllers/mobileAppReleaseController.ts | 12 ++ backend/src/routes/index.ts | 1 + .../src/services/mobileAppReleaseService.ts | 50 +++++++- docs/MOBILE_APP_RELEASES.md | 10 +- prediqo_mobile/README.md | 21 ++-- prediqo_mobile/TESTING.md | 3 +- prediqo_mobile/dart_defines.prod.example.json | 5 + .../lib/core/config/app_config.dart | 19 ++- .../src/features/landing/api/publicApkApi.ts | 28 +++++ .../landing/components/LandingHeader.tsx | 119 +++++++++++++++++- prediqo_web/src/shared/i18n/locales/en.ts | 3 + prediqo_web/src/shared/i18n/locales/fr.ts | 3 + 12 files changed, 254 insertions(+), 20 deletions(-) create mode 100644 prediqo_mobile/dart_defines.prod.example.json create mode 100644 prediqo_web/src/features/landing/api/publicApkApi.ts diff --git a/backend/src/controllers/mobileAppReleaseController.ts b/backend/src/controllers/mobileAppReleaseController.ts index 9327bc75..e9262b69 100644 --- a/backend/src/controllers/mobileAppReleaseController.ts +++ b/backend/src/controllers/mobileAppReleaseController.ts @@ -38,6 +38,18 @@ export async function checkAndroid(req: AuthRequest, res: Response): Promise { + try { + const channel = (String(req.query.channel || 'production') as 'production' | 'beta' | 'internal'); + const result = await mobileAppReleaseService.getLatestPublicAndroidApk(channel); + res.setHeader('Cache-Control', 'no-store'); + res.json(result); + } catch (e) { + sendError(res, e, 'Latest APK lookup failed'); + } +} + export async function downloadAndroid(req: AuthRequest, res: Response): Promise { try { const id = String(req.params.id || ''); diff --git a/backend/src/routes/index.ts b/backend/src/routes/index.ts index 0555f83f..e8b8cb71 100644 --- a/backend/src/routes/index.ts +++ b/backend/src/routes/index.ts @@ -193,6 +193,7 @@ router.get('/marketing/withdrawal-feed', marketingController.getWithdrawalFeed); router.get('/share-cards/verify/:token', shareCardController.verifyShareCard); /** Public Android in-app update check / APK download / telemetry (no auth). */ +router.get('/app-updates/android/latest', mobileAppReleaseController.latestAndroid); router.get( '/app-updates/android', validateQuery(mobileUpdateCheckQuerySchema), diff --git a/backend/src/services/mobileAppReleaseService.ts b/backend/src/services/mobileAppReleaseService.ts index 19cf55de..bce2fb5d 100644 --- a/backend/src/services/mobileAppReleaseService.ts +++ b/backend/src/services/mobileAppReleaseService.ts @@ -77,10 +77,56 @@ export function verifyDownloadToken(token: string, releaseId: string): boolean { } } +function publicDownloadPath(releaseId: string, token: string): string { + const api = config.apiPrefix.replace(/\/$/, ''); + return `${api}/app-updates/android/download/${releaseId}?token=${encodeURIComponent(token)}`; +} + function publicDownloadUrl(releaseId: string, token: string): string { const base = config.app.publicUrl.replace(/\/$/, ''); - const api = config.apiPrefix.replace(/\/$/, ''); - return `${base}${api}/app-updates/android/download/${releaseId}?token=${encodeURIComponent(token)}`; + return `${base}${publicDownloadPath(releaseId, token)}`; +} + +/** Latest published Android APK for the public website (ignores staged rollout). */ +export async function getLatestPublicAndroidApk(channel: MobileChannel = 'production'): Promise<{ + available: boolean; + versionName: string | null; + versionCode: number | null; + fileSizeBytes: number | null; + sha256: string | null; + /** Absolute URL using APP_PUBLIC_URL (may differ from the browser origin). */ + downloadUrl: string | null; + /** Same-origin path — prefer this on the landing page. */ + downloadPath: string | null; + publishedAt: string | null; + packageName: string | null; +}> { + const latest = await repo.findLatestPublished('android', channel); + if (!latest || !latest.storageKey || !latest.sha256) { + return { + available: false, + versionName: null, + versionCode: null, + fileSizeBytes: null, + sha256: null, + downloadUrl: null, + downloadPath: null, + publishedAt: null, + packageName: null, + }; + } + const { token } = createDownloadToken(latest.id); + return { + available: true, + versionName: latest.versionName, + versionCode: latest.versionCode, + fileSizeBytes: latest.fileSizeBytes, + sha256: latest.sha256, + downloadUrl: publicDownloadUrl(latest.id, token), + downloadPath: publicDownloadPath(latest.id, token), + publishedAt: latest.publishedAt?.toISOString() ?? null, + packageName: latest.packageName, + }; } export async function getSettings() { diff --git a/docs/MOBILE_APP_RELEASES.md b/docs/MOBILE_APP_RELEASES.md index 79d0a55d..eb1ec82c 100644 --- a/docs/MOBILE_APP_RELEASES.md +++ b/docs/MOBILE_APP_RELEASES.md @@ -30,7 +30,15 @@ Store the `.jks` **offline** (password manager / encrypted backup). Never commit 1. Copy `prediqo_mobile/android/key.properties.example` → `key.properties` 2. Fill `storePassword`, `keyPassword`, `keyAlias`, `storeFile` -3. Build: `flutter build apk --release` +3. Build release APK (production API URL is the default in release mode): + +```bash +flutter build apk --release +# or pin explicitly: +flutter build apk --release --dart-define-from-file=dart_defines.prod.example.json +``` + +The public website header exposes an Android download icon that calls `GET /api/v1/app-updates/android/latest` (only works once a release is **published**). ### Fingerprint for the backend diff --git a/prediqo_mobile/README.md b/prediqo_mobile/README.md index 48ca0d38..f94abf90 100644 --- a/prediqo_mobile/README.md +++ b/prediqo_mobile/README.md @@ -2,24 +2,29 @@ ## API URL (compile-time) -`API_BASE_URL` is set via `--dart-define` / `--dart-define-from-file`. -Without it, the app defaults to `http://10.0.2.2:9080` (Android emulator only). +`API_BASE_URL` is set via `--dart-define` / `--dart-define-from-file`. + +| Build | Default without dart-define | +|-------|-----------------------------| +| **Release** (`flutter build apk --release`) | `http://prediqo.redirectme.net:9080` | +| **Debug / emulator** | `http://10.0.2.2:9080` | + +### Local device / custom host 1. Copy `dart_defines.dev.example.json` → `dart_defines.dev.json` -2. Set your API base, e.g. `http://prediqo.redirectme.net:9080` - -### Run +2. Set your API base, e.g. `http://prediqo.redirectme.net:9080` or `http://:9080` ```bash flutter run --dart-define-from-file=dart_defines.dev.json ``` -### Release APK +### Release APK (production) ```bash -flutter build apk --release --dart-define-from-file=dart_defines.dev.json +# Uses built-in production default, or pin explicitly: +flutter build apk --release --dart-define-from-file=dart_defines.prod.example.json ``` Output: `build/app/outputs/flutter-apk/app-release.apk` -A plain `flutter build apk --release` **will not** include your VPS URL. +An older APK built with only the emulator default (`10.0.2.2`) **cannot** reach the VPS — rebuild and reinstall. diff --git a/prediqo_mobile/TESTING.md b/prediqo_mobile/TESTING.md index 139641ec..c4d7dbb2 100644 --- a/prediqo_mobile/TESTING.md +++ b/prediqo_mobile/TESTING.md @@ -96,7 +96,8 @@ Par défaut (sans dart-define) : `http://10.0.2.2:9080`. ### APK release -`flutter build apk --release` **sans** `--dart-define-from-file` compile avec `http://10.0.2.2:9080` → connexion impossible sur un vrai téléphone. +`flutter build apk --release` utilise par défaut `http://prediqo.redirectme.net:9080`. +Les APKs plus anciens compilés avec `10.0.2.2` ne peuvent pas joindre le VPS — il faut reconstruire. ```bash cd prediqo_mobile diff --git a/prediqo_mobile/dart_defines.prod.example.json b/prediqo_mobile/dart_defines.prod.example.json new file mode 100644 index 00000000..9055531b --- /dev/null +++ b/prediqo_mobile/dart_defines.prod.example.json @@ -0,0 +1,5 @@ +{ + "API_BASE_URL": "http://prediqo.redirectme.net:9080", + "APP_ENV": "production", + "WS_ENABLED": "true" +} diff --git a/prediqo_mobile/lib/core/config/app_config.dart b/prediqo_mobile/lib/core/config/app_config.dart index 85df5fa2..a1834ad5 100644 --- a/prediqo_mobile/lib/core/config/app_config.dart +++ b/prediqo_mobile/lib/core/config/app_config.dart @@ -73,19 +73,26 @@ class AppConfig { ).toString(); } + /// Production API reached by release APKs on real devices (override with dart-define). + static const String defaultProductionApiBaseUrl = + 'http://prediqo.redirectme.net:9080'; + + /// Emulator-only loopback to the host machine's Docker nginx. + static const String defaultEmulatorApiBaseUrl = 'http://10.0.2.2:9080'; + static AppConfig fromEnvironment() { - // Docker local exposes the API via nginx on 9080 (port 4000 is internal only). - // Emulator → 10.0.2.2 ; physical device → --dart-define=API_BASE_URL=http://:9080 - const apiBaseUrl = String.fromEnvironment( - 'API_BASE_URL', - defaultValue: 'http://10.0.2.2:9080', - ); + // Release builds without --dart-define must still hit the public VPS. + // Debug/emulator keeps 10.0.2.2 unless API_BASE_URL is set. + const apiBaseUrlDefine = String.fromEnvironment('API_BASE_URL', defaultValue: ''); const wsUrl = String.fromEnvironment('WS_URL', defaultValue: ''); // Enabled by default so local Docker (nginx /ws/) works without extra flags. const wsEnabled = bool.fromEnvironment('WS_ENABLED', defaultValue: true); const env = String.fromEnvironment('APP_ENV', defaultValue: ''); final isProd = env == 'production' || const bool.fromEnvironment('dart.vm.product', defaultValue: false); + final apiBaseUrl = apiBaseUrlDefine.isNotEmpty + ? apiBaseUrlDefine + : (isProd ? defaultProductionApiBaseUrl : defaultEmulatorApiBaseUrl); const latestAppVersion = String.fromEnvironment('LATEST_APP_VERSION', defaultValue: ''); const appStoreUrl = String.fromEnvironment('APP_STORE_URL', defaultValue: ''); diff --git a/prediqo_web/src/features/landing/api/publicApkApi.ts b/prediqo_web/src/features/landing/api/publicApkApi.ts new file mode 100644 index 00000000..2973cddf --- /dev/null +++ b/prediqo_web/src/features/landing/api/publicApkApi.ts @@ -0,0 +1,28 @@ +/** + * Public Android APK metadata for the landing-page download button. + */ + +export type LatestAndroidApk = { + available: boolean; + versionName: string | null; + versionCode: number | null; + fileSizeBytes: number | null; + sha256: string | null; + downloadUrl: string | null; + downloadPath: string | null; + publishedAt: string | null; + packageName: string | null; +}; + +/** Fetches the latest published production APK (no auth). */ +export async function fetchLatestAndroidApk(): Promise { + const res = await fetch('/api/v1/app-updates/android/latest', { + method: 'GET', + headers: { Accept: 'application/json' }, + cache: 'no-store', + }); + if (!res.ok) { + throw new Error(`Latest APK request failed (${res.status})`); + } + return (await res.json()) as LatestAndroidApk; +} diff --git a/prediqo_web/src/features/landing/components/LandingHeader.tsx b/prediqo_web/src/features/landing/components/LandingHeader.tsx index b0f4790c..0c81fea1 100644 --- a/prediqo_web/src/features/landing/components/LandingHeader.tsx +++ b/prediqo_web/src/features/landing/components/LandingHeader.tsx @@ -1,11 +1,12 @@ /** - * Fixed public nav — brand, section anchors, language, login + signup once. + * Fixed public nav — brand, section anchors, language, APK download, login + signup once. */ -import { useState, type CSSProperties } from 'react'; +import { useCallback, useState, type CSSProperties } from 'react'; import { Link as RouterLink } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { LanguageSwitcher } from '../../../shared/i18n/LanguageSwitcher'; import { PLATFORM_WORDMARK } from '../../../shared/brand/platformName'; +import { fetchLatestAndroidApk } from '../api/publicApkApi'; type NavItem = { id: string; label: string }; @@ -17,6 +18,33 @@ type LandingHeaderProps = { export function LandingHeader({ navItems, onNavigateSection }: LandingHeaderProps) { const { t } = useTranslation(); const [open, setOpen] = useState(false); + const [apkBusy, setApkBusy] = useState(false); + const [apkHint, setApkHint] = useState(null); + + const onDownloadApk = useCallback(async () => { + if (apkBusy) return; + setApkBusy(true); + setApkHint(null); + try { + const latest = await fetchLatestAndroidApk(); + if (!latest.available || (!latest.downloadPath && !latest.downloadUrl)) { + setApkHint(t('landing.nav.apkUnavailable')); + return; + } + const href = latest.downloadPath || latest.downloadUrl!; + const a = document.createElement('a'); + a.href = href; + a.rel = 'noopener'; + a.download = latest.versionName ? `tloto-${latest.versionName}.apk` : 'tloto.apk'; + document.body.appendChild(a); + a.click(); + a.remove(); + } catch { + setApkHint(t('landing.nav.apkError')); + } finally { + setApkBusy(false); + } + }, [apkBusy, t]); return (
+ {t('landing.nav.login')} @@ -110,6 +148,26 @@ export function LandingHeader({ navItems, onNavigateSection }: LandingHeaderProp + {apkHint && ( +
+ {apkHint} +
+ )} + {open && (
{t('landing.nav.faq')} + setOpen(false)} @@ -177,6 +255,43 @@ export function LandingHeader({ navItems, onNavigateSection }: LandingHeaderProp ); } +function ApkDownloadIcon({ busy }: { busy: boolean }) { + return ( + + + + + ); +} + +const apkIconBtnStyle: CSSProperties = { + appearance: 'none', + width: 40, + height: 40, + borderRadius: 999, + border: '1px solid rgba(255,255,255,0.16)', + background: 'rgba(255,255,255,0.04)', + color: 'rgba(238,243,251,0.92)', + display: 'inline-flex', + alignItems: 'center', + justifyContent: 'center', + cursor: 'pointer', + flexShrink: 0, + padding: 0, +}; + const navLinkStyle: CSSProperties = { appearance: 'none', border: 0, diff --git a/prediqo_web/src/shared/i18n/locales/en.ts b/prediqo_web/src/shared/i18n/locales/en.ts index 793c2689..fa667cd9 100644 --- a/prediqo_web/src/shared/i18n/locales/en.ts +++ b/prediqo_web/src/shared/i18n/locales/en.ts @@ -83,6 +83,9 @@ export const en = { live: 'Live', why: 'Why Tloto', faq: 'FAQ', + downloadApk: 'Download Android app', + apkUnavailable: 'No Android app is published yet. Check back soon.', + apkError: 'Could not start the Android download. Please try again.', }, hero: { tagline: 'Premium prediction platform', diff --git a/prediqo_web/src/shared/i18n/locales/fr.ts b/prediqo_web/src/shared/i18n/locales/fr.ts index cd40fc2f..a8c1f9cb 100644 --- a/prediqo_web/src/shared/i18n/locales/fr.ts +++ b/prediqo_web/src/shared/i18n/locales/fr.ts @@ -83,6 +83,9 @@ export const fr = { live: 'En direct', why: 'Pourquoi Tloto', faq: 'FAQ', + downloadApk: 'Télécharger l’app Android', + apkUnavailable: 'Aucune app Android n’est encore publiée. Réessayez bientôt.', + apkError: 'Impossible de lancer le téléchargement Android. Réessayez.', }, hero: { tagline: 'Plateforme de prédiction premium',