Compare commits

...

77 Commits

Author SHA1 Message Date
Claude 69e09c2708 Fix API paths and add missing settings
- Fix PHP syntax error in auto-screenshot.php (cron comment)
- Change fetch paths from absolute /api/ to relative api/
- Add missing settings to settings.json (weekly_timelapse_enabled, auto_screenshot, sharing)
2026-01-30 20:55:52 +00:00
Claude 6a24b564a4 Add 4 new features: timelapse toggle, auto-screenshot, video search, email sharing
- Weekly timelapse button now toggleable via settings (zoom_timelapse.weekly_timelapse_enabled)
- Auto-screenshot API for cron-based gallery capture every 10 min
- Date/time video search with filter UI in archive section
- Email sharing with share links and PHPMailer integration
- New API endpoints: auto-screenshot.php, gallery.php, video-search.php, share.php
- New settings: auto_screenshot.*, sharing.* for feature configuration
2026-01-30 18:33:54 +00:00
Claude 16673b91d3 Add Billing/Stripe integration and Landing Page (Phase 4+5)
Phase 4 - Billing/Stripe:
- src/Billing/StripeService.php: Stripe API wrapper
  - Checkout session creation
  - Customer management
  - Billing portal sessions
  - Webhook signature verification
- src/Billing/SubscriptionManager.php: Subscription logic
  - Plan management (CRUD)
  - Trial handling
  - Feature access checks
  - Invoice storage
- src/Billing/WebhookHandler.php: Stripe webhook processing
  - checkout.session.completed
  - customer.subscription.* events
  - invoice.paid / payment_failed
- api/stripe-webhook.php: Webhook endpoint
- dashboard/billing.php: Billing dashboard
  - Current plan display with features
  - Plan comparison grid
  - Upgrade buttons with Stripe Checkout
  - Invoice history

Phase 5 - Landing Page:
- landing/index.php: Marketing homepage
  - Hero section with CTA
  - Feature grid (6 features)
  - How it works (3 steps)
  - Final CTA section
  - Responsive design
- landing/pricing.php: Pricing page
  - Dynamic plan cards from DB
  - Monthly/yearly toggle (2 months free)
  - Feature comparison
  - FAQ accordion

All features respect saas_features toggles in settings.
2026-01-23 19:16:18 +00:00
Claude ac77e27089 Add automatic onboarding system (Phase 3)
Onboarding Wizard:
- register.php: User registration with validation
- verify.php: Email verification (with demo mode)
- stream.php: Stream URL configuration & validation
- branding.php: Quick branding setup with live preview
- complete.php: Success page with confetti animation

Backend Classes (src/Onboarding/):
- OnboardingManager.php: Orchestrates the onboarding flow
  - Registration with automatic subdomain generation
  - Email verification tokens
  - Step tracking in tenant_onboarding table
- StreamValidator.php: Validates stream URLs
  - HLS (.m3u8) validation with playlist check
  - RTMP format validation
  - iframe/embed URL detection (YouTube, Vimeo, Twitch)
  - Generic HTTP reachability check

Features:
- 4-step wizard with progress indicator
- Stream type auto-detection
- Live branding preview
- Skip options for optional steps
- Trial period display
2026-01-23 18:41:53 +00:00
Claude 7bd62b3527 Add tenant dashboard (Phase 2)
Dashboard Features:
- Login page with session-based auth
- Overview page with live stats (viewers, stream status)
- Stream settings (URL, type configuration)
- Branding editor (colors, texts, custom CSS)
- Settings page (weather, content toggles, UI options)

New Files:
- dashboard/index.php: Main overview with stats
- dashboard/login.php: Authentication page
- dashboard/logout.php: Session cleanup
- dashboard/stream.php: Stream configuration
- dashboard/branding.php: Visual customization
- dashboard/settings.php: Feature toggles
- dashboard/templates/layout.php: Shared layout
- dashboard/api/stats.php: Stats API endpoint
- dashboard/assets/dashboard.css: Modern dashboard UI
- dashboard/assets/dashboard.js: Client-side functionality
- src/Auth/AuthManager.php: Secure auth with Argon2, remember-me

Auth Features:
- Secure password hashing (Argon2ID)
- Remember-me tokens
- Role-based access (super_admin, tenant_admin, tenant_user)
- Legacy fallback for existing admin credentials
2026-01-23 17:09:38 +00:00
Claude 402604b4cc Add Multi-Tenant SaaS foundation for customer management
Phase 1 implementation includes:

Database:
- schema.sql with tables for tenants, domains, settings, branding,
  streams, users, subscriptions, plans, invoices, viewer_stats

Core Classes (src/Core/):
- Database.php: PDO wrapper with singleton pattern
- TenantResolver.php: Domain-to-tenant resolution with fallback

Tenant Classes (src/Tenant/):
- TenantManager.php: CRUD operations for tenants
- TenantSettingsManager.php: DB-based settings per tenant

Configuration:
- config.example.php: Template for database/stripe/mail config
- bootstrap.php: Initializes multi-tenant environment
- .gitignore: Excludes config.php and cache files

Integration:
- SettingsManager.php: Added saas_features toggles (all off by default)
- index.php: Uses getSiteConfig() from bootstrap when multi-tenant enabled,
  falls back to legacy hardcoded domains when disabled

All SaaS features are disabled by default (saas_features.multi_tenant_enabled = false),
ensuring zero breaking changes to existing installations.
2026-01-23 16:40:42 +00:00
Claude 328b5b5b15 Fix cached weather error being returned
The weather cache was returning old errors (like "API Key fehlt"
from the previous OpenWeatherMap implementation) even after
switching to Open-Meteo which doesn't require an API key.

Changes:
- Delete cache file if it contains an error
- Prevent errors from being cached in the first place
2026-01-22 21:40:09 +00:00
admin 6472bbf162 Merge pull request #40 from metacube2/claude/mail-finetuning-webapp-01BsRXQNeVFrCBky8aw35YHw
asdf
2026-01-22 22:30:35 +01:00
admin c66a5b9f64 Merge pull request #39 from metacube2/claude/fix-layout-centering-cdX7d
Add error handling to weather widget
2026-01-22 22:29:27 +01:00
Claude 7e468d51ca Add error handling to weather widget
- Wrap getCurrentWeather() in try-catch block
- Prevents white screen if weather API fails
- Shows error message in widget instead
- Add test-weather.php for debugging
2026-01-22 21:06:50 +00:00
admin 7b3f99e837 Merge pull request #38 from metacube2/claude/fix-layout-centering-cdX7d
Switch weather API from OpenWeatherMap to Open-Meteo
2026-01-22 22:00:25 +01:00
Claude 20704b3cd8 Switch weather API from OpenWeatherMap to Open-Meteo
🎉 **100% KOSTENLOS - KEIN API KEY MEHR NÖTIG!**

**Warum Open-Meteo?**
-  Komplett kostenlos ohne Limits
-  KEIN API Key erforderlich
-  Keine Kreditkarte, keine Anmeldung
-  Open Source & Non-Profit
-  Sehr präzise Daten für die Schweiz
-  Schnell & zuverlässig

**Technische Änderungen:**

**WeatherManager.php:**
- API URL auf api.open-meteo.com gewechselt
- WMO Weather Codes (0-99) implementiert
- Deutsche Wetterbeschreibungen für alle Codes
- Temperatur, Wind, Luftdruck, Feuchtigkeit, Niederschlag
- Icon-Mapping von WMO Codes zu Emojis
- Timezone: Europe/Zurich

**index.php Admin-Panel:**
- API Key Feld entfernt (nicht mehr nötig!)
- Überschrift: "Open-Meteo - 100% kostenlos!"
- Event Listener für API Key entfernt
- Funktioniert sofort ohne Setup!

**API Endpoint:**
https://api.open-meteo.com/v1/forecast
- Keine Authentication nötig
- Parameter: lat, lon, current weather vars
- Response: JSON mit aktuellen Wetterdaten

**Unterstützte Wetter-Codes:**
- 0: Klar
- 1-3: Bewölkt (verschiedene Grade)
- 45-48: Nebel
- 51-65: Regen (Niesel bis stark)
- 71-77: Schnee
- 80-86: Schauer
- 95-99: Gewitter & Hagel

**Resultat:**
Widget funktioniert SOFORT ohne jegliches Setup!
Einfach aktivieren und fertig! 🌤️
2026-01-22 20:59:07 +00:00
admin 6fa64baa35 Merge pull request #37 from metacube2/claude/fix-layout-centering-cdX7d
Fix undefined siteConfig variable in admin panel
2026-01-22 21:52:18 +01:00
Claude 6bca898488 Fix undefined siteConfig variable in admin panel
- Add global $siteConfig declaration in displayAdminContent()
- Fixes warning in SEO settings placeholder
2026-01-22 20:50:53 +00:00
admin 8bd90629f9 Merge pull request #36 from metacube2/claude/fix-layout-centering-cdX7d
Claude/fix layout centering cd x7d
2026-01-22 21:39:19 +01:00
Claude 7eda2fbbe8 Add weather widget with OpenWeatherMap integration
Implementiert vollständiges Wetter-Widget oberhalb des Webcam-Videos:

**Features:**
🌤️ **Wetter-Anzeige:**
- Temperatur (°C)
- Windgeschwindigkeit & Richtung (km/h)
- Luftdruck (hPa)
- Luftfeuchtigkeit (%)
- Wetterbeschreibung mit Emoji-Icons
- Niederschlag (Regen/Schnee) wenn vorhanden

⚙️ **Technisch:**
- OpenWeatherMap API Integration
- 5 Minuten Cache (konfigurierbar)
- Auto-Update alle X Minuten (Frontend)
- WeatherManager Klasse für Backend
- Schönes Gradient-Design mit Hover-Effekten
- Responsive für Mobile

🎛️ **Admin-Settings:**
- Wetter-Widget ein/aus
- API Key Eingabefeld + Registrierungs-Link
- Standort konfigurierbar (Stadt,Land)
- GPS-Koordinaten (Lat/Lon)
- Update-Intervall (5-60 Minuten)
- Einheiten (Metrisch/Imperial)

**Dateien:**
- WeatherManager.php: Neue Klasse für API-Calls & Caching
- SettingsManager.php: Weather Settings Defaults & Helper
- index.php: Widget HTML, CSS, JavaScript Auto-Update
- settings.json: Weather Defaults initialisiert

**Koordinaten Oberdürnten:**
Lat: 47.2833, Lon: 8.7167

**Setup für User:**
1. Gratis Account auf openweathermap.org erstellen
2. API Key im Admin-Panel einfügen
3. Fertig! Widget zeigt Live-Wetter an
2026-01-22 18:50:16 +00:00
Claude 5b8200a4ff Add cache clear utility script
- Add clear-cache.php for clearing PHP OPcache and realpath cache
- Useful for debugging and ensuring latest code changes are visible
2026-01-22 17:57:17 +00:00
Claude ac6632e24f Initialize settings.json with all new defaults
- Add all new settings groups (ui_display, zoom_timelapse, content, technical, theme, seo)
- Set default values for all new admin settings
- Enable all features by default for smooth transition
2026-01-22 17:54:57 +00:00
Claude 0ce527c69e Add comprehensive admin settings control panel
Erweitere Admin-Bereich um umfangreiche Settings-Steuerung:

**Punkt 2 - UI Anzeige:**
- Empfehlungs-Banner ein/aus
- QR-Code Section ein/aus
- Social Media Links ein/aus
- Patrouille Suisse Section ein/aus

**Punkt 3 - Zoom & Timelapse:**
- Zoom-Controls anzeigen/verstecken
- Max Zoom-Level konfigurierbar (1.5x - 4.0x)
- Timelapse Rückwärts-Modus ein/aus

**Punkt 5 - Content Management:**
- Gästebuch aktivieren/deaktivieren
- Galerie aktivieren/deaktivieren
- KI-Events anzeigen/verstecken
- Max Gästebuch-Einträge limit

**Punkt 6 - Technische Settings:**
- Viewer Update-Intervall konfigurierbar
- Session Timeout einstellbar

**Punkt 7 - Theme & Design:**
- Standard-Theme auswählbar (Legacy/Alpine/Modern)
- Theme-Switcher anzeigen/verstecken (war auskommentiert)

**Punkt 8 - SEO & Meta:**
- Custom Title konfigurierbar
- Meta Description editierbar
- Meta Keywords verwaltbar

**Technische Änderungen:**
- SettingsManager.php: Neue Defaults und Helper-Methoden
- Admin-Panel: Neue Settings-Gruppen mit Toggle-Switches
- JavaScript: Live-Apply ohne Reload für alle Settings
- HTML: Sections mit PHP-Settings verbunden
- CSS: Admin-Panel Styling hinzugefügt
- TimelapseController: reverseEnabled Setting integriert
2026-01-22 17:47:56 +00:00
admin 8f46ffb695 Merge pull request #35 from metacube2/claude/fix-layout-centering-cdX7d
Center header layout and adjust navigation alignment
2026-01-22 18:26:26 +01:00
Claude 36558e97cb Fix layout centering in aurora-livecam
- Center header container and navigation
- Add padding-right to header to prevent overlap with language selector buttons
- Change nav ul justify-content from space-around to center for better alignment
2026-01-22 17:25:58 +00:00
admin 1cf30a0c8b Update fmt.Println message from 'Hello' to 'Goodbye' 2026-01-22 18:17:50 +01:00
admin ff4bda1e53 Merge pull request #34 from metacube2/codex/create-power-bi-training-manual-for-hr
Add HR Power BI consumer and trainer manuals
2026-01-20 13:11:19 +01:00
admin af87ef329b Merge branch 'main' into codex/create-power-bi-training-manual-for-hr 2026-01-20 13:11:10 +01:00
admin 9c1d820876 Add HR Power BI consumer and trainer manuals 2026-01-20 13:08:29 +01:00
admin cbe215cfc2 Merge pull request #33 from metacube2/codex/create-power-bi-training-manual-for-hr
Add HTML Power BI HR manual with embedded SVG diagram
2026-01-20 09:52:43 +01:00
admin 12d64bf009 Expand HTML Power BI HR manual 2026-01-20 09:02:03 +01:00
admin 0871068ff8 Add HTML Power BI HR manual 2026-01-20 08:08:49 +01:00
admin 3d7cee81da Merge pull request #32 from metacube2/codex/create-power-bi-training-manual-for-hr
Provide Power BI HR training manual (Markdown; omit Word binary)
2026-01-20 07:49:14 +01:00
admin 0aa5a62754 Provide Power BI HR training manual 2026-01-20 07:48:59 +01:00
admin f487713a92 Add Power BI training manual for HR
Created a comprehensive training manual for Power BI tailored for HR staff, including step-by-step instructions, target audience details, data sources, KPIs, and troubleshooting tips.
2026-01-20 07:41:01 +01:00
admin 5eb27d1c28 Merge pull request #31 from metacube2/codex/check-translations-in-index2.php
Complete missing translations for language switcher and page content (IT/FR/ZH)
2026-01-19 22:25:50 +01:00
admin 4060115749 Complete missing translations in index2 2026-01-19 22:25:15 +01:00
admin b98a6761c2 Merge pull request #30 from metacube2/claude/seecam-domain-config-njiwU
Add missing translations for Patrouille Suisse and Blog sections
2026-01-18 17:14:56 +01:00
Claude 6a8e879898 Add missing translations for Patrouille Suisse and Blog sections
- About section: additional paragraph translated
- Admin section: headings translated
- Patrouille Suisse: complete section with all texts, headings and lists
- Blog section: subtitle, all 3 articles with titles, dates and content
- Footer blog text translated

All elements now have data-en and data-de attributes for language switching
2026-01-18 13:23:33 +00:00
admin b9407a9f13 Merge pull request #28 from metacube2/claude/seecam-domain-config-njiwU
Add domain-based site configuration for seecam.ch
2026-01-18 10:08:40 +01:00
Claude 53ae1ba76f Add domain-based site configuration for seecam.ch
- Detect domain (seecam.ch vs aurora-weather-livecam.com) via PHP
- Load different logo (seecam.jpg vs logo.png) based on domain
- Dynamic meta tags (title, og:*, twitter:*, canonical URL)
- Dynamic Schema.org JSON-LD (WebSite, LocalBusiness, VideoObject)
- Dynamic site texts (welcome, about, blog title, footer, copyright)
- QR-code URL adapts to current domain
- Email sender name remains unchanged (Aurora Livecam)
2026-01-18 09:06:02 +00:00
admin 9a418aa213 Merge pull request #27 from metacube2/claude/add-video-download-sppLI
Claude/add video download spp li
2026-01-17 18:18:37 +01:00
Claude 9a88b5cad2 Add SEO-optimized index2.php with comprehensive improvements
- Add SEO meta tags (title, description, keywords, robots)
- Add Schema.org JSON-LD structured data (WebSite, LocalBusiness, VideoObject)
- Add local SEO geo tags for Zürich Oberland/Dürnten
- Improve Open Graph and Twitter Card meta tags
- Add optimized alt-texts for images
- Add social media links to footer (Instagram, Facebook, YouTube, TikTok)
- Add Patrouille Suisse dedicated section with training info
- Add Blog section with weather and webcam articles
- Add accessibility CSS for screen-readers
2026-01-17 17:13:46 +00:00
admin 3e6a584f4f Merge pull request #25 from metacube2/claude/add-macos-support-uYBaj
Add macOS Catalyst support for RollkofferSimulator
2026-01-15 15:24:17 +01:00
admin 6ad6167c52 Merge pull request #24 from metacube2/main-aurora
Main aurora
2026-01-15 15:10:46 +01:00
Claude f9b84e4d3c Add separate zoom wrapper layers for all video modes
- Added live-video-wrapper around webcam-player
- Added timelapse-wrapper inside timelapse-viewer
- Added daily-video-wrapper inside daily-video-player
- Zoom now applies to wrapper divs, not video elements directly
- Pan works by dragging inside video container when zoomed
- Double-click to reset zoom
- Cursor changes to grab when zoomed > 1x
- Touch support for mobile pan
2026-01-15 14:09:42 +00:00
admin 2e6fd332ac Merge pull request #23 from metacube2/claude/add-video-download-sppLI
Claude/add video download spp li
2026-01-15 14:54:33 +01:00
Claude b5376f46e5 Merge zoom & pan improvements from main-aurora 2026-01-15 13:51:42 +00:00
Claude 98f1fcae14 Add zoom & pan for all video modes
- Zoom now works for livestream, timelapse and daily videos
- Added pan function: drag to move zoomed area with mouse
- Added touch support for mobile pan
- Added +/- zoom buttons and reset button
- Reduced max zoom from 100x to 4x
- Dynamically detects active video element
- Pan limits based on zoom level
- Cursor changes to grab when zoomed
2026-01-15 13:51:25 +00:00
Claude 14c064de64 Fix video overlay issue - add z-index and disable zoom temporarily
- Added z-index: 10 to webcam-player (higher than overlays)
- Added z-index: 5 and display: none to timelapse/daily-video overlays
- Disabled zoom script temporarily for testing
2026-01-13 09:39:44 +00:00
Claude 313c2108a9 Fix live video player - add native HLS support and debugging
- Added display:block and background:#000 to video element
- Added native HLS detection for Safari (canPlayType check)
- Added console.log debugging for video loading
- Added error event handlers for better debugging
- Added fallback for browsers without HLS support
- Improved HLS.js error handling
2026-01-13 09:34:42 +00:00
admin c12ac16557 Merge pull request #22 from metacube2/claude/add-video-download-sppLI
Claude/add video download spp li
2026-01-13 10:27:17 +01:00
Claude b686d4506c Update aurora-livecam with new design and fixed zoom
- New design from main-aurora branch
- Fixed zoom: maxZoom reduced from 100 to 4
- Added zoom +/- buttons
- Added zoom slider with step 0.5
- Fixed video-zoom.js to not apply transform at 1x
2026-01-13 09:26:08 +00:00
admin c38bd130e5 Merge pull request #21 from metacube2/codex/fix-saving-changes-in-audora-project-tgc6un
Add 1–100x zoom controls for all video modes and wire video-zoom.js
2026-01-12 12:59:49 +01:00
admin de343364ad Merge branch 'main-aurora' into codex/fix-saving-changes-in-audora-project-tgc6un 2026-01-12 12:58:08 +01:00
admin e8385adb87 Add zoom controls for video modes 2026-01-12 12:40:22 +01:00
admin f7843e5e35 Merge pull request #20 from metacube2/codex/fix-saving-changes-in-audora-project-4bnojk
Add design switcher (Alpine/Modern) with Swiss-cross and sun overlay; harden settings save
2026-01-12 12:27:48 +01:00
admin 3a78d09399 Add design switcher themes 2026-01-12 12:27:32 +01:00
admin 28d2032f23 Merge pull request #19 from metacube2/codex/fix-saving-changes-in-audora-project
Fix settings save path and improve save reliability
2026-01-12 11:33:52 +01:00
admin 1ec8d734ee Fix settings save path 2026-01-12 11:33:33 +01:00
admin 60dab1e9df Add advertisement banner styles and functionality 2026-01-12 11:26:12 +01:00
Claude 9e175fdf56 Add indexmiau.php as copy of index.php 2026-01-11 03:23:53 +00:00
admin 13024c5ae8 Merge pull request #18 from metacube2/claude/add-video-download-sppLI
Claude/add video download spp li
2026-01-11 03:49:25 +01:00
Claude 42b12c5c36 Add zoom for all video modes and fix settings saving
- Zoom now works on live video, timelapse images, and archive video player
- Added zoom level indicator (shows percentage)
- Increased max zoom from 3x to 4x
- Fixed settings AJAX handler using FormData for reliable POST
- Settings event handlers now properly bound after DOM load
- Added error handling and visual feedback for settings changes
2026-01-11 02:48:33 +00:00
Claude a033d15912 Refactor index.php with cleaner AJAX handling and simplified code
- Inline AJAX settings handler for better control flow
- Simplify video download logic with condensed code
- Clean up domain redirect handling
- Remove redundant headers and verbose comments
2026-01-11 02:34:26 +00:00
admin 1f9bc08682 Merge pull request #17 from metacube2/main-aurora
Merge pull request #16 from metacube2/claude/mail-finetuning-webapp-0…
2026-01-10 11:49:50 +01:00
admin 191381ece4 Merge pull request #16 from metacube2/claude/mail-finetuning-webapp-01BsRXQNeVFrCBky8aw35YHw
neue funktionen aurora index
2026-01-10 11:49:22 +01:00
admin fabdfb121a Merge pull request #15 from metacube2/claude/add-video-download-sppLI
Add complete index.php with all video player enhancements
2026-01-10 11:48:02 +01:00
Claude 4454adca59 Add complete index.php with all video player enhancements
Features integrated:
- Timelapse controls: slider, speed (1x/10x/100x), reverse playback
- Daily video player: plays videos in main player with controls
- Back to Live button for both timelapse and daily videos
- Admin settings panel: viewer display toggle, min viewers, video mode
- Conditional viewer count display based on admin settings
- AJAX settings updates without page reload
- SettingsManager integration throughout the application
2026-01-10 10:19:04 +00:00
admin 9ae417cb03 Merge pull request #14 from metacube2/claude/add-video-download-sppLI
Implement video download functionality
2026-01-10 11:10:29 +01:00
Claude 367aa4c67b Add Aurora Livecam video player enhancements
- Add SettingsManager class for admin settings (settings.json)
- Add timelapse controls with slider, speed (1x/10x/100x), and reverse playback
- Add daily video player to play videos in main player window
- Add admin settings panel for viewer display and video mode configuration
- Add CSS styles for new player controls
- Include integration guide for existing index.php
2026-01-10 10:08:17 +00:00
admin cac3768885 Merge pull request #13 from metacube2/claude/healthbridge-sync-app-XxRm8
Build intelligent health data synchronization app
2025-12-25 18:00:52 +01:00
Claude b953908f58 Add HealthBridge iOS app for intelligent health data synchronization
Complete implementation of a SwiftUI iOS app that serves as a "Single Source
of Truth" for health data. The app reads from all Apple Health sources,
detects conflicts between devices, merges data using configurable strategies,
and writes cleaned data back.

Features:
- Phase 1: HealthKit integration with automatic source discovery
- Phase 2: DataReader with conflict detection (time-window based)
- Phase 3: RuleEngine with 8 merge strategies (exclusive, priority, higher wins, etc.)
- Phase 4: MergeEngine for conflict resolution + DataWriter for HealthKit writes
- Phase 5: SwiftUI UI for dashboard, conflicts, rules, and sources management
- Phase 6: Background sync with configurable intervals and push notifications
- Phase 7: Complete rule editor and polished UI components

Supported data types:
- Steps, Heart Rate, Blood Pressure, SpO2, Sleep
- Distance, Floors Climbed, Active Energy, HRV, Respiratory Rate

Architecture: SourceManager -> DataReader -> RuleEngine -> MergeEngine -> DataWriter
2025-12-25 16:59:48 +00:00
admin 0ffb1c771e Merge pull request #12 from metacube2/claude/dctp-delta-transfer-swz4W
Build Delta Code Transfer Protocol tool
2025-12-25 14:51:10 +01:00
Claude 8858a08a32 Add DCTP (Delta Code Transfer Protocol) tool for efficient AI code transfers
DCTP enables efficient transfer of AI-generated code using delta operations
instead of sending complete files for each modification. Features include:

- Parser for DCTP control commands (NEW, DELETE, INSERT_AFTER, REPLACE, RENUMBER)
- Line-numbered code with language-specific comment formats
- Backup/Undo system with session management
- Diff generation for preview functionality
- CustomTkinter GUI with project management, preview, and diff views
2025-12-25 12:59:07 +00:00
admin faa36d0e5e Merge pull request #11 from metacube2/claude/family-albums-portal-jhT0O
Build family photo album portal backend
2025-12-25 11:52:32 +01:00
Claude 25766959f1 Add FamilyAlbums family photo portal with Nextcloud integration
A PHP-based family photo album portal featuring:
- Public gallery with year/month filtering and search
- Mobile-responsive design with Tailwind CSS
- Comment system for family members
- Admin interface for album management
- Flat-file JSON database (no MySQL needed)
- CSRF protection and XSS prevention
- Rate limiting and honeypot spam protection
2025-12-25 09:57:51 +00:00
admin 5f949121bf Merge pull request #10 from metacube2/claude/psytrance-visualizer-swift-bFGOw
Build Psytrance Visualizer with Swift and Metal
2025-12-22 22:38:56 +01:00
Claude a22c238dc4 Add Psytrance Visualizer macOS app with Metal rendering
A complete audio-reactive visualizer for psytrance music featuring:

Audio Analysis (DSPEngine):
- FFT spectrum analysis via Accelerate/vDSP
- 64-band Mel spectrogram
- Sub-bass energy extraction (<100Hz)
- Automatic sidechain pump detection
- Harmonic-to-Noise ratio (HNR) calculation
- Peak/transient detection

8 Visualization Modes (Metal Shaders):
1. FFT Classic - Frequency spectrum bars with glow
2. Mel Spectrogram - Waterfall display
3. Sub-Bass - Pulsating rings
4. Sidechain Pump - Breathing zoom effect
5. Harmonic/Noise - Geometric vs chaotic particles
6. Mandelbrot - Audio-reactive fractal zoom
7. Tunnel Warp - Infinite tunnel with distortion
8. DMT Geometry - Sacred geometry patterns

Features:
- Selectable audio input device (BlackHole support)
- Configurable buffer size (512/1024)
- Reactivity slider for visual intensity
- Auto-hiding control panel
- Fullscreen support with keyboard shortcuts (1-8, F, ESC)
- Persistent settings via UserDefaults
- Psytrance-inspired neon/UV color palette
2025-12-22 21:36:45 +00:00
Claude 9363a2dd99 Add macOS Catalyst support for RollkofferSimulator
- Enable Mac Catalyst in Xcode project (SUPPORTS_MACCATALYST=YES)
- Set macOS deployment target to 13.0 (Ventura+)
- Add keyboard support for all scenes (Escape, Space, Enter)
- Add macOS menu bar with game commands (Cmd+P pause, Cmd+R restart)
- Configure window size restrictions for macOS
- Update Info.plist with macOS minimum version
2025-12-20 17:46:26 +00:00
admin b607a9cd8a Merge pull request #9 from metacube2/claude/suitcase-arcade-game-6y4do
Add RollkofferSimulator iOS SpriteKit arcade game
2025-12-20 18:20:36 +01:00
134 changed files with 35480 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
import SwiftUI
import HealthKit
import BackgroundTasks
@main
struct HealthBridgeApp: App {
@StateObject private var appState = AppState()
@StateObject private var healthKitManager = HealthKitManager.shared
@StateObject private var syncCoordinator = SyncCoordinator.shared
init() {
registerBackgroundTasks()
}
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(appState)
.environmentObject(healthKitManager)
.environmentObject(syncCoordinator)
.onAppear {
Task {
await requestHealthKitAuthorization()
}
}
}
}
private func registerBackgroundTasks() {
BGTaskScheduler.shared.register(
forTaskWithIdentifier: "com.healthbridge.sync",
using: nil
) { task in
guard let bgTask = task as? BGAppRefreshTask else { return }
handleBackgroundSync(task: bgTask)
}
}
private func handleBackgroundSync(task: BGAppRefreshTask) {
scheduleNextBackgroundSync()
let syncTask = Task {
do {
try await syncCoordinator.performSync()
task.setTaskCompleted(success: true)
} catch {
task.setTaskCompleted(success: false)
}
}
task.expirationHandler = {
syncTask.cancel()
}
}
private func scheduleNextBackgroundSync() {
let request = BGAppRefreshTaskRequest(identifier: "com.healthbridge.sync")
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // 15 min
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Failed to schedule background sync: \(error)")
}
}
private func requestHealthKitAuthorization() async {
do {
try await healthKitManager.requestAuthorization()
} catch {
print("HealthKit authorization failed: \(error)")
}
}
}
// MARK: - App State
@MainActor
class AppState: ObservableObject {
@Published var selectedTab: Tab = .dashboard
@Published var showingConflictDetail: Conflict?
@Published var isLoading = false
@Published var lastSyncDate: Date?
@Published var pendingConflicts: [Conflict] = []
enum Tab {
case dashboard
case conflicts
case rules
case sources
}
}
+315
View File
@@ -0,0 +1,315 @@
import Foundation
// MARK: - Conflict
struct Conflict: Identifiable, Codable {
let id: UUID
let dataType: HealthDataType
let timeWindow: TimeWindow
var readings: [SourceReading]
var status: ConflictStatus
var resolution: ConflictResolution?
var appliedStrategy: MergeStrategy?
let detectedAt: Date
var resolvedAt: Date?
init(
id: UUID = UUID(),
dataType: HealthDataType,
timeWindow: TimeWindow,
readings: [SourceReading],
status: ConflictStatus = .pending,
resolution: ConflictResolution? = nil,
appliedStrategy: MergeStrategy? = nil,
detectedAt: Date = Date(),
resolvedAt: Date? = nil
) {
self.id = id
self.dataType = dataType
self.timeWindow = timeWindow
self.readings = readings
self.status = status
self.resolution = resolution
self.appliedStrategy = appliedStrategy
self.detectedAt = detectedAt
self.resolvedAt = resolvedAt
}
var valueDifference: Double {
guard readings.count >= 2 else { return 0 }
let values = readings.map { $0.value }
return (values.max() ?? 0) - (values.min() ?? 0)
}
var percentageDifference: Double {
guard readings.count >= 2 else { return 0 }
let values = readings.map { $0.value }
guard let min = values.min(), min > 0 else { return 0 }
guard let max = values.max() else { return 0 }
return ((max - min) / min) * 100
}
var severity: ConflictSeverity {
let pctDiff = percentageDifference
if pctDiff < 5 { return .minor }
if pctDiff < 20 { return .moderate }
if pctDiff < 50 { return .significant }
return .major
}
var highestValueReading: SourceReading? {
readings.max(by: { $0.value < $1.value })
}
var lowestValueReading: SourceReading? {
readings.min(by: { $0.value < $1.value })
}
var primarySourceReading: SourceReading? {
readings.max(by: { $0.sourceCategory.priority < $1.sourceCategory.priority })
}
}
// MARK: - Conflict Status
enum ConflictStatus: String, Codable {
case pending = "pending"
case resolved = "resolved"
case manualReview = "manual_review"
case ignored = "ignored"
var displayName: String {
switch self {
case .pending: return "Offen"
case .resolved: return "Gelöst"
case .manualReview: return "Manuelle Prüfung"
case .ignored: return "Ignoriert"
}
}
var icon: String {
switch self {
case .pending: return "clock.fill"
case .resolved: return "checkmark.circle.fill"
case .manualReview: return "hand.raised.fill"
case .ignored: return "eye.slash.fill"
}
}
}
// MARK: - Conflict Severity
enum ConflictSeverity: String, Codable {
case minor = "minor"
case moderate = "moderate"
case significant = "significant"
case major = "major"
var displayName: String {
switch self {
case .minor: return "Gering"
case .moderate: return "Moderat"
case .significant: return "Erheblich"
case .major: return "Gross"
}
}
var color: String {
switch self {
case .minor: return "green"
case .moderate: return "yellow"
case .significant: return "orange"
case .major: return "red"
}
}
}
// MARK: - Conflict Resolution
struct ConflictResolution: Codable {
let resolvedValue: Double
let secondaryResolvedValue: Double? // For blood pressure
let winningSourceId: String
let strategy: MergeStrategy
let isManual: Bool
let resolvedAt: Date
let notes: String?
init(
resolvedValue: Double,
secondaryResolvedValue: Double? = nil,
winningSourceId: String,
strategy: MergeStrategy,
isManual: Bool = false,
resolvedAt: Date = Date(),
notes: String? = nil
) {
self.resolvedValue = resolvedValue
self.secondaryResolvedValue = secondaryResolvedValue
self.winningSourceId = winningSourceId
self.strategy = strategy
self.isManual = isManual
self.resolvedAt = resolvedAt
self.notes = notes
}
}
// MARK: - Merge Strategy
enum MergeStrategy: String, Codable, CaseIterable, Identifiable {
case exclusive = "exclusive"
case priority = "priority"
case higherWins = "higher_wins"
case lowerWins = "lower_wins"
case average = "average"
case coverage = "coverage"
case coverageThenHigher = "coverage_then_higher"
case manual = "manual"
case mostRecent = "most_recent"
var id: String { rawValue }
var displayName: String {
switch self {
case .exclusive: return "Exklusiv"
case .priority: return "Priorität"
case .higherWins: return "Höherer Wert"
case .lowerWins: return "Niedrigerer Wert"
case .average: return "Durchschnitt"
case .coverage: return "Abdeckung"
case .coverageThenHigher: return "Abdeckung + Höher"
case .manual: return "Manuell"
case .mostRecent: return "Neuester"
}
}
var description: String {
switch self {
case .exclusive:
return "Nur eine Quelle kann diesen Datentyp liefern"
case .priority:
return "Höchste Priorität gewinnt basierend auf Benutzereinstellungen"
case .higherWins:
return "Der grössere Wert wird verwendet (z.B. mehr Schritte = war aktiv)"
case .lowerWins:
return "Der kleinere Wert wird verwendet"
case .average:
return "Durchschnitt aller Quellen"
case .coverage:
return "Quelle mit Daten für dieses Zeitfenster"
case .coverageThenHigher:
return "Erst Abdeckung prüfen, dann höherer Wert bei Konflikt"
case .manual:
return "Benutzer entscheidet bei jedem Konflikt"
case .mostRecent:
return "Zuletzt erfasster Wert"
}
}
var icon: String {
switch self {
case .exclusive: return "1.circle.fill"
case .priority: return "list.number"
case .higherWins: return "arrow.up.circle.fill"
case .lowerWins: return "arrow.down.circle.fill"
case .average: return "divide.circle.fill"
case .coverage: return "square.fill.on.square.fill"
case .coverageThenHigher: return "square.stack.3d.up.fill"
case .manual: return "hand.raised.fill"
case .mostRecent: return "clock.arrow.circlepath"
}
}
}
// MARK: - Merge Rule
struct MergeRule: Identifiable, Codable {
let id: UUID
let dataType: HealthDataType
var strategy: MergeStrategy
var primarySourceId: String?
var fallbackSourceId: String?
var sourcePriorities: [String: Int]
var autoApply: Bool
var thresholdForManualReview: Double? // Percentage difference threshold
init(
id: UUID = UUID(),
dataType: HealthDataType,
strategy: MergeStrategy,
primarySourceId: String? = nil,
fallbackSourceId: String? = nil,
sourcePriorities: [String: Int] = [:],
autoApply: Bool = true,
thresholdForManualReview: Double? = nil
) {
self.id = id
self.dataType = dataType
self.strategy = strategy
self.primarySourceId = primarySourceId
self.fallbackSourceId = fallbackSourceId
self.sourcePriorities = sourcePriorities
self.autoApply = autoApply
self.thresholdForManualReview = thresholdForManualReview
}
static func defaultRule(for dataType: HealthDataType) -> MergeRule {
switch dataType {
case .bloodPressureSystolic, .bloodPressureDiastolic, .bloodOxygen,
.heartRate, .restingHeartRate, .heartRateVariability, .respiratoryRate:
return MergeRule(dataType: dataType, strategy: .exclusive)
case .floorsClimbed:
return MergeRule(dataType: dataType, strategy: .exclusive)
case .steps, .distance, .activeEnergy:
return MergeRule(dataType: dataType, strategy: .coverageThenHigher)
case .sleep:
return MergeRule(dataType: dataType, strategy: .priority)
}
}
}
// MARK: - Sync Record
struct SyncRecord: Identifiable, Codable {
let id: UUID
let dataType: HealthDataType
let timeWindow: TimeWindow
var readings: [SourceReading]
var mergedValue: Double?
var secondaryMergedValue: Double? // For blood pressure
var strategy: MergeStrategy
var status: SyncStatus
var hasConflict: Bool
var conflictId: UUID?
let createdAt: Date
var processedAt: Date?
enum SyncStatus: String, Codable {
case pending = "pending"
case processing = "processing"
case completed = "completed"
case failed = "failed"
case requiresManualReview = "requires_manual"
}
init(
id: UUID = UUID(),
dataType: HealthDataType,
timeWindow: TimeWindow,
readings: [SourceReading],
mergedValue: Double? = nil,
secondaryMergedValue: Double? = nil,
strategy: MergeStrategy = .priority,
status: SyncStatus = .pending,
hasConflict: Bool = false,
conflictId: UUID? = nil,
createdAt: Date = Date(),
processedAt: Date? = nil
) {
self.id = id
self.dataType = dataType
self.timeWindow = timeWindow
self.readings = readings
self.mergedValue = mergedValue
self.secondaryMergedValue = secondaryMergedValue
self.strategy = strategy
self.status = status
self.hasConflict = hasConflict
self.conflictId = conflictId
self.createdAt = createdAt
self.processedAt = processedAt
}
}
+269
View File
@@ -0,0 +1,269 @@
import Foundation
import HealthKit
// MARK: - Health Data Type
enum HealthDataType: String, CaseIterable, Codable, Identifiable {
case steps = "steps"
case heartRate = "heart_rate"
case bloodPressureSystolic = "blood_pressure_systolic"
case bloodPressureDiastolic = "blood_pressure_diastolic"
case bloodOxygen = "blood_oxygen"
case sleep = "sleep"
case distance = "distance"
case floorsClimbed = "floors_climbed"
case activeEnergy = "active_energy"
case restingHeartRate = "resting_heart_rate"
case heartRateVariability = "hrv"
case respiratoryRate = "respiratory_rate"
var id: String { rawValue }
var displayName: String {
switch self {
case .steps: return "Schritte"
case .heartRate: return "Herzfrequenz"
case .bloodPressureSystolic: return "Blutdruck (Systolisch)"
case .bloodPressureDiastolic: return "Blutdruck (Diastolisch)"
case .bloodOxygen: return "Blutsauerstoff (SpO2)"
case .sleep: return "Schlaf"
case .distance: return "Distanz"
case .floorsClimbed: return "Stockwerke"
case .activeEnergy: return "Aktive Energie"
case .restingHeartRate: return "Ruhepuls"
case .heartRateVariability: return "HRV"
case .respiratoryRate: return "Atemfrequenz"
}
}
var icon: String {
switch self {
case .steps: return "figure.walk"
case .heartRate, .restingHeartRate: return "heart.fill"
case .bloodPressureSystolic, .bloodPressureDiastolic: return "drop.fill"
case .bloodOxygen: return "lungs.fill"
case .sleep: return "bed.double.fill"
case .distance: return "map.fill"
case .floorsClimbed: return "stairs"
case .activeEnergy: return "flame.fill"
case .heartRateVariability: return "waveform.path.ecg"
case .respiratoryRate: return "wind"
}
}
var unit: String {
switch self {
case .steps: return "Schritte"
case .heartRate, .restingHeartRate: return "bpm"
case .bloodPressureSystolic, .bloodPressureDiastolic: return "mmHg"
case .bloodOxygen: return "%"
case .sleep: return "h"
case .distance: return "km"
case .floorsClimbed: return "Stockwerke"
case .activeEnergy: return "kcal"
case .heartRateVariability: return "ms"
case .respiratoryRate: return "/min"
}
}
var hkQuantityType: HKQuantityType? {
switch self {
case .steps:
return HKQuantityType.quantityType(forIdentifier: .stepCount)
case .heartRate:
return HKQuantityType.quantityType(forIdentifier: .heartRate)
case .bloodPressureSystolic:
return HKQuantityType.quantityType(forIdentifier: .bloodPressureSystolic)
case .bloodPressureDiastolic:
return HKQuantityType.quantityType(forIdentifier: .bloodPressureDiastolic)
case .bloodOxygen:
return HKQuantityType.quantityType(forIdentifier: .oxygenSaturation)
case .distance:
return HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)
case .floorsClimbed:
return HKQuantityType.quantityType(forIdentifier: .flightsClimbed)
case .activeEnergy:
return HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)
case .restingHeartRate:
return HKQuantityType.quantityType(forIdentifier: .restingHeartRate)
case .heartRateVariability:
return HKQuantityType.quantityType(forIdentifier: .heartRateVariabilitySDNN)
case .respiratoryRate:
return HKQuantityType.quantityType(forIdentifier: .respiratoryRate)
case .sleep:
return nil // Sleep uses category type
}
}
var hkCategoryType: HKCategoryType? {
switch self {
case .sleep:
return HKCategoryType.categoryType(forIdentifier: .sleepAnalysis)
default:
return nil
}
}
var hkUnit: HKUnit {
switch self {
case .steps, .floorsClimbed:
return .count()
case .heartRate, .restingHeartRate, .respiratoryRate:
return HKUnit.count().unitDivided(by: .minute())
case .bloodPressureSystolic, .bloodPressureDiastolic:
return .millimeterOfMercury()
case .bloodOxygen:
return .percent()
case .sleep:
return .hour()
case .distance:
return .meterUnit(with: .kilo)
case .activeEnergy:
return .kilocalorie()
case .heartRateVariability:
return .secondUnit(with: .milli)
}
}
/// Default primary source for this data type
var defaultPrimarySource: SourceCategory {
switch self {
case .floorsClimbed:
return .iPhone
case .steps, .heartRate, .bloodPressureSystolic, .bloodPressureDiastolic,
.bloodOxygen, .sleep, .distance, .activeEnergy, .restingHeartRate,
.heartRateVariability, .respiratoryRate:
return .watch
}
}
/// Whether this data type typically has only one source
var isExclusive: Bool {
switch self {
case .bloodPressureSystolic, .bloodPressureDiastolic, .bloodOxygen,
.heartRate, .restingHeartRate, .heartRateVariability, .respiratoryRate, .sleep:
return true
default:
return false
}
}
}
// MARK: - Source Category
enum SourceCategory: String, Codable, CaseIterable {
case iPhone = "iphone"
case watch = "watch"
case thirdPartyWatch = "third_party_watch"
case thirdPartyApp = "third_party_app"
case healthBridge = "health_bridge"
case unknown = "unknown"
var displayName: String {
switch self {
case .iPhone: return "iPhone"
case .watch: return "Apple Watch"
case .thirdPartyWatch: return "Drittanbieter-Watch"
case .thirdPartyApp: return "Drittanbieter-App"
case .healthBridge: return "HealthBridge"
case .unknown: return "Unbekannt"
}
}
var icon: String {
switch self {
case .iPhone: return "iphone"
case .watch: return "applewatch"
case .thirdPartyWatch: return "applewatch.side.right"
case .thirdPartyApp: return "app.badge"
case .healthBridge: return "arrow.triangle.2.circlepath"
case .unknown: return "questionmark.circle"
}
}
var priority: Int {
switch self {
case .healthBridge: return 100
case .watch: return 80
case .thirdPartyWatch: return 70
case .iPhone: return 50
case .thirdPartyApp: return 30
case .unknown: return 0
}
}
}
// MARK: - Data Quality
enum DataQuality: String, Codable {
case complete = "complete"
case partial = "partial"
case missing = "missing"
case invalid = "invalid"
var icon: String {
switch self {
case .complete: return "checkmark.circle.fill"
case .partial: return "circle.lefthalf.filled"
case .missing: return "circle.dashed"
case .invalid: return "xmark.circle.fill"
}
}
var color: String {
switch self {
case .complete: return "green"
case .partial: return "yellow"
case .missing: return "gray"
case .invalid: return "red"
}
}
}
// MARK: - Time Window
struct TimeWindow: Codable, Hashable, Identifiable {
let start: Date
let end: Date
var id: String { "\(start.timeIntervalSince1970)-\(end.timeIntervalSince1970)" }
var interval: DateInterval {
DateInterval(start: start, end: end)
}
var duration: TimeInterval {
end.timeIntervalSince(start)
}
var formattedRange: String {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return "\(formatter.string(from: start)) - \(formatter.string(from: end))"
}
var formattedDate: String {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
return formatter.string(from: start)
}
static func windows(for date: Date, intervalMinutes: Int = 15) -> [TimeWindow] {
let calendar = Calendar.current
let startOfDay = calendar.startOfDay(for: date)
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
var windows: [TimeWindow] = []
var current = startOfDay
while current < endOfDay {
let windowEnd = calendar.date(byAdding: .minute, value: intervalMinutes, to: current)!
windows.append(TimeWindow(start: current, end: min(windowEnd, endOfDay)))
current = windowEnd
}
return windows
}
static func hourlyWindows(for date: Date) -> [TimeWindow] {
windows(for: date, intervalMinutes: 60)
}
}
+183
View File
@@ -0,0 +1,183 @@
import Foundation
import HealthKit
// MARK: - Health Source
struct HealthSource: Identifiable, Codable, Hashable {
let id: String
let bundleIdentifier: String
let name: String
let category: SourceCategory
var supportedDataTypes: Set<HealthDataType>
var lastActivityDate: Date?
var userPriorities: [HealthDataType: Int]
var isEnabled: Bool
init(
id: String = UUID().uuidString,
bundleIdentifier: String,
name: String,
category: SourceCategory,
supportedDataTypes: Set<HealthDataType> = [],
lastActivityDate: Date? = nil,
userPriorities: [HealthDataType: Int] = [:],
isEnabled: Bool = true
) {
self.id = id
self.bundleIdentifier = bundleIdentifier
self.name = name
self.category = category
self.supportedDataTypes = supportedDataTypes
self.lastActivityDate = lastActivityDate
self.userPriorities = userPriorities
self.isEnabled = isEnabled
}
var displayName: String {
if name.isEmpty {
return bundleIdentifier.components(separatedBy: ".").last ?? bundleIdentifier
}
return name
}
var isHealthBridge: Bool {
bundleIdentifier == HealthBridgeConstants.bundleIdentifier
}
func priority(for dataType: HealthDataType) -> Int {
userPriorities[dataType] ?? category.priority
}
static func from(hkSource: HKSource) -> HealthSource {
let category = classifySource(bundleId: hkSource.bundleIdentifier)
return HealthSource(
id: hkSource.bundleIdentifier,
bundleIdentifier: hkSource.bundleIdentifier,
name: hkSource.name,
category: category
)
}
private static func classifySource(bundleId: String) -> SourceCategory {
let lowercased = bundleId.lowercased()
if lowercased.contains("healthbridge") {
return .healthBridge
} else if lowercased.contains("apple.health") {
return .iPhone
} else if lowercased.contains("watch") || lowercased.contains("applewatch") {
return .watch
} else if lowercased.contains("huawei") || lowercased.contains("samsung") ||
lowercased.contains("fitbit") || lowercased.contains("garmin") ||
lowercased.contains("polar") || lowercased.contains("withings") {
return .thirdPartyWatch
} else {
return .thirdPartyApp
}
}
}
// MARK: - Source Reading
struct SourceReading: Identifiable, Codable {
let id: UUID
let sourceId: String
let sourceName: String
let sourceCategory: SourceCategory
let value: Double
let secondaryValue: Double? // For blood pressure (diastolic)
let timestamp: Date
let originalRecordId: String?
let quality: DataQuality
init(
id: UUID = UUID(),
sourceId: String,
sourceName: String,
sourceCategory: SourceCategory,
value: Double,
secondaryValue: Double? = nil,
timestamp: Date,
originalRecordId: String? = nil,
quality: DataQuality = .complete
) {
self.id = id
self.sourceId = sourceId
self.sourceName = sourceName
self.sourceCategory = sourceCategory
self.value = value
self.secondaryValue = secondaryValue
self.timestamp = timestamp
self.originalRecordId = originalRecordId
self.quality = quality
}
var formattedValue: String {
if value == floor(value) {
return String(format: "%.0f", value)
}
return String(format: "%.1f", value)
}
}
// MARK: - Source Health Status
struct SourceHealthStatus: Identifiable {
let id: String
let source: HealthSource
let lastSync: Date?
let recordCount: Int
let dataGaps: [TimeWindow]
let overallQuality: DataQuality
var syncStatus: SyncStatus {
guard let lastSync = lastSync else {
return .neverSynced
}
let hoursSinceSync = Date().timeIntervalSince(lastSync) / 3600
if hoursSinceSync < 1 {
return .recentlySynced
} else if hoursSinceSync < 24 {
return .syncedToday
} else if hoursSinceSync < 72 {
return .stale
} else {
return .veryStale
}
}
enum SyncStatus {
case recentlySynced
case syncedToday
case stale
case veryStale
case neverSynced
var icon: String {
switch self {
case .recentlySynced: return "checkmark.circle.fill"
case .syncedToday: return "checkmark.circle"
case .stale: return "exclamationmark.circle"
case .veryStale: return "exclamationmark.triangle"
case .neverSynced: return "xmark.circle"
}
}
var description: String {
switch self {
case .recentlySynced: return "Kürzlich synchronisiert"
case .syncedToday: return "Heute synchronisiert"
case .stale: return "Sync überfällig"
case .veryStale: return "Lange nicht synchronisiert"
case .neverSynced: return "Nie synchronisiert"
}
}
}
}
// MARK: - Constants
enum HealthBridgeConstants {
static let bundleIdentifier = "com.healthbridge.merged"
static let displayName = "HealthBridge"
static let defaultSyncInterval: TimeInterval = 15 * 60 // 15 minutes
static let conflictThreshold: TimeInterval = 60 // 1 minute overlap tolerance
}
+140
View File
@@ -0,0 +1,140 @@
# HealthBridge
Intelligente Health-Daten-Synchronisation für iOS Eine "Single Source of Truth" für Gesundheitsdaten.
## Übersicht
HealthBridge liest alle Quellen aus Apple Health, erkennt Konflikte zwischen verschiedenen Geräten und Apps, merged intelligent basierend auf konfigurierbaren Regeln und schreibt bereinigte Daten zurück.
## Features
### Source Discovery
- Automatische Erkennung aller verbundenen Datenquellen
- Klassifizierung nach Gerätetyp (iPhone, Apple Watch, Drittanbieter-Watch, Apps)
- Übersicht über Fähigkeiten und unterstützte Datentypen pro Quelle
### Konflikt-Erkennung
- Automatische Erkennung von Datenkonflikten zwischen Quellen
- Zeitfenster-basierte Analyse (15-Minuten-Intervalle)
- Schweregrad-Klassifizierung (minor, moderate, significant, major)
### Merge-Strategien
- **Exclusive**: Nur eine Quelle möglich (z.B. Blutdruck, SpO2)
- **Priority**: Fixe Rangfolge basierend auf Benutzereinstellungen
- **Higher Wins**: Grösserer Wert gewinnt (ideal für Schritte)
- **Coverage**: Quelle mit Daten für Zeitfenster gewinnt
- **Coverage Then Higher**: Erst Abdeckung, dann höherer Wert
- **Average**: Durchschnitt aller Quellen
- **Manual**: Benutzer entscheidet bei jedem Konflikt
### UI-Komponenten
- **Dashboard**: Tagesübersicht aller Gesundheitsdaten mit Sync-Status
- **Konflikte**: Liste offener Konflikte mit One-Tap-Auflösung
- **Regeln**: Konfiguration der Merge-Strategien pro Datentyp
- **Quellen**: Übersicht aller erkannten Datenquellen
### Background Sync
- Automatische Synchronisierung im Hintergrund
- Konfigurierbares Intervall (15 Min bis 2 Stunden)
- Push-Benachrichtigungen bei neuen Konflikten
## Unterstützte Datentypen
| Datentyp | Primärquelle | Strategie |
|----------|--------------|-----------|
| Schritte | Watch | Coverage + Higher |
| Herzfrequenz | Watch | Exclusive |
| Blutdruck | Watch D2 | Exclusive |
| SpO2 | Watch | Exclusive |
| Schlaf | Watch | Priority |
| Distanz | Watch/iPhone | Coverage + Higher |
| Stockwerke | iPhone | Exclusive |
| Aktive Energie | Watch | Coverage + Higher |
## Architektur
```
┌─────────────────────────────────────────────────────────────────┐
│ Apple Health │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ HealthBridge App │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ DataReader │→ │ MergeEngine │→ │ DataWriter │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ ▲ ▲ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │SourceManager│ │ RuleEngine │ │
│ └─────────────┘ └─────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ SyncCoordinator │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
## Projektstruktur
```
HealthBridge/
├── App/
│ └── HealthBridgeApp.swift # App-Entry, Background Tasks
├── Models/
│ ├── HealthDataTypes.swift # Datentypen, TimeWindow
│ ├── Source.swift # HealthSource, SourceReading
│ └── Conflict.swift # Conflict, MergeStrategy, MergeRule
├── Services/
│ ├── HealthKitManager.swift # HealthKit-Integration
│ ├── SourceManager.swift # Source Discovery & Management
│ ├── DataReader.swift # Daten lesen, Konflikte erkennen
│ ├── RuleEngine.swift # Merge-Regeln verwalten & anwenden
│ ├── MergeEngine.swift # Konflikte analysieren & lösen
│ ├── DataWriter.swift # Daten zurückschreiben
│ └── SyncCoordinator.swift # Orchestrierung aller Services
├── Views/
│ ├── ContentView.swift # Tab-Navigation
│ ├── DashboardView.swift # Hauptübersicht
│ ├── ConflictsView.swift # Konflikt-Liste & Detail
│ ├── RulesView.swift # Regelwerk-Editor
│ ├── SourcesView.swift # Quellen-Übersicht
│ ├── SettingsView.swift # Einstellungen
│ └── Components/
│ └── HealthChart.swift # Diagramm-Komponenten
├── ViewModels/
│ └── DashboardViewModel.swift # Dashboard-Logik
├── Utils/
│ ├── Extensions.swift # Swift-Erweiterungen
│ └── NotificationManager.swift # Push-Benachrichtigungen
└── Resources/
├── Info.plist # App-Konfiguration
└── HealthBridge.entitlements # HealthKit-Berechtigungen
```
## Voraussetzungen
- iOS 16.0+
- Xcode 15.0+
- Apple Developer Account (für HealthKit-Entitlements)
- Physisches Gerät (HealthKit nicht im Simulator verfügbar)
## Installation
1. Projekt in Xcode öffnen
2. Team für Code Signing auswählen
3. HealthKit-Capability aktivieren
4. Auf physischem Gerät ausführen
## Berechtigungen
Die App benötigt folgende Berechtigungen:
- **HealthKit Read**: Lesen aller Gesundheitsdaten
- **HealthKit Write**: Schreiben gemergter Daten
- **Background App Refresh**: Für automatische Synchronisierung
- **Notifications**: Für Konflikt-Benachrichtigungen
## Lizenz
MIT License
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.healthkit</key>
<true/>
<key>com.apple.developer.healthkit.access</key>
<array>
<string>health-records</string>
</array>
<key>com.apple.developer.healthkit.background-delivery</key>
<true/>
<key>aps-environment</key>
<string>development</string>
</dict>
</plist>
+74
View File
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>HealthBridge</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
</dict>
<key>UILaunchScreen</key>
<dict>
<key>UIColorName</key>
<string>LaunchScreenBackground</string>
<key>UIImageName</key>
<string>LaunchIcon</string>
</dict>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
<string>healthkit</string>
</array>
<!-- HealthKit -->
<key>NSHealthShareUsageDescription</key>
<string>HealthBridge benötigt Zugriff auf Ihre Gesundheitsdaten, um diese zwischen verschiedenen Quellen zu synchronisieren und Konflikte zu lösen.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>HealthBridge schreibt bereinigte Gesundheitsdaten zurück in Apple Health, um eine konsistente Datenbasis zu gewährleisten.</string>
<!-- Background Modes -->
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
</array>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.healthbridge.sync</string>
<string>com.healthbridge.cleanup</string>
</array>
</dict>
</plist>
+488
View File
@@ -0,0 +1,488 @@
import Foundation
import HealthKit
import Combine
// MARK: - Data Reader
@MainActor
class DataReader: ObservableObject {
static let shared = DataReader()
private let healthKitManager = HealthKitManager.shared
private let sourceManager = SourceManager.shared
@Published var isReading = false
@Published var lastReadDate: Date?
@Published var detectedConflicts: [Conflict] = []
@Published var readingProgress: Double = 0
private init() {}
// MARK: - Fetch Data by Type and Date Range
func fetchData(
for dataType: HealthDataType,
from startDate: Date,
to endDate: Date,
groupByWindow intervalMinutes: Int = 15
) async throws -> [TimeWindowData] {
isReading = true
defer { isReading = false }
let samples = try await healthKitManager.fetchSamples(
for: dataType,
from: startDate,
to: endDate
)
// Group samples by source
let groupedBySource = groupBySource(samples: samples, dataType: dataType)
// Create time windows
let windows = generateTimeWindows(from: startDate, to: endDate, intervalMinutes: intervalMinutes)
// Assign samples to windows
var windowDataList: [TimeWindowData] = []
for window in windows {
let windowData = createWindowData(
window: window,
dataType: dataType,
groupedBySource: groupedBySource
)
windowDataList.append(windowData)
}
lastReadDate = Date()
return windowDataList
}
private func groupBySource(samples: [HKSample], dataType: HealthDataType) -> [String: [HKSample]] {
var grouped: [String: [HKSample]] = [:]
for sample in samples {
let sourceId = sample.sourceRevision.source.bundleIdentifier
if grouped[sourceId] == nil {
grouped[sourceId] = []
}
grouped[sourceId]?.append(sample)
}
return grouped
}
private func generateTimeWindows(from start: Date, to end: Date, intervalMinutes: Int) -> [TimeWindow] {
var windows: [TimeWindow] = []
var current = start
while current < end {
let windowEnd = min(
Calendar.current.date(byAdding: .minute, value: intervalMinutes, to: current)!,
end
)
windows.append(TimeWindow(start: current, end: windowEnd))
current = windowEnd
}
return windows
}
private func createWindowData(
window: TimeWindow,
dataType: HealthDataType,
groupedBySource: [String: [HKSample]]
) -> TimeWindowData {
var readings: [SourceReading] = []
for (sourceId, samples) in groupedBySource {
let windowSamples = samples.filter { sample in
sample.startDate < window.end && sample.endDate > window.start
}
if !windowSamples.isEmpty {
let reading = createReading(
from: windowSamples,
sourceId: sourceId,
dataType: dataType,
window: window
)
readings.append(reading)
}
}
let hasConflict = detectConflict(in: readings, dataType: dataType)
return TimeWindowData(
timeWindow: window,
dataType: dataType,
readings: readings,
hasConflict: hasConflict
)
}
private func createReading(
from samples: [HKSample],
sourceId: String,
dataType: HealthDataType,
window: TimeWindow
) -> SourceReading {
let value: Double
var secondaryValue: Double? = nil
switch dataType {
case .steps, .floorsClimbed, .activeEnergy, .distance:
// Sum up values for cumulative types
value = samples.compactMap { sample -> Double? in
guard let quantitySample = sample as? HKQuantitySample else { return nil }
return quantitySample.quantity.doubleValue(for: dataType.hkUnit)
}.reduce(0, +)
case .heartRate, .restingHeartRate, .respiratoryRate, .heartRateVariability, .bloodOxygen:
// Average for rate-based types
let values = samples.compactMap { sample -> Double? in
guard let quantitySample = sample as? HKQuantitySample else { return nil }
return quantitySample.quantity.doubleValue(for: dataType.hkUnit)
}
value = values.isEmpty ? 0 : values.reduce(0, +) / Double(values.count)
case .bloodPressureSystolic, .bloodPressureDiastolic:
// For blood pressure, we need to handle correlations
let values = samples.compactMap { sample -> Double? in
guard let quantitySample = sample as? HKQuantitySample else { return nil }
return quantitySample.quantity.doubleValue(for: dataType.hkUnit)
}
value = values.isEmpty ? 0 : values.reduce(0, +) / Double(values.count)
case .sleep:
// Sum up sleep duration
value = samples.reduce(0) { acc, sample in
acc + sample.endDate.timeIntervalSince(sample.startDate) / 3600
}
}
let source = sourceManager.sources.first { $0.bundleIdentifier == sourceId }
let category = source?.category ?? sourceManager.classifySource(sourceId)
return SourceReading(
sourceId: sourceId,
sourceName: source?.name ?? sourceId,
sourceCategory: category,
value: value,
secondaryValue: secondaryValue,
timestamp: window.start,
originalRecordId: samples.first?.uuid.uuidString,
quality: samples.isEmpty ? .missing : .complete
)
}
// MARK: - Conflict Detection
private func detectConflict(in readings: [SourceReading], dataType: HealthDataType) -> Bool {
// No conflict if less than 2 readings
guard readings.count >= 2 else { return false }
// Filter out zero values (device wasn't tracking)
let nonZeroReadings = readings.filter { $0.value > 0 }
guard nonZeroReadings.count >= 2 else { return false }
// Check if values differ significantly
let values = nonZeroReadings.map { $0.value }
guard let minVal = values.min(), let maxVal = values.max() else { return false }
// Threshold varies by data type
let threshold = conflictThreshold(for: dataType)
if minVal == 0 {
return maxVal > threshold.absoluteThreshold
}
let percentDiff = (maxVal - minVal) / minVal * 100
return percentDiff > threshold.percentageThreshold
}
private func conflictThreshold(for dataType: HealthDataType) -> ConflictThreshold {
switch dataType {
case .steps:
return ConflictThreshold(percentageThreshold: 10, absoluteThreshold: 100)
case .distance:
return ConflictThreshold(percentageThreshold: 10, absoluteThreshold: 0.1) // 100m
case .heartRate:
return ConflictThreshold(percentageThreshold: 15, absoluteThreshold: 10)
case .bloodPressureSystolic, .bloodPressureDiastolic:
return ConflictThreshold(percentageThreshold: 5, absoluteThreshold: 5)
case .bloodOxygen:
return ConflictThreshold(percentageThreshold: 2, absoluteThreshold: 2)
case .floorsClimbed:
return ConflictThreshold(percentageThreshold: 20, absoluteThreshold: 2)
case .activeEnergy:
return ConflictThreshold(percentageThreshold: 15, absoluteThreshold: 50)
case .sleep:
return ConflictThreshold(percentageThreshold: 10, absoluteThreshold: 0.5) // 30 min
case .restingHeartRate:
return ConflictThreshold(percentageThreshold: 10, absoluteThreshold: 5)
case .heartRateVariability:
return ConflictThreshold(percentageThreshold: 20, absoluteThreshold: 10)
case .respiratoryRate:
return ConflictThreshold(percentageThreshold: 15, absoluteThreshold: 2)
}
}
// MARK: - Detect All Conflicts
func detectConflicts(
for date: Date,
dataTypes: [HealthDataType] = HealthDataType.allCases
) async throws -> [Conflict] {
let calendar = Calendar.current
let startOfDay = calendar.startOfDay(for: date)
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
var allConflicts: [Conflict] = []
for (index, dataType) in dataTypes.enumerated() {
readingProgress = Double(index) / Double(dataTypes.count)
do {
let windowData = try await fetchData(
for: dataType,
from: startOfDay,
to: endOfDay
)
let conflicts = windowData
.filter { $0.hasConflict }
.map { data in
Conflict(
dataType: dataType,
timeWindow: data.timeWindow,
readings: data.readings,
status: .pending
)
}
allConflicts.append(contentsOf: conflicts)
} catch {
print("Failed to detect conflicts for \(dataType): \(error)")
}
}
readingProgress = 1.0
detectedConflicts = allConflicts
return allConflicts
}
// MARK: - Data Gaps Detection
func detectGaps(
for dataType: HealthDataType,
from startDate: Date,
to endDate: Date,
expectedIntervalMinutes: Int = 15
) async throws -> [DataGap] {
let samples = try await healthKitManager.fetchSamples(
for: dataType,
from: startDate,
to: endDate
)
guard !samples.isEmpty else {
return [DataGap(
dataType: dataType,
timeWindow: TimeWindow(start: startDate, end: endDate),
expectedRecordCount: 0,
actualRecordCount: 0
)]
}
let sortedSamples = samples.sorted { $0.startDate < $1.startDate }
var gaps: [DataGap] = []
let expectedInterval = TimeInterval(expectedIntervalMinutes * 60)
// Check gap at start
if let firstSample = sortedSamples.first,
firstSample.startDate.timeIntervalSince(startDate) > expectedInterval * 2 {
gaps.append(DataGap(
dataType: dataType,
timeWindow: TimeWindow(start: startDate, end: firstSample.startDate),
expectedRecordCount: Int(firstSample.startDate.timeIntervalSince(startDate) / expectedInterval),
actualRecordCount: 0
))
}
// Check gaps between samples
for i in 0..<(sortedSamples.count - 1) {
let current = sortedSamples[i]
let next = sortedSamples[i + 1]
let gap = next.startDate.timeIntervalSince(current.endDate)
if gap > expectedInterval * 2 {
gaps.append(DataGap(
dataType: dataType,
timeWindow: TimeWindow(start: current.endDate, end: next.startDate),
expectedRecordCount: Int(gap / expectedInterval),
actualRecordCount: 0
))
}
}
// Check gap at end
if let lastSample = sortedSamples.last,
endDate.timeIntervalSince(lastSample.endDate) > expectedInterval * 2 {
gaps.append(DataGap(
dataType: dataType,
timeWindow: TimeWindow(start: lastSample.endDate, end: endDate),
expectedRecordCount: Int(endDate.timeIntervalSince(lastSample.endDate) / expectedInterval),
actualRecordCount: 0
))
}
return gaps
}
// MARK: - Aggregated Data
func fetchDailySummary(for date: Date) async throws -> DailySummary {
let calendar = Calendar.current
let startOfDay = calendar.startOfDay(for: date)
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
var summary = DailySummary(date: date)
for dataType in HealthDataType.allCases {
do {
let samples = try await healthKitManager.fetchSamples(
for: dataType,
from: startOfDay,
to: endOfDay
)
let value = aggregateValue(samples: samples, dataType: dataType)
summary.values[dataType] = value
// Check for conflicts
let windowData = try await fetchData(
for: dataType,
from: startOfDay,
to: endOfDay
)
let conflictCount = windowData.filter { $0.hasConflict }.count
summary.conflictCounts[dataType] = conflictCount
} catch {
print("Failed to fetch \(dataType) for summary: \(error)")
}
}
return summary
}
private func aggregateValue(samples: [HKSample], dataType: HealthDataType) -> Double {
switch dataType {
case .steps, .floorsClimbed, .activeEnergy, .distance:
return samples.compactMap { sample -> Double? in
guard let quantitySample = sample as? HKQuantitySample else { return nil }
return quantitySample.quantity.doubleValue(for: dataType.hkUnit)
}.reduce(0, +)
case .heartRate, .restingHeartRate, .respiratoryRate, .heartRateVariability,
.bloodOxygen, .bloodPressureSystolic, .bloodPressureDiastolic:
let values = samples.compactMap { sample -> Double? in
guard let quantitySample = sample as? HKQuantitySample else { return nil }
return quantitySample.quantity.doubleValue(for: dataType.hkUnit)
}
return values.isEmpty ? 0 : values.reduce(0, +) / Double(values.count)
case .sleep:
return samples.reduce(0) { acc, sample in
acc + sample.endDate.timeIntervalSince(sample.startDate) / 3600
}
}
}
}
// MARK: - Supporting Types
struct TimeWindowData: Identifiable {
let id = UUID()
let timeWindow: TimeWindow
let dataType: HealthDataType
let readings: [SourceReading]
let hasConflict: Bool
var primaryReading: SourceReading? {
readings.max { $0.sourceCategory.priority < $1.sourceCategory.priority }
}
var conflictSeverity: ConflictSeverity? {
guard hasConflict, readings.count >= 2 else { return nil }
let values = readings.map { $0.value }.filter { $0 > 0 }
guard let min = values.min(), let max = values.max(), min > 0 else { return nil }
let percentDiff = (max - min) / min * 100
if percentDiff < 5 { return .minor }
if percentDiff < 20 { return .moderate }
if percentDiff < 50 { return .significant }
return .major
}
}
struct ConflictThreshold {
let percentageThreshold: Double
let absoluteThreshold: Double
}
struct DataGap: Identifiable {
let id = UUID()
let dataType: HealthDataType
let timeWindow: TimeWindow
let expectedRecordCount: Int
let actualRecordCount: Int
var severity: GapSeverity {
let duration = timeWindow.duration
if duration < 3600 { return .minor } // < 1 hour
if duration < 4 * 3600 { return .moderate } // < 4 hours
if duration < 12 * 3600 { return .significant } // < 12 hours
return .major
}
enum GapSeverity {
case minor, moderate, significant, major
}
}
struct DailySummary {
let date: Date
var values: [HealthDataType: Double] = [:]
var conflictCounts: [HealthDataType: Int] = [:]
var lastUpdated = Date()
var totalConflicts: Int {
conflictCounts.values.reduce(0, +)
}
func formattedValue(for dataType: HealthDataType) -> String {
guard let value = values[dataType] else { return "" }
switch dataType {
case .steps, .floorsClimbed:
return String(format: "%.0f", value)
case .distance:
return String(format: "%.2f km", value)
case .heartRate, .restingHeartRate, .respiratoryRate:
return String(format: "%.0f %@", value, dataType.unit)
case .bloodPressureSystolic, .bloodPressureDiastolic:
return String(format: "%.0f mmHg", value)
case .bloodOxygen:
return String(format: "%.0f%%", value * 100)
case .activeEnergy:
return String(format: "%.0f kcal", value)
case .sleep:
let hours = Int(value)
let minutes = Int((value - Double(hours)) * 60)
return "\(hours)h \(minutes)min"
case .heartRateVariability:
return String(format: "%.0f ms", value)
}
}
}
+395
View File
@@ -0,0 +1,395 @@
import Foundation
import HealthKit
import Combine
// MARK: - Data Writer
@MainActor
class DataWriter: ObservableObject {
static let shared = DataWriter()
private let healthKitManager = HealthKitManager.shared
private let healthStore = HKHealthStore()
@Published var isWriting = false
@Published var writeProgress: Double = 0
@Published var lastWriteDate: Date?
@Published var writtenRecords: [WrittenRecord] = []
@Published var failedWrites: [FailedWrite] = []
private let processedRecordsKey = "healthbridge.processed.records"
private init() {
loadProcessedRecords()
}
// MARK: - Write Single Record
func writeRecord(_ mergedRecord: MergedRecord) async throws -> WrittenRecord {
isWriting = true
defer { isWriting = false }
// Check if already written
if isAlreadyWritten(mergedRecord) {
throw DataWriterError.duplicateRecord
}
let metadata = createMetadata(from: mergedRecord)
switch mergedRecord.dataType {
case .bloodPressureSystolic, .bloodPressureDiastolic:
// Blood pressure needs special handling
guard let diastolic = mergedRecord.secondaryValue else {
throw DataWriterError.missingSecondaryValue
}
try await writeBloodPressure(
systolic: mergedRecord.value,
diastolic: diastolic,
date: mergedRecord.timeWindow.start,
metadata: metadata
)
default:
try await writeSample(
dataType: mergedRecord.dataType,
value: mergedRecord.value,
date: mergedRecord.timeWindow.start,
metadata: metadata
)
}
let writtenRecord = WrittenRecord(
id: UUID(),
mergedRecordId: mergedRecord.id,
dataType: mergedRecord.dataType,
value: mergedRecord.value,
secondaryValue: mergedRecord.secondaryValue,
writtenAt: Date(),
timeWindow: mergedRecord.timeWindow
)
writtenRecords.append(writtenRecord)
markAsProcessed(mergedRecord)
lastWriteDate = Date()
return writtenRecord
}
// MARK: - Write Batch
func writeBatch(_ mergedRecords: [MergedRecord]) async -> BatchWriteResult {
isWriting = true
defer { isWriting = false }
var successful: [WrittenRecord] = []
var failed: [FailedWrite] = []
for (index, record) in mergedRecords.enumerated() {
writeProgress = Double(index) / Double(mergedRecords.count)
do {
let writtenRecord = try await writeRecord(record)
successful.append(writtenRecord)
} catch {
let failedWrite = FailedWrite(
mergedRecord: record,
error: error,
attemptedAt: Date()
)
failed.append(failedWrite)
failedWrites.append(failedWrite)
}
}
writeProgress = 1.0
return BatchWriteResult(
successful: successful,
failed: failed,
completedAt: Date()
)
}
// MARK: - Private Write Methods
private func writeSample(
dataType: HealthDataType,
value: Double,
date: Date,
metadata: [String: Any]
) async throws {
guard let quantityType = dataType.hkQuantityType else {
throw DataWriterError.unsupportedDataType
}
let quantity = HKQuantity(unit: dataType.hkUnit, doubleValue: value)
let sample = HKQuantitySample(
type: quantityType,
quantity: quantity,
start: date,
end: date,
metadata: metadata
)
try await healthStore.save(sample)
}
private func writeBloodPressure(
systolic: Double,
diastolic: Double,
date: Date,
metadata: [String: Any]
) async throws {
// Validate blood pressure values
let validation = BloodPressureHandler.shared.validate(
systolic: systolic,
diastolic: diastolic
)
if !validation.isValid {
throw DataWriterError.invalidValue(validation.issues.joined(separator: ", "))
}
guard let systolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureSystolic),
let diastolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureDiastolic),
let correlationType = HKCorrelationType.correlationType(forIdentifier: .bloodPressure) else {
throw DataWriterError.unsupportedDataType
}
let systolicQuantity = HKQuantity(unit: .millimeterOfMercury(), doubleValue: systolic)
let diastolicQuantity = HKQuantity(unit: .millimeterOfMercury(), doubleValue: diastolic)
let systolicSample = HKQuantitySample(
type: systolicType,
quantity: systolicQuantity,
start: date,
end: date,
metadata: metadata
)
let diastolicSample = HKQuantitySample(
type: diastolicType,
quantity: diastolicQuantity,
start: date,
end: date,
metadata: metadata
)
let correlation = HKCorrelation(
type: correlationType,
start: date,
end: date,
objects: [systolicSample, diastolicSample],
metadata: metadata
)
try await healthStore.save(correlation)
}
// MARK: - Metadata
private func createMetadata(from record: MergedRecord) -> [String: Any] {
var metadata: [String: Any] = [
HKMetadataKeyWasUserEntered: false,
"HealthBridgeSource": HealthBridgeConstants.bundleIdentifier,
"OriginalSourceId": record.originalSourceId,
"MergeStrategy": record.strategy.rawValue,
"MergedRecordId": record.id.uuidString,
"MergedAt": ISO8601DateFormatter().string(from: record.createdAt)
]
for (key, value) in record.metadata {
metadata["HB_\(key)"] = value
}
return metadata
}
// MARK: - Duplicate Prevention
private var processedRecordIds: Set<String> = []
private func loadProcessedRecords() {
if let data = UserDefaults.standard.data(forKey: processedRecordsKey),
let ids = try? JSONDecoder().decode(Set<String>.self, from: data) {
processedRecordIds = ids
}
}
private func saveProcessedRecords() {
if let data = try? JSONEncoder().encode(processedRecordIds) {
UserDefaults.standard.set(data, forKey: processedRecordsKey)
}
}
private func isAlreadyWritten(_ record: MergedRecord) -> Bool {
let identifier = createRecordIdentifier(record)
return processedRecordIds.contains(identifier)
}
private func markAsProcessed(_ record: MergedRecord) {
let identifier = createRecordIdentifier(record)
processedRecordIds.insert(identifier)
saveProcessedRecords()
// Cleanup old records (keep last 7 days)
cleanupOldRecords()
}
private func createRecordIdentifier(_ record: MergedRecord) -> String {
let components = [
record.dataType.rawValue,
String(record.timeWindow.start.timeIntervalSince1970),
String(record.value)
]
return components.joined(separator: "-")
}
private func cleanupOldRecords() {
// Keep only identifiers that contain recent timestamps
let sevenDaysAgo = Date().addingTimeInterval(-7 * 24 * 60 * 60)
let cutoffTimestamp = sevenDaysAgo.timeIntervalSince1970
processedRecordIds = processedRecordIds.filter { identifier in
guard let parts = identifier.split(separator: "-").dropFirst().first,
let timestamp = Double(parts) else {
return false
}
return timestamp > cutoffTimestamp
}
saveProcessedRecords()
}
// MARK: - Delete Records
func deleteHealthBridgeRecords(
for dataType: HealthDataType,
from startDate: Date,
to endDate: Date
) async throws -> Int {
guard let sampleType = dataType.hkQuantityType else {
throw DataWriterError.unsupportedDataType
}
let predicate = HKQuery.predicateForSamples(
withStart: startDate,
end: endDate,
options: .strictStartDate
)
return try await withCheckedThrowingContinuation { continuation in
let query = HKSampleQuery(
sampleType: sampleType,
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: nil
) { [weak self] _, samplesOrNil, errorOrNil in
guard let self = self else {
continuation.resume(throwing: DataWriterError.unknownError)
return
}
if let error = errorOrNil {
continuation.resume(throwing: error)
return
}
guard let samples = samplesOrNil else {
continuation.resume(returning: 0)
return
}
// Filter to only HealthBridge records
let healthBridgeSamples = samples.filter { sample in
if let metadata = sample.metadata,
let source = metadata["HealthBridgeSource"] as? String {
return source == HealthBridgeConstants.bundleIdentifier
}
return false
}
guard !healthBridgeSamples.isEmpty else {
continuation.resume(returning: 0)
return
}
Task {
do {
try await self.healthStore.delete(healthBridgeSamples)
continuation.resume(returning: healthBridgeSamples.count)
} catch {
continuation.resume(throwing: error)
}
}
}
healthStore.execute(query)
}
}
}
// MARK: - Supporting Types
struct WrittenRecord: Identifiable, Codable {
let id: UUID
let mergedRecordId: UUID
let dataType: HealthDataType
let value: Double
let secondaryValue: Double?
let writtenAt: Date
let timeWindow: TimeWindow
}
struct FailedWrite: Identifiable {
let id = UUID()
let mergedRecord: MergedRecord
let error: Error
let attemptedAt: Date
var errorMessage: String {
error.localizedDescription
}
}
struct BatchWriteResult {
let successful: [WrittenRecord]
let failed: [FailedWrite]
let completedAt: Date
var successCount: Int { successful.count }
var failureCount: Int { failed.count }
var totalCount: Int { successCount + failureCount }
var successRate: Double {
guard totalCount > 0 else { return 1.0 }
return Double(successCount) / Double(totalCount)
}
}
// MARK: - Errors
enum DataWriterError: LocalizedError {
case unsupportedDataType
case duplicateRecord
case missingSecondaryValue
case invalidValue(String)
case writeFailed(String)
case unknownError
var errorDescription: String? {
switch self {
case .unsupportedDataType:
return "Dieser Datentyp wird nicht unterstützt"
case .duplicateRecord:
return "Dieser Datensatz wurde bereits geschrieben"
case .missingSecondaryValue:
return "Fehlender sekundärer Wert (z.B. diastolischer Blutdruck)"
case .invalidValue(let message):
return "Ungültiger Wert: \(message)"
case .writeFailed(let message):
return "Schreiben fehlgeschlagen: \(message)"
case .unknownError:
return "Unbekannter Fehler"
}
}
}
@@ -0,0 +1,391 @@
import Foundation
import HealthKit
import Combine
// MARK: - HealthKit Manager
@MainActor
class HealthKitManager: ObservableObject {
static let shared = HealthKitManager()
private let healthStore = HKHealthStore()
@Published var isAuthorized = false
@Published var authorizationStatus: [HealthDataType: HKAuthorizationStatus] = [:]
@Published var discoveredSources: [HealthSource] = []
@Published var sourceHealthStatus: [String: SourceHealthStatus] = [:]
@Published var lastError: Error?
private init() {}
// MARK: - Authorization
var allQuantityTypes: Set<HKQuantityType> {
var types = Set<HKQuantityType>()
for dataType in HealthDataType.allCases {
if let quantityType = dataType.hkQuantityType {
types.insert(quantityType)
}
}
return types
}
var allCategoryTypes: Set<HKCategoryType> {
var types = Set<HKCategoryType>()
for dataType in HealthDataType.allCases {
if let categoryType = dataType.hkCategoryType {
types.insert(categoryType)
}
}
return types
}
var allSampleTypes: Set<HKSampleType> {
var types = Set<HKSampleType>()
allQuantityTypes.forEach { types.insert($0) }
allCategoryTypes.forEach { types.insert($0) }
return types
}
func requestAuthorization() async throws {
guard HKHealthStore.isHealthDataAvailable() else {
throw HealthKitError.healthDataNotAvailable
}
let typesToRead = allSampleTypes
let typesToWrite = allQuantityTypes
try await healthStore.requestAuthorization(toShare: typesToWrite, read: typesToRead)
isAuthorized = true
await updateAuthorizationStatus()
await discoverSources()
}
func updateAuthorizationStatus() async {
for dataType in HealthDataType.allCases {
if let quantityType = dataType.hkQuantityType {
let status = healthStore.authorizationStatus(for: quantityType)
authorizationStatus[dataType] = status
} else if let categoryType = dataType.hkCategoryType {
let status = healthStore.authorizationStatus(for: categoryType)
authorizationStatus[dataType] = status
}
}
}
// MARK: - Source Discovery
func discoverSources() async {
var allSources: [String: HealthSource] = [:]
for dataType in HealthDataType.allCases {
guard let sampleType = dataType.hkQuantityType ?? dataType.hkCategoryType else {
continue
}
do {
let sources = try await fetchSources(for: sampleType)
for source in sources {
if var existingSource = allSources[source.bundleIdentifier] {
existingSource.supportedDataTypes.insert(dataType)
allSources[source.bundleIdentifier] = existingSource
} else {
var newSource = source
newSource.supportedDataTypes.insert(dataType)
allSources[source.bundleIdentifier] = newSource
}
}
} catch {
print("Failed to fetch sources for \(dataType): \(error)")
}
}
discoveredSources = Array(allSources.values).sorted { $0.category.priority > $1.category.priority }
// Update source health status
for source in discoveredSources {
await updateSourceHealth(source)
}
}
private func fetchSources(for sampleType: HKSampleType) async throws -> [HealthSource] {
let query = HKSourceQuery(sampleType: sampleType, samplePredicate: nil) { _, sourcesOrNil, errorOrNil in
// Handled via continuation
}
return try await withCheckedThrowingContinuation { continuation in
let query = HKSourceQuery(sampleType: sampleType, samplePredicate: nil) { _, sourcesOrNil, errorOrNil in
if let error = errorOrNil {
continuation.resume(throwing: error)
return
}
guard let sources = sourcesOrNil else {
continuation.resume(returning: [])
return
}
let healthSources = sources.map { HealthSource.from(hkSource: $0) }
continuation.resume(returning: healthSources)
}
healthStore.execute(query)
}
}
private func updateSourceHealth(_ source: HealthSource) async {
var recordCount = 0
var lastActivity: Date?
for dataType in source.supportedDataTypes {
if let quantityType = dataType.hkQuantityType {
let predicate = HKQuery.predicateForObjects(from: HKSource(bundleIdentifier: source.bundleIdentifier, name: source.name) )
// Simplified: just get count
if let count = try? await fetchRecordCount(for: quantityType, source: source) {
recordCount += count
}
if let date = try? await fetchLastActivityDate(for: quantityType, source: source) {
if lastActivity == nil || date > lastActivity! {
lastActivity = date
}
}
}
}
let status = SourceHealthStatus(
id: source.id,
source: source,
lastSync: lastActivity,
recordCount: recordCount,
dataGaps: [], // TODO: Implement gap detection
overallQuality: recordCount > 0 ? .complete : .missing
)
sourceHealthStatus[source.id] = status
}
private func fetchRecordCount(for sampleType: HKSampleType, source: HealthSource) async throws -> Int {
let calendar = Calendar.current
let now = Date()
let startOfDay = calendar.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
return try await withCheckedThrowingContinuation { continuation in
let query = HKSampleQuery(
sampleType: sampleType,
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: nil
) { _, samplesOrNil, errorOrNil in
if let error = errorOrNil {
continuation.resume(throwing: error)
return
}
let samples = samplesOrNil ?? []
let matchingSamples = samples.filter { $0.sourceRevision.source.bundleIdentifier == source.bundleIdentifier }
continuation.resume(returning: matchingSamples.count)
}
self.healthStore.execute(query)
}
}
private func fetchLastActivityDate(for sampleType: HKSampleType, source: HealthSource) async throws -> Date? {
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
return try await withCheckedThrowingContinuation { continuation in
let query = HKSampleQuery(
sampleType: sampleType,
predicate: nil,
limit: 1,
sortDescriptors: [sortDescriptor]
) { _, samplesOrNil, errorOrNil in
if let error = errorOrNil {
continuation.resume(throwing: error)
return
}
let matchingSample = samplesOrNil?.first { $0.sourceRevision.source.bundleIdentifier == source.bundleIdentifier }
continuation.resume(returning: matchingSample?.endDate)
}
self.healthStore.execute(query)
}
}
// MARK: - Source Classification
func classifySource(_ source: HKSource) -> SourceCategory {
let bundleId = source.bundleIdentifier.lowercased()
if bundleId.contains("healthbridge") {
return .healthBridge
} else if bundleId.contains("apple.health") && !bundleId.contains("watch") {
return .iPhone
} else if bundleId.contains("apple") && bundleId.contains("watch") {
return .watch
} else if bundleId.contains("huawei") {
return .thirdPartyWatch
} else if bundleId.contains("samsung") || bundleId.contains("galaxy") {
return .thirdPartyWatch
} else if bundleId.contains("fitbit") {
return .thirdPartyWatch
} else if bundleId.contains("garmin") {
return .thirdPartyWatch
} else if bundleId.contains("polar") {
return .thirdPartyWatch
} else if bundleId.contains("withings") {
return .thirdPartyWatch
} else {
return .thirdPartyApp
}
}
func getSourceCapabilities(_ source: HealthSource) -> Set<HealthDataType> {
return source.supportedDataTypes
}
// MARK: - Data Fetching (Basic)
func fetchSamples(
for dataType: HealthDataType,
from startDate: Date,
to endDate: Date
) async throws -> [HKSample] {
guard let sampleType = dataType.hkQuantityType ?? dataType.hkCategoryType else {
throw HealthKitError.unsupportedDataType
}
let predicate = HKQuery.predicateForSamples(
withStart: startDate,
end: endDate,
options: .strictStartDate
)
return try await withCheckedThrowingContinuation { continuation in
let query = HKSampleQuery(
sampleType: sampleType,
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)]
) { _, samplesOrNil, errorOrNil in
if let error = errorOrNil {
continuation.resume(throwing: error)
return
}
continuation.resume(returning: samplesOrNil ?? [])
}
self.healthStore.execute(query)
}
}
// MARK: - Data Writing
func writeSample(
dataType: HealthDataType,
value: Double,
secondaryValue: Double? = nil,
date: Date,
metadata: [String: Any]? = nil
) async throws {
guard let quantityType = dataType.hkQuantityType else {
throw HealthKitError.unsupportedDataType
}
let quantity = HKQuantity(unit: dataType.hkUnit, doubleValue: value)
let sample = HKQuantitySample(
type: quantityType,
quantity: quantity,
start: date,
end: date,
metadata: metadata
)
try await healthStore.save(sample)
}
func writeBloodPressure(
systolic: Double,
diastolic: Double,
date: Date,
metadata: [String: Any]? = nil
) async throws {
guard let systolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureSystolic),
let diastolicType = HKQuantityType.quantityType(forIdentifier: .bloodPressureDiastolic) else {
throw HealthKitError.unsupportedDataType
}
let systolicQuantity = HKQuantity(unit: .millimeterOfMercury(), doubleValue: systolic)
let diastolicQuantity = HKQuantity(unit: .millimeterOfMercury(), doubleValue: diastolic)
let systolicSample = HKQuantitySample(
type: systolicType,
quantity: systolicQuantity,
start: date,
end: date,
metadata: metadata
)
let diastolicSample = HKQuantitySample(
type: diastolicType,
quantity: diastolicQuantity,
start: date,
end: date,
metadata: metadata
)
// Create correlation for blood pressure
guard let correlationType = HKCorrelationType.correlationType(forIdentifier: .bloodPressure) else {
throw HealthKitError.unsupportedDataType
}
let correlation = HKCorrelation(
type: correlationType,
start: date,
end: date,
objects: [systolicSample, diastolicSample],
metadata: metadata
)
try await healthStore.save(correlation)
}
}
// MARK: - HealthKit Errors
enum HealthKitError: LocalizedError {
case healthDataNotAvailable
case authorizationDenied
case unsupportedDataType
case noDataFound
case writeFailed(Error)
case queryFailed(Error)
var errorDescription: String? {
switch self {
case .healthDataNotAvailable:
return "Health-Daten sind auf diesem Gerät nicht verfügbar"
case .authorizationDenied:
return "Zugriff auf Health-Daten wurde verweigert"
case .unsupportedDataType:
return "Dieser Datentyp wird nicht unterstützt"
case .noDataFound:
return "Keine Daten gefunden"
case .writeFailed(let error):
return "Schreiben fehlgeschlagen: \(error.localizedDescription)"
case .queryFailed(let error):
return "Abfrage fehlgeschlagen: \(error.localizedDescription)"
}
}
}
// MARK: - HKSource Extension
extension HKSource {
convenience init(bundleIdentifier: String, name: String) {
// Note: This is a workaround since HKSource doesn't have a public initializer
// In production, sources come from HealthKit queries
fatalError("HKSource cannot be initialized directly - use source from HKSample")
}
}
+287
View File
@@ -0,0 +1,287 @@
import Foundation
import Combine
// MARK: - Merge Engine
@MainActor
class MergeEngine: ObservableObject {
static let shared = MergeEngine()
private let ruleEngine = RuleEngine.shared
private let dataReader = DataReader.shared
@Published var pendingMerges: [MergeOperation] = []
@Published var completedMerges: [MergeOperation] = []
@Published var isMerging = false
@Published var mergeProgress: Double = 0
private init() {}
// MARK: - Analyze Window
func analyze(windowData: TimeWindowData) -> WindowAnalysis {
let readings = windowData.readings
let dataType = windowData.dataType
// No analysis needed for single reading
if readings.count <= 1 {
return WindowAnalysis(
windowData: windowData,
hasConflict: false,
conflictSeverity: nil,
recommendedReading: readings.first,
alternativeReadings: [],
confidence: .high,
analysisNotes: readings.isEmpty ? "Keine Daten" : "Einzelne Quelle"
)
}
// Apply rule to get recommendation
let result = ruleEngine.applyRule(to: readings, dataType: dataType)
// Calculate conflict severity
let values = readings.map { $0.value }.filter { $0 > 0 }
var severity: ConflictSeverity? = nil
if values.count >= 2, let min = values.min(), let max = values.max(), min > 0 {
let percentDiff = (max - min) / min * 100
if percentDiff >= 5 {
if percentDiff < 10 { severity = .minor }
else if percentDiff < 25 { severity = .moderate }
else if percentDiff < 50 { severity = .significant }
else { severity = .major }
}
}
let alternativeReadings = readings.filter { $0.id != result.selectedReading?.id }
return WindowAnalysis(
windowData: windowData,
hasConflict: windowData.hasConflict,
conflictSeverity: severity,
recommendedReading: result.selectedReading,
alternativeReadings: alternativeReadings,
confidence: result.confidence,
analysisNotes: result.reason
)
}
// MARK: - Resolve Conflict
func resolveConflict(_ conflict: Conflict, using result: RuleApplicationResult) -> ConflictResolution? {
guard let selectedReading = result.selectedReading else {
return nil
}
return ConflictResolution(
resolvedValue: selectedReading.value,
secondaryResolvedValue: selectedReading.secondaryValue,
winningSourceId: selectedReading.sourceId,
strategy: result.strategy,
isManual: result.strategy == .manual
)
}
func resolveConflictManually(
_ conflict: Conflict,
selectedReadingId: UUID
) -> ConflictResolution? {
guard let selectedReading = conflict.readings.first(where: { $0.id == selectedReadingId }) else {
return nil
}
return ConflictResolution(
resolvedValue: selectedReading.value,
secondaryResolvedValue: selectedReading.secondaryValue,
winningSourceId: selectedReading.sourceId,
strategy: .manual,
isManual: true
)
}
// MARK: - Create Merged Record
func createMergedRecord(from conflict: Conflict, resolution: ConflictResolution) -> MergedRecord {
return MergedRecord(
id: UUID(),
dataType: conflict.dataType,
timeWindow: conflict.timeWindow,
value: resolution.resolvedValue,
secondaryValue: resolution.secondaryResolvedValue,
originalSourceId: resolution.winningSourceId,
strategy: resolution.strategy,
createdAt: Date(),
metadata: [
"conflictId": conflict.id.uuidString,
"originalSourceCount": String(conflict.readings.count),
"isManualResolution": String(resolution.isManual)
]
)
}
// MARK: - Batch Processing
func processConflicts(_ conflicts: [Conflict]) async -> [MergeOperation] {
isMerging = true
defer { isMerging = false }
var operations: [MergeOperation] = []
for (index, conflict) in conflicts.enumerated() {
mergeProgress = Double(index) / Double(conflicts.count)
let result = ruleEngine.applyRule(to: conflict.readings, dataType: conflict.dataType)
if result.confidence == .requiresManual ||
ruleEngine.shouldRequestManualReview(readings: conflict.readings, dataType: conflict.dataType) {
// Add to pending for manual review
let operation = MergeOperation(
conflict: conflict,
status: .pendingManualReview,
result: result
)
pendingMerges.append(operation)
operations.append(operation)
} else if let resolution = resolveConflict(conflict, using: result) {
// Auto-resolve
let mergedRecord = createMergedRecord(from: conflict, resolution: resolution)
let operation = MergeOperation(
conflict: conflict,
status: .resolved,
result: result,
resolution: resolution,
mergedRecord: mergedRecord
)
completedMerges.append(operation)
operations.append(operation)
}
}
mergeProgress = 1.0
return operations
}
// MARK: - Daily Merge
func performDailyMerge(for date: Date) async throws -> DailyMergeReport {
let calendar = Calendar.current
let startOfDay = calendar.startOfDay(for: date)
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
var report = DailyMergeReport(date: date)
for dataType in HealthDataType.allCases {
do {
let windowData = try await dataReader.fetchData(
for: dataType,
from: startOfDay,
to: endOfDay
)
let conflictWindows = windowData.filter { $0.hasConflict }
report.totalConflicts += conflictWindows.count
for window in conflictWindows {
let analysis = analyze(windowData: window)
if analysis.confidence == .requiresManual {
report.pendingManualReview += 1
} else {
report.autoResolved += 1
}
report.analysesByType[dataType, default: []].append(analysis)
}
} catch {
report.errors.append("Fehler bei \(dataType.displayName): \(error.localizedDescription)")
}
}
return report
}
}
// MARK: - Supporting Types
struct WindowAnalysis {
let windowData: TimeWindowData
let hasConflict: Bool
let conflictSeverity: ConflictSeverity?
let recommendedReading: SourceReading?
let alternativeReadings: [SourceReading]
let confidence: RuleConfidence
let analysisNotes: String
var recommendedValue: Double? {
recommendedReading?.value
}
var valueDifference: Double? {
guard let recommended = recommendedReading?.value,
let alternative = alternativeReadings.first?.value else {
return nil
}
return abs(recommended - alternative)
}
}
struct MergeOperation: Identifiable {
let id = UUID()
let conflict: Conflict
var status: MergeStatus
let result: RuleApplicationResult
var resolution: ConflictResolution?
var mergedRecord: MergedRecord?
let createdAt = Date()
var processedAt: Date?
enum MergeStatus {
case pending
case pendingManualReview
case resolved
case written
case failed
}
}
struct MergedRecord: Identifiable, Codable {
let id: UUID
let dataType: HealthDataType
let timeWindow: TimeWindow
let value: Double
let secondaryValue: Double?
let originalSourceId: String
let strategy: MergeStrategy
let createdAt: Date
var writtenAt: Date?
var healthKitRecordId: String?
var metadata: [String: String]
var formattedValue: String {
if value == floor(value) {
return String(format: "%.0f", value)
}
return String(format: "%.1f", value)
}
}
struct DailyMergeReport {
let date: Date
var totalConflicts = 0
var autoResolved = 0
var pendingManualReview = 0
var analysesByType: [HealthDataType: [WindowAnalysis]] = [:]
var errors: [String] = []
let generatedAt = Date()
var successRate: Double {
guard totalConflicts > 0 else { return 1.0 }
return Double(autoResolved) / Double(totalConflicts)
}
var summary: String {
if totalConflicts == 0 {
return "Keine Konflikte gefunden"
}
return "\(autoResolved)/\(totalConflicts) automatisch gelöst, \(pendingManualReview) zur Prüfung"
}
}
+571
View File
@@ -0,0 +1,571 @@
import Foundation
import Combine
// MARK: - Rule Engine
@MainActor
class RuleEngine: ObservableObject {
static let shared = RuleEngine()
@Published var rules: [HealthDataType: MergeRule] = [:]
@Published var isLoaded = false
private let storage = RuleStorageManager()
private let sourceManager = SourceManager.shared
private init() {
loadRules()
}
// MARK: - Rule Loading
func loadRules() {
let savedRules = storage.loadRules()
if savedRules.isEmpty {
// Initialize with defaults
for dataType in HealthDataType.allCases {
rules[dataType] = MergeRule.defaultRule(for: dataType)
}
saveRules()
} else {
rules = savedRules
}
isLoaded = true
}
func saveRules() {
storage.saveRules(rules)
}
// MARK: - Rule Access
func getRule(for dataType: HealthDataType) -> MergeRule {
return rules[dataType] ?? MergeRule.defaultRule(for: dataType)
}
func setRule(_ rule: MergeRule, for dataType: HealthDataType) {
rules[dataType] = rule
saveRules()
}
func resetToDefault(for dataType: HealthDataType) {
rules[dataType] = MergeRule.defaultRule(for: dataType)
saveRules()
}
func resetAllToDefaults() {
for dataType in HealthDataType.allCases {
rules[dataType] = MergeRule.defaultRule(for: dataType)
}
saveRules()
}
// MARK: - Rule Application
func applyRule(
to readings: [SourceReading],
dataType: HealthDataType
) -> RuleApplicationResult {
let rule = getRule(for: dataType)
// Filter out empty/zero readings for most strategies
let validReadings = readings.filter { $0.value > 0 || $0.quality == .complete }
guard !validReadings.isEmpty else {
return RuleApplicationResult(
selectedReading: nil,
strategy: rule.strategy,
confidence: .low,
reason: "Keine gültigen Werte vorhanden"
)
}
// If only one valid reading, no conflict
if validReadings.count == 1 {
return RuleApplicationResult(
selectedReading: validReadings[0],
strategy: rule.strategy,
confidence: .high,
reason: "Nur eine Quelle verfügbar"
)
}
// Apply strategy
switch rule.strategy {
case .exclusive:
return applyExclusiveStrategy(readings: validReadings, rule: rule)
case .priority:
return applyPriorityStrategy(readings: validReadings, rule: rule, dataType: dataType)
case .higherWins:
return applyHigherWinsStrategy(readings: validReadings, rule: rule)
case .lowerWins:
return applyLowerWinsStrategy(readings: validReadings, rule: rule)
case .average:
return applyAverageStrategy(readings: validReadings, rule: rule)
case .coverage:
return applyCoverageStrategy(readings: validReadings, rule: rule)
case .coverageThenHigher:
return applyCoverageThenHigherStrategy(readings: validReadings, rule: rule)
case .mostRecent:
return applyMostRecentStrategy(readings: validReadings, rule: rule)
case .manual:
return RuleApplicationResult(
selectedReading: nil,
strategy: .manual,
confidence: .requiresManual,
reason: "Manuelle Entscheidung erforderlich"
)
}
}
// MARK: - Strategy Implementations
private func applyExclusiveStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
// If primary source is specified, use it
if let primaryId = rule.primarySourceId,
let reading = readings.first(where: { $0.sourceId == primaryId }) {
return RuleApplicationResult(
selectedReading: reading,
strategy: .exclusive,
confidence: .high,
reason: "Exklusive Quelle: \(reading.sourceName)"
)
}
// Otherwise use highest priority source
let sorted = readings.sorted { $0.sourceCategory.priority > $1.sourceCategory.priority }
if let first = sorted.first {
return RuleApplicationResult(
selectedReading: first,
strategy: .exclusive,
confidence: .high,
reason: "Höchste Priorität: \(first.sourceName)"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .exclusive,
confidence: .low,
reason: "Keine geeignete Quelle gefunden"
)
}
private func applyPriorityStrategy(
readings: [SourceReading],
rule: MergeRule,
dataType: HealthDataType
) -> RuleApplicationResult {
// Sort by user-defined priority, then by category priority
let sorted = readings.sorted { r1, r2 in
let p1 = rule.sourcePriorities[r1.sourceId] ?? r1.sourceCategory.priority
let p2 = rule.sourcePriorities[r2.sourceId] ?? r2.sourceCategory.priority
return p1 > p2
}
if let first = sorted.first {
return RuleApplicationResult(
selectedReading: first,
strategy: .priority,
confidence: .high,
reason: "Höchste Priorität: \(first.sourceName)"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .priority,
confidence: .low,
reason: "Keine Quelle mit Priorität gefunden"
)
}
private func applyHigherWinsStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
let sorted = readings.sorted { $0.value > $1.value }
if let highest = sorted.first {
// Check if there's a significant difference
let values = readings.map { $0.value }
let spread = (values.max() ?? 0) - (values.min() ?? 0)
let avgValue = values.reduce(0, +) / Double(values.count)
let spreadPercent = avgValue > 0 ? (spread / avgValue) * 100 : 0
let confidence: RuleConfidence = spreadPercent < 10 ? .high : .medium
return RuleApplicationResult(
selectedReading: highest,
strategy: .higherWins,
confidence: confidence,
reason: "Höchster Wert: \(highest.formattedValue) von \(highest.sourceName)"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .higherWins,
confidence: .low,
reason: "Keine Werte zum Vergleich"
)
}
private func applyLowerWinsStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
let sorted = readings.sorted { $0.value < $1.value }
if let lowest = sorted.first {
return RuleApplicationResult(
selectedReading: lowest,
strategy: .lowerWins,
confidence: .medium,
reason: "Niedrigster Wert: \(lowest.formattedValue) von \(lowest.sourceName)"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .lowerWins,
confidence: .low,
reason: "Keine Werte zum Vergleich"
)
}
private func applyAverageStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
let values = readings.map { $0.value }
let average = values.reduce(0, +) / Double(values.count)
// Create a synthetic reading for the average
let syntheticReading = SourceReading(
sourceId: HealthBridgeConstants.bundleIdentifier,
sourceName: "Durchschnitt",
sourceCategory: .healthBridge,
value: average,
timestamp: readings.first?.timestamp ?? Date(),
quality: .complete
)
return RuleApplicationResult(
selectedReading: syntheticReading,
strategy: .average,
confidence: .medium,
reason: "Durchschnitt aus \(readings.count) Quellen"
)
}
private func applyCoverageStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
// Prefer readings with complete quality
let completeReadings = readings.filter { $0.quality == .complete }
if completeReadings.count == 1 {
return RuleApplicationResult(
selectedReading: completeReadings[0],
strategy: .coverage,
confidence: .high,
reason: "Einzige Quelle mit vollständigen Daten: \(completeReadings[0].sourceName)"
)
}
// If multiple complete readings, fall back to priority
if !completeReadings.isEmpty {
let sorted = completeReadings.sorted { $0.sourceCategory.priority > $1.sourceCategory.priority }
if let first = sorted.first {
return RuleApplicationResult(
selectedReading: first,
strategy: .coverage,
confidence: .medium,
reason: "Mehrere Quellen verfügbar, gewählt: \(first.sourceName)"
)
}
}
// No complete readings, use any reading with highest priority
let sorted = readings.sorted { $0.sourceCategory.priority > $1.sourceCategory.priority }
if let first = sorted.first {
return RuleApplicationResult(
selectedReading: first,
strategy: .coverage,
confidence: .low,
reason: "Keine vollständigen Daten, gewählt: \(first.sourceName)"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .coverage,
confidence: .low,
reason: "Keine Quelle mit Daten gefunden"
)
}
private func applyCoverageThenHigherStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
// First check if one source has data and others don't (coverage)
let nonZeroReadings = readings.filter { $0.value > 0 }
let zeroReadings = readings.filter { $0.value == 0 }
// If only one source has data, it wins on coverage
if nonZeroReadings.count == 1 && !zeroReadings.isEmpty {
return RuleApplicationResult(
selectedReading: nonZeroReadings[0],
strategy: .coverageThenHigher,
confidence: .high,
reason: "Einzige Quelle mit Daten: \(nonZeroReadings[0].sourceName)"
)
}
// Multiple sources have data, use higher wins
if nonZeroReadings.count > 1 {
let sorted = nonZeroReadings.sorted { $0.value > $1.value }
if let highest = sorted.first {
return RuleApplicationResult(
selectedReading: highest,
strategy: .coverageThenHigher,
confidence: .medium,
reason: "Höherer Wert bei Konflikt: \(highest.formattedValue) von \(highest.sourceName)"
)
}
}
// Fallback
if let first = readings.first {
return RuleApplicationResult(
selectedReading: first,
strategy: .coverageThenHigher,
confidence: .low,
reason: "Fallback auf erste Quelle"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .coverageThenHigher,
confidence: .low,
reason: "Keine Daten verfügbar"
)
}
private func applyMostRecentStrategy(
readings: [SourceReading],
rule: MergeRule
) -> RuleApplicationResult {
let sorted = readings.sorted { $0.timestamp > $1.timestamp }
if let mostRecent = sorted.first {
return RuleApplicationResult(
selectedReading: mostRecent,
strategy: .mostRecent,
confidence: .high,
reason: "Neuester Wert von \(mostRecent.sourceName)"
)
}
return RuleApplicationResult(
selectedReading: nil,
strategy: .mostRecent,
confidence: .low,
reason: "Keine Zeitstempel verfügbar"
)
}
// MARK: - Threshold Check
func shouldRequestManualReview(
readings: [SourceReading],
dataType: HealthDataType
) -> Bool {
let rule = getRule(for: dataType)
guard let threshold = rule.thresholdForManualReview else {
return rule.strategy == .manual
}
let values = readings.map { $0.value }.filter { $0 > 0 }
guard values.count >= 2,
let min = values.min(),
let max = values.max(),
min > 0 else {
return false
}
let percentDiff = (max - min) / min * 100
return percentDiff > threshold
}
}
// MARK: - Rule Application Result
struct RuleApplicationResult {
let selectedReading: SourceReading?
let strategy: MergeStrategy
let confidence: RuleConfidence
let reason: String
var resolvedValue: Double? {
selectedReading?.value
}
var winningSourceId: String? {
selectedReading?.sourceId
}
}
enum RuleConfidence: String, Codable {
case high = "high"
case medium = "medium"
case low = "low"
case requiresManual = "requires_manual"
var displayName: String {
switch self {
case .high: return "Hohe Sicherheit"
case .medium: return "Mittlere Sicherheit"
case .low: return "Geringe Sicherheit"
case .requiresManual: return "Manuelle Prüfung"
}
}
var icon: String {
switch self {
case .high: return "checkmark.seal.fill"
case .medium: return "checkmark.seal"
case .low: return "questionmark.circle"
case .requiresManual: return "hand.raised.fill"
}
}
}
// MARK: - Rule Storage Manager
class RuleStorageManager {
private let userDefaults = UserDefaults.standard
private let rulesKey = "healthbridge.merge.rules"
func saveRules(_ rules: [HealthDataType: MergeRule]) {
do {
let data = try JSONEncoder().encode(rules)
userDefaults.set(data, forKey: rulesKey)
} catch {
print("Failed to save rules: \(error)")
}
}
func loadRules() -> [HealthDataType: MergeRule] {
guard let data = userDefaults.data(forKey: rulesKey) else {
return [:]
}
do {
return try JSONDecoder().decode([HealthDataType: MergeRule].self, from: data)
} catch {
print("Failed to load rules: \(error)")
return [:]
}
}
}
// MARK: - Blood Pressure Handler
class BloodPressureHandler {
static let shared = BloodPressureHandler()
struct ValidationResult {
let isValid: Bool
let issues: [String]
}
func validate(systolic: Double, diastolic: Double) -> ValidationResult {
var issues: [String] = []
// Range validation
if systolic < 70 || systolic > 200 {
issues.append("Systolischer Wert ausserhalb des Normalbereichs (70-200 mmHg)")
}
if diastolic < 40 || diastolic > 130 {
issues.append("Diastolischer Wert ausserhalb des Normalbereichs (40-130 mmHg)")
}
// Plausibility check
if diastolic >= systolic {
issues.append("Diastolischer Wert muss kleiner als systolischer Wert sein")
}
if systolic - diastolic < 20 {
issues.append("Pulsdruck zu gering (< 20 mmHg)")
}
if systolic - diastolic > 100 {
issues.append("Pulsdruck zu hoch (> 100 mmHg)")
}
return ValidationResult(isValid: issues.isEmpty, issues: issues)
}
func classifyBloodPressure(systolic: Double, diastolic: Double) -> BloodPressureClassification {
if systolic < 120 && diastolic < 80 {
return .normal
} else if systolic < 130 && diastolic < 80 {
return .elevated
} else if systolic < 140 || diastolic < 90 {
return .hypertensionStage1
} else if systolic < 180 || diastolic < 120 {
return .hypertensionStage2
} else {
return .hypertensiveCrisis
}
}
enum BloodPressureClassification: String {
case normal = "Normal"
case elevated = "Erhöht"
case hypertensionStage1 = "Bluthochdruck Stufe 1"
case hypertensionStage2 = "Bluthochdruck Stufe 2"
case hypertensiveCrisis = "Hypertensive Krise"
var color: String {
switch self {
case .normal: return "green"
case .elevated: return "yellow"
case .hypertensionStage1: return "orange"
case .hypertensionStage2: return "red"
case .hypertensiveCrisis: return "purple"
}
}
var recommendation: String {
switch self {
case .normal:
return "Weiter so! Regelmässige Kontrolle empfohlen."
case .elevated:
return "Lebensstiländerungen empfohlen. Mehr Bewegung, weniger Salz."
case .hypertensionStage1:
return "Arztbesuch empfohlen. Möglicherweise Medikation erforderlich."
case .hypertensionStage2:
return "Zeitnaher Arztbesuch erforderlich. Medikation wahrscheinlich notwendig."
case .hypertensiveCrisis:
return "SOFORT medizinische Hilfe aufsuchen!"
}
}
}
}
+409
View File
@@ -0,0 +1,409 @@
import Foundation
import HealthKit
import Combine
// MARK: - Source Manager
@MainActor
class SourceManager: ObservableObject {
static let shared = SourceManager()
@Published var sources: [HealthSource] = []
@Published var sourceProfiles: [String: SourceProfile] = [:]
@Published var isDiscovering = false
private let healthKitManager = HealthKitManager.shared
private let storage = SourceStorageManager()
private init() {}
// MARK: - Source Discovery
func discoverSources() async {
isDiscovering = true
defer { isDiscovering = false }
await healthKitManager.discoverSources()
sources = healthKitManager.discoveredSources
// Load saved source profiles and merge with discovered sources
let savedProfiles = storage.loadSourceProfiles()
for source in sources {
if let savedProfile = savedProfiles[source.bundleIdentifier] {
sourceProfiles[source.bundleIdentifier] = savedProfile
} else {
sourceProfiles[source.bundleIdentifier] = SourceProfile(source: source)
}
}
}
// MARK: - Source Classification
func classifySource(_ bundleIdentifier: String) -> SourceCategory {
let lowercased = bundleIdentifier.lowercased()
// HealthBridge
if lowercased.contains("healthbridge") {
return .healthBridge
}
// Apple Devices
if lowercased.contains("com.apple") {
if lowercased.contains("watch") || lowercased.contains("nano") {
return .watch
}
if lowercased.contains("health") {
return .iPhone
}
}
// Known Watch Brands
let watchBrands = ["huawei", "samsung", "galaxy", "fitbit", "garmin", "polar",
"withings", "amazfit", "xiaomi", "honor", "oppo", "suunto",
"coros", "whoop", "oura"]
for brand in watchBrands {
if lowercased.contains(brand) {
return .thirdPartyWatch
}
}
// Known Health Apps
let healthApps = ["strava", "nike", "adidas", "runtastic", "runkeeper",
"mapmyrun", "endomondo", "myfitnesspal", "flo", "clue"]
for app in healthApps {
if lowercased.contains(app) {
return .thirdPartyApp
}
}
return .unknown
}
// MARK: - Source Capabilities
func getSourceCapabilities(_ source: HealthSource) -> SourceCapabilities {
let category = source.category
switch category {
case .iPhone:
return SourceCapabilities(
canMeasureSteps: true,
canMeasureDistance: true,
canMeasureFloors: true,
canMeasureHeartRate: false,
canMeasureBloodPressure: false,
canMeasureBloodOxygen: false,
canMeasureSleep: false,
canMeasureWorkouts: true,
hasGPS: true,
hasBarometer: true,
hasAccelerometer: true
)
case .watch:
return SourceCapabilities(
canMeasureSteps: true,
canMeasureDistance: true,
canMeasureFloors: false,
canMeasureHeartRate: true,
canMeasureBloodPressure: false,
canMeasureBloodOxygen: true,
canMeasureSleep: true,
canMeasureWorkouts: true,
hasGPS: true,
hasBarometer: false,
hasAccelerometer: true
)
case .thirdPartyWatch:
// Check for specific features based on name
let name = source.name.lowercased()
let isHuaweiD2 = name.contains("huawei") && (name.contains("d2") || name.contains("watch d"))
return SourceCapabilities(
canMeasureSteps: true,
canMeasureDistance: true,
canMeasureFloors: false,
canMeasureHeartRate: true,
canMeasureBloodPressure: isHuaweiD2, // Huawei Watch D2 has BP sensor
canMeasureBloodOxygen: true,
canMeasureSleep: true,
canMeasureWorkouts: true,
hasGPS: true,
hasBarometer: false,
hasAccelerometer: true
)
case .thirdPartyApp:
return SourceCapabilities(
canMeasureSteps: true,
canMeasureDistance: true,
canMeasureFloors: false,
canMeasureHeartRate: false,
canMeasureBloodPressure: false,
canMeasureBloodOxygen: false,
canMeasureSleep: false,
canMeasureWorkouts: true,
hasGPS: true,
hasBarometer: false,
hasAccelerometer: false
)
case .healthBridge, .unknown:
return SourceCapabilities.empty
}
}
// MARK: - Source Health
func getSourceHealth(_ source: HealthSource) async -> SourceHealthReport {
var dataGaps: [HealthDataType: [TimeWindow]] = [:]
var lastActivityByType: [HealthDataType: Date] = [:]
var recordCountByType: [HealthDataType: Int] = [:]
let calendar = Calendar.current
let now = Date()
let yesterday = calendar.date(byAdding: .day, value: -1, to: now)!
for dataType in source.supportedDataTypes {
do {
let samples = try await healthKitManager.fetchSamples(
for: dataType,
from: yesterday,
to: now
)
let matchingSamples = samples.filter {
$0.sourceRevision.source.bundleIdentifier == source.bundleIdentifier
}
recordCountByType[dataType] = matchingSamples.count
if let lastSample = matchingSamples.last {
lastActivityByType[dataType] = lastSample.endDate
}
// Detect gaps
let gaps = detectDataGaps(
in: matchingSamples,
from: yesterday,
to: now,
expectedInterval: 15 * 60 // 15 minutes
)
if !gaps.isEmpty {
dataGaps[dataType] = gaps
}
} catch {
print("Error checking health for \(dataType): \(error)")
}
}
let overallQuality: DataQuality
if recordCountByType.values.reduce(0, +) == 0 {
overallQuality = .missing
} else if dataGaps.isEmpty {
overallQuality = .complete
} else {
overallQuality = .partial
}
return SourceHealthReport(
source: source,
lastActivityByType: lastActivityByType,
recordCountByType: recordCountByType,
dataGaps: dataGaps,
overallQuality: overallQuality,
checkedAt: Date()
)
}
private func detectDataGaps(
in samples: [HKSample],
from start: Date,
to end: Date,
expectedInterval: TimeInterval
) -> [TimeWindow] {
guard !samples.isEmpty else {
return [TimeWindow(start: start, end: end)]
}
var gaps: [TimeWindow] = []
let sortedSamples = samples.sorted { $0.startDate < $1.startDate }
// Check gap at beginning
if let firstSample = sortedSamples.first,
firstSample.startDate.timeIntervalSince(start) > expectedInterval * 2 {
gaps.append(TimeWindow(start: start, end: firstSample.startDate))
}
// Check gaps between samples
for i in 0..<(sortedSamples.count - 1) {
let currentEnd = sortedSamples[i].endDate
let nextStart = sortedSamples[i + 1].startDate
let gap = nextStart.timeIntervalSince(currentEnd)
if gap > expectedInterval * 2 {
gaps.append(TimeWindow(start: currentEnd, end: nextStart))
}
}
// Check gap at end
if let lastSample = sortedSamples.last,
end.timeIntervalSince(lastSample.endDate) > expectedInterval * 2 {
gaps.append(TimeWindow(start: lastSample.endDate, end: end))
}
return gaps
}
// MARK: - Priority Management
func setPriority(_ priority: Int, for source: HealthSource, dataType: HealthDataType) {
guard var profile = sourceProfiles[source.bundleIdentifier] else { return }
profile.priorities[dataType] = priority
sourceProfiles[source.bundleIdentifier] = profile
storage.saveSourceProfiles(sourceProfiles)
}
func getPriority(for source: HealthSource, dataType: HealthDataType) -> Int {
if let profile = sourceProfiles[source.bundleIdentifier],
let priority = profile.priorities[dataType] {
return priority
}
return source.category.priority
}
func getSourcesByPriority(for dataType: HealthDataType) -> [HealthSource] {
return sources
.filter { $0.supportedDataTypes.contains(dataType) }
.sorted { getPriority(for: $0, dataType: dataType) > getPriority(for: $1, dataType: dataType) }
}
// MARK: - Source Enable/Disable
func setEnabled(_ enabled: Bool, for source: HealthSource) {
guard var profile = sourceProfiles[source.bundleIdentifier] else { return }
profile.isEnabled = enabled
sourceProfiles[source.bundleIdentifier] = profile
storage.saveSourceProfiles(sourceProfiles)
}
func isEnabled(_ source: HealthSource) -> Bool {
return sourceProfiles[source.bundleIdentifier]?.isEnabled ?? true
}
}
// MARK: - Source Profile
struct SourceProfile: Codable {
let bundleIdentifier: String
var priorities: [HealthDataType: Int]
var isEnabled: Bool
var customName: String?
var notes: String?
let addedAt: Date
init(source: HealthSource) {
self.bundleIdentifier = source.bundleIdentifier
self.priorities = [:]
self.isEnabled = true
self.customName = nil
self.notes = nil
self.addedAt = Date()
}
}
// MARK: - Source Capabilities
struct SourceCapabilities {
let canMeasureSteps: Bool
let canMeasureDistance: Bool
let canMeasureFloors: Bool
let canMeasureHeartRate: Bool
let canMeasureBloodPressure: Bool
let canMeasureBloodOxygen: Bool
let canMeasureSleep: Bool
let canMeasureWorkouts: Bool
let hasGPS: Bool
let hasBarometer: Bool
let hasAccelerometer: Bool
static let empty = SourceCapabilities(
canMeasureSteps: false,
canMeasureDistance: false,
canMeasureFloors: false,
canMeasureHeartRate: false,
canMeasureBloodPressure: false,
canMeasureBloodOxygen: false,
canMeasureSleep: false,
canMeasureWorkouts: false,
hasGPS: false,
hasBarometer: false,
hasAccelerometer: false
)
var supportedDataTypes: Set<HealthDataType> {
var types = Set<HealthDataType>()
if canMeasureSteps { types.insert(.steps) }
if canMeasureDistance { types.insert(.distance) }
if canMeasureFloors { types.insert(.floorsClimbed) }
if canMeasureHeartRate {
types.insert(.heartRate)
types.insert(.restingHeartRate)
}
if canMeasureBloodPressure {
types.insert(.bloodPressureSystolic)
types.insert(.bloodPressureDiastolic)
}
if canMeasureBloodOxygen { types.insert(.bloodOxygen) }
if canMeasureSleep { types.insert(.sleep) }
return types
}
}
// MARK: - Source Health Report
struct SourceHealthReport {
let source: HealthSource
let lastActivityByType: [HealthDataType: Date]
let recordCountByType: [HealthDataType: Int]
let dataGaps: [HealthDataType: [TimeWindow]]
let overallQuality: DataQuality
let checkedAt: Date
var lastOverallActivity: Date? {
lastActivityByType.values.max()
}
var totalRecordCount: Int {
recordCountByType.values.reduce(0, +)
}
var hasSignificantGaps: Bool {
dataGaps.values.flatMap { $0 }.contains { $0.duration > 3600 } // > 1 hour gap
}
}
// MARK: - Source Storage Manager
class SourceStorageManager {
private let userDefaults = UserDefaults.standard
private let profilesKey = "healthbridge.source.profiles"
func saveSourceProfiles(_ profiles: [String: SourceProfile]) {
do {
let data = try JSONEncoder().encode(profiles)
userDefaults.set(data, forKey: profilesKey)
} catch {
print("Failed to save source profiles: \(error)")
}
}
func loadSourceProfiles() -> [String: SourceProfile] {
guard let data = userDefaults.data(forKey: profilesKey) else {
return [:]
}
do {
return try JSONDecoder().decode([String: SourceProfile].self, from: data)
} catch {
print("Failed to load source profiles: \(error)")
return [:]
}
}
}
+301
View File
@@ -0,0 +1,301 @@
import Foundation
import Combine
import UserNotifications
// MARK: - Sync Coordinator
@MainActor
class SyncCoordinator: ObservableObject {
static let shared = SyncCoordinator()
private let healthKitManager = HealthKitManager.shared
private let sourceManager = SourceManager.shared
private let dataReader = DataReader.shared
private let ruleEngine = RuleEngine.shared
private let mergeEngine = MergeEngine.shared
private let dataWriter = DataWriter.shared
@Published var isSyncing = false
@Published var syncProgress: Double = 0
@Published var lastSyncDate: Date?
@Published var lastSyncResult: SyncResult?
@Published var pendingConflicts: [Conflict] = []
@Published var syncHistory: [SyncResult] = []
private let syncHistoryKey = "healthbridge.sync.history"
private let maxHistoryItems = 50
private init() {
loadSyncHistory()
}
// MARK: - Main Sync
func performSync(
for date: Date = Date(),
dataTypes: [HealthDataType] = HealthDataType.allCases
) async throws {
guard !isSyncing else { return }
isSyncing = true
syncProgress = 0
defer { isSyncing = false }
let startTime = Date()
var result = SyncResult(startedAt: startTime)
do {
// Step 1: Refresh sources (10%)
syncProgress = 0.05
await sourceManager.discoverSources()
syncProgress = 0.1
// Step 2: Detect conflicts (40%)
let conflicts = try await dataReader.detectConflicts(for: date, dataTypes: dataTypes)
result.totalConflicts = conflicts.count
syncProgress = 0.4
// Step 3: Process conflicts with merge engine (70%)
let operations = await mergeEngine.processConflicts(conflicts)
syncProgress = 0.7
let autoResolved = operations.filter { $0.status == .resolved }
let pendingManual = operations.filter { $0.status == .pendingManualReview }
result.autoResolved = autoResolved.count
result.pendingManualReview = pendingManual.count
// Update pending conflicts
pendingConflicts = pendingManual.map { $0.conflict }
// Step 4: Write resolved records (90%)
let recordsToWrite = autoResolved.compactMap { $0.mergedRecord }
if !recordsToWrite.isEmpty {
let writeResult = await dataWriter.writeBatch(recordsToWrite)
result.writtenRecords = writeResult.successCount
result.writeErrors = writeResult.failureCount
}
syncProgress = 0.9
// Step 5: Finalize (100%)
result.completedAt = Date()
result.status = .success
syncProgress = 1.0
} catch {
result.status = .failed
result.error = error.localizedDescription
result.completedAt = Date()
throw error
}
lastSyncDate = Date()
lastSyncResult = result
addToHistory(result)
// Send notification if there are pending conflicts
if result.pendingManualReview > 0 {
await sendConflictNotification(count: result.pendingManualReview)
}
}
// MARK: - Quick Sync
func performQuickSync() async throws {
try await performSync(
for: Date(),
dataTypes: [.steps, .heartRate, .activeEnergy]
)
}
// MARK: - Sync Specific Data Type
func syncDataType(_ dataType: HealthDataType, for date: Date = Date()) async throws {
try await performSync(for: date, dataTypes: [dataType])
}
// MARK: - Manual Conflict Resolution
func resolveConflict(_ conflict: Conflict, selectedReadingId: UUID) async throws {
guard let resolution = mergeEngine.resolveConflictManually(conflict, selectedReadingId: selectedReadingId) else {
throw SyncError.resolutionFailed
}
var resolvedConflict = conflict
resolvedConflict.status = .resolved
resolvedConflict.resolution = resolution
resolvedConflict.resolvedAt = Date()
let mergedRecord = mergeEngine.createMergedRecord(from: resolvedConflict, resolution: resolution)
// Write the record
_ = try await dataWriter.writeRecord(mergedRecord)
// Remove from pending
pendingConflicts.removeAll { $0.id == conflict.id }
}
func ignoreConflict(_ conflict: Conflict) {
pendingConflicts.removeAll { $0.id == conflict.id }
}
// MARK: - Sync History
private func loadSyncHistory() {
guard let data = UserDefaults.standard.data(forKey: syncHistoryKey),
let history = try? JSONDecoder().decode([SyncResult].self, from: data) else {
return
}
syncHistory = history
}
private func addToHistory(_ result: SyncResult) {
syncHistory.insert(result, at: 0)
if syncHistory.count > maxHistoryItems {
syncHistory = Array(syncHistory.prefix(maxHistoryItems))
}
saveSyncHistory()
}
private func saveSyncHistory() {
guard let data = try? JSONEncoder().encode(syncHistory) else { return }
UserDefaults.standard.set(data, forKey: syncHistoryKey)
}
func clearHistory() {
syncHistory.removeAll()
UserDefaults.standard.removeObject(forKey: syncHistoryKey)
}
// MARK: - Notifications
private func sendConflictNotification(count: Int) async {
let center = UNUserNotificationCenter.current()
let settings = await center.notificationSettings()
guard settings.authorizationStatus == .authorized else { return }
let content = UNMutableNotificationContent()
content.title = "HealthBridge"
content.body = count == 1
? "1 Konflikt erfordert Ihre Aufmerksamkeit"
: "\(count) Konflikte erfordern Ihre Aufmerksamkeit"
content.sound = .default
content.badge = NSNumber(value: count)
let request = UNNotificationRequest(
identifier: "healthbridge.conflicts",
content: content,
trigger: nil
)
try? await center.add(request)
}
func requestNotificationPermission() async -> Bool {
let center = UNUserNotificationCenter.current()
do {
return try await center.requestAuthorization(options: [.alert, .sound, .badge])
} catch {
return false
}
}
// MARK: - Statistics
var todayStats: TodayStats {
let today = Calendar.current.startOfDay(for: Date())
let todaySyncs = syncHistory.filter {
Calendar.current.isDate($0.startedAt, inSameDayAs: today)
}
return TodayStats(
syncCount: todaySyncs.count,
totalConflicts: todaySyncs.reduce(0) { $0 + $1.totalConflicts },
autoResolved: todaySyncs.reduce(0) { $0 + $1.autoResolved },
pendingManual: pendingConflicts.count,
lastSync: lastSyncDate
)
}
}
// MARK: - Supporting Types
struct SyncResult: Identifiable, Codable {
let id = UUID()
let startedAt: Date
var completedAt: Date?
var status: SyncStatus = .inProgress
var totalConflicts = 0
var autoResolved = 0
var pendingManualReview = 0
var writtenRecords = 0
var writeErrors = 0
var error: String?
enum SyncStatus: String, Codable {
case inProgress = "in_progress"
case success = "success"
case partialSuccess = "partial_success"
case failed = "failed"
}
var duration: TimeInterval? {
guard let completed = completedAt else { return nil }
return completed.timeIntervalSince(startedAt)
}
var formattedDuration: String {
guard let duration = duration else { return "" }
if duration < 1 {
return "< 1s"
}
return String(format: "%.1fs", duration)
}
var successRate: Double {
guard totalConflicts > 0 else { return 1.0 }
return Double(autoResolved) / Double(totalConflicts)
}
}
struct TodayStats {
let syncCount: Int
let totalConflicts: Int
let autoResolved: Int
let pendingManual: Int
let lastSync: Date?
var resolutionRate: Double {
guard totalConflicts > 0 else { return 1.0 }
return Double(autoResolved) / Double(totalConflicts)
}
var formattedLastSync: String {
guard let date = lastSync else { return "Nie" }
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .abbreviated
return formatter.localizedString(for: date, relativeTo: Date())
}
}
enum SyncError: LocalizedError {
case notAuthorized
case syncInProgress
case resolutionFailed
case writeFailed
var errorDescription: String? {
switch self {
case .notAuthorized:
return "Keine Berechtigung für HealthKit"
case .syncInProgress:
return "Synchronisierung läuft bereits"
case .resolutionFailed:
return "Konfliktauflösung fehlgeschlagen"
case .writeFailed:
return "Schreiben der Daten fehlgeschlagen"
}
}
}
+153
View File
@@ -0,0 +1,153 @@
import Foundation
import SwiftUI
// MARK: - Date Extensions
extension Date {
var startOfDay: Date {
Calendar.current.startOfDay(for: self)
}
var endOfDay: Date {
Calendar.current.date(byAdding: .day, value: 1, to: startOfDay)!.addingTimeInterval(-1)
}
var isToday: Bool {
Calendar.current.isDateInToday(self)
}
var isYesterday: Bool {
Calendar.current.isDateInYesterday(self)
}
func formatted(style: DateFormatter.Style) -> String {
let formatter = DateFormatter()
formatter.dateStyle = style
formatter.timeStyle = .none
return formatter.string(from: self)
}
func formattedTime() -> String {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter.string(from: self)
}
func formattedRelative() -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .abbreviated
return formatter.localizedString(for: self, relativeTo: Date())
}
func adding(days: Int) -> Date {
Calendar.current.date(byAdding: .day, value: days, to: self)!
}
func adding(hours: Int) -> Date {
Calendar.current.date(byAdding: .hour, value: hours, to: self)!
}
func adding(minutes: Int) -> Date {
Calendar.current.date(byAdding: .minute, value: minutes, to: self)!
}
}
// MARK: - Double Extensions
extension Double {
func formatted(decimals: Int = 1) -> String {
String(format: "%.\(decimals)f", self)
}
var formattedAsInteger: String {
String(format: "%.0f", self)
}
var formattedAsPercentage: String {
String(format: "%.1f%%", self * 100)
}
}
// MARK: - Array Extensions
extension Array {
func chunked(into size: Int) -> [[Element]] {
stride(from: 0, to: count, by: size).map {
Array(self[$0..<Swift.min($0 + size, count)])
}
}
}
// MARK: - Color Extensions
extension Color {
static let healthBridgePrimary = Color.blue
static let healthBridgeSecondary = Color.cyan
static let healthBridgeAccent = Color.orange
static func forSeverity(_ severity: ConflictSeverity) -> Color {
switch severity {
case .minor: return .green
case .moderate: return .yellow
case .significant: return .orange
case .major: return .red
}
}
static func forQuality(_ quality: DataQuality) -> Color {
switch quality {
case .complete: return .green
case .partial: return .yellow
case .missing: return .gray
case .invalid: return .red
}
}
}
// MARK: - View Extensions
extension View {
func cardStyle() -> some View {
self
.padding()
.background(Color(.systemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
.shadow(color: .black.opacity(0.05), radius: 4, y: 2)
}
func sectionHeader(_ title: String) -> some View {
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.headline)
.foregroundStyle(.primary)
self
}
}
}
// MARK: - Binding Extensions
extension Binding {
func onChange(_ handler: @escaping (Value) -> Void) -> Binding<Value> {
Binding(
get: { self.wrappedValue },
set: { newValue in
self.wrappedValue = newValue
handler(newValue)
}
)
}
}
// MARK: - Optional Extensions
extension Optional where Wrapped == String {
var orEmpty: String {
self ?? ""
}
var isNilOrEmpty: Bool {
self?.isEmpty ?? true
}
}
// MARK: - Collection Extensions
extension Collection {
var isNotEmpty: Bool {
!isEmpty
}
}
@@ -0,0 +1,185 @@
import Foundation
import UserNotifications
@MainActor
class NotificationManager: ObservableObject {
static let shared = NotificationManager()
@Published var isAuthorized = false
@Published var pendingNotifications: [String] = []
private let center = UNUserNotificationCenter.current()
private init() {
Task {
await checkAuthorization()
}
}
// MARK: - Authorization
func requestAuthorization() async -> Bool {
do {
let granted = try await center.requestAuthorization(options: [.alert, .sound, .badge])
isAuthorized = granted
return granted
} catch {
print("Notification authorization failed: \(error)")
return false
}
}
func checkAuthorization() async {
let settings = await center.notificationSettings()
isAuthorized = settings.authorizationStatus == .authorized
}
// MARK: - Conflict Notifications
func sendConflictNotification(count: Int) async {
guard isAuthorized else { return }
let content = UNMutableNotificationContent()
content.title = "HealthBridge"
if count == 1 {
content.body = "1 neuer Konflikt erfordert Ihre Aufmerksamkeit"
} else {
content.body = "\(count) neue Konflikte erfordern Ihre Aufmerksamkeit"
}
content.sound = .default
content.badge = NSNumber(value: count)
content.categoryIdentifier = "CONFLICT"
let request = UNNotificationRequest(
identifier: "healthbridge.conflict.\(Date().timeIntervalSince1970)",
content: content,
trigger: nil
)
do {
try await center.add(request)
} catch {
print("Failed to send notification: \(error)")
}
}
// MARK: - Sync Notifications
func sendSyncCompleteNotification(
conflictsResolved: Int,
pendingConflicts: Int
) async {
guard isAuthorized else { return }
let content = UNMutableNotificationContent()
content.title = "Sync abgeschlossen"
if pendingConflicts > 0 {
content.body = "\(conflictsResolved) Konflikte gelöst, \(pendingConflicts) offen"
} else {
content.body = "Alle \(conflictsResolved) Konflikte wurden gelöst"
}
content.sound = .default
content.categoryIdentifier = "SYNC_COMPLETE"
let request = UNNotificationRequest(
identifier: "healthbridge.sync.\(Date().timeIntervalSince1970)",
content: content,
trigger: nil
)
do {
try await center.add(request)
} catch {
print("Failed to send notification: \(error)")
}
}
// MARK: - Scheduled Notifications
func scheduleReminder(at hour: Int, minute: Int) async {
guard isAuthorized else { return }
let content = UNMutableNotificationContent()
content.title = "HealthBridge Erinnerung"
content.body = "Vergessen Sie nicht, Ihre Gesundheitsdaten zu synchronisieren"
content.sound = .default
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(
identifier: "healthbridge.reminder.daily",
content: content,
trigger: trigger
)
do {
try await center.add(request)
} catch {
print("Failed to schedule reminder: \(error)")
}
}
func cancelReminder() {
center.removePendingNotificationRequests(withIdentifiers: ["healthbridge.reminder.daily"])
}
// MARK: - Badge Management
func clearBadge() async {
do {
try await center.setBadgeCount(0)
} catch {
print("Failed to clear badge: \(error)")
}
}
func updateBadge(count: Int) async {
do {
try await center.setBadgeCount(count)
} catch {
print("Failed to update badge: \(error)")
}
}
// MARK: - Notification Categories
func registerCategories() {
// Conflict category with actions
let resolveAction = UNNotificationAction(
identifier: "RESOLVE_AUTO",
title: "Automatisch lösen",
options: []
)
let viewAction = UNNotificationAction(
identifier: "VIEW_CONFLICTS",
title: "Anzeigen",
options: [.foreground]
)
let conflictCategory = UNNotificationCategory(
identifier: "CONFLICT",
actions: [resolveAction, viewAction],
intentIdentifiers: [],
options: []
)
// Sync complete category
let syncCategory = UNNotificationCategory(
identifier: "SYNC_COMPLETE",
actions: [viewAction],
intentIdentifiers: [],
options: []
)
center.setNotificationCategories([conflictCategory, syncCategory])
}
}
@@ -0,0 +1,103 @@
import Foundation
import Combine
@MainActor
class DashboardViewModel: ObservableObject {
private let syncCoordinator = SyncCoordinator.shared
private let dataReader = DataReader.shared
private let sourceManager = SourceManager.shared
@Published var dailySummary: DailySummary?
@Published var isLoading = false
@Published var selectedDate = Date()
@Published var errorMessage: String?
private var cancellables = Set<AnyCancellable>()
init() {
setupBindings()
}
private func setupBindings() {
$selectedDate
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.sink { [weak self] date in
Task {
await self?.loadData(for: date)
}
}
.store(in: &cancellables)
}
func loadData(for date: Date = Date()) async {
isLoading = true
errorMessage = nil
do {
dailySummary = try await dataReader.fetchDailySummary(for: date)
} catch {
errorMessage = error.localizedDescription
}
isLoading = false
}
func performSync() async {
do {
try await syncCoordinator.performSync(for: selectedDate)
await loadData(for: selectedDate)
} catch {
errorMessage = "Sync fehlgeschlagen: \(error.localizedDescription)"
}
}
func refresh() async {
await sourceManager.discoverSources()
await loadData(for: selectedDate)
}
var syncStatus: SyncStatus {
if syncCoordinator.isSyncing {
return .syncing
} else if let lastSync = syncCoordinator.lastSyncDate {
let hoursSinceSync = Date().timeIntervalSince(lastSync) / 3600
if hoursSinceSync < 1 {
return .synced
} else if hoursSinceSync < 24 {
return .stale
} else {
return .veryStale
}
} else {
return .neverSynced
}
}
enum SyncStatus {
case syncing
case synced
case stale
case veryStale
case neverSynced
var description: String {
switch self {
case .syncing: return "Synchronisiere..."
case .synced: return "Synchronisiert"
case .stale: return "Sync empfohlen"
case .veryStale: return "Sync überfällig"
case .neverSynced: return "Nie synchronisiert"
}
}
var icon: String {
switch self {
case .syncing: return "arrow.triangle.2.circlepath"
case .synced: return "checkmark.circle.fill"
case .stale: return "exclamationmark.circle"
case .veryStale: return "exclamationmark.triangle"
case .neverSynced: return "xmark.circle"
}
}
}
}
@@ -0,0 +1,241 @@
import SwiftUI
import Charts
struct HealthChart: View {
let dataType: HealthDataType
let data: [ChartDataPoint]
let showConflicts: Bool
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: dataType.icon)
.foregroundStyle(.blue)
Text(dataType.displayName)
.font(.headline)
Spacer()
if let latest = data.last {
Text(latest.formattedValue)
.font(.title3)
.fontWeight(.semibold)
Text(dataType.unit)
.font(.caption)
.foregroundStyle(.secondary)
}
}
if #available(iOS 16.0, *) {
Chart(data) { point in
LineMark(
x: .value("Zeit", point.date),
y: .value("Wert", point.value)
)
.foregroundStyle(.blue)
PointMark(
x: .value("Zeit", point.date),
y: .value("Wert", point.value)
)
.foregroundStyle(point.hasConflict && showConflicts ? .orange : .blue)
.symbolSize(point.hasConflict && showConflicts ? 100 : 50)
}
.frame(height: 150)
.chartXAxis {
AxisMarks(values: .stride(by: .hour, count: 4)) { value in
AxisValueLabel(format: .dateTime.hour())
}
}
.chartYAxis {
AxisMarks(position: .leading)
}
} else {
// Fallback for iOS 15
simpleChart
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
private var simpleChart: some View {
GeometryReader { geometry in
let maxValue = data.map { $0.value }.max() ?? 1
let minValue = data.map { $0.value }.min() ?? 0
let range = maxValue - minValue
Path { path in
guard !data.isEmpty else { return }
let xStep = geometry.size.width / CGFloat(max(1, data.count - 1))
for (index, point) in data.enumerated() {
let x = CGFloat(index) * xStep
let normalizedY = range > 0 ? (point.value - minValue) / range : 0.5
let y = geometry.size.height * (1 - normalizedY)
if index == 0 {
path.move(to: CGPoint(x: x, y: y))
} else {
path.addLine(to: CGPoint(x: x, y: y))
}
}
}
.stroke(Color.blue, lineWidth: 2)
}
.frame(height: 150)
}
}
struct ChartDataPoint: Identifiable {
let id = UUID()
let date: Date
let value: Double
let hasConflict: Bool
let sourceId: String?
var formattedValue: String {
if value == floor(value) {
return String(format: "%.0f", value)
}
return String(format: "%.1f", value)
}
}
// MARK: - Blood Pressure Chart
struct BloodPressureChart: View {
let data: [BloodPressurePoint]
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: "drop.fill")
.foregroundStyle(.red)
Text("Blutdruck")
.font(.headline)
Spacer()
if let latest = data.last {
Text("\(Int(latest.systolic))/\(Int(latest.diastolic))")
.font(.title3)
.fontWeight(.semibold)
Text("mmHg")
.font(.caption)
.foregroundStyle(.secondary)
}
}
if #available(iOS 16.0, *) {
Chart(data) { point in
// Systolic
LineMark(
x: .value("Zeit", point.date),
y: .value("Systolisch", point.systolic)
)
.foregroundStyle(.red)
// Diastolic
LineMark(
x: .value("Zeit", point.date),
y: .value("Diastolisch", point.diastolic)
)
.foregroundStyle(.blue)
}
.frame(height: 150)
.chartYScale(domain: 40...180)
.chartLegend(position: .bottom)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
struct BloodPressurePoint: Identifiable {
let id = UUID()
let date: Date
let systolic: Double
let diastolic: Double
let classification: BloodPressureHandler.BloodPressureClassification
}
// MARK: - Summary Ring
struct SummaryRing: View {
let progress: Double
let color: Color
let icon: String
let value: String
let label: String
var body: some View {
VStack(spacing: 4) {
ZStack {
Circle()
.stroke(color.opacity(0.2), lineWidth: 8)
Circle()
.trim(from: 0, to: min(progress, 1.0))
.stroke(color, style: StrokeStyle(lineWidth: 8, lineCap: .round))
.rotationEffect(.degrees(-90))
VStack(spacing: 2) {
Image(systemName: icon)
.font(.caption)
.foregroundStyle(color)
Text(value)
.font(.caption2)
.fontWeight(.semibold)
}
}
.frame(width: 60, height: 60)
Text(label)
.font(.caption2)
.foregroundStyle(.secondary)
}
}
}
#Preview {
VStack(spacing: 20) {
HealthChart(
dataType: .steps,
data: (0..<24).map { hour in
ChartDataPoint(
date: Calendar.current.date(byAdding: .hour, value: hour, to: Calendar.current.startOfDay(for: Date()))!,
value: Double.random(in: 0...1000),
hasConflict: hour % 5 == 0,
sourceId: nil
)
},
showConflicts: true
)
HStack(spacing: 20) {
SummaryRing(
progress: 0.75,
color: .blue,
icon: "figure.walk",
value: "7.5k",
label: "Schritte"
)
SummaryRing(
progress: 0.5,
color: .red,
icon: "heart.fill",
value: "72",
label: "Puls"
)
SummaryRing(
progress: 1.0,
color: .green,
icon: "checkmark.circle",
value: "100%",
label: "Synced"
)
}
}
.padding()
}
+348
View File
@@ -0,0 +1,348 @@
import SwiftUI
struct ConflictsView: View {
@EnvironmentObject var syncCoordinator: SyncCoordinator
@State private var selectedConflict: Conflict?
@State private var showingDetail = false
var body: some View {
NavigationStack {
Group {
if syncCoordinator.pendingConflicts.isEmpty {
emptyState
} else {
conflictsList
}
}
.navigationTitle("Konflikte")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
Button("Alle automatisch lösen") {
Task { await resolveAllAuto() }
}
Button("Alle ignorieren", role: .destructive) {
ignoreAll()
}
} label: {
Image(systemName: "ellipsis.circle")
}
.disabled(syncCoordinator.pendingConflicts.isEmpty)
}
}
.sheet(item: $selectedConflict) { conflict in
ConflictDetailView(conflict: conflict) { selectedReadingId in
Task {
await resolveConflict(conflict, selectedReadingId: selectedReadingId)
}
}
}
}
}
// MARK: - Empty State
private var emptyState: some View {
VStack(spacing: 16) {
Image(systemName: "checkmark.circle.fill")
.font(.system(size: 64))
.foregroundStyle(.green)
Text("Keine Konflikte")
.font(.title2)
.fontWeight(.semibold)
Text("Alle Ihre Gesundheitsdaten sind synchronisiert")
.font(.subheadline)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.padding()
}
// MARK: - Conflicts List
private var conflictsList: some View {
List {
ForEach(groupedConflicts.keys.sorted(by: { $0.displayName < $1.displayName }), id: \.self) { dataType in
Section(dataType.displayName) {
ForEach(groupedConflicts[dataType] ?? []) { conflict in
ConflictRow(conflict: conflict)
.contentShape(Rectangle())
.onTapGesture {
selectedConflict = conflict
}
}
}
}
}
.listStyle(.insetGrouped)
}
private var groupedConflicts: [HealthDataType: [Conflict]] {
Dictionary(grouping: syncCoordinator.pendingConflicts, by: { $0.dataType })
}
// MARK: - Actions
private func resolveConflict(_ conflict: Conflict, selectedReadingId: UUID) async {
do {
try await syncCoordinator.resolveConflict(conflict, selectedReadingId: selectedReadingId)
selectedConflict = nil
} catch {
print("Failed to resolve conflict: \(error)")
}
}
private func resolveAllAuto() async {
for conflict in syncCoordinator.pendingConflicts {
if let primaryReading = conflict.primarySourceReading {
try? await syncCoordinator.resolveConflict(conflict, selectedReadingId: primaryReading.id)
}
}
}
private func ignoreAll() {
for conflict in syncCoordinator.pendingConflicts {
syncCoordinator.ignoreConflict(conflict)
}
}
}
// MARK: - Conflict Row
struct ConflictRow: View {
let conflict: Conflict
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: conflict.dataType.icon)
.foregroundStyle(.blue)
Text(conflict.timeWindow.formattedRange)
.font(.headline)
Spacer()
severityBadge
}
HStack(spacing: 16) {
ForEach(conflict.readings.prefix(3)) { reading in
VStack(alignment: .leading, spacing: 2) {
Text(reading.sourceName)
.font(.caption)
.foregroundStyle(.secondary)
Text(reading.formattedValue)
.font(.subheadline)
.fontWeight(.medium)
}
}
}
if conflict.readings.count > 3 {
Text("+\(conflict.readings.count - 3) weitere")
.font(.caption)
.foregroundStyle(.secondary)
}
}
.padding(.vertical, 4)
}
private var severityBadge: some View {
Text(conflict.severity.displayName)
.font(.caption2)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(severityColor.opacity(0.2))
.foregroundStyle(severityColor)
.clipShape(Capsule())
}
private var severityColor: Color {
switch conflict.severity {
case .minor: return .green
case .moderate: return .yellow
case .significant: return .orange
case .major: return .red
}
}
}
// MARK: - Conflict Detail View
struct ConflictDetailView: View {
let conflict: Conflict
let onResolve: (UUID) -> Void
@Environment(\.dismiss) private var dismiss
@State private var selectedReadingId: UUID?
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 20) {
// Header
headerSection
// Readings
readingsSection
// Difference Info
differenceSection
Spacer()
}
.padding()
}
.navigationTitle("Konflikt lösen")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Abbrechen") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Auswählen") {
if let id = selectedReadingId {
onResolve(id)
}
}
.disabled(selectedReadingId == nil)
}
}
}
}
// MARK: - Header
private var headerSection: some View {
VStack(spacing: 8) {
Image(systemName: conflict.dataType.icon)
.font(.largeTitle)
.foregroundStyle(.blue)
Text(conflict.dataType.displayName)
.font(.title2)
.fontWeight(.semibold)
Text(conflict.timeWindow.formattedDate)
.font(.subheadline)
.foregroundStyle(.secondary)
Text(conflict.timeWindow.formattedRange)
.font(.subheadline)
.foregroundStyle(.secondary)
}
.padding()
}
// MARK: - Readings
private var readingsSection: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Quellen")
.font(.headline)
ForEach(conflict.readings) { reading in
ReadingCard(
reading: reading,
isSelected: selectedReadingId == reading.id,
dataType: conflict.dataType
)
.onTapGesture {
selectedReadingId = reading.id
}
}
}
}
// MARK: - Difference
private var differenceSection: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Analyse")
.font(.headline)
HStack {
VStack(alignment: .leading) {
Text("Differenz")
.font(.caption)
.foregroundStyle(.secondary)
Text(String(format: "%.1f %@", conflict.valueDifference, conflict.dataType.unit))
.font(.title3)
.fontWeight(.medium)
}
Spacer()
VStack(alignment: .trailing) {
Text("Prozentual")
.font(.caption)
.foregroundStyle(.secondary)
Text(String(format: "%.1f%%", conflict.percentageDifference))
.font(.title3)
.fontWeight(.medium)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
}
// MARK: - Reading Card
struct ReadingCard: View {
let reading: SourceReading
let isSelected: Bool
let dataType: HealthDataType
var body: some View {
HStack {
VStack(alignment: .leading, spacing: 4) {
HStack {
Image(systemName: reading.sourceCategory.icon)
.foregroundStyle(.blue)
Text(reading.sourceName)
.font(.headline)
}
Text(reading.sourceCategory.displayName)
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
VStack(alignment: .trailing, spacing: 4) {
Text(reading.formattedValue)
.font(.title2)
.fontWeight(.semibold)
Text(dataType.unit)
.font(.caption)
.foregroundStyle(.secondary)
}
Image(systemName: isSelected ? "checkmark.circle.fill" : "circle")
.font(.title2)
.foregroundStyle(isSelected ? .blue : .secondary)
}
.padding()
.background(isSelected ? Color.blue.opacity(0.1) : Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(isSelected ? Color.blue : Color.clear, lineWidth: 2)
)
}
}
#Preview {
ConflictsView()
.environmentObject(SyncCoordinator.shared)
}
+42
View File
@@ -0,0 +1,42 @@
import SwiftUI
struct ContentView: View {
@EnvironmentObject var appState: AppState
@EnvironmentObject var syncCoordinator: SyncCoordinator
var body: some View {
TabView(selection: $appState.selectedTab) {
DashboardView()
.tabItem {
Label("Dashboard", systemImage: "chart.bar.fill")
}
.tag(AppState.Tab.dashboard)
ConflictsView()
.tabItem {
Label("Konflikte", systemImage: "arrow.triangle.2.circlepath")
}
.tag(AppState.Tab.conflicts)
.badge(syncCoordinator.pendingConflicts.count)
RulesView()
.tabItem {
Label("Regeln", systemImage: "slider.horizontal.3")
}
.tag(AppState.Tab.rules)
SourcesView()
.tabItem {
Label("Quellen", systemImage: "antenna.radiowaves.left.and.right")
}
.tag(AppState.Tab.sources)
}
.tint(.blue)
}
}
#Preview {
ContentView()
.environmentObject(AppState())
.environmentObject(SyncCoordinator.shared)
}
+379
View File
@@ -0,0 +1,379 @@
import SwiftUI
struct DashboardView: View {
@EnvironmentObject var appState: AppState
@EnvironmentObject var syncCoordinator: SyncCoordinator
@StateObject private var dataReader = DataReader.shared
@State private var dailySummary: DailySummary?
@State private var isLoading = false
@State private var selectedDate = Date()
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 20) {
// Sync Status Card
syncStatusCard
// Date Picker
datePicker
// Health Metrics
if let summary = dailySummary {
healthMetricsGrid(summary: summary)
} else if isLoading {
loadingView
} else {
emptyStateView
}
// Pending Conflicts Alert
if !syncCoordinator.pendingConflicts.isEmpty {
pendingConflictsCard
}
// Recent Sync History
recentSyncHistory
}
.padding()
}
.navigationTitle("HealthBridge")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
Task { await performSync() }
} label: {
if syncCoordinator.isSyncing {
ProgressView()
.scaleEffect(0.8)
} else {
Image(systemName: "arrow.clockwise")
}
}
.disabled(syncCoordinator.isSyncing)
}
}
.refreshable {
await loadData()
}
.task {
await loadData()
}
.onChange(of: selectedDate) {
Task { await loadData() }
}
}
}
// MARK: - Sync Status Card
private var syncStatusCard: some View {
VStack(spacing: 12) {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text("Heute")
.font(.headline)
Text(syncCoordinator.todayStats.formattedLastSync)
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
if syncCoordinator.isSyncing {
VStack(alignment: .trailing) {
ProgressView(value: syncCoordinator.syncProgress)
.frame(width: 60)
Text("Synchronisiere...")
.font(.caption2)
.foregroundStyle(.secondary)
}
} else {
Image(systemName: "checkmark.circle.fill")
.font(.title2)
.foregroundStyle(.green)
}
}
Divider()
HStack(spacing: 20) {
StatItem(
value: "\(syncCoordinator.todayStats.syncCount)",
label: "Syncs",
icon: "arrow.triangle.2.circlepath"
)
StatItem(
value: "\(syncCoordinator.todayStats.totalConflicts)",
label: "Konflikte",
icon: "exclamationmark.triangle"
)
StatItem(
value: "\(Int(syncCoordinator.todayStats.resolutionRate * 100))%",
label: "Gelöst",
icon: "checkmark.circle"
)
}
}
.padding()
.background(Color(.systemBackground))
.clipShape(RoundedRectangle(cornerRadius: 16))
.shadow(color: .black.opacity(0.05), radius: 8, y: 4)
}
// MARK: - Date Picker
private var datePicker: some View {
DatePicker(
"Datum",
selection: $selectedDate,
in: ...Date(),
displayedComponents: .date
)
.datePickerStyle(.compact)
.padding(.horizontal)
}
// MARK: - Health Metrics Grid
private func healthMetricsGrid(summary: DailySummary) -> some View {
LazyVGrid(columns: [
GridItem(.flexible()),
GridItem(.flexible())
], spacing: 16) {
ForEach(HealthDataType.allCases) { dataType in
if let value = summary.values[dataType], value > 0 {
HealthMetricCard(
dataType: dataType,
value: summary.formattedValue(for: dataType),
conflictCount: summary.conflictCounts[dataType] ?? 0
)
}
}
}
}
// MARK: - Pending Conflicts Card
private var pendingConflictsCard: some View {
Button {
appState.selectedTab = .conflicts
} label: {
HStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(.orange)
VStack(alignment: .leading) {
Text("\(syncCoordinator.pendingConflicts.count) Konflikte zu prüfen")
.font(.headline)
Text("Tippen zum Anzeigen")
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
Image(systemName: "chevron.right")
.foregroundStyle(.secondary)
}
.padding()
.background(Color.orange.opacity(0.1))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
.buttonStyle(.plain)
}
// MARK: - Recent Sync History
private var recentSyncHistory: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Letzte Synchronisierungen")
.font(.headline)
ForEach(syncCoordinator.syncHistory.prefix(5)) { result in
SyncHistoryRow(result: result)
}
if syncCoordinator.syncHistory.isEmpty {
Text("Keine Synchronisierungen")
.font(.subheadline)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity)
.padding()
}
}
.padding()
.background(Color(.systemBackground))
.clipShape(RoundedRectangle(cornerRadius: 16))
.shadow(color: .black.opacity(0.05), radius: 8, y: 4)
}
// MARK: - Loading & Empty States
private var loadingView: some View {
VStack(spacing: 12) {
ProgressView()
Text("Lade Daten...")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 40)
}
private var emptyStateView: some View {
VStack(spacing: 12) {
Image(systemName: "heart.text.square")
.font(.system(size: 48))
.foregroundStyle(.secondary)
Text("Keine Daten verfügbar")
.font(.headline)
Text("Synchronisieren Sie, um Daten zu laden")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 40)
}
// MARK: - Actions
private func loadData() async {
isLoading = true
defer { isLoading = false }
do {
dailySummary = try await dataReader.fetchDailySummary(for: selectedDate)
} catch {
print("Failed to load data: \(error)")
}
}
private func performSync() async {
do {
try await syncCoordinator.performSync(for: selectedDate)
await loadData()
} catch {
print("Sync failed: \(error)")
}
}
}
// MARK: - Supporting Views
struct StatItem: View {
let value: String
let label: String
let icon: String
var body: some View {
VStack(spacing: 4) {
Image(systemName: icon)
.font(.caption)
.foregroundStyle(.secondary)
Text(value)
.font(.title3)
.fontWeight(.semibold)
Text(label)
.font(.caption2)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
}
}
struct HealthMetricCard: View {
let dataType: HealthDataType
let value: String
let conflictCount: Int
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Image(systemName: dataType.icon)
.foregroundStyle(.blue)
Spacer()
if conflictCount > 0 {
Text("\(conflictCount)")
.font(.caption2)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background(Color.orange.opacity(0.2))
.clipShape(Capsule())
}
}
Text(value)
.font(.title2)
.fontWeight(.semibold)
Text(dataType.displayName)
.font(.caption)
.foregroundStyle(.secondary)
}
.padding()
.background(Color(.systemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
.shadow(color: .black.opacity(0.05), radius: 4, y: 2)
}
}
struct SyncHistoryRow: View {
let result: SyncResult
var body: some View {
HStack {
Image(systemName: statusIcon)
.foregroundStyle(statusColor)
VStack(alignment: .leading, spacing: 2) {
Text(formattedDate)
.font(.subheadline)
Text("\(result.autoResolved)/\(result.totalConflicts) gelöst")
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
Text(result.formattedDuration)
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.vertical, 4)
}
private var statusIcon: String {
switch result.status {
case .success: return "checkmark.circle.fill"
case .partialSuccess: return "exclamationmark.circle.fill"
case .failed: return "xmark.circle.fill"
case .inProgress: return "arrow.clockwise"
}
}
private var statusColor: Color {
switch result.status {
case .success: return .green
case .partialSuccess: return .orange
case .failed: return .red
case .inProgress: return .blue
}
}
private var formattedDate: String {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
return formatter.string(from: result.startedAt)
}
}
#Preview {
DashboardView()
.environmentObject(AppState())
.environmentObject(SyncCoordinator.shared)
}
+252
View File
@@ -0,0 +1,252 @@
import SwiftUI
struct RulesView: View {
@StateObject private var ruleEngine = RuleEngine.shared
@StateObject private var sourceManager = SourceManager.shared
@State private var selectedDataType: HealthDataType?
@State private var showingResetConfirmation = false
var body: some View {
NavigationStack {
List {
Section {
Text("Regeln bestimmen, wie Konflikte zwischen verschiedenen Datenquellen automatisch gelöst werden.")
.font(.caption)
.foregroundStyle(.secondary)
}
ForEach(HealthDataType.allCases) { dataType in
RuleRow(
dataType: dataType,
rule: ruleEngine.getRule(for: dataType)
)
.contentShape(Rectangle())
.onTapGesture {
selectedDataType = dataType
}
}
}
.listStyle(.insetGrouped)
.navigationTitle("Regeln")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Menu {
Button("Alle zurücksetzen", role: .destructive) {
showingResetConfirmation = true
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
.sheet(item: $selectedDataType) { dataType in
RuleEditorView(dataType: dataType)
}
.confirmationDialog(
"Alle Regeln zurücksetzen?",
isPresented: $showingResetConfirmation,
titleVisibility: .visible
) {
Button("Zurücksetzen", role: .destructive) {
ruleEngine.resetAllToDefaults()
}
Button("Abbrechen", role: .cancel) {}
} message: {
Text("Alle Regeln werden auf die Standardwerte zurückgesetzt.")
}
}
}
}
// MARK: - Rule Row
struct RuleRow: View {
let dataType: HealthDataType
let rule: MergeRule
var body: some View {
HStack {
Image(systemName: dataType.icon)
.foregroundStyle(.blue)
.frame(width: 30)
VStack(alignment: .leading, spacing: 4) {
Text(dataType.displayName)
.font(.headline)
HStack(spacing: 8) {
Image(systemName: rule.strategy.icon)
.font(.caption)
Text(rule.strategy.displayName)
.font(.caption)
}
.foregroundStyle(.secondary)
}
Spacer()
if !rule.autoApply {
Image(systemName: "hand.raised.fill")
.foregroundStyle(.orange)
.font(.caption)
}
Image(systemName: "chevron.right")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.vertical, 4)
}
}
// MARK: - Rule Editor View
struct RuleEditorView: View {
let dataType: HealthDataType
@Environment(\.dismiss) private var dismiss
@StateObject private var ruleEngine = RuleEngine.shared
@StateObject private var sourceManager = SourceManager.shared
@State private var selectedStrategy: MergeStrategy
@State private var autoApply: Bool
@State private var primarySourceId: String?
@State private var thresholdForManualReview: Double?
@State private var useThreshold: Bool
init(dataType: HealthDataType) {
self.dataType = dataType
let rule = RuleEngine.shared.getRule(for: dataType)
_selectedStrategy = State(initialValue: rule.strategy)
_autoApply = State(initialValue: rule.autoApply)
_primarySourceId = State(initialValue: rule.primarySourceId)
_thresholdForManualReview = State(initialValue: rule.thresholdForManualReview)
_useThreshold = State(initialValue: rule.thresholdForManualReview != nil)
}
var body: some View {
NavigationStack {
Form {
// Data Type Info
Section {
HStack {
Image(systemName: dataType.icon)
.font(.title2)
.foregroundStyle(.blue)
VStack(alignment: .leading) {
Text(dataType.displayName)
.font(.headline)
Text(dataType.unit)
.font(.caption)
.foregroundStyle(.secondary)
}
}
}
// Strategy Selection
Section("Strategie") {
Picker("Merge-Strategie", selection: $selectedStrategy) {
ForEach(MergeStrategy.allCases) { strategy in
Label(strategy.displayName, systemImage: strategy.icon)
.tag(strategy)
}
}
.pickerStyle(.navigationLink)
Text(selectedStrategy.description)
.font(.caption)
.foregroundStyle(.secondary)
}
// Primary Source (for exclusive/priority)
if selectedStrategy == .exclusive || selectedStrategy == .priority {
Section("Primäre Quelle") {
let sources = sourceManager.sources.filter {
$0.supportedDataTypes.contains(dataType)
}
if sources.isEmpty {
Text("Keine Quellen für diesen Datentyp")
.foregroundStyle(.secondary)
} else {
Picker("Quelle", selection: $primarySourceId) {
Text("Automatisch").tag(nil as String?)
ForEach(sources) { source in
Text(source.displayName).tag(source.bundleIdentifier as String?)
}
}
}
}
}
// Auto Apply
Section("Automatisierung") {
Toggle("Automatisch anwenden", isOn: $autoApply)
if autoApply {
Toggle("Schwellenwert für manuelle Prüfung", isOn: $useThreshold)
if useThreshold {
VStack(alignment: .leading) {
Text("Bei Differenz über \(Int(thresholdForManualReview ?? 20))% nachfragen")
.font(.caption)
.foregroundStyle(.secondary)
Slider(
value: Binding(
get: { thresholdForManualReview ?? 20 },
set: { thresholdForManualReview = $0 }
),
in: 5...50,
step: 5
)
}
}
}
}
// Reset
Section {
Button("Auf Standard zurücksetzen", role: .destructive) {
let defaultRule = MergeRule.defaultRule(for: dataType)
selectedStrategy = defaultRule.strategy
autoApply = defaultRule.autoApply
primarySourceId = defaultRule.primarySourceId
thresholdForManualReview = defaultRule.thresholdForManualReview
useThreshold = defaultRule.thresholdForManualReview != nil
}
}
}
.navigationTitle("Regel bearbeiten")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Abbrechen") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Speichern") {
saveRule()
dismiss()
}
}
}
}
}
private func saveRule() {
let rule = MergeRule(
dataType: dataType,
strategy: selectedStrategy,
primarySourceId: primarySourceId,
autoApply: autoApply,
thresholdForManualReview: useThreshold ? thresholdForManualReview : nil
)
ruleEngine.setRule(rule, for: dataType)
}
}
#Preview {
RulesView()
}
+218
View File
@@ -0,0 +1,218 @@
import SwiftUI
struct SettingsView: View {
@AppStorage("backgroundSyncEnabled") private var backgroundSyncEnabled = true
@AppStorage("syncIntervalMinutes") private var syncIntervalMinutes = 15
@AppStorage("notificationsEnabled") private var notificationsEnabled = true
@AppStorage("notifyOnConflict") private var notifyOnConflict = true
@AppStorage("notifyOnSyncComplete") private var notifyOnSyncComplete = false
@AppStorage("autoResolveMinorConflicts") private var autoResolveMinorConflicts = true
@StateObject private var syncCoordinator = SyncCoordinator.shared
@StateObject private var healthKitManager = HealthKitManager.shared
@State private var showingClearDataConfirmation = false
@State private var showingExportSheet = false
var body: some View {
NavigationStack {
Form {
// Sync Settings
Section("Synchronisierung") {
Toggle("Hintergrund-Sync", isOn: $backgroundSyncEnabled)
if backgroundSyncEnabled {
Picker("Intervall", selection: $syncIntervalMinutes) {
Text("15 Minuten").tag(15)
Text("30 Minuten").tag(30)
Text("1 Stunde").tag(60)
Text("2 Stunden").tag(120)
}
}
Toggle("Kleine Konflikte automatisch lösen", isOn: $autoResolveMinorConflicts)
}
// Notification Settings
Section("Benachrichtigungen") {
Toggle("Benachrichtigungen aktivieren", isOn: $notificationsEnabled)
if notificationsEnabled {
Toggle("Bei neuen Konflikten", isOn: $notifyOnConflict)
Toggle("Nach Synchronisierung", isOn: $notifyOnSyncComplete)
}
}
// Health Status
Section("HealthKit Status") {
HStack {
Text("Autorisierung")
Spacer()
if healthKitManager.isAuthorized {
Label("Erteilt", systemImage: "checkmark.circle.fill")
.foregroundStyle(.green)
} else {
Label("Ausstehend", systemImage: "exclamationmark.circle")
.foregroundStyle(.orange)
}
}
if !healthKitManager.isAuthorized {
Button("HealthKit-Zugriff anfordern") {
Task {
try? await healthKitManager.requestAuthorization()
}
}
}
}
// Statistics
Section("Statistiken") {
StatRow(label: "Syncs heute", value: "\(syncCoordinator.todayStats.syncCount)")
StatRow(label: "Konflikte heute", value: "\(syncCoordinator.todayStats.totalConflicts)")
StatRow(label: "Automatisch gelöst", value: "\(syncCoordinator.todayStats.autoResolved)")
StatRow(label: "Auflösungsrate", value: "\(Int(syncCoordinator.todayStats.resolutionRate * 100))%")
}
// Data Management
Section("Daten") {
Button("Sync-Verlauf exportieren") {
showingExportSheet = true
}
Button("Sync-Verlauf löschen", role: .destructive) {
showingClearDataConfirmation = true
}
}
// About
Section("Info") {
HStack {
Text("Version")
Spacer()
Text("1.0.0")
.foregroundStyle(.secondary)
}
Link(destination: URL(string: "https://apple.com/health")!) {
HStack {
Text("Apple Health")
Spacer()
Image(systemName: "arrow.up.right.square")
.foregroundStyle(.secondary)
}
}
}
}
.navigationTitle("Einstellungen")
.confirmationDialog(
"Sync-Verlauf löschen?",
isPresented: $showingClearDataConfirmation,
titleVisibility: .visible
) {
Button("Löschen", role: .destructive) {
syncCoordinator.clearHistory()
}
Button("Abbrechen", role: .cancel) {}
} message: {
Text("Der gesamte Sync-Verlauf wird gelöscht. Dies kann nicht rückgängig gemacht werden.")
}
.sheet(isPresented: $showingExportSheet) {
ExportView()
}
}
}
}
// MARK: - Stat Row
struct StatRow: View {
let label: String
let value: String
var body: some View {
HStack {
Text(label)
Spacer()
Text(value)
.foregroundStyle(.secondary)
}
}
}
// MARK: - Export View
struct ExportView: View {
@Environment(\.dismiss) private var dismiss
@StateObject private var syncCoordinator = SyncCoordinator.shared
@State private var exportFormat: ExportFormat = .json
@State private var isExporting = false
enum ExportFormat: String, CaseIterable {
case json = "JSON"
case csv = "CSV"
}
var body: some View {
NavigationStack {
VStack(spacing: 20) {
Image(systemName: "square.and.arrow.up")
.font(.system(size: 48))
.foregroundStyle(.blue)
Text("Daten exportieren")
.font(.title2)
.fontWeight(.semibold)
Picker("Format", selection: $exportFormat) {
ForEach(ExportFormat.allCases, id: \.self) { format in
Text(format.rawValue).tag(format)
}
}
.pickerStyle(.segmented)
.padding(.horizontal)
Text("\(syncCoordinator.syncHistory.count) Sync-Einträge")
.font(.subheadline)
.foregroundStyle(.secondary)
Spacer()
Button {
exportData()
} label: {
Label("Exportieren", systemImage: "square.and.arrow.up")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.padding()
.disabled(isExporting || syncCoordinator.syncHistory.isEmpty)
}
.padding()
.navigationTitle("Export")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Abbrechen") {
dismiss()
}
}
}
}
}
private func exportData() {
isExporting = true
// In a real app, this would create and share a file
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
isExporting = false
dismiss()
}
}
}
#Preview {
SettingsView()
}
+407
View File
@@ -0,0 +1,407 @@
import SwiftUI
struct SourcesView: View {
@StateObject private var sourceManager = SourceManager.shared
@State private var selectedSource: HealthSource?
@State private var isRefreshing = false
var body: some View {
NavigationStack {
Group {
if sourceManager.sources.isEmpty && !sourceManager.isDiscovering {
emptyState
} else {
sourcesList
}
}
.navigationTitle("Quellen")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
Task { await refreshSources() }
} label: {
if isRefreshing {
ProgressView()
.scaleEffect(0.8)
} else {
Image(systemName: "arrow.clockwise")
}
}
.disabled(isRefreshing)
}
}
.sheet(item: $selectedSource) { source in
SourceDetailView(source: source)
}
.task {
if sourceManager.sources.isEmpty {
await refreshSources()
}
}
}
}
// MARK: - Empty State
private var emptyState: some View {
VStack(spacing: 16) {
Image(systemName: "antenna.radiowaves.left.and.right")
.font(.system(size: 64))
.foregroundStyle(.secondary)
Text("Keine Quellen gefunden")
.font(.title2)
.fontWeight(.semibold)
Text("Verbinden Sie Geräte mit Apple Health")
.font(.subheadline)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
Button {
Task { await refreshSources() }
} label: {
Label("Aktualisieren", systemImage: "arrow.clockwise")
}
.buttonStyle(.bordered)
}
.padding()
}
// MARK: - Sources List
private var sourcesList: some View {
List {
ForEach(groupedSources.keys.sorted(by: { $0.priority > $1.priority }), id: \.self) { category in
Section(category.displayName) {
ForEach(groupedSources[category] ?? []) { source in
SourceRow(source: source)
.contentShape(Rectangle())
.onTapGesture {
selectedSource = source
}
}
}
}
}
.listStyle(.insetGrouped)
.refreshable {
await refreshSources()
}
}
private var groupedSources: [SourceCategory: [HealthSource]] {
Dictionary(grouping: sourceManager.sources, by: { $0.category })
}
private func refreshSources() async {
isRefreshing = true
defer { isRefreshing = false }
await sourceManager.discoverSources()
}
}
// MARK: - Source Row
struct SourceRow: View {
let source: HealthSource
@StateObject private var sourceManager = SourceManager.shared
var body: some View {
HStack {
Image(systemName: source.category.icon)
.font(.title2)
.foregroundStyle(.blue)
.frame(width: 40)
VStack(alignment: .leading, spacing: 4) {
Text(source.displayName)
.font(.headline)
Text("\(source.supportedDataTypes.count) Datentypen")
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
if let status = sourceManager.sourceHealthStatus[source.id] {
Image(systemName: status.syncStatus.icon)
.foregroundStyle(statusColor(for: status.syncStatus))
}
Image(systemName: "chevron.right")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.vertical, 4)
}
private func statusColor(for status: SourceHealthStatus.SyncStatus) -> Color {
switch status {
case .recentlySynced: return .green
case .syncedToday: return .blue
case .stale: return .orange
case .veryStale, .neverSynced: return .red
}
}
}
// MARK: - Source Detail View
struct SourceDetailView: View {
let source: HealthSource
@Environment(\.dismiss) private var dismiss
@StateObject private var sourceManager = SourceManager.shared
@State private var healthReport: SourceHealthReport?
@State private var isLoading = false
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 20) {
// Header
headerSection
// Capabilities
capabilitiesSection
// Data Types
dataTypesSection
// Health Report
if let report = healthReport {
healthReportSection(report)
}
// Priority Settings
prioritySection
}
.padding()
}
.navigationTitle(source.displayName)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Fertig") {
dismiss()
}
}
}
.task {
await loadHealthReport()
}
}
}
// MARK: - Header
private var headerSection: some View {
VStack(spacing: 12) {
Image(systemName: source.category.icon)
.font(.system(size: 48))
.foregroundStyle(.blue)
Text(source.displayName)
.font(.title2)
.fontWeight(.semibold)
Text(source.category.displayName)
.font(.subheadline)
.foregroundStyle(.secondary)
Text(source.bundleIdentifier)
.font(.caption)
.foregroundStyle(.secondary)
.textSelection(.enabled)
}
.padding()
}
// MARK: - Capabilities
private var capabilitiesSection: some View {
let capabilities = sourceManager.getSourceCapabilities(source)
return VStack(alignment: .leading, spacing: 12) {
Text("Fähigkeiten")
.font(.headline)
LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))], spacing: 8) {
CapabilityBadge(name: "Schritte", available: capabilities.canMeasureSteps)
CapabilityBadge(name: "Herzfrequenz", available: capabilities.canMeasureHeartRate)
CapabilityBadge(name: "Blutdruck", available: capabilities.canMeasureBloodPressure)
CapabilityBadge(name: "SpO2", available: capabilities.canMeasureBloodOxygen)
CapabilityBadge(name: "Schlaf", available: capabilities.canMeasureSleep)
CapabilityBadge(name: "GPS", available: capabilities.hasGPS)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
// MARK: - Data Types
private var dataTypesSection: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Unterstützte Datentypen")
.font(.headline)
ForEach(Array(source.supportedDataTypes).sorted(by: { $0.displayName < $1.displayName }), id: \.self) { dataType in
HStack {
Image(systemName: dataType.icon)
.foregroundStyle(.blue)
.frame(width: 24)
Text(dataType.displayName)
Spacer()
Text(dataType.unit)
.font(.caption)
.foregroundStyle(.secondary)
}
.padding(.vertical, 4)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
// MARK: - Health Report
private func healthReportSection(_ report: SourceHealthReport) -> some View {
VStack(alignment: .leading, spacing: 12) {
Text("Zustand")
.font(.headline)
HStack {
VStack(alignment: .leading) {
Text("Datensätze (24h)")
.font(.caption)
.foregroundStyle(.secondary)
Text("\(report.totalRecordCount)")
.font(.title3)
.fontWeight(.medium)
}
Spacer()
VStack(alignment: .trailing) {
Text("Qualität")
.font(.caption)
.foregroundStyle(.secondary)
Image(systemName: report.overallQuality.icon)
.font(.title3)
.foregroundStyle(qualityColor(report.overallQuality))
}
}
if let lastActivity = report.lastOverallActivity {
HStack {
Text("Letzte Aktivität")
.font(.caption)
.foregroundStyle(.secondary)
Spacer()
Text(formattedDate(lastActivity))
.font(.subheadline)
}
}
if report.hasSignificantGaps {
HStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(.orange)
Text("Datenlücken erkannt")
.font(.subheadline)
}
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
// MARK: - Priority Section
private var prioritySection: some View {
VStack(alignment: .leading, spacing: 12) {
Text("Priorität")
.font(.headline)
Text("Höhere Priorität bedeutet, dass Daten dieser Quelle bevorzugt werden")
.font(.caption)
.foregroundStyle(.secondary)
ForEach(Array(source.supportedDataTypes).sorted(by: { $0.displayName < $1.displayName }), id: \.self) { dataType in
HStack {
Text(dataType.displayName)
Spacer()
Stepper(
"\(sourceManager.getPriority(for: source, dataType: dataType))",
value: Binding(
get: { sourceManager.getPriority(for: source, dataType: dataType) },
set: { sourceManager.setPriority($0, for: source, dataType: dataType) }
),
in: 0...100,
step: 10
)
.frame(width: 150)
}
.padding(.vertical, 4)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
// MARK: - Helpers
private func loadHealthReport() async {
isLoading = true
defer { isLoading = false }
healthReport = await sourceManager.getSourceHealth(source)
}
private func qualityColor(_ quality: DataQuality) -> Color {
switch quality {
case .complete: return .green
case .partial: return .yellow
case .missing: return .gray
case .invalid: return .red
}
}
private func formattedDate(_ date: Date) -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .abbreviated
return formatter.localizedString(for: date, relativeTo: Date())
}
}
// MARK: - Capability Badge
struct CapabilityBadge: View {
let name: String
let available: Bool
var body: some View {
HStack(spacing: 4) {
Image(systemName: available ? "checkmark.circle.fill" : "xmark.circle")
.foregroundStyle(available ? .green : .secondary)
Text(name)
.font(.caption)
.foregroundStyle(available ? .primary : .secondary)
}
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(available ? Color.green.opacity(0.1) : Color.gray.opacity(0.1))
.clipShape(Capsule())
}
}
#Preview {
SourcesView()
}
+120
View File
@@ -0,0 +1,120 @@
//
// AppDelegate.swift
// PsytranceVisualizer
//
// Application delegate handling app lifecycle
//
import AppKit
import AVFoundation
/// Application delegate
final class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: - Properties
private var mainWindowController: MainWindowController?
// MARK: - App Lifecycle
func applicationDidFinishLaunching(_ notification: Notification) {
// Request microphone permission
requestMicrophonePermission()
// Create and show main window
mainWindowController = MainWindowController()
mainWindowController?.showWindow(nil)
mainWindowController?.window?.makeKeyAndOrderFront(nil)
// Activate the application
NSApp.activate(ignoringOtherApps: true)
print("[AppDelegate] Application launched")
}
func applicationWillTerminate(_ notification: Notification) {
// Save settings
SettingsManager.shared.saveNow()
print("[AppDelegate] Application terminating")
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
// MARK: - Permissions
private func requestMicrophonePermission() {
switch AVCaptureDevice.authorizationStatus(for: .audio) {
case .authorized:
print("[AppDelegate] Microphone access already authorized")
case .notDetermined:
AVCaptureDevice.requestAccess(for: .audio) { granted in
if granted {
print("[AppDelegate] Microphone access granted")
} else {
print("[AppDelegate] Microphone access denied")
self.showMicrophonePermissionAlert()
}
}
case .denied, .restricted:
print("[AppDelegate] Microphone access denied or restricted")
showMicrophonePermissionAlert()
@unknown default:
break
}
}
private func showMicrophonePermissionAlert() {
DispatchQueue.main.async {
let alert = NSAlert()
alert.messageText = "Microphone Access Required"
alert.informativeText = "Psytrance Visualizer needs access to your audio input to visualize music. Please enable microphone access in System Preferences > Security & Privacy > Privacy > Microphone."
alert.alertStyle = .warning
alert.addButton(withTitle: "Open System Preferences")
alert.addButton(withTitle: "Cancel")
if alert.runModal() == .alertFirstButtonReturn {
if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone") {
NSWorkspace.shared.open(url)
}
}
}
}
// MARK: - Menu Actions
@IBAction func showAbout(_ sender: Any) {
let alert = NSAlert()
alert.messageText = "Psytrance Visualizer"
alert.informativeText = """
An audio-reactive visualizer for psytrance music.
8 Visualization Modes:
1 - FFT Classic
2 - Mel Spectrogram
3 - Sub-Bass
4 - Sidechain Pump
5 - Harmonic/Noise
6 - Mandelbrot
7 - Tunnel Warp
8 - DMT Geometry
Keyboard Shortcuts:
1-8: Switch visualization mode
F: Toggle fullscreen
ESC: Exit fullscreen
Tip: Use a virtual audio device like BlackHole to route system audio.
"""
alert.alertStyle = .informational
alert.runModal()
}
}
@@ -0,0 +1,133 @@
//
// PsytranceVisualizerApp.swift
// PsytranceVisualizer
//
// Main application entry point
//
import AppKit
// MARK: - Main Entry Point
/// Application entry point
@main
struct PsytranceVisualizerApp {
static func main() {
// Create the application
let app = NSApplication.shared
// Set up the delegate
let delegate = AppDelegate()
app.delegate = delegate
// Set activation policy
app.setActivationPolicy(.regular)
// Create the main menu
setupMainMenu()
// Run the application
app.run()
}
/// Sets up the application's main menu
private static func setupMainMenu() {
let mainMenu = NSMenu()
// Application menu
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "About Psytrance Visualizer",
action: #selector(AppDelegate.showAbout(_:)),
keyEquivalent: "")
appMenu.addItem(NSMenuItem.separator())
appMenu.addItem(withTitle: "Hide Psytrance Visualizer",
action: #selector(NSApplication.hide(_:)),
keyEquivalent: "h")
let hideOthersItem = appMenu.addItem(withTitle: "Hide Others",
action: #selector(NSApplication.hideOtherApplications(_:)),
keyEquivalent: "h")
hideOthersItem.keyEquivalentModifierMask = [.command, .option]
appMenu.addItem(withTitle: "Show All",
action: #selector(NSApplication.unhideAllApplications(_:)),
keyEquivalent: "")
appMenu.addItem(NSMenuItem.separator())
appMenu.addItem(withTitle: "Quit Psytrance Visualizer",
action: #selector(NSApplication.terminate(_:)),
keyEquivalent: "q")
// View menu
let viewMenuItem = NSMenuItem()
mainMenu.addItem(viewMenuItem)
let viewMenu = NSMenu(title: "View")
viewMenuItem.submenu = viewMenu
viewMenu.addItem(withTitle: "Toggle Fullscreen",
action: #selector(NSWindow.toggleFullScreen(_:)),
keyEquivalent: "f")
viewMenu.addItem(NSMenuItem.separator())
// Visualization mode submenu
let modesMenuItem = NSMenuItem(title: "Visualization Mode", action: nil, keyEquivalent: "")
let modesMenu = NSMenu()
for mode in VisualizationMode.allCases {
let item = NSMenuItem(title: mode.displayName,
action: nil,
keyEquivalent: mode.shortcut)
item.tag = mode.rawValue
modesMenu.addItem(item)
}
modesMenuItem.submenu = modesMenu
viewMenu.addItem(modesMenuItem)
// Window menu
let windowMenuItem = NSMenuItem()
mainMenu.addItem(windowMenuItem)
let windowMenu = NSMenu(title: "Window")
windowMenuItem.submenu = windowMenu
windowMenu.addItem(withTitle: "Minimize",
action: #selector(NSWindow.miniaturize(_:)),
keyEquivalent: "m")
windowMenu.addItem(withTitle: "Zoom",
action: #selector(NSWindow.zoom(_:)),
keyEquivalent: "")
windowMenu.addItem(NSMenuItem.separator())
windowMenu.addItem(withTitle: "Bring All to Front",
action: #selector(NSApplication.arrangeInFront(_:)),
keyEquivalent: "")
// Help menu
let helpMenuItem = NSMenuItem()
mainMenu.addItem(helpMenuItem)
let helpMenu = NSMenu(title: "Help")
helpMenuItem.submenu = helpMenu
helpMenu.addItem(withTitle: "Psytrance Visualizer Help",
action: #selector(AppDelegate.showAbout(_:)),
keyEquivalent: "?")
NSApp.mainMenu = mainMenu
NSApp.windowsMenu = windowMenu
NSApp.helpMenu = helpMenu
}
}
@@ -0,0 +1,357 @@
//
// AudioInputManager.swift
// PsytranceVisualizer
//
// Manages audio input devices and captures audio buffers
//
import AVFoundation
import CoreAudio
import Combine
/// Represents an audio input device
struct AudioDevice: Identifiable, Hashable {
let id: AudioDeviceID
let uid: String
let name: String
let manufacturer: String
let isInput: Bool
func hash(into hasher: inout Hasher) {
hasher.combine(uid)
}
static func == (lhs: AudioDevice, rhs: AudioDevice) -> Bool {
lhs.uid == rhs.uid
}
}
/// Manages audio input capture using AVAudioEngine
final class AudioInputManager: ObservableObject {
// MARK: - Published Properties
@Published private(set) var availableDevices: [AudioDevice] = []
@Published private(set) var selectedDevice: AudioDevice?
@Published private(set) var isRunning = false
@Published private(set) var currentBufferSize: Int = 1024
// MARK: - Audio Properties
private var audioEngine: AVAudioEngine?
private var inputNode: AVAudioInputNode?
private let sampleRate: Double = 44100.0
// MARK: - Callbacks
var onAudioBuffer: ((AVAudioPCMBuffer) -> Void)?
// MARK: - Private Properties
private var deviceListenerBlock: AudioObjectPropertyListenerBlock?
private let processingQueue = DispatchQueue(label: "com.psytrance.audio", qos: .userInteractive)
// MARK: - Initialization
init() {
refreshDeviceList()
setupDeviceChangeListener()
}
deinit {
stop()
removeDeviceChangeListener()
}
// MARK: - Public Methods
/// Returns list of available audio input devices
func getAvailableInputDevices() -> [AudioDevice] {
return availableDevices
}
/// Refreshes the list of available audio input devices
func refreshDeviceList() {
var propertyAddress = AudioObjectPropertyAddress(
mSelector: kAudioHardwarePropertyDevices,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain
)
var dataSize: UInt32 = 0
var status = AudioObjectGetPropertyDataSize(
AudioObjectID(kAudioObjectSystemObject),
&propertyAddress,
0,
nil,
&dataSize
)
guard status == noErr else {
print("[AudioInputManager] Failed to get device list size: \(status)")
return
}
let deviceCount = Int(dataSize) / MemoryLayout<AudioDeviceID>.size
var deviceIDs = [AudioDeviceID](repeating: 0, count: deviceCount)
status = AudioObjectGetPropertyData(
AudioObjectID(kAudioObjectSystemObject),
&propertyAddress,
0,
nil,
&dataSize,
&deviceIDs
)
guard status == noErr else {
print("[AudioInputManager] Failed to get device list: \(status)")
return
}
var devices: [AudioDevice] = []
for deviceID in deviceIDs {
if let device = getDeviceInfo(deviceID: deviceID), device.isInput {
devices.append(device)
}
}
DispatchQueue.main.async {
self.availableDevices = devices
print("[AudioInputManager] Found \(devices.count) input devices")
}
}
/// Selects an audio input device by UID
func selectDevice(uid: String) {
guard let device = availableDevices.first(where: { $0.uid == uid }) else {
print("[AudioInputManager] Device not found: \(uid)")
return
}
let wasRunning = isRunning
if wasRunning {
stop()
}
selectedDevice = device
setSystemInputDevice(deviceID: device.id)
if wasRunning {
start()
}
print("[AudioInputManager] Selected device: \(device.name)")
}
/// Sets the buffer size (512 or 1024)
func setBufferSize(_ size: Int) {
guard [512, 1024].contains(size) else {
print("[AudioInputManager] Invalid buffer size: \(size)")
return
}
let wasRunning = isRunning
if wasRunning {
stop()
}
currentBufferSize = size
if wasRunning {
start()
}
print("[AudioInputManager] Buffer size set to: \(size)")
}
/// Starts audio capture
func start() {
guard !isRunning else { return }
do {
// Create new audio engine
audioEngine = AVAudioEngine()
guard let engine = audioEngine else { return }
inputNode = engine.inputNode
guard let inputNode = inputNode else {
print("[AudioInputManager] No input node available")
return
}
// Get the input format
let inputFormat = inputNode.outputFormat(forBus: 0)
print("[AudioInputManager] Input format: \(inputFormat)")
// Install tap on input node
let bufferSize = AVAudioFrameCount(currentBufferSize)
inputNode.installTap(onBus: 0, bufferSize: bufferSize, format: inputFormat) { [weak self] buffer, _ in
self?.processingQueue.async {
self?.onAudioBuffer?(buffer)
}
}
// Prepare and start the engine
engine.prepare()
try engine.start()
DispatchQueue.main.async {
self.isRunning = true
}
print("[AudioInputManager] Audio capture started")
} catch {
print("[AudioInputManager] Failed to start audio capture: \(error)")
}
}
/// Stops audio capture
func stop() {
guard isRunning else { return }
inputNode?.removeTap(onBus: 0)
audioEngine?.stop()
audioEngine = nil
inputNode = nil
DispatchQueue.main.async {
self.isRunning = false
}
print("[AudioInputManager] Audio capture stopped")
}
// MARK: - Private Methods
/// Gets device info for a specific device ID
private func getDeviceInfo(deviceID: AudioDeviceID) -> AudioDevice? {
// Check if device has input channels
var propertyAddress = AudioObjectPropertyAddress(
mSelector: kAudioDevicePropertyStreamConfiguration,
mScope: kAudioDevicePropertyScopeInput,
mElement: kAudioObjectPropertyElementMain
)
var dataSize: UInt32 = 0
var status = AudioObjectGetPropertyDataSize(deviceID, &propertyAddress, 0, nil, &dataSize)
guard status == noErr, dataSize > 0 else { return nil }
let bufferListPointer = UnsafeMutablePointer<AudioBufferList>.allocate(capacity: Int(dataSize))
defer { bufferListPointer.deallocate() }
status = AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, nil, &dataSize, bufferListPointer)
guard status == noErr else { return nil }
let bufferList = UnsafeMutableAudioBufferListPointer(bufferListPointer)
var inputChannelCount: UInt32 = 0
for buffer in bufferList {
inputChannelCount += buffer.mNumberChannels
}
guard inputChannelCount > 0 else { return nil }
// Get device UID
var uid: CFString = "" as CFString
var uidSize = UInt32(MemoryLayout<CFString>.size)
propertyAddress.mSelector = kAudioDevicePropertyDeviceUID
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal
status = AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, nil, &uidSize, &uid)
guard status == noErr else { return nil }
// Get device name
var name: CFString = "" as CFString
var nameSize = UInt32(MemoryLayout<CFString>.size)
propertyAddress.mSelector = kAudioDevicePropertyDeviceNameCFString
status = AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, nil, &nameSize, &name)
guard status == noErr else { return nil }
// Get manufacturer
var manufacturer: CFString = "" as CFString
var manufacturerSize = UInt32(MemoryLayout<CFString>.size)
propertyAddress.mSelector = kAudioDevicePropertyDeviceManufacturerCFString
AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, nil, &manufacturerSize, &manufacturer)
return AudioDevice(
id: deviceID,
uid: uid as String,
name: name as String,
manufacturer: manufacturer as String,
isInput: true
)
}
/// Sets the system default input device
private func setSystemInputDevice(deviceID: AudioDeviceID) {
var deviceIDCopy = deviceID
var propertyAddress = AudioObjectPropertyAddress(
mSelector: kAudioHardwarePropertyDefaultInputDevice,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain
)
let status = AudioObjectSetPropertyData(
AudioObjectID(kAudioObjectSystemObject),
&propertyAddress,
0,
nil,
UInt32(MemoryLayout<AudioDeviceID>.size),
&deviceIDCopy
)
if status != noErr {
print("[AudioInputManager] Failed to set input device: \(status)")
}
}
/// Sets up listener for device changes
private func setupDeviceChangeListener() {
var propertyAddress = AudioObjectPropertyAddress(
mSelector: kAudioHardwarePropertyDevices,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain
)
deviceListenerBlock = { [weak self] _, _ in
DispatchQueue.main.async {
self?.refreshDeviceList()
}
}
if let block = deviceListenerBlock {
AudioObjectAddPropertyListenerBlock(
AudioObjectID(kAudioObjectSystemObject),
&propertyAddress,
DispatchQueue.main,
block
)
}
}
/// Removes device change listener
private func removeDeviceChangeListener() {
guard let block = deviceListenerBlock else { return }
var propertyAddress = AudioObjectPropertyAddress(
mSelector: kAudioHardwarePropertyDevices,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain
)
AudioObjectRemovePropertyListenerBlock(
AudioObjectID(kAudioObjectSystemObject),
&propertyAddress,
DispatchQueue.main,
block
)
}
}
+468
View File
@@ -0,0 +1,468 @@
//
// DSPEngine.swift
// PsytranceVisualizer
//
// Digital Signal Processing engine for audio analysis
//
import Accelerate
import AVFoundation
/// DSP Engine for real-time audio analysis
final class DSPEngine {
// MARK: - Configuration
private let sampleRate: Float = 44100.0
private var fftSize: Int
private let melBandCount: Int = 64
private let subBassUpperFreq: Float = 100.0
private let historySize: Int = 128
// MARK: - FFT Setup
private var fftSetup: vDSP_DFT_Setup?
private var window: [Float]
private var realPart: [Float]
private var imagPart: [Float]
private var magnitudes: [Float]
// MARK: - Mel Filterbank
private var melFilterbank: [[Float]]
private var melOutput: [Float]
// MARK: - Analysis State
private var subBassHistory: [Float]
private var previousMagnitudes: [Float]
private var envelopeValue: Float = 0
private var previousEnvelope: Float = 0
private var pumpHistory: [Float]
private var lastPeakTime: Double = 0
private var peakThreshold: Float = 0.3
// MARK: - Reactivity
private var reactivity: Float = 0.5
private var smoothingFactor: Float = 0.3
// MARK: - Initialization
init(bufferSize: Int = 1024) {
self.fftSize = bufferSize
// Initialize FFT arrays
self.window = [Float](repeating: 0, count: fftSize)
self.realPart = [Float](repeating: 0, count: fftSize)
self.imagPart = [Float](repeating: 0, count: fftSize)
self.magnitudes = [Float](repeating: 0, count: fftSize / 2)
self.previousMagnitudes = [Float](repeating: 0, count: fftSize / 2)
// Initialize Mel arrays
self.melOutput = [Float](repeating: 0, count: melBandCount)
self.melFilterbank = []
// Initialize history arrays
self.subBassHistory = [Float](repeating: 0, count: historySize)
self.pumpHistory = [Float](repeating: 0, count: 64)
// Create Hann window
vDSP_hann_window(&window, vDSP_Length(fftSize), Int32(vDSP_HANN_NORM))
// Create FFT setup
fftSetup = vDSP_DFT_zop_CreateSetup(
nil,
vDSP_Length(fftSize),
.FORWARD
)
// Build Mel filterbank
buildMelFilterbank()
}
deinit {
if let setup = fftSetup {
vDSP_DFT_DestroySetup(setup)
}
}
// MARK: - Public Methods
/// Sets reactivity value (0.0 - 1.0)
func setReactivity(_ value: Float) {
reactivity = max(0.0, min(1.0, value))
// Adjust smoothing based on reactivity (higher reactivity = less smoothing)
smoothingFactor = 0.1 + (1.0 - reactivity) * 0.4
}
/// Reconfigures for new buffer size
func setBufferSize(_ size: Int) {
guard size != fftSize else { return }
fftSize = size
// Reinitialize arrays
window = [Float](repeating: 0, count: fftSize)
realPart = [Float](repeating: 0, count: fftSize)
imagPart = [Float](repeating: 0, count: fftSize)
magnitudes = [Float](repeating: 0, count: fftSize / 2)
previousMagnitudes = [Float](repeating: 0, count: fftSize / 2)
// Recreate window
vDSP_hann_window(&window, vDSP_Length(fftSize), Int32(vDSP_HANN_NORM))
// Recreate FFT setup
if let setup = fftSetup {
vDSP_DFT_DestroySetup(setup)
}
fftSetup = vDSP_DFT_zop_CreateSetup(nil, vDSP_Length(fftSize), .FORWARD)
// Rebuild filterbank
buildMelFilterbank()
}
/// Processes audio buffer and returns analysis data
func process(buffer: AVAudioPCMBuffer) -> AudioAnalysisData {
guard let channelData = buffer.floatChannelData else {
return .empty
}
let frameCount = Int(buffer.frameLength)
let channelCount = Int(buffer.format.channelCount)
// Extract stereo channels
var leftChannel = [Float](repeating: 0, count: frameCount)
var rightChannel = [Float](repeating: 0, count: frameCount)
if channelCount >= 1 {
leftChannel = Array(UnsafeBufferPointer(start: channelData[0], count: frameCount))
}
if channelCount >= 2 {
rightChannel = Array(UnsafeBufferPointer(start: channelData[1], count: frameCount))
} else {
rightChannel = leftChannel
}
// Mix to mono for analysis
var monoBuffer = [Float](repeating: 0, count: frameCount)
vDSP_vadd(leftChannel, 1, rightChannel, 1, &monoBuffer, 1, vDSP_Length(frameCount))
var half: Float = 0.5
vDSP_vsmul(monoBuffer, 1, &half, &monoBuffer, 1, vDSP_Length(frameCount))
// Calculate RMS
var rmsValue: Float = 0
vDSP_rmsqv(monoBuffer, 1, &rmsValue, vDSP_Length(frameCount))
// Perform FFT
let fftMagnitudes = performFFT(monoBuffer)
// Calculate Mel bands
let melBands = calculateMelBands(from: fftMagnitudes)
// Extract sub-bass
let subBassEnergy = calculateSubBassEnergy(from: fftMagnitudes)
// Update sub-bass history
subBassHistory.removeFirst()
subBassHistory.append(subBassEnergy)
// Calculate sidechain envelope and pump detection
let (envelope, pumpAmount, isPumping) = detectSidechainPump(subBassEnergy: subBassEnergy)
// Calculate HNR
let hnrRatio = calculateHNR(buffer: monoBuffer)
// Detect peaks/transients
let (isPeak, peakIntensity) = detectPeak(rms: rmsValue)
// Calculate spectral centroid
let spectralCentroid = calculateSpectralCentroid(magnitudes: fftMagnitudes)
return AudioAnalysisData(
fftMagnitudes: fftMagnitudes,
melBands: melBands,
subBassEnergy: subBassEnergy,
subBassHistory: subBassHistory,
sidechainEnvelope: envelope,
sidechainPumpAmount: pumpAmount,
isPumping: isPumping,
hnrRatio: hnrRatio,
isPeak: isPeak,
peakIntensity: peakIntensity,
leftChannel: leftChannel,
rightChannel: rightChannel,
spectralCentroid: spectralCentroid,
rmsLevel: rmsValue
)
}
// MARK: - FFT
private func performFFT(_ buffer: [Float]) -> [Float] {
guard let setup = fftSetup else { return magnitudes }
let count = min(buffer.count, fftSize)
// Apply window
var windowedBuffer = [Float](repeating: 0, count: fftSize)
for i in 0..<count {
windowedBuffer[i] = buffer[i] * window[i]
}
// Prepare for DFT (separate into real and imaginary)
for i in 0..<fftSize {
realPart[i] = windowedBuffer[i]
imagPart[i] = 0
}
// Perform DFT
var outputReal = [Float](repeating: 0, count: fftSize)
var outputImag = [Float](repeating: 0, count: fftSize)
vDSP_DFT_Execute(setup, realPart, imagPart, &outputReal, &outputImag)
// Calculate magnitudes
let halfSize = fftSize / 2
var newMagnitudes = [Float](repeating: 0, count: halfSize)
for i in 0..<halfSize {
let real = outputReal[i]
let imag = outputImag[i]
newMagnitudes[i] = sqrt(real * real + imag * imag) / Float(fftSize)
}
// Apply smoothing
for i in 0..<halfSize {
magnitudes[i] = magnitudes[i] * smoothingFactor + newMagnitudes[i] * (1.0 - smoothingFactor)
}
previousMagnitudes = magnitudes
return magnitudes
}
// MARK: - Mel Filterbank
private func buildMelFilterbank() {
let halfFFT = fftSize / 2
let nyquist = sampleRate / 2.0
// Mel scale conversion
func hzToMel(_ hz: Float) -> Float {
return 2595.0 * log10(1.0 + hz / 700.0)
}
func melToHz(_ mel: Float) -> Float {
return 700.0 * (pow(10.0, mel / 2595.0) - 1.0)
}
let melMin = hzToMel(20.0)
let melMax = hzToMel(nyquist)
// Create mel points
var melPoints = [Float](repeating: 0, count: melBandCount + 2)
for i in 0..<melBandCount + 2 {
melPoints[i] = melMin + Float(i) * (melMax - melMin) / Float(melBandCount + 1)
}
// Convert back to Hz
var hzPoints = melPoints.map { melToHz($0) }
// Convert to FFT bins
var binPoints = hzPoints.map { Int($0 / nyquist * Float(halfFFT)) }
// Build triangular filters
melFilterbank = []
for m in 1...melBandCount {
var filter = [Float](repeating: 0, count: halfFFT)
let startBin = binPoints[m - 1]
let centerBin = binPoints[m]
let endBin = binPoints[m + 1]
// Rising edge
for k in startBin..<centerBin {
if centerBin != startBin {
filter[k] = Float(k - startBin) / Float(centerBin - startBin)
}
}
// Falling edge
for k in centerBin..<endBin {
if endBin != centerBin {
filter[k] = Float(endBin - k) / Float(endBin - centerBin)
}
}
melFilterbank.append(filter)
}
}
private func calculateMelBands(from magnitudes: [Float]) -> [Float] {
var result = [Float](repeating: 0, count: melBandCount)
for (i, filter) in melFilterbank.enumerated() {
var sum: Float = 0
let count = min(filter.count, magnitudes.count)
for j in 0..<count {
sum += magnitudes[j] * filter[j]
}
// Apply logarithmic scaling
result[i] = log10(1.0 + sum * 10.0) / log10(11.0)
}
// Apply smoothing to mel output
for i in 0..<melBandCount {
melOutput[i] = melOutput[i] * smoothingFactor + result[i] * (1.0 - smoothingFactor)
}
return melOutput
}
// MARK: - Sub-Bass Analysis
private func calculateSubBassEnergy(from magnitudes: [Float]) -> Float {
let binFrequency = sampleRate / Float(fftSize)
let subBassBinCount = Int(subBassUpperFreq / binFrequency)
guard subBassBinCount > 0, magnitudes.count >= subBassBinCount else { return 0 }
var sum: Float = 0
for i in 0..<subBassBinCount {
sum += magnitudes[i] * magnitudes[i]
}
let rms = sqrt(sum / Float(subBassBinCount))
// Normalize and apply gain
let normalized = min(1.0, rms * 5.0 * (1.0 + reactivity))
return normalized
}
// MARK: - Sidechain Pump Detection
private func detectSidechainPump(subBassEnergy: Float) -> (envelope: Float, pumpAmount: Float, isPumping: Bool) {
// Envelope follower with fast attack, slow release
let attackTime: Float = 0.005 // 5ms attack
let releaseTime: Float = 0.15 // 150ms release
let attackCoeff = exp(-1.0 / (sampleRate * attackTime))
let releaseCoeff = exp(-1.0 / (sampleRate * releaseTime))
if subBassEnergy > envelopeValue {
envelopeValue = attackCoeff * envelopeValue + (1.0 - attackCoeff) * subBassEnergy
} else {
envelopeValue = releaseCoeff * envelopeValue + (1.0 - releaseCoeff) * subBassEnergy
}
// Update pump history
pumpHistory.removeFirst()
pumpHistory.append(envelopeValue)
// Analyze pump periodicity
var pumpAmount: Float = 0
var isPumping = false
// Look for characteristic pump pattern (rise and fall)
let derivative = envelopeValue - previousEnvelope
previousEnvelope = envelopeValue
// Detect pump by finding periodic envelope variations
if pumpHistory.count >= 32 {
let recent = Array(pumpHistory.suffix(32))
var variance: Float = 0
let mean = recent.reduce(0, +) / Float(recent.count)
for value in recent {
variance += (value - mean) * (value - mean)
}
variance /= Float(recent.count)
// Higher variance = more pumping
pumpAmount = min(1.0, sqrt(variance) * 4.0)
isPumping = pumpAmount > 0.3 && abs(derivative) > 0.02
}
return (envelopeValue, pumpAmount, isPumping)
}
// MARK: - HNR Calculation
private func calculateHNR(buffer: [Float]) -> Float {
// Use autocorrelation to estimate harmonicity
let frameSize = min(buffer.count, 512)
var autocorr = [Float](repeating: 0, count: frameSize)
// Compute autocorrelation
vDSP_conv(buffer, 1, buffer, 1, &autocorr, 1, vDSP_Length(frameSize), vDSP_Length(frameSize))
// Find the peak in autocorrelation (excluding lag 0)
let minLag = 20 // Minimum lag to avoid DC component
let maxLag = min(frameSize - 1, 400) // Maximum lag
guard maxLag > minLag else { return 0.5 }
var maxValue: Float = 0
var maxIndex: vDSP_Length = 0
let searchRange = Array(autocorr[minLag...maxLag])
vDSP_maxvi(searchRange, 1, &maxValue, &maxIndex, vDSP_Length(searchRange.count))
// Calculate HNR as ratio of peak to first value
let noiseFloor = autocorr.suffix(from: maxLag).reduce(0) { $0 + abs($1) } / Float(frameSize - maxLag)
let harmonicPower = maxValue
let noisePower = max(noiseFloor, 0.0001)
// Convert to 0-1 range
let hnr = harmonicPower / (harmonicPower + noisePower)
return max(0.0, min(1.0, hnr))
}
// MARK: - Peak Detection
private var previousRMS: Float = 0
private var rmsHistory: [Float] = Array(repeating: 0, count: 16)
private func detectPeak(rms: Float) -> (isPeak: Bool, intensity: Float) {
// Update history
rmsHistory.removeFirst()
rmsHistory.append(rms)
// Calculate moving average
let average = rmsHistory.reduce(0, +) / Float(rmsHistory.count)
// Detect sudden increase
let increase = rms - previousRMS
let threshold = average * (0.5 + reactivity * 0.5)
previousRMS = rms
let isPeak = increase > threshold && rms > average * 1.5
let intensity = isPeak ? min(1.0, increase / max(average, 0.01) * 2.0) : 0
return (isPeak, intensity)
}
// MARK: - Spectral Centroid
private func calculateSpectralCentroid(magnitudes: [Float]) -> Float {
var weightedSum: Float = 0
var sum: Float = 0
for (i, mag) in magnitudes.enumerated() {
weightedSum += Float(i) * mag
sum += mag
}
guard sum > 0 else { return 0.5 }
let centroid = weightedSum / sum
let normalized = centroid / Float(magnitudes.count)
return max(0.0, min(1.0, normalized))
}
}
@@ -0,0 +1,90 @@
//
// AppSettings.swift
// PsytranceVisualizer
//
// Persistent application settings
//
import Foundation
/// Application settings that are persisted between sessions
struct AppSettings: Codable {
/// Selected audio input device UID
var selectedAudioDeviceUID: String?
/// Audio buffer size (512 or 1024 samples)
var bufferSize: Int
/// Last used visualization mode (1-8)
var lastVisualizationMode: Int
/// Reactivity slider value (0.0 - 1.0)
var reactivity: Float
/// Whether app was in fullscreen mode
var isFullscreen: Bool
/// Last window frame (for restoration)
var windowFrame: CodableRect?
/// Volume/gain adjustment
var inputGain: Float
/// Whether to show FPS counter
var showFPS: Bool
/// Default settings
static var `default`: AppSettings {
AppSettings(
selectedAudioDeviceUID: nil,
bufferSize: 1024,
lastVisualizationMode: 1,
reactivity: 0.5,
isFullscreen: false,
windowFrame: nil,
inputGain: 1.0,
showFPS: false
)
}
/// Available buffer sizes
static let availableBufferSizes = [512, 1024]
/// Validates and clamps settings to valid ranges
mutating func validate() {
// Clamp buffer size to valid options
if !AppSettings.availableBufferSizes.contains(bufferSize) {
bufferSize = 1024
}
// Clamp visualization mode
if lastVisualizationMode < 1 || lastVisualizationMode > 8 {
lastVisualizationMode = 1
}
// Clamp reactivity
reactivity = max(0.0, min(1.0, reactivity))
// Clamp input gain
inputGain = max(0.0, min(2.0, inputGain))
}
}
/// Codable wrapper for CGRect
struct CodableRect: Codable {
var x: Double
var y: Double
var width: Double
var height: Double
init(from rect: CGRect) {
self.x = Double(rect.origin.x)
self.y = Double(rect.origin.y)
self.width = Double(rect.size.width)
self.height = Double(rect.size.height)
}
var cgRect: CGRect {
CGRect(x: x, y: y, width: width, height: height)
}
}
@@ -0,0 +1,111 @@
//
// AudioAnalysisData.swift
// PsytranceVisualizer
//
// Audio analysis data structure containing all DSP results
//
import Foundation
/// Contains all audio analysis data computed by DSPEngine
struct AudioAnalysisData {
// MARK: - FFT Data
/// Raw FFT magnitude spectrum
var fftMagnitudes: [Float]
// MARK: - Mel Spectrogram
/// 64 Mel frequency bands
var melBands: [Float]
// MARK: - Sub-Bass Analysis
/// RMS energy below 100Hz (0.0 - 1.0)
var subBassEnergy: Float
/// History buffer for time-based visualization
var subBassHistory: [Float]
// MARK: - Sidechain Detection
/// Current envelope follower value (0.0 - 1.0)
var sidechainEnvelope: Float
/// Detected pumping amount (0.0 - 1.0)
var sidechainPumpAmount: Float
/// Whether pump is currently active
var isPumping: Bool
// MARK: - Harmonic-to-Noise Ratio
/// HNR ratio (0.0 = noise, 1.0 = pure harmonic)
var hnrRatio: Float
// MARK: - Transient Detection
/// Whether a transient peak was detected
var isPeak: Bool
/// Intensity of the detected peak (0.0 - 1.0)
var peakIntensity: Float
// MARK: - Stereo Channels
/// Left channel samples
var leftChannel: [Float]
/// Right channel samples
var rightChannel: [Float]
// MARK: - Additional Analysis
/// Spectral centroid (brightness) normalized 0.0 - 1.0
var spectralCentroid: Float
/// Overall RMS level
var rmsLevel: Float
// MARK: - Initialization
/// Creates an empty AudioAnalysisData with default values
static var empty: AudioAnalysisData {
AudioAnalysisData(
fftMagnitudes: [],
melBands: Array(repeating: 0, count: 64),
subBassEnergy: 0,
subBassHistory: [],
sidechainEnvelope: 0,
sidechainPumpAmount: 0,
isPumping: false,
hnrRatio: 0.5,
isPeak: false,
peakIntensity: 0,
leftChannel: [],
rightChannel: [],
spectralCentroid: 0.5,
rmsLevel: 0
)
}
/// Creates AudioAnalysisData with specified FFT size
static func create(fftSize: Int) -> AudioAnalysisData {
AudioAnalysisData(
fftMagnitudes: Array(repeating: 0, count: fftSize / 2),
melBands: Array(repeating: 0, count: 64),
subBassEnergy: 0,
subBassHistory: Array(repeating: 0, count: 128),
sidechainEnvelope: 0,
sidechainPumpAmount: 0,
isPumping: false,
hnrRatio: 0.5,
isPeak: false,
peakIntensity: 0,
leftChannel: [],
rightChannel: [],
spectralCentroid: 0.5,
rmsLevel: 0
)
}
}
@@ -0,0 +1,109 @@
//
// VisualizationMode.swift
// PsytranceVisualizer
//
// Enumeration of all available visualization modes
//
import Foundation
/// Available visualization modes, accessible via keyboard shortcuts 1-8
enum VisualizationMode: Int, CaseIterable, Codable {
case fftClassic = 1
case melSpectrogram = 2
case subBass = 3
case sidechainPump = 4
case hnr = 5
case mandelbrot = 6
case tunnelWarp = 7
case dmtGeometry = 8
/// Display name for UI
var displayName: String {
switch self {
case .fftClassic:
return "FFT Classic"
case .melSpectrogram:
return "Mel Spektrogramm"
case .subBass:
return "Sub-Bass (<100Hz)"
case .sidechainPump:
return "Sidechain Pump"
case .hnr:
return "Harmonic/Noise"
case .mandelbrot:
return "Mandelbrot"
case .tunnelWarp:
return "Tunnel Warp"
case .dmtGeometry:
return "DMT Geometry"
}
}
/// Keyboard shortcut (1-8)
var shortcut: String {
return "\(self.rawValue)"
}
/// Metal shader function name
var shaderFunctionName: String {
switch self {
case .fftClassic:
return "fftClassicFragment"
case .melSpectrogram:
return "melSpectrogramFragment"
case .subBass:
return "subBassFragment"
case .sidechainPump:
return "sidechainPumpFragment"
case .hnr:
return "hnrFragment"
case .mandelbrot:
return "mandelbrotFragment"
case .tunnelWarp:
return "tunnelWarpFragment"
case .dmtGeometry:
return "dmtGeometryFragment"
}
}
/// Description of the visualization
var description: String {
switch self {
case .fftClassic:
return "Classic frequency spectrum bars with glow effects"
case .melSpectrogram:
return "64-band Mel spectrogram with scrolling waterfall display"
case .subBass:
return "Pulsating rings visualizing sub-bass energy below 100Hz"
case .sidechainPump:
return "Breathing zoom effect synchronized to sidechain pumping"
case .hnr:
return "Harmonic vs noise visualization with geometric shapes"
case .mandelbrot:
return "Audio-reactive Mandelbrot fractal with zoom and color cycling"
case .tunnelWarp:
return "Infinite tunnel effect with warp distortion"
case .dmtGeometry:
return "Sacred geometry patterns: Flower of Life, Metatron's Cube, Sri Yantra"
}
}
/// Creates mode from keyboard key code
static func fromKeyCode(_ keyCode: UInt16) -> VisualizationMode? {
// Key codes for 1-8 on US keyboard
let keyCodes: [UInt16: Int] = [
18: 1, // 1
19: 2, // 2
20: 3, // 3
21: 4, // 4
23: 5, // 5
22: 6, // 6
26: 7, // 7
28: 8 // 8
]
guard let modeNumber = keyCodes[keyCode] else { return nil }
return VisualizationMode(rawValue: modeNumber)
}
}
+41
View File
@@ -0,0 +1,41 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "PsytranceVisualizer",
platforms: [
.macOS(.v13)
],
products: [
.executable(
name: "PsytranceVisualizer",
targets: ["PsytranceVisualizer"]
)
],
targets: [
.executableTarget(
name: "PsytranceVisualizer",
path: ".",
exclude: [
"Package.swift",
"README.md"
],
sources: [
"App",
"Audio",
"Models",
"Rendering",
"UI",
"Utilities"
],
resources: [
.process("Resources")
],
swiftSettings: [
.unsafeFlags(["-enable-bare-slash-regex"])
]
)
]
)
@@ -0,0 +1,465 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 56;
objects = {
/* Begin PBXBuildFile section */
1100000000000001 /* PsytranceVisualizerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000001 /* PsytranceVisualizerApp.swift */; };
1100000000000002 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000002 /* AppDelegate.swift */; };
1100000000000003 /* AudioInputManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000003 /* AudioInputManager.swift */; };
1100000000000004 /* DSPEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000004 /* DSPEngine.swift */; };
1100000000000005 /* AudioAnalysisData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000005 /* AudioAnalysisData.swift */; };
1100000000000006 /* VisualizationMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000006 /* VisualizationMode.swift */; };
1100000000000007 /* AppSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000007 /* AppSettings.swift */; };
1100000000000008 /* MetalRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000008 /* MetalRenderer.swift */; };
1100000000000009 /* Common.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000009 /* Common.metal */; };
1100000000000010 /* FFTClassicShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000010 /* FFTClassicShader.metal */; };
1100000000000011 /* MelSpectrogramShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000011 /* MelSpectrogramShader.metal */; };
1100000000000012 /* SubBassShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000012 /* SubBassShader.metal */; };
1100000000000013 /* SidechainPumpShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000013 /* SidechainPumpShader.metal */; };
1100000000000014 /* HNRShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000014 /* HNRShader.metal */; };
1100000000000015 /* MandelbrotShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000015 /* MandelbrotShader.metal */; };
1100000000000016 /* TunnelWarpShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000016 /* TunnelWarpShader.metal */; };
1100000000000017 /* DMTGeometryShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000017 /* DMTGeometryShader.metal */; };
1100000000000018 /* MainWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000018 /* MainWindow.swift */; };
1100000000000019 /* ControlPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000019 /* ControlPanel.swift */; };
1100000000000020 /* VisualizerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000020 /* VisualizerView.swift */; };
1100000000000021 /* SettingsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000021 /* SettingsManager.swift */; };
1100000000000022 /* ColorPalette.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2100000000000022 /* ColorPalette.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
2000000000000001 /* PsytranceVisualizer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PsytranceVisualizer.app; sourceTree = BUILT_PRODUCTS_DIR; };
2100000000000001 /* PsytranceVisualizerApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PsytranceVisualizerApp.swift; sourceTree = "<group>"; };
2100000000000002 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2100000000000003 /* AudioInputManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioInputManager.swift; sourceTree = "<group>"; };
2100000000000004 /* DSPEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DSPEngine.swift; sourceTree = "<group>"; };
2100000000000005 /* AudioAnalysisData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioAnalysisData.swift; sourceTree = "<group>"; };
2100000000000006 /* VisualizationMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisualizationMode.swift; sourceTree = "<group>"; };
2100000000000007 /* AppSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppSettings.swift; sourceTree = "<group>"; };
2100000000000008 /* MetalRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetalRenderer.swift; sourceTree = "<group>"; };
2100000000000009 /* Common.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Common.metal; sourceTree = "<group>"; };
2100000000000010 /* FFTClassicShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = FFTClassicShader.metal; sourceTree = "<group>"; };
2100000000000011 /* MelSpectrogramShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = MelSpectrogramShader.metal; sourceTree = "<group>"; };
2100000000000012 /* SubBassShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = SubBassShader.metal; sourceTree = "<group>"; };
2100000000000013 /* SidechainPumpShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = SidechainPumpShader.metal; sourceTree = "<group>"; };
2100000000000014 /* HNRShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = HNRShader.metal; sourceTree = "<group>"; };
2100000000000015 /* MandelbrotShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = MandelbrotShader.metal; sourceTree = "<group>"; };
2100000000000016 /* TunnelWarpShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = TunnelWarpShader.metal; sourceTree = "<group>"; };
2100000000000017 /* DMTGeometryShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = DMTGeometryShader.metal; sourceTree = "<group>"; };
2100000000000018 /* MainWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainWindow.swift; sourceTree = "<group>"; };
2100000000000019 /* ControlPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControlPanel.swift; sourceTree = "<group>"; };
2100000000000020 /* VisualizerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisualizerView.swift; sourceTree = "<group>"; };
2100000000000021 /* SettingsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsManager.swift; sourceTree = "<group>"; };
2100000000000022 /* ColorPalette.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPalette.swift; sourceTree = "<group>"; };
2100000000000023 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2100000000000024 /* PsytranceVisualizer.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PsytranceVisualizer.entitlements; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
3000000000000001 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
4000000000000001 = {
isa = PBXGroup;
children = (
4000000000000002 /* App */,
4000000000000003 /* Audio */,
4000000000000004 /* Models */,
4000000000000005 /* Rendering */,
4000000000000007 /* UI */,
4000000000000008 /* Utilities */,
4000000000000009 /* Resources */,
4000000000000010 /* Products */,
);
sourceTree = "<group>";
};
4000000000000002 /* App */ = {
isa = PBXGroup;
children = (
2100000000000001 /* PsytranceVisualizerApp.swift */,
2100000000000002 /* AppDelegate.swift */,
);
path = App;
sourceTree = "<group>";
};
4000000000000003 /* Audio */ = {
isa = PBXGroup;
children = (
2100000000000003 /* AudioInputManager.swift */,
2100000000000004 /* DSPEngine.swift */,
);
path = Audio;
sourceTree = "<group>";
};
4000000000000004 /* Models */ = {
isa = PBXGroup;
children = (
2100000000000005 /* AudioAnalysisData.swift */,
2100000000000006 /* VisualizationMode.swift */,
2100000000000007 /* AppSettings.swift */,
);
path = Models;
sourceTree = "<group>";
};
4000000000000005 /* Rendering */ = {
isa = PBXGroup;
children = (
2100000000000008 /* MetalRenderer.swift */,
4000000000000006 /* Shaders */,
);
path = Rendering;
sourceTree = "<group>";
};
4000000000000006 /* Shaders */ = {
isa = PBXGroup;
children = (
2100000000000009 /* Common.metal */,
2100000000000010 /* FFTClassicShader.metal */,
2100000000000011 /* MelSpectrogramShader.metal */,
2100000000000012 /* SubBassShader.metal */,
2100000000000013 /* SidechainPumpShader.metal */,
2100000000000014 /* HNRShader.metal */,
2100000000000015 /* MandelbrotShader.metal */,
2100000000000016 /* TunnelWarpShader.metal */,
2100000000000017 /* DMTGeometryShader.metal */,
);
path = Shaders;
sourceTree = "<group>";
};
4000000000000007 /* UI */ = {
isa = PBXGroup;
children = (
2100000000000018 /* MainWindow.swift */,
2100000000000019 /* ControlPanel.swift */,
2100000000000020 /* VisualizerView.swift */,
);
path = UI;
sourceTree = "<group>";
};
4000000000000008 /* Utilities */ = {
isa = PBXGroup;
children = (
2100000000000021 /* SettingsManager.swift */,
2100000000000022 /* ColorPalette.swift */,
);
path = Utilities;
sourceTree = "<group>";
};
4000000000000009 /* Resources */ = {
isa = PBXGroup;
children = (
2100000000000023 /* Info.plist */,
2100000000000024 /* PsytranceVisualizer.entitlements */,
);
path = Resources;
sourceTree = "<group>";
};
4000000000000010 /* Products */ = {
isa = PBXGroup;
children = (
2000000000000001 /* PsytranceVisualizer.app */,
);
name = Products;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
5000000000000001 /* PsytranceVisualizer */ = {
isa = PBXNativeTarget;
buildConfigurationList = 6000000000000003 /* Build configuration list for PBXNativeTarget "PsytranceVisualizer" */;
buildPhases = (
5000000000000002 /* Sources */,
3000000000000001 /* Frameworks */,
5000000000000003 /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = PsytranceVisualizer;
productName = PsytranceVisualizer;
productReference = 2000000000000001 /* PsytranceVisualizer.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
0000000000000001 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1500;
LastUpgradeCheck = 1500;
TargetAttributes = {
5000000000000001 = {
CreatedOnToolsVersion = 15.0;
};
};
};
buildConfigurationList = 6000000000000001 /* Build configuration list for PBXProject "PsytranceVisualizer" */;
compatibilityVersion = "Xcode 14.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 4000000000000001;
productRefGroup = 4000000000000010 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
5000000000000001 /* PsytranceVisualizer */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
5000000000000003 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
5000000000000002 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1100000000000001 /* PsytranceVisualizerApp.swift in Sources */,
1100000000000002 /* AppDelegate.swift in Sources */,
1100000000000003 /* AudioInputManager.swift in Sources */,
1100000000000004 /* DSPEngine.swift in Sources */,
1100000000000005 /* AudioAnalysisData.swift in Sources */,
1100000000000006 /* VisualizationMode.swift in Sources */,
1100000000000007 /* AppSettings.swift in Sources */,
1100000000000008 /* MetalRenderer.swift in Sources */,
1100000000000009 /* Common.metal in Sources */,
1100000000000010 /* FFTClassicShader.metal in Sources */,
1100000000000011 /* MelSpectrogramShader.metal in Sources */,
1100000000000012 /* SubBassShader.metal in Sources */,
1100000000000013 /* SidechainPumpShader.metal in Sources */,
1100000000000014 /* HNRShader.metal in Sources */,
1100000000000015 /* MandelbrotShader.metal in Sources */,
1100000000000016 /* TunnelWarpShader.metal in Sources */,
1100000000000017 /* DMTGeometryShader.metal in Sources */,
1100000000000018 /* MainWindow.swift in Sources */,
1100000000000019 /* ControlPanel.swift in Sources */,
1100000000000020 /* VisualizerView.swift in Sources */,
1100000000000021 /* SettingsManager.swift in Sources */,
1100000000000022 /* ColorPalette.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
6100000000000001 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
6100000000000002 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
};
name = Release;
};
6100000000000003 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Resources/PsytranceVisualizer.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Resources/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.music";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_NSMicrophoneUsageDescription = "Psytrance Visualizer needs access to your audio input to visualize music in real-time.";
INFOPLIST_KEY_NSPrincipalClass = NSApplication;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.psytrance.visualizer;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
6100000000000004 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Resources/PsytranceVisualizer.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Resources/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.music";
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_NSMicrophoneUsageDescription = "Psytrance Visualizer needs access to your audio input to visualize music in real-time.";
INFOPLIST_KEY_NSPrincipalClass = NSApplication;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.psytrance.visualizer;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
6000000000000001 /* Build configuration list for PBXProject "PsytranceVisualizer" */ = {
isa = XCConfigurationList;
buildConfigurations = (
6100000000000001 /* Debug */,
6100000000000002 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
6000000000000003 /* Build configuration list for PBXNativeTarget "PsytranceVisualizer" */ = {
isa = XCConfigurationList;
buildConfigurations = (
6100000000000003 /* Debug */,
6100000000000004 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 0000000000000001 /* Project object */;
}
@@ -0,0 +1,279 @@
//
// MetalRenderer.swift
// PsytranceVisualizer
//
// Metal-based renderer for all visualization modes
//
import MetalKit
import simd
/// Uniform data passed to all shaders
struct ShaderUniforms {
var time: Float
var resolution: SIMD2<Float>
var reactivity: Float
// Audio analysis data
var subBassEnergy: Float
var sidechainPump: Float
var sidechainEnvelope: Float
var hnrRatio: Float
var isPeak: Float
var peakIntensity: Float
var spectralCentroid: Float
var rmsLevel: Float
// Visualization mode (1-8)
var mode: Int32
// Padding for Metal alignment
var padding: SIMD2<Float> = .zero
}
/// Metal renderer managing all visualization shaders
final class MetalRenderer: NSObject, ObservableObject {
// MARK: - Properties
private let device: MTLDevice
private let commandQueue: MTLCommandQueue
private var pipelineStates: [VisualizationMode: MTLRenderPipelineState] = [:]
private var currentPipelineState: MTLRenderPipelineState?
@Published private(set) var currentMode: VisualizationMode = .fftClassic
// MARK: - Buffers
private var uniformBuffer: MTLBuffer?
private var fftBuffer: MTLBuffer?
private var melBuffer: MTLBuffer?
private var subBassHistoryBuffer: MTLBuffer?
// MARK: - State
private var startTime: CFAbsoluteTime
private var uniforms = ShaderUniforms(
time: 0,
resolution: SIMD2<Float>(1920, 1080),
reactivity: 0.5,
subBassEnergy: 0,
sidechainPump: 0,
sidechainEnvelope: 0,
hnrRatio: 0.5,
isPeak: 0,
peakIntensity: 0,
spectralCentroid: 0.5,
rmsLevel: 0,
mode: 1
)
private var audioData: AudioAnalysisData = .empty
// MARK: - Constants
private let maxFFTSize = 1024
private let melBandCount = 64
private let historySize = 128
// MARK: - Initialization
init?(device: MTLDevice) {
guard let queue = device.makeCommandQueue() else {
print("[MetalRenderer] Failed to create command queue")
return nil
}
self.device = device
self.commandQueue = queue
self.startTime = CFAbsoluteTimeGetCurrent()
super.init()
createBuffers()
loadShaders()
}
// MARK: - Public Methods
/// Sets the current visualization mode
func setVisualizationMode(_ mode: VisualizationMode) {
currentMode = mode
currentPipelineState = pipelineStates[mode]
uniforms.mode = Int32(mode.rawValue)
print("[MetalRenderer] Mode changed to: \(mode.displayName)")
}
/// Updates audio analysis data
func updateAudioData(_ data: AudioAnalysisData) {
audioData = data
// Update uniforms
uniforms.subBassEnergy = data.subBassEnergy
uniforms.sidechainPump = data.sidechainPumpAmount
uniforms.sidechainEnvelope = data.sidechainEnvelope
uniforms.hnrRatio = data.hnrRatio
uniforms.isPeak = data.isPeak ? 1.0 : 0.0
uniforms.peakIntensity = data.peakIntensity
uniforms.spectralCentroid = data.spectralCentroid
uniforms.rmsLevel = data.rmsLevel
// Update FFT buffer
updateFFTBuffer(data.fftMagnitudes)
// Update Mel buffer
updateMelBuffer(data.melBands)
// Update sub-bass history buffer
updateSubBassHistoryBuffer(data.subBassHistory)
}
/// Sets reactivity value
func setReactivity(_ value: Float) {
uniforms.reactivity = max(0.0, min(1.0, value))
}
// MARK: - Private Methods
private func createBuffers() {
// Uniform buffer
uniformBuffer = device.makeBuffer(
length: MemoryLayout<ShaderUniforms>.stride,
options: .storageModeShared
)
// FFT magnitude buffer
fftBuffer = device.makeBuffer(
length: maxFFTSize * MemoryLayout<Float>.stride,
options: .storageModeShared
)
// Mel bands buffer
melBuffer = device.makeBuffer(
length: melBandCount * MemoryLayout<Float>.stride,
options: .storageModeShared
)
// Sub-bass history buffer
subBassHistoryBuffer = device.makeBuffer(
length: historySize * MemoryLayout<Float>.stride,
options: .storageModeShared
)
}
private func updateFFTBuffer(_ magnitudes: [Float]) {
guard let buffer = fftBuffer else { return }
let count = min(magnitudes.count, maxFFTSize)
memcpy(buffer.contents(), magnitudes, count * MemoryLayout<Float>.stride)
}
private func updateMelBuffer(_ bands: [Float]) {
guard let buffer = melBuffer else { return }
let count = min(bands.count, melBandCount)
memcpy(buffer.contents(), bands, count * MemoryLayout<Float>.stride)
}
private func updateSubBassHistoryBuffer(_ history: [Float]) {
guard let buffer = subBassHistoryBuffer else { return }
let count = min(history.count, historySize)
memcpy(buffer.contents(), history, count * MemoryLayout<Float>.stride)
}
private func loadShaders() {
guard let library = device.makeDefaultLibrary() else {
print("[MetalRenderer] Failed to load shader library")
return
}
// Load vertex shader (shared)
guard let vertexFunction = library.makeFunction(name: "vertexShader") else {
print("[MetalRenderer] Failed to load vertex shader")
return
}
// Load all fragment shaders
for mode in VisualizationMode.allCases {
guard let fragmentFunction = library.makeFunction(name: mode.shaderFunctionName) else {
print("[MetalRenderer] Failed to load shader: \(mode.shaderFunctionName)")
continue
}
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = vertexFunction
descriptor.fragmentFunction = fragmentFunction
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
// Enable blending for glow effects
descriptor.colorAttachments[0].isBlendingEnabled = true
descriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
descriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
descriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
descriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
do {
let pipelineState = try device.makeRenderPipelineState(descriptor: descriptor)
pipelineStates[mode] = pipelineState
print("[MetalRenderer] Loaded shader: \(mode.displayName)")
} catch {
print("[MetalRenderer] Failed to create pipeline state for \(mode.displayName): \(error)")
}
}
// Set initial pipeline state
currentPipelineState = pipelineStates[.fftClassic]
}
}
// MARK: - MTKViewDelegate
extension MetalRenderer: MTKViewDelegate {
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
uniforms.resolution = SIMD2<Float>(Float(size.width), Float(size.height))
}
func draw(in view: MTKView) {
guard let pipelineState = currentPipelineState,
let drawable = view.currentDrawable,
let renderPassDescriptor = view.currentRenderPassDescriptor else {
return
}
// Update time
uniforms.time = Float(CFAbsoluteTimeGetCurrent() - startTime)
// Update uniform buffer
if let buffer = uniformBuffer {
memcpy(buffer.contents(), &uniforms, MemoryLayout<ShaderUniforms>.stride)
}
// Create command buffer
guard let commandBuffer = commandQueue.makeCommandBuffer(),
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) else {
return
}
// Set pipeline state
renderEncoder.setRenderPipelineState(pipelineState)
// Set buffers
if let buffer = uniformBuffer {
renderEncoder.setFragmentBuffer(buffer, offset: 0, index: 0)
}
if let buffer = fftBuffer {
renderEncoder.setFragmentBuffer(buffer, offset: 0, index: 1)
}
if let buffer = melBuffer {
renderEncoder.setFragmentBuffer(buffer, offset: 0, index: 2)
}
if let buffer = subBassHistoryBuffer {
renderEncoder.setFragmentBuffer(buffer, offset: 0, index: 3)
}
// Draw fullscreen quad
renderEncoder.drawPrimitives(type: .triangleStrip, vertexStart: 0, vertexCount: 4)
renderEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
}
}
@@ -0,0 +1,241 @@
//
// Common.metal
// PsytranceVisualizer
//
// Shared shader functions, types, and psytrance color palette
//
#include <metal_stdlib>
using namespace metal;
// MARK: - Uniforms Structure
struct ShaderUniforms {
float time;
float2 resolution;
float reactivity;
float subBassEnergy;
float sidechainPump;
float sidechainEnvelope;
float hnrRatio;
float isPeak;
float peakIntensity;
float spectralCentroid;
float rmsLevel;
int mode;
float2 padding;
};
// MARK: - Vertex Data
struct VertexOut {
float4 position [[position]];
float2 uv;
};
// MARK: - Psytrance Color Palette
constant float3 neonMagenta = float3(1.0, 0.0, 1.0);
constant float3 neonCyan = float3(0.0, 1.0, 1.0);
constant float3 neonGreen = float3(0.224, 1.0, 0.078);
constant float3 uvViolet = float3(0.482, 0.0, 1.0);
constant float3 hotPink = float3(1.0, 0.2, 0.6);
constant float3 electricBlue = float3(0.0, 0.5, 1.0);
constant float3 deepPurple = float3(0.1, 0.0, 0.15);
// MARK: - Palette Functions
inline float3 getPaletteColor(int index) {
switch (index % 6) {
case 0: return neonMagenta;
case 1: return neonCyan;
case 2: return neonGreen;
case 3: return uvViolet;
case 4: return hotPink;
default: return electricBlue;
}
}
inline float3 rainbowPalette(float t) {
float3 a = float3(0.5, 0.5, 0.5);
float3 b = float3(0.5, 0.5, 0.5);
float3 c = float3(1.0, 1.0, 1.0);
float3 d = float3(0.0, 0.33, 0.67);
return a + b * cos(6.28318 * (c * t + d));
}
inline float3 psytrancePalette(float t, float time) {
// Cycle through psytrance colors
float phase = fract(t + time * 0.1);
if (phase < 0.2) {
return mix(uvViolet, neonMagenta, phase * 5.0);
} else if (phase < 0.4) {
return mix(neonMagenta, hotPink, (phase - 0.2) * 5.0);
} else if (phase < 0.6) {
return mix(hotPink, neonCyan, (phase - 0.4) * 5.0);
} else if (phase < 0.8) {
return mix(neonCyan, neonGreen, (phase - 0.6) * 5.0);
} else {
return mix(neonGreen, uvViolet, (phase - 0.8) * 5.0);
}
}
// MARK: - Heatmap for Spectrogram
inline float3 heatmap(float t) {
// Low energy: dark purple
// High energy: white through neon colors
if (t < 0.2) {
return mix(float3(0.05, 0.0, 0.1), uvViolet, t * 5.0);
} else if (t < 0.4) {
return mix(uvViolet, neonMagenta, (t - 0.2) * 5.0);
} else if (t < 0.6) {
return mix(neonMagenta, hotPink, (t - 0.4) * 5.0);
} else if (t < 0.8) {
return mix(hotPink, neonCyan, (t - 0.6) * 5.0);
} else {
return mix(neonCyan, float3(1.0), (t - 0.8) * 5.0);
}
}
// MARK: - Noise Functions
// Simplex-like noise
inline float hash(float2 p) {
float3 p3 = fract(float3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
inline float noise(float2 p) {
float2 i = floor(p);
float2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = hash(i);
float b = hash(i + float2(1.0, 0.0));
float c = hash(i + float2(0.0, 1.0));
float d = hash(i + float2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
inline float fbm(float2 p, int octaves) {
float value = 0.0;
float amplitude = 0.5;
float frequency = 1.0;
for (int i = 0; i < octaves; i++) {
value += amplitude * noise(p * frequency);
frequency *= 2.0;
amplitude *= 0.5;
}
return value;
}
// 3D noise for volumetric effects
inline float noise3D(float3 p) {
float3 i = floor(p);
float3 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float2 uv = i.xy + float2(37.0, 17.0) * i.z;
float a = hash(uv);
float b = hash(uv + float2(1.0, 0.0));
float c = hash(uv + float2(0.0, 1.0));
float d = hash(uv + float2(1.0, 1.0));
float2 uv2 = uv + float2(37.0, 17.0);
float e = hash(uv2);
float ff = hash(uv2 + float2(1.0, 0.0));
float g = hash(uv2 + float2(0.0, 1.0));
float h = hash(uv2 + float2(1.0, 1.0));
float x1 = mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
float x2 = mix(mix(e, ff, f.x), mix(g, h, f.x), f.y);
return mix(x1, x2, f.z);
}
// MARK: - Utility Functions
inline float2 rotate(float2 p, float angle) {
float c = cos(angle);
float s = sin(angle);
return float2(p.x * c - p.y * s, p.x * s + p.y * c);
}
inline float map(float value, float inMin, float inMax, float outMin, float outMax) {
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}
inline float smoothstepEdge(float edge0, float edge1, float x) {
float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}
// MARK: - Glow Effect
inline float3 addGlow(float3 color, float intensity, float3 glowColor) {
return color + glowColor * intensity * intensity;
}
// MARK: - SDF Functions for Geometry
inline float sdCircle(float2 p, float r) {
return length(p) - r;
}
inline float sdBox(float2 p, float2 b) {
float2 d = abs(p) - b;
return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
}
inline float sdHexagon(float2 p, float r) {
const float3 k = float3(-0.866025404, 0.5, 0.577350269);
p = abs(p);
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
p -= float2(clamp(p.x, -k.z * r, k.z * r), r);
return length(p) * sign(p.y);
}
inline float sdStar(float2 p, float r, int n, float m) {
float an = 3.141593 / float(n);
float en = 3.141593 / m;
float2 acs = float2(cos(an), sin(an));
float2 ecs = float2(cos(en), sin(en));
float bn = fmod(atan2(p.x, p.y), 2.0 * an) - an;
p = length(p) * float2(cos(bn), abs(sin(bn)));
p -= r * acs;
p += ecs * clamp(-dot(p, ecs), 0.0, r * acs.y / ecs.y);
return length(p) * sign(p.x);
}
// MARK: - Vertex Shader (Fullscreen Quad)
vertex VertexOut vertexShader(uint vertexID [[vertex_id]]) {
// Generate fullscreen quad
float2 positions[4] = {
float2(-1.0, -1.0),
float2( 1.0, -1.0),
float2(-1.0, 1.0),
float2( 1.0, 1.0)
};
float2 uvs[4] = {
float2(0.0, 1.0),
float2(1.0, 1.0),
float2(0.0, 0.0),
float2(1.0, 0.0)
};
VertexOut out;
out.position = float4(positions[vertexID], 0.0, 1.0);
out.uv = uvs[vertexID];
return out;
}
@@ -0,0 +1,290 @@
//
// DMTGeometryShader.metal
// PsytranceVisualizer
//
// Sacred geometry patterns: Flower of Life, Metatron's Cube, Sri Yantra, Hexagonal
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
// === SACRED GEOMETRY PRIMITIVES ===
// Flower of Life - overlapping circles
float flowerOfLife(float2 p, float scale, float time) {
p *= scale;
float result = 0.0;
float circleRadius = 0.5;
// Center circle
result = max(result, 1.0 - smoothstep(circleRadius - 0.02, circleRadius, length(p)));
// 6 circles around center
for (int i = 0; i < 6; i++) {
float angle = float(i) * 3.14159 / 3.0 + time * 0.1;
float2 offset = float2(cos(angle), sin(angle)) * circleRadius;
float d = length(p - offset);
result = max(result, 1.0 - smoothstep(circleRadius - 0.02, circleRadius, d));
}
// Second ring of 12 circles
for (int i = 0; i < 12; i++) {
float angle = float(i) * 3.14159 / 6.0 + time * 0.05;
float2 offset = float2(cos(angle), sin(angle)) * circleRadius * 2.0;
float d = length(p - offset);
result = max(result, 0.5 * (1.0 - smoothstep(circleRadius - 0.02, circleRadius, d)));
}
return result;
}
// Metatron's Cube - 13 circles with connecting lines
float metatronsCube(float2 p, float scale, float time) {
p *= scale;
float result = 0.0;
float nodeRadius = 0.08;
float lineWidth = 0.01;
// Define the 13 points of Metatron's Cube
float2 points[13];
points[0] = float2(0.0, 0.0); // Center
// Inner hexagon
for (int i = 0; i < 6; i++) {
float angle = float(i) * 3.14159 / 3.0 + time * 0.1;
points[i + 1] = float2(cos(angle), sin(angle)) * 0.5;
}
// Outer hexagon (rotated)
for (int i = 0; i < 6; i++) {
float angle = float(i) * 3.14159 / 3.0 + 3.14159 / 6.0 + time * 0.1;
points[i + 7] = float2(cos(angle), sin(angle)) * 0.866;
}
// Draw nodes
for (int i = 0; i < 13; i++) {
float d = length(p - points[i]);
float node = 1.0 - smoothstep(nodeRadius - 0.01, nodeRadius, d);
result = max(result, node);
}
// Draw connecting lines
for (int i = 0; i < 13; i++) {
for (int j = i + 1; j < 13; j++) {
float2 a = points[i];
float2 b = points[j];
float2 pa = p - a;
float2 ba = b - a;
float t = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
float d = length(pa - ba * t);
float line = 1.0 - smoothstep(lineWidth, lineWidth + 0.005, d);
result = max(result, line * 0.5);
}
}
return result;
}
// Sri Yantra - 9 interlocking triangles
float sriYantra(float2 p, float scale, float time) {
p *= scale;
float result = 0.0;
float lineWidth = 0.015;
// Rotating factor
float rot = time * 0.05;
// Draw 4 upward triangles
for (int i = 0; i < 4; i++) {
float size = 0.3 + float(i) * 0.15;
float yOffset = -0.1 + float(i) * 0.05;
float2 tp = p - float2(0.0, yOffset);
tp = rotate(tp, rot);
// Triangle SDF
float2 a = float2(0.0, size);
float2 b = float2(-size * 0.866, -size * 0.5);
float2 c = float2(size * 0.866, -size * 0.5);
float d1 = dot(tp - a, normalize(float2(b.y - a.y, a.x - b.x)));
float d2 = dot(tp - b, normalize(float2(c.y - b.y, b.x - c.x)));
float d3 = dot(tp - c, normalize(float2(a.y - c.y, c.x - a.x)));
float triangleDist = max(max(d1, d2), d3);
float edge = 1.0 - smoothstep(0.0, lineWidth, abs(triangleDist));
result = max(result, edge * (1.0 - float(i) * 0.15));
}
// Draw 5 downward triangles
for (int i = 0; i < 5; i++) {
float size = 0.25 + float(i) * 0.12;
float yOffset = 0.1 - float(i) * 0.04;
float2 tp = p - float2(0.0, yOffset);
tp = rotate(tp, -rot);
float2 a = float2(0.0, -size);
float2 b = float2(-size * 0.866, size * 0.5);
float2 c = float2(size * 0.866, size * 0.5);
float d1 = dot(tp - a, normalize(float2(b.y - a.y, a.x - b.x)));
float d2 = dot(tp - b, normalize(float2(c.y - b.y, b.x - c.x)));
float d3 = dot(tp - c, normalize(float2(a.y - c.y, c.x - a.x)));
float triangleDist = max(max(d1, d2), d3);
float edge = 1.0 - smoothstep(0.0, lineWidth, abs(triangleDist));
result = max(result, edge * (1.0 - float(i) * 0.12));
}
// Central bindu (point)
float bindu = 1.0 - smoothstep(0.03, 0.04, length(p));
result = max(result, bindu);
return result;
}
// Hexagonal grid pattern
float hexagonalPattern(float2 p, float scale, float time) {
p *= scale;
// Hexagonal grid transformation
float2 s = float2(1.0, 1.732);
float2 h = s * 0.5;
float2 a = fmod(p, s) - h;
float2 b = fmod(p + h, s) - h;
float2 gv = dot(a, a) < dot(b, b) ? a : b;
float hexDist = max(abs(gv.x), dot(abs(gv), normalize(float2(1.0, 1.732))));
float edge = 1.0 - smoothstep(0.4, 0.42, hexDist);
float fill = smoothstep(0.38, 0.4, hexDist);
// Animate individual hexagons
float2 cellId = floor(p / s);
float cellPhase = hash(cellId + floor(time * 0.5)) * 2.0 * 3.14159;
float pulse = 0.5 + 0.5 * sin(time * 3.0 + cellPhase);
return edge + fill * pulse * 0.3;
}
// === MAIN FRAGMENT SHADER ===
fragment float4 dmtGeometryFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
float subBass = uniforms.subBassEnergy;
float hnr = uniforms.hnrRatio;
float peak = uniforms.isPeak;
float peakIntensity = uniforms.peakIntensity;
// Aspect ratio correction
float aspectRatio = resolution.x / resolution.y;
float2 p = (uv - 0.5) * 2.0;
p.x *= aspectRatio;
// Scale pulsing with sub-bass
float scale = 2.0 + subBass * 0.5 * (0.5 + reactivity * 0.5);
p *= scale;
// Rotation
float rotation = time * 0.1;
p = rotate(p, rotation);
// Determine which geometry to show
// Changes on peaks or every few seconds
float cycleTime = 8.0; // Seconds per geometry
float cyclePhase = fmod(time, cycleTime * 4.0) / cycleTime;
int geometryIndex = int(cyclePhase);
// Force change on strong peaks
if (peak > 0.5 && peakIntensity > 0.7) {
geometryIndex = int(fmod(float(geometryIndex) + 1.0, 4.0));
}
// Calculate all geometries (for blending)
float flower = flowerOfLife(p, 1.0, time);
float metatron = metatronsCube(p, 1.5, time);
float yantra = sriYantra(p, 1.2, time);
float hexGrid = hexagonalPattern(p, 3.0, time);
// Select primary and secondary for blending
float primary = 0.0;
float secondary = 0.0;
float blendPhase = fract(cyclePhase);
switch (geometryIndex) {
case 0:
primary = flower;
secondary = metatron;
break;
case 1:
primary = metatron;
secondary = yantra;
break;
case 2:
primary = yantra;
secondary = hexGrid;
break;
default:
primary = hexGrid;
secondary = flower;
break;
}
// Smooth transition
float transitionWindow = 0.2; // 20% of cycle for transition
float blend = smoothstep(1.0 - transitionWindow, 1.0, blendPhase);
float geometry = mix(primary, secondary, blend);
// Complexity based on HNR (more harmonic = more detail)
geometry *= 0.7 + hnr * 0.3;
// Color based on geometry and audio
float colorPhase = time * 0.1 + geometry * 0.5;
float3 geometryColor = psytrancePalette(colorPhase, time);
// Glow intensity from peak
float glowIntensity = 0.5 + peakIntensity * 0.5;
float3 glowColor = mix(neonMagenta, neonCyan, 0.5 + 0.5 * sin(time));
// Compose final color
float3 finalColor = geometryColor * geometry;
// Add glow
finalColor = addGlow(finalColor, geometry * glowIntensity, glowColor);
// Background - subtle pulsing gradient
float dist = length(uv - 0.5);
float3 bgColor = mix(deepPurple, uvViolet * 0.3, dist);
bgColor *= 0.8 + 0.2 * subBass;
finalColor = mix(bgColor, finalColor, clamp(geometry * 1.5, 0.0, 1.0));
// Peak flash
if (peak > 0.5) {
finalColor += float3(1.0) * peakIntensity * 0.2;
}
// Outer glow
float outerGlow = exp(-dist * 3.0);
finalColor += neonMagenta * outerGlow * 0.1 * subBass;
return float4(finalColor, 1.0);
}
@@ -0,0 +1,117 @@
//
// FFTClassicShader.metal
// PsytranceVisualizer
//
// Classic FFT bar visualization with glow effects
//
#include <metal_stdlib>
using namespace metal;
// Include common definitions
#include "Common.metal"
fragment float4 fftClassicFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
// Number of bars to display
const int numBars = 64;
const float barWidth = 1.0 / float(numBars);
const float barGap = barWidth * 0.2;
const float actualBarWidth = barWidth - barGap;
// Determine which bar this pixel belongs to
int barIndex = int(uv.x * float(numBars));
barIndex = clamp(barIndex, 0, numBars - 1);
// Get FFT magnitude for this bar (with some averaging for smoothness)
float magnitude = fftData[barIndex];
// Apply reactivity scaling
magnitude = magnitude * (0.5 + reactivity * 1.5);
magnitude = clamp(magnitude, 0.0, 1.0);
// Calculate bar position within its cell
float barCellX = fract(uv.x * float(numBars));
float barCenterX = 0.5;
// Distance from bar center (for width calculation)
float distFromCenter = abs(barCellX - barCenterX);
float halfWidth = actualBarWidth * 0.5 / barWidth;
// Check if we're inside the bar horizontally
bool insideBarX = distFromCenter < halfWidth;
// Bar height from bottom
float barHeight = magnitude;
// Add some bounce on peaks
if (uniforms.isPeak > 0.5) {
barHeight += uniforms.peakIntensity * 0.1 * sin(time * 20.0 + float(barIndex) * 0.3);
}
// Check if we're inside the bar vertically (from bottom)
float yFromBottom = 1.0 - uv.y;
bool insideBarY = yFromBottom < barHeight;
// Color based on frequency and magnitude
float colorPhase = float(barIndex) / float(numBars) + time * 0.05;
float3 barColor = psytrancePalette(colorPhase, time);
// Intensity gradient from bottom to top
float intensityGradient = yFromBottom / max(barHeight, 0.01);
intensityGradient = clamp(intensityGradient, 0.0, 1.0);
// Make top of bars brighter
barColor = mix(barColor * 0.6, barColor * 1.5, intensityGradient);
// Calculate glow
float glowRadius = 0.05 * (1.0 + magnitude);
float distToBar = 0.0;
if (!insideBarX) {
distToBar = (distFromCenter - halfWidth) * barWidth;
}
if (!insideBarY && yFromBottom >= barHeight) {
float vertDist = yFromBottom - barHeight;
distToBar = max(distToBar, vertDist);
}
float glow = exp(-distToBar * distToBar / (glowRadius * glowRadius * 2.0));
glow *= magnitude;
// Final color
float3 finalColor = float3(0.0);
if (insideBarX && insideBarY) {
// Inside the bar
finalColor = barColor;
// Add peak cap (bright line at top)
float capThickness = 0.01;
if (abs(yFromBottom - barHeight) < capThickness) {
finalColor = float3(1.0); // White cap
}
} else {
// Add glow outside bars
finalColor = barColor * glow * 0.5;
}
// Add subtle background pulse with sub-bass
float bgPulse = uniforms.subBassEnergy * 0.05;
finalColor += deepPurple * bgPulse;
// Add overall glow at peaks
if (uniforms.isPeak > 0.5) {
finalColor += neonMagenta * uniforms.peakIntensity * 0.1;
}
return float4(finalColor, 1.0);
}
@@ -0,0 +1,142 @@
//
// HNRShader.metal
// PsytranceVisualizer
//
// Harmonic-to-Noise ratio visualization with geometric shapes vs chaos
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
fragment float4 hnrFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
float hnr = uniforms.hnrRatio;
float subBass = uniforms.subBassEnergy;
// Center coordinates
float2 center = float2(0.5, 0.5);
float aspectRatio = resolution.x / resolution.y;
float2 p = uv - center;
p.x *= aspectRatio;
float dist = length(p);
float angle = atan2(p.y, p.x);
// === HARMONIC SIDE (High HNR = Clear geometric shapes) ===
// Rotating hexagon
float2 rotP = rotate(p, time * 0.5);
float hexDist = sdHexagon(rotP, 0.2 + subBass * 0.1);
float hexEdge = 1.0 - smoothstep(0.0, 0.02, abs(hexDist));
// Inner rotating triangle (star)
float2 rotP2 = rotate(p, -time * 0.3);
float starDist = sdStar(rotP2, 0.12 + subBass * 0.05, 3, 2.5);
float starEdge = 1.0 - smoothstep(0.0, 0.015, abs(starDist));
// Concentric circles
float circles = 0.0;
for (int i = 0; i < 4; i++) {
float radius = 0.1 + float(i) * 0.08 + sin(time + float(i)) * 0.02;
float circleDist = abs(dist - radius);
float circle = 1.0 - smoothstep(0.0, 0.008, circleDist);
circles += circle;
}
// Combine harmonic shapes
float harmonicShapes = hexEdge + starEdge * 0.8 + circles * 0.5;
harmonicShapes = clamp(harmonicShapes, 0.0, 1.0);
// Harmonic color - clean neon
float3 harmonicColor = mix(neonCyan, neonMagenta, 0.5 + 0.5 * sin(angle * 2.0 + time));
// === NOISE SIDE (Low HNR = Chaotic particles) ===
// Noise-based particles
float noiseField = 0.0;
for (int i = 0; i < 5; i++) {
float2 noiseP = p * (3.0 + float(i) * 2.0);
noiseP += time * float(i + 1) * 0.1;
float n = noise(noiseP);
n = pow(n, 2.0);
noiseField += n * (1.0 / float(i + 1));
}
noiseField = clamp(noiseField, 0.0, 1.0);
// Turbulent swirls
float2 turbP = p * 4.0;
float turbulence = fbm(turbP + time * 0.5, 4);
// Chaotic speckles
float speckles = 0.0;
for (int i = 0; i < 30; i++) {
float2 specklePos = float2(
hash(float2(float(i) * 0.1, time * 0.01)) - 0.5,
hash(float2(float(i) * 0.2, time * 0.01 + 0.5)) - 0.5
);
specklePos *= 0.8;
specklePos.x *= aspectRatio;
float speckleDist = length(p - specklePos);
float speckle = exp(-speckleDist * speckleDist * 500.0);
speckle *= hash(float2(float(i), floor(time * 2.0)));
speckles += speckle;
}
float noiseVisual = noiseField * 0.4 + turbulence * 0.3 + speckles * 0.3;
noiseVisual = clamp(noiseVisual, 0.0, 1.0);
// Noise color - harsh, flickering
float3 noiseColor = mix(hotPink, uvViolet, turbulence);
noiseColor *= 0.8 + 0.2 * sin(time * 20.0 + noise(p * 10.0) * 10.0);
// === BLEND based on HNR ===
// HNR determines the mix: 1.0 = pure harmonic, 0.0 = pure noise
float harmonicAmount = hnr;
float noiseAmount = 1.0 - hnr;
// Apply reactivity to make transition more dramatic
harmonicAmount = pow(harmonicAmount, 1.0 / (1.0 + reactivity));
float3 harmonicContrib = harmonicColor * harmonicShapes * harmonicAmount;
float3 noiseContrib = noiseColor * noiseVisual * noiseAmount;
float3 finalColor = harmonicContrib + noiseContrib;
// Add center indicator showing current HNR
float indicator = smoothstep(0.25, 0.24, dist) - smoothstep(0.24, 0.23, dist);
float indicatorFill = smoothstep(0.23, 0.22, dist);
// Split indicator by HNR
float harmonicSide = step(0.0, p.x);
float noiseSide = 1.0 - harmonicSide;
finalColor += neonCyan * indicator * 0.3;
finalColor += neonCyan * indicatorFill * harmonicSide * hnr * 0.2;
finalColor += hotPink * indicatorFill * noiseSide * (1.0 - hnr) * 0.2;
// Background glow
float bgGlow = exp(-dist * dist * 4.0);
float3 bgColor = mix(deepPurple, uvViolet * 0.3, dist);
finalColor += bgColor * (1.0 - clamp(harmonicShapes + noiseVisual, 0.0, 1.0));
// Peak flash
if (uniforms.isPeak > 0.5) {
finalColor += float3(1.0) * uniforms.peakIntensity * 0.15 * exp(-dist * 3.0);
}
return float4(finalColor, 1.0);
}
@@ -0,0 +1,121 @@
//
// MandelbrotShader.metal
// PsytranceVisualizer
//
// Audio-reactive Mandelbrot fractal with zoom and color cycling
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
fragment float4 mandelbrotFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
float subBass = uniforms.subBassEnergy;
float pump = uniforms.sidechainPump;
float centroid = uniforms.spectralCentroid;
// Aspect ratio correction
float aspectRatio = resolution.x / resolution.y;
// Map UV to complex plane
float2 c = (uv - 0.5) * 2.0;
c.x *= aspectRatio;
// Audio-reactive zoom level
// Base zoom increases over time, modulated by sub-bass
float baseZoom = 1.0 + time * 0.02;
float audioZoom = subBass * 0.5 * (0.5 + reactivity * 0.5);
float zoom = pow(2.0, baseZoom + audioZoom);
// Zoom center - drifts based on sidechain
float2 zoomCenter = float2(-0.7, 0.0);
zoomCenter.x += sin(time * 0.1) * 0.3 + pump * 0.1 * sin(time);
zoomCenter.y += cos(time * 0.13) * 0.2 + pump * 0.1 * cos(time);
// Apply zoom
c = c / zoom + zoomCenter;
// Mandelbrot iteration
float2 z = float2(0.0);
int maxIterations = int(50.0 + reactivity * 100.0);
int iterations = 0;
float smoothIter = 0.0;
for (int i = 0; i < 150; i++) {
if (i >= maxIterations) break;
// z = z^2 + c
float2 zNew = float2(
z.x * z.x - z.y * z.y + c.x,
2.0 * z.x * z.y + c.y
);
z = zNew;
float mag2 = dot(z, z);
if (mag2 > 256.0) {
// Smooth iteration count
smoothIter = float(i) - log2(log2(mag2)) + 4.0;
break;
}
iterations = i;
}
// Normalize iteration count
float normalizedIter = smoothIter / float(maxIterations);
// Color based on iterations
float3 color;
if (iterations >= maxIterations - 1) {
// Inside the set - deep color
color = deepPurple * (0.5 + 0.5 * subBass);
} else {
// Outside - color cycling based on iterations and audio
float colorPhase = normalizedIter + time * 0.1 + centroid;
// Use psytrance palette with color rotation
color = psytrancePalette(colorPhase, time);
// Modulate brightness by iteration depth
float brightness = 0.5 + 0.5 * sin(smoothIter * 0.3);
color *= brightness;
// Add glow at boundary
float edgeFactor = 1.0 - normalizedIter;
edgeFactor = pow(edgeFactor, 3.0);
color = addGlow(color, edgeFactor * 0.5, neonCyan);
}
// Sub-bass pulse effect
color *= 0.8 + 0.2 * subBass;
// Sidechain breathing
float breathe = 1.0 + pump * 0.1;
color *= breathe;
// Peak flash in bright areas
if (uniforms.isPeak > 0.5 && iterations < maxIterations - 1) {
color += neonMagenta * uniforms.peakIntensity * 0.2 * normalizedIter;
}
// Subtle vignette
float2 vignetteuv = uv - 0.5;
float vignette = 1.0 - dot(vignetteuv, vignetteuv) * 0.5;
color *= vignette;
return float4(color, 1.0);
}
@@ -0,0 +1,95 @@
//
// MelSpectrogramShader.metal
// PsytranceVisualizer
//
// Mel spectrogram with scrolling waterfall display
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
fragment float4 melSpectrogramFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
// Configuration
const int numBands = 64;
const int historyLength = 128;
// Map UV to mel band and history position
int bandIndex = int(uv.x * float(numBands));
bandIndex = clamp(bandIndex, 0, numBands - 1);
// Scrolling effect - newer data at bottom
float scrollOffset = fract(time * 0.5); // Scroll speed
float yPos = fract(uv.y + scrollOffset);
// Get mel magnitude
float magnitude = melData[bandIndex];
magnitude = magnitude * (0.5 + reactivity * 1.5);
magnitude = clamp(magnitude, 0.0, 1.0);
// Create waterfall effect using history
int historyIndex = int(yPos * float(historyLength));
historyIndex = clamp(historyIndex, 0, historyLength - 1);
// Combine current and historical data for waterfall
float historicalValue = historyData[historyIndex];
// Blend between current magnitude and position-based intensity
float intensity = magnitude;
// Add some variance based on band position
float bandPhase = float(bandIndex) / float(numBands);
intensity *= 0.8 + 0.2 * sin(bandPhase * 6.28318 + time);
// Apply fade for older data (top of screen)
float ageFade = 1.0 - uv.y * 0.3;
intensity *= ageFade;
// Generate color using heatmap
float3 color = heatmap(intensity);
// Add frequency-dependent hue shift
float hueShift = bandPhase * 0.3;
color = psytrancePalette(intensity + hueShift, time);
// Modulate by actual intensity
color *= 0.3 + intensity * 0.7;
// Add grid lines for visual reference
float gridX = abs(fract(uv.x * float(numBands)) - 0.5) * 2.0;
float gridY = abs(fract(uv.y * 16.0) - 0.5) * 2.0;
float gridLine = smoothstep(0.95, 1.0, gridX) + smoothstep(0.95, 1.0, gridY);
gridLine *= 0.1;
color += float3(gridLine) * uvViolet;
// Add glow on high energy
if (intensity > 0.7) {
float glow = (intensity - 0.7) / 0.3;
color = addGlow(color, glow * 0.5, neonCyan);
}
// Peak flash
if (uniforms.isPeak > 0.5) {
color += neonMagenta * uniforms.peakIntensity * 0.15;
}
// Sub-bass emphasis on lower bands
if (bandIndex < 8) {
color += uvViolet * uniforms.subBassEnergy * 0.3;
}
return float4(color, 1.0);
}
@@ -0,0 +1,130 @@
//
// SidechainPumpShader.metal
// PsytranceVisualizer
//
// Visualizes sidechain pumping with breathing zoom effect
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
fragment float4 sidechainPumpFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
float pump = uniforms.sidechainPump;
float envelope = uniforms.sidechainEnvelope;
float subBass = uniforms.subBassEnergy;
// Center and aspect ratio correction
float2 center = float2(0.5, 0.5);
float aspectRatio = resolution.x / resolution.y;
float2 p = uv - center;
p.x *= aspectRatio;
// Apply breathing zoom effect
float zoomAmount = 1.0 + pump * 0.3 * (0.5 + reactivity * 0.5);
p /= zoomAmount;
// Radial distortion synchronized with pump
float dist = length(p);
float angle = atan2(p.y, p.x);
// Pump-synced radial waves
float radialWave = sin(dist * 15.0 - time * 3.0 + envelope * 10.0);
radialWave *= pump * 0.3;
// Apply distortion
float2 distortedP = p;
distortedP *= 1.0 + radialWave * 0.1;
// Create concentric pulse rings
float rings = 0.0;
const int numRings = 5;
for (int i = 0; i < numRings; i++) {
float ringPhase = fract(time * 0.5 + float(i) * 0.2 - envelope * 0.5);
float ringRadius = ringPhase * 0.6;
float ringWidth = 0.02 + pump * 0.03;
float ringDist = abs(dist - ringRadius);
float ring = exp(-ringDist * ringDist / (ringWidth * ringWidth));
ring *= 1.0 - ringPhase; // Fade out as it expands
ring *= pump;
rings += ring;
}
// Breathing glow in center
float breathIntensity = 0.5 + 0.5 * sin(time * 4.0 + envelope * 6.28318);
breathIntensity *= pump;
float centerGlow = exp(-dist * dist * 8.0);
centerGlow *= breathIntensity;
// Color based on pump phase
float3 pumpColor = mix(uvViolet, neonMagenta, envelope);
float3 ringColor = mix(neonCyan, hotPink, pump);
// Background pattern - angular sectors that pulse
float sectors = 8.0;
float sectorAngle = fract(angle / (2.0 * 3.14159) * sectors);
float sectorPulse = smoothstep(0.4, 0.5, sectorAngle) - smoothstep(0.5, 0.6, sectorAngle);
sectorPulse *= pump * 0.3;
sectorPulse *= exp(-dist * 3.0);
// Spiral pattern
float spiral = fract(angle / (2.0 * 3.14159) * 3.0 + dist * 5.0 - time * 0.5);
spiral = smoothstep(0.4, 0.5, spiral) - smoothstep(0.5, 0.6, spiral);
spiral *= pump * 0.2;
spiral *= exp(-dist * 2.0);
// Compose final color
float3 finalColor = float3(0.0);
// Base gradient
float3 bgGradient = mix(deepPurple, uvViolet * 0.3, dist);
finalColor += bgGradient;
// Add rings
finalColor += ringColor * rings;
// Add center glow
finalColor += pumpColor * centerGlow;
// Add sector pulse
finalColor += neonGreen * sectorPulse;
// Add spiral
finalColor += electricBlue * spiral;
// Screen flash on strong pump
if (pump > 0.7) {
float flash = (pump - 0.7) / 0.3;
flash *= 0.2;
finalColor += neonMagenta * flash;
}
// Peak highlight
if (uniforms.isPeak > 0.5) {
float peakFlash = uniforms.peakIntensity * 0.2;
finalColor += float3(1.0) * peakFlash * exp(-dist * 5.0);
}
// Vignette
float vignette = 1.0 - smoothstep(0.4, 0.8, dist);
finalColor *= 0.7 + vignette * 0.3;
return float4(finalColor, 1.0);
}
@@ -0,0 +1,116 @@
//
// SubBassShader.metal
// PsytranceVisualizer
//
// Pulsating rings visualizing sub-bass energy below 100Hz
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
fragment float4 subBassFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
float subBass = uniforms.subBassEnergy;
// Center coordinates
float2 center = float2(0.5, 0.5);
float aspectRatio = resolution.x / resolution.y;
// Correct for aspect ratio
float2 p = uv - center;
p.x *= aspectRatio;
float dist = length(p);
float angle = atan2(p.y, p.x);
// Main pulsating circle
float baseRadius = 0.15;
float pulseAmount = subBass * (0.5 + reactivity * 0.5);
float mainRadius = baseRadius + pulseAmount * 0.2;
// Add wobble based on angle
float wobble = sin(angle * 4.0 + time * 2.0) * 0.02 * subBass;
mainRadius += wobble;
// Core circle
float coreDist = abs(dist - mainRadius);
float coreGlow = exp(-coreDist * coreDist * 200.0);
// Inner fill with gradient
float innerFill = smoothstep(mainRadius, mainRadius * 0.3, dist);
innerFill *= 0.5 + 0.5 * subBass;
// Expanding rings
const int numRings = 6;
float ringIntensity = 0.0;
for (int i = 0; i < numRings; i++) {
// Each ring expands outward over time
float ringPhase = fract(time * 0.3 - float(i) * 0.15);
float ringRadius = mainRadius + ringPhase * 0.5;
// Get historical sub-bass value for this ring
int histIndex = clamp(int(ringPhase * 64.0), 0, 63);
float histValue = historyData[histIndex];
// Ring thickness based on historical energy
float thickness = 0.005 + histValue * 0.01;
float ringDist = abs(dist - ringRadius);
// Ring visibility
float ring = exp(-ringDist * ringDist / (thickness * thickness));
ring *= (1.0 - ringPhase); // Fade as it expands
ring *= histValue; // Intensity based on history
ringIntensity += ring;
}
// Color composition
float3 coreColor = mix(uvViolet, neonMagenta, subBass);
float3 ringColor = mix(neonMagenta, hotPink, 0.5 + 0.5 * sin(time));
float3 finalColor = float3(0.0);
// Add core
finalColor += coreColor * (innerFill + coreGlow * 2.0);
// Add rings
finalColor += ringColor * ringIntensity * 0.8;
// Add central glow
float centerGlow = exp(-dist * dist * 10.0) * subBass;
finalColor += uvViolet * centerGlow * 0.5;
// Add angular rays on peaks
if (uniforms.isPeak > 0.5) {
float rays = abs(sin(angle * 8.0 + time * 5.0));
rays = pow(rays, 4.0) * exp(-dist * 2.0);
rays *= uniforms.peakIntensity;
finalColor += neonCyan * rays * 0.5;
}
// Outer vignette
float vignette = 1.0 - smoothstep(0.3, 0.8, dist);
finalColor *= vignette;
// Background pulse
float bgPulse = subBass * 0.1;
finalColor += deepPurple * bgPulse;
// Add noise texture for organic feel
float noiseVal = noise(p * 20.0 + time);
finalColor += uvViolet * noiseVal * 0.02 * subBass;
return float4(finalColor, 1.0);
}
@@ -0,0 +1,136 @@
//
// TunnelWarpShader.metal
// PsytranceVisualizer
//
// Infinite tunnel effect with warp distortion
//
#include <metal_stdlib>
using namespace metal;
#include "Common.metal"
fragment float4 tunnelWarpFragment(
VertexOut in [[stage_in]],
constant ShaderUniforms& uniforms [[buffer(0)]],
constant float* fftData [[buffer(1)]],
constant float* melData [[buffer(2)]],
constant float* historyData [[buffer(3)]]
) {
float2 uv = in.uv;
float2 resolution = uniforms.resolution;
float time = uniforms.time;
float reactivity = uniforms.reactivity;
float subBass = uniforms.subBassEnergy;
float pump = uniforms.sidechainPump;
float hnr = uniforms.hnrRatio;
// Center and aspect correction
float aspectRatio = resolution.x / resolution.y;
float2 p = (uv - 0.5) * 2.0;
p.x *= aspectRatio;
// Convert to polar coordinates for tunnel
float dist = length(p);
float angle = atan2(p.y, p.x);
// Avoid division by zero at center
dist = max(dist, 0.001);
// Tunnel depth (inverse of distance)
float depth = 1.0 / dist;
// Speed controlled by sub-bass
float baseSpeed = 2.0;
float audioSpeed = subBass * 3.0 * (0.5 + reactivity * 0.5);
float speed = baseSpeed + audioSpeed;
// Warp distortion from sidechain pump
float warpAmount = pump * 0.5;
depth += sin(angle * 4.0 + time * 2.0) * warpAmount * 0.5;
angle += sin(depth * 2.0 + time) * warpAmount * 0.3;
// Create tunnel coordinates
float2 tunnelUV = float2(
angle / (2.0 * 3.14159) + 0.5, // Angular coordinate [0, 1]
depth + time * speed // Depth with movement
);
// === TUNNEL WALL PATTERNS ===
// Hexagonal grid pattern
float2 hexUV = tunnelUV * float2(8.0, 2.0);
float2 hexCell = floor(hexUV);
float2 hexFrac = fract(hexUV);
// Offset every other row
if (fmod(hexCell.y, 2.0) > 0.5) {
hexFrac.x = fract(hexFrac.x + 0.5);
}
float hexDist = length(hexFrac - 0.5);
float hexPattern = smoothstep(0.4, 0.35, hexDist);
// Add concentric rings
float rings = sin(tunnelUV.y * 20.0) * 0.5 + 0.5;
rings = smoothstep(0.3, 0.7, rings);
// Angular segments
float segments = 8.0;
float angularLines = abs(sin(angle * segments));
angularLines = smoothstep(0.95, 1.0, angularLines);
// Combine patterns
float pattern = hexPattern * 0.5 + rings * 0.3 + angularLines * 0.2;
// === COLORING ===
// Base color cycles with depth and time
float colorPhase = tunnelUV.y * 0.1 + time * 0.2;
float3 tunnelColor = psytrancePalette(colorPhase, time);
// Depth fog (darker towards center/infinity)
float fog = exp(-dist * 2.0);
tunnelColor *= fog;
// Pattern overlay
float3 patternColor = mix(uvViolet, neonCyan, rings);
tunnelColor = mix(tunnelColor, patternColor, pattern * 0.5);
// Edge glow (bright at tunnel edges)
float edgeGlow = exp(-dist * 5.0);
tunnelColor = addGlow(tunnelColor, (1.0 - edgeGlow) * 0.3, neonMagenta);
// Center light (looking into the tunnel)
float centerLight = exp(-dist * dist * 50.0);
tunnelColor += float3(1.0) * centerLight * 0.5;
// HNR affects pattern complexity
float patternIntensity = hnr;
tunnelColor *= 0.7 + patternIntensity * 0.3;
// Add noise for texture
float noiseVal = noise(tunnelUV * 10.0 + time);
tunnelColor += uvViolet * noiseVal * 0.1;
// Pump flash
if (pump > 0.5) {
float pumpFlash = (pump - 0.5) * 2.0;
tunnelColor += neonMagenta * pumpFlash * 0.2;
}
// Peak flash
if (uniforms.isPeak > 0.5) {
float peakFlash = uniforms.peakIntensity;
tunnelColor += float3(1.0) * peakFlash * 0.15 * (1.0 - edgeGlow);
}
// Speed lines effect
float speedLines = fract(tunnelUV.y * 50.0 - time * speed * 2.0);
speedLines = smoothstep(0.95, 1.0, speedLines);
speedLines *= subBass * 0.5;
tunnelColor += neonCyan * speedLines;
return float4(tunnelColor, 1.0);
}
@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "1.000",
"green" : "0.000",
"red" : "1.000"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
+38
View File
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.music</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2024. All rights reserved.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Psytrance Visualizer needs access to your audio input to visualize music in real-time. You can use a virtual audio device like BlackHole to route system audio.</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSSupportsAutomaticGraphicsSwitching</key>
<true/>
</dict>
</plist>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
</dict>
</plist>
+315
View File
@@ -0,0 +1,315 @@
//
// ControlPanel.swift
// PsytranceVisualizer
//
// Auto-hiding control panel with audio and visualization settings
//
import AppKit
import Combine
/// Delegate protocol for control panel actions
protocol ControlPanelDelegate: AnyObject {
func controlPanel(_ panel: ControlPanel, didSelectDevice uid: String)
func controlPanel(_ panel: ControlPanel, didSelectBufferSize size: Int)
func controlPanel(_ panel: ControlPanel, didSelectMode mode: VisualizationMode)
func controlPanel(_ panel: ControlPanel, didChangeReactivity value: Float)
func controlPanelDidRequestFullscreen(_ panel: ControlPanel)
}
/// Auto-hiding control panel overlay
final class ControlPanel: NSView {
// MARK: - Properties
weak var delegate: ControlPanelDelegate?
private var isVisible = true
private var hideTimer: Timer?
private let hideDelay: TimeInterval = 3.0
private var audioDevices: [AudioDevice] = []
private var selectedMode: VisualizationMode = .fftClassic
// MARK: - UI Elements
private let containerView = NSVisualEffectView()
private let devicePopup = NSPopUpButton()
private let bufferSizePopup = NSPopUpButton()
private let modeSegment = NSSegmentedControl()
private let reactivitySlider = NSSlider()
private let reactivityLabel = NSTextField(labelWithString: "Reactivity")
private let fullscreenButton = NSButton()
// MARK: - Layout Constants
private let panelHeight: CGFloat = 60
private let padding: CGFloat = 12
private let elementHeight: CGFloat = 24
// MARK: - Initialization
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setupUI()
setupConstraints()
startHideTimer()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Setup
private func setupUI() {
// Container with vibrancy effect
containerView.material = .hudWindow
containerView.blendingMode = .behindWindow
containerView.state = .active
containerView.wantsLayer = true
containerView.layer?.cornerRadius = 12
containerView.layer?.masksToBounds = true
addSubview(containerView)
// Device popup
devicePopup.target = self
devicePopup.action = #selector(deviceChanged)
devicePopup.controlSize = .small
devicePopup.font = .systemFont(ofSize: 11)
containerView.addSubview(devicePopup)
// Buffer size popup
bufferSizePopup.target = self
bufferSizePopup.action = #selector(bufferSizeChanged)
bufferSizePopup.controlSize = .small
bufferSizePopup.font = .systemFont(ofSize: 11)
bufferSizePopup.addItems(withTitles: ["512", "1024"])
bufferSizePopup.selectItem(withTitle: "1024")
containerView.addSubview(bufferSizePopup)
// Mode segment control
modeSegment.segmentCount = 8
for mode in VisualizationMode.allCases {
modeSegment.setLabel(mode.shortcut, forSegment: mode.rawValue - 1)
modeSegment.setToolTip(mode.displayName, forSegment: mode.rawValue - 1)
}
modeSegment.selectedSegment = 0
modeSegment.target = self
modeSegment.action = #selector(modeChanged)
modeSegment.controlSize = .small
modeSegment.segmentStyle = .capsule
containerView.addSubview(modeSegment)
// Reactivity label
reactivityLabel.font = .systemFont(ofSize: 10)
reactivityLabel.textColor = .secondaryLabelColor
containerView.addSubview(reactivityLabel)
// Reactivity slider
reactivitySlider.minValue = 0.0
reactivitySlider.maxValue = 1.0
reactivitySlider.doubleValue = 0.5
reactivitySlider.target = self
reactivitySlider.action = #selector(reactivityChanged)
reactivitySlider.controlSize = .small
containerView.addSubview(reactivitySlider)
// Fullscreen button
fullscreenButton.title = ""
fullscreenButton.bezelStyle = .accessoryBarAction
fullscreenButton.target = self
fullscreenButton.action = #selector(fullscreenClicked)
fullscreenButton.toolTip = "Toggle Fullscreen (F)"
containerView.addSubview(fullscreenButton)
// Set colors
applyPsytranceTheme()
}
private func applyPsytranceTheme() {
// Custom appearance for psytrance aesthetic
containerView.appearance = NSAppearance(named: .darkAqua)
}
private func setupConstraints() {
containerView.translatesAutoresizingMaskIntoConstraints = false
devicePopup.translatesAutoresizingMaskIntoConstraints = false
bufferSizePopup.translatesAutoresizingMaskIntoConstraints = false
modeSegment.translatesAutoresizingMaskIntoConstraints = false
reactivityLabel.translatesAutoresizingMaskIntoConstraints = false
reactivitySlider.translatesAutoresizingMaskIntoConstraints = false
fullscreenButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
// Container
containerView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding),
containerView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding),
containerView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding),
containerView.heightAnchor.constraint(equalToConstant: panelHeight),
// Device popup
devicePopup.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: padding),
devicePopup.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
devicePopup.widthAnchor.constraint(equalToConstant: 150),
// Buffer size popup
bufferSizePopup.leadingAnchor.constraint(equalTo: devicePopup.trailingAnchor, constant: 8),
bufferSizePopup.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
bufferSizePopup.widthAnchor.constraint(equalToConstant: 60),
// Mode segment
modeSegment.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
modeSegment.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
// Reactivity label
reactivityLabel.trailingAnchor.constraint(equalTo: reactivitySlider.leadingAnchor, constant: -4),
reactivityLabel.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
// Reactivity slider
reactivitySlider.trailingAnchor.constraint(equalTo: fullscreenButton.leadingAnchor, constant: -padding),
reactivitySlider.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
reactivitySlider.widthAnchor.constraint(equalToConstant: 80),
// Fullscreen button
fullscreenButton.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -padding),
fullscreenButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
])
}
// MARK: - Public Methods
/// Updates the list of available audio devices
func updateDevices(_ devices: [AudioDevice], selectedUID: String?) {
audioDevices = devices
devicePopup.removeAllItems()
for device in devices {
devicePopup.addItem(withTitle: device.name)
devicePopup.lastItem?.representedObject = device.uid
}
if let uid = selectedUID,
let index = devices.firstIndex(where: { $0.uid == uid }) {
devicePopup.selectItem(at: index)
}
}
/// Updates the selected buffer size
func updateBufferSize(_ size: Int) {
bufferSizePopup.selectItem(withTitle: "\(size)")
}
/// Updates the selected visualization mode
func updateMode(_ mode: VisualizationMode) {
selectedMode = mode
modeSegment.selectedSegment = mode.rawValue - 1
}
/// Updates the reactivity slider
func updateReactivity(_ value: Float) {
reactivitySlider.doubleValue = Double(value)
}
/// Shows the control panel
func show(animated: Bool = true) {
guard !isVisible else { return }
isVisible = true
if animated {
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.3
self.animator().alphaValue = 1.0
}
} else {
alphaValue = 1.0
}
startHideTimer()
}
/// Hides the control panel
func hide(animated: Bool = true) {
guard isVisible else { return }
isVisible = false
hideTimer?.invalidate()
if animated {
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.3
self.animator().alphaValue = 0.0
}
} else {
alphaValue = 0.0
}
}
/// Resets the hide timer (call on mouse movement)
func resetHideTimer() {
show()
startHideTimer()
}
// MARK: - Private Methods
private func startHideTimer() {
hideTimer?.invalidate()
hideTimer = Timer.scheduledTimer(withTimeInterval: hideDelay, repeats: false) { [weak self] _ in
self?.hide()
}
}
// MARK: - Actions
@objc private func deviceChanged() {
guard let uid = devicePopup.selectedItem?.representedObject as? String else { return }
delegate?.controlPanel(self, didSelectDevice: uid)
}
@objc private func bufferSizeChanged() {
guard let title = bufferSizePopup.selectedItem?.title,
let size = Int(title) else { return }
delegate?.controlPanel(self, didSelectBufferSize: size)
}
@objc private func modeChanged() {
let modeIndex = modeSegment.selectedSegment + 1
guard let mode = VisualizationMode(rawValue: modeIndex) else { return }
selectedMode = mode
delegate?.controlPanel(self, didSelectMode: mode)
}
@objc private func reactivityChanged() {
let value = Float(reactivitySlider.doubleValue)
delegate?.controlPanel(self, didChangeReactivity: value)
}
@objc private func fullscreenClicked() {
delegate?.controlPanelDidRequestFullscreen(self)
}
// MARK: - Mouse Tracking
override func updateTrackingAreas() {
super.updateTrackingAreas()
// Remove existing tracking areas
for area in trackingAreas {
removeTrackingArea(area)
}
// Add new tracking area
let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .mouseMoved, .activeAlways]
let trackingArea = NSTrackingArea(rect: bounds, options: options, owner: self, userInfo: nil)
addTrackingArea(trackingArea)
}
override func mouseMoved(with event: NSEvent) {
super.mouseMoved(with: event)
resetHideTimer()
}
override func mouseEntered(with event: NSEvent) {
super.mouseEntered(with: event)
show()
}
}
+323
View File
@@ -0,0 +1,323 @@
//
// MainWindow.swift
// PsytranceVisualizer
//
// Main application window with keyboard handling
//
import AppKit
import Combine
/// Main window controller for the visualizer
final class MainWindowController: NSWindowController {
// MARK: - Properties
private var visualizerView: VisualizerView!
private var controlPanel: ControlPanel!
private var audioManager: AudioInputManager!
private var dspEngine: DSPEngine!
private var settingsManager: SettingsManager { .shared }
private var cancellables = Set<AnyCancellable>()
private var displayLink: CVDisplayLink?
// MARK: - Initialization
convenience init() {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 1280, height: 720),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false
)
window.title = "Psytrance Visualizer"
window.minSize = NSSize(width: 800, height: 600)
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
window.isMovableByWindowBackground = true
window.backgroundColor = .black
window.collectionBehavior = [.fullScreenPrimary]
// Restore window frame if saved
if let savedFrame = SettingsManager.shared.settings.windowFrame?.cgRect {
window.setFrame(savedFrame, display: false)
} else {
window.center()
}
self.init(window: window)
setupContent()
setupAudio()
setupKeyboardHandling()
restoreSettings()
}
// MARK: - Setup
private func setupContent() {
guard let contentView = window?.contentView else { return }
// Visualizer view (fills entire window)
visualizerView = VisualizerView()
visualizerView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(visualizerView)
// Control panel (overlay at bottom)
controlPanel = ControlPanel()
controlPanel.translatesAutoresizingMaskIntoConstraints = false
controlPanel.delegate = self
contentView.addSubview(controlPanel)
NSLayoutConstraint.activate([
// Visualizer fills entire window
visualizerView.topAnchor.constraint(equalTo: contentView.topAnchor),
visualizerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
visualizerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
visualizerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
// Control panel at bottom
controlPanel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
controlPanel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
controlPanel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
controlPanel.heightAnchor.constraint(equalToConstant: 90),
])
// Mouse tracking for control panel
setupMouseTracking()
}
private func setupAudio() {
audioManager = AudioInputManager()
dspEngine = DSPEngine(bufferSize: settingsManager.settings.bufferSize)
// Audio buffer callback
audioManager.onAudioBuffer = { [weak self] buffer in
guard let self = self else { return }
let analysisData = self.dspEngine.process(buffer: buffer)
DispatchQueue.main.async {
self.visualizerView.updateAudioData(analysisData)
}
}
// Update control panel when devices change
audioManager.$availableDevices
.receive(on: DispatchQueue.main)
.sink { [weak self] devices in
self?.controlPanel.updateDevices(
devices,
selectedUID: self?.settingsManager.settings.selectedAudioDeviceUID
)
}
.store(in: &cancellables)
// Start audio
audioManager.start()
}
private func setupKeyboardHandling() {
// Monitor for key events
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
if self?.handleKeyDown(event) == true {
return nil // Event handled
}
return event
}
}
private func setupMouseTracking() {
guard let contentView = window?.contentView else { return }
let options: NSTrackingArea.Options = [.mouseMoved, .activeAlways, .inVisibleRect]
let trackingArea = NSTrackingArea(
rect: contentView.bounds,
options: options,
owner: self,
userInfo: nil
)
contentView.addTrackingArea(trackingArea)
}
private func restoreSettings() {
let settings = settingsManager.settings
// Restore visualization mode
if let mode = VisualizationMode(rawValue: settings.lastVisualizationMode) {
visualizerView.setVisualizationMode(mode)
controlPanel.updateMode(mode)
}
// Restore reactivity
visualizerView.setReactivity(settings.reactivity)
dspEngine.setReactivity(settings.reactivity)
controlPanel.updateReactivity(settings.reactivity)
// Restore buffer size
dspEngine.setBufferSize(settings.bufferSize)
audioManager.setBufferSize(settings.bufferSize)
controlPanel.updateBufferSize(settings.bufferSize)
// Restore audio device
if let deviceUID = settings.selectedAudioDeviceUID {
audioManager.selectDevice(uid: deviceUID)
}
// Restore fullscreen state
if settings.isFullscreen {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.window?.toggleFullScreen(nil)
}
}
}
// MARK: - Keyboard Handling
private func handleKeyDown(_ event: NSEvent) -> Bool {
// Check for visualization mode shortcuts (1-8)
if let mode = VisualizationMode.fromKeyCode(event.keyCode) {
setVisualizationMode(mode)
return true
}
// Other keyboard shortcuts
switch event.keyCode {
case 3: // F key
toggleFullscreen()
return true
case 53: // Escape
if window?.styleMask.contains(.fullScreen) == true {
window?.toggleFullScreen(nil)
}
return true
case 49: // Space
// Toggle pause (could be implemented)
return true
default:
break
}
// Cmd+F for fullscreen
if event.modifierFlags.contains(.command) && event.keyCode == 3 {
toggleFullscreen()
return true
}
return false
}
// MARK: - Mode Switching
private func setVisualizationMode(_ mode: VisualizationMode) {
visualizerView.setVisualizationMode(mode)
controlPanel.updateMode(mode)
settingsManager.setVisualizationMode(mode)
}
// MARK: - Fullscreen
private func toggleFullscreen() {
window?.toggleFullScreen(nil)
}
// MARK: - Mouse Events
override func mouseMoved(with event: NSEvent) {
controlPanel.resetHideTimer()
}
// MARK: - Window Events
override func windowDidLoad() {
super.windowDidLoad()
// Save window frame on move/resize
NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidResize),
name: NSWindow.didResizeNotification,
object: window
)
NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidMove),
name: NSWindow.didMoveNotification,
object: window
)
NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidEnterFullScreen),
name: NSWindow.didEnterFullScreenNotification,
object: window
)
NotificationCenter.default.addObserver(
self,
selector: #selector(windowDidExitFullScreen),
name: NSWindow.didExitFullScreenNotification,
object: window
)
}
@objc private func windowDidResize(_ notification: Notification) {
if let frame = window?.frame {
settingsManager.setWindowFrame(frame)
}
}
@objc private func windowDidMove(_ notification: Notification) {
if let frame = window?.frame {
settingsManager.setWindowFrame(frame)
}
}
@objc private func windowDidEnterFullScreen(_ notification: Notification) {
settingsManager.setFullscreen(true)
controlPanel.hide()
}
@objc private func windowDidExitFullScreen(_ notification: Notification) {
settingsManager.setFullscreen(false)
controlPanel.show()
}
// MARK: - Cleanup
deinit {
audioManager.stop()
settingsManager.saveNow()
}
}
// MARK: - ControlPanelDelegate
extension MainWindowController: ControlPanelDelegate {
func controlPanel(_ panel: ControlPanel, didSelectDevice uid: String) {
audioManager.selectDevice(uid: uid)
settingsManager.setAudioDevice(uid: uid)
}
func controlPanel(_ panel: ControlPanel, didSelectBufferSize size: Int) {
audioManager.setBufferSize(size)
dspEngine.setBufferSize(size)
settingsManager.setBufferSize(size)
}
func controlPanel(_ panel: ControlPanel, didSelectMode mode: VisualizationMode) {
setVisualizationMode(mode)
}
func controlPanel(_ panel: ControlPanel, didChangeReactivity value: Float) {
visualizerView.setReactivity(value)
dspEngine.setReactivity(value)
settingsManager.setReactivity(value)
}
func controlPanelDidRequestFullscreen(_ panel: ControlPanel) {
toggleFullscreen()
}
}
+122
View File
@@ -0,0 +1,122 @@
//
// VisualizerView.swift
// PsytranceVisualizer
//
// MTKView subclass for rendering visualizations
//
import MetalKit
import Combine
/// MTKView subclass that displays audio-reactive visualizations
final class VisualizerView: MTKView {
// MARK: - Properties
private var renderer: MetalRenderer?
private var cancellables = Set<AnyCancellable>()
// MARK: - Initialization
init() {
// Get default Metal device
guard let device = MTLCreateSystemDefaultDevice() else {
fatalError("Metal is not supported on this device")
}
super.init(frame: .zero, device: device)
configure()
setupRenderer()
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Configuration
private func configure() {
// Background color
clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 1)
// Color format
colorPixelFormat = .bgra8Unorm
// Enable display link for smooth rendering
isPaused = false
enableSetNeedsDisplay = false
// Use display refresh rate
preferredFramesPerSecond = 120 // Will cap to display refresh
// Layer configuration
layer?.isOpaque = true
// Allow high DPI
layer?.contentsScale = NSScreen.main?.backingScaleFactor ?? 2.0
}
private func setupRenderer() {
guard let device = device else { return }
renderer = MetalRenderer(device: device)
delegate = renderer
// Initial size update
if let renderer = renderer {
let size = drawableSize
renderer.mtkView(self, drawableSizeWillChange: size)
}
}
// MARK: - Public Methods
/// Returns the Metal renderer
func getRenderer() -> MetalRenderer? {
return renderer
}
/// Updates audio data for visualization
func updateAudioData(_ data: AudioAnalysisData) {
renderer?.updateAudioData(data)
}
/// Sets the visualization mode
func setVisualizationMode(_ mode: VisualizationMode) {
renderer?.setVisualizationMode(mode)
}
/// Sets reactivity value
func setReactivity(_ value: Float) {
renderer?.setReactivity(value)
}
/// Gets current visualization mode
var currentMode: VisualizationMode {
renderer?.currentMode ?? .fftClassic
}
}
// MARK: - SwiftUI Bridge
#if canImport(SwiftUI)
import SwiftUI
/// SwiftUI wrapper for VisualizerView
struct VisualizerViewRepresentable: NSViewRepresentable {
@Binding var audioData: AudioAnalysisData
@Binding var mode: VisualizationMode
@Binding var reactivity: Float
func makeNSView(context: Context) -> VisualizerView {
let view = VisualizerView()
return view
}
func updateNSView(_ nsView: VisualizerView, context: Context) {
nsView.updateAudioData(audioData)
nsView.setVisualizationMode(mode)
nsView.setReactivity(reactivity)
}
}
#endif
@@ -0,0 +1,140 @@
//
// ColorPalette.swift
// PsytranceVisualizer
//
// Psytrance color palette for UI and shaders
//
import AppKit
import simd
/// Psytrance-inspired neon/UV color palette
struct PsytranceColors {
// MARK: - Primary Colors (NSColor for UI)
/// Neon Magenta - Primary accent color
static let neonMagenta = NSColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0)
/// Neon Cyan - Secondary accent color
static let neonCyan = NSColor(red: 0.0, green: 1.0, blue: 1.0, alpha: 1.0)
/// Neon Green - High energy accents
static let neonGreen = NSColor(red: 0.224, green: 1.0, blue: 0.078, alpha: 1.0)
/// UV Violet - Deep purple for backgrounds
static let uvViolet = NSColor(red: 0.482, green: 0.0, blue: 1.0, alpha: 1.0)
/// Deep Black - Background color
static let background = NSColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
/// Dark Purple - Alternative background
static let darkPurple = NSColor(red: 0.1, green: 0.0, blue: 0.15, alpha: 1.0)
/// Hot Pink - Peak indicators
static let hotPink = NSColor(red: 1.0, green: 0.2, blue: 0.6, alpha: 1.0)
/// Electric Blue - UI elements
static let electricBlue = NSColor(red: 0.0, green: 0.5, blue: 1.0, alpha: 1.0)
// MARK: - SIMD3<Float> Colors (for Metal shaders)
struct Metal {
static let neonMagenta = SIMD3<Float>(1.0, 0.0, 1.0)
static let neonCyan = SIMD3<Float>(0.0, 1.0, 1.0)
static let neonGreen = SIMD3<Float>(0.224, 1.0, 0.078)
static let uvViolet = SIMD3<Float>(0.482, 0.0, 1.0)
static let background = SIMD3<Float>(0.0, 0.0, 0.0)
static let darkPurple = SIMD3<Float>(0.1, 0.0, 0.15)
static let hotPink = SIMD3<Float>(1.0, 0.2, 0.6)
static let electricBlue = SIMD3<Float>(0.0, 0.5, 1.0)
/// Array of all palette colors for cycling
static let palette: [SIMD3<Float>] = [
neonMagenta,
neonCyan,
neonGreen,
uvViolet,
hotPink,
electricBlue
]
/// Get color from palette by index (wraps around)
static func color(at index: Int) -> SIMD3<Float> {
palette[index % palette.count]
}
/// Interpolate between two colors
static func lerp(_ a: SIMD3<Float>, _ b: SIMD3<Float>, t: Float) -> SIMD3<Float> {
a + (b - a) * t
}
/// Get rainbow color from normalized value (0-1)
static func rainbow(_ t: Float) -> SIMD3<Float> {
let index = Int(t * Float(palette.count))
let nextIndex = (index + 1) % palette.count
let localT = (t * Float(palette.count)) - Float(index)
return lerp(palette[index % palette.count], palette[nextIndex], t: localT)
}
}
// MARK: - Gradient Helpers
/// Creates a gradient from UV Violet through Magenta to Cyan
static var spectrumGradient: NSGradient? {
NSGradient(colors: [uvViolet, neonMagenta, hotPink, neonCyan, neonGreen])
}
/// Creates a gradient for heat maps (low to high energy)
static var heatmapGradient: NSGradient? {
NSGradient(colors: [
NSColor(red: 0.1, green: 0.0, blue: 0.2, alpha: 1.0), // Dark purple (low)
uvViolet,
neonMagenta,
hotPink,
neonCyan,
neonGreen,
NSColor.white // White (peak)
])
}
// MARK: - UI Theme Colors
struct UI {
static let panelBackground = NSColor(red: 0.05, green: 0.02, blue: 0.08, alpha: 0.9)
static let buttonBackground = NSColor(red: 0.15, green: 0.05, blue: 0.2, alpha: 1.0)
static let buttonHighlight = neonMagenta.withAlphaComponent(0.8)
static let sliderTint = neonCyan
static let labelText = NSColor.white
static let secondaryText = NSColor(white: 0.7, alpha: 1.0)
static let border = uvViolet.withAlphaComponent(0.5)
}
}
// MARK: - NSColor Extension
extension NSColor {
/// Converts NSColor to SIMD3<Float> for Metal
var simd3: SIMD3<Float> {
guard let rgb = usingColorSpace(.deviceRGB) else {
return SIMD3<Float>(0, 0, 0)
}
return SIMD3<Float>(
Float(rgb.redComponent),
Float(rgb.greenComponent),
Float(rgb.blueComponent)
)
}
/// Converts NSColor to SIMD4<Float> for Metal (with alpha)
var simd4: SIMD4<Float> {
guard let rgb = usingColorSpace(.deviceRGB) else {
return SIMD4<Float>(0, 0, 0, 1)
}
return SIMD4<Float>(
Float(rgb.redComponent),
Float(rgb.greenComponent),
Float(rgb.blueComponent),
Float(rgb.alphaComponent)
)
}
}
@@ -0,0 +1,185 @@
//
// SettingsManager.swift
// PsytranceVisualizer
//
// Handles loading and saving of application settings
//
import Foundation
import Combine
/// Manages persistent storage and retrieval of application settings
final class SettingsManager: ObservableObject {
// MARK: - Singleton
static let shared = SettingsManager()
// MARK: - Published Properties
@Published private(set) var settings: AppSettings
// MARK: - Private Properties
private let settingsKey = "PsytranceVisualizerSettings"
private let fileManager = FileManager.default
private var saveWorkItem: DispatchWorkItem?
// MARK: - Initialization
private init() {
self.settings = SettingsManager.loadSettings()
}
// MARK: - Public Methods
/// Updates settings and triggers auto-save
func updateSettings(_ update: (inout AppSettings) -> Void) {
update(&settings)
settings.validate()
scheduleSave()
}
/// Updates selected audio device
func setAudioDevice(uid: String?) {
updateSettings { $0.selectedAudioDeviceUID = uid }
}
/// Updates buffer size
func setBufferSize(_ size: Int) {
guard AppSettings.availableBufferSizes.contains(size) else { return }
updateSettings { $0.bufferSize = size }
}
/// Updates visualization mode
func setVisualizationMode(_ mode: VisualizationMode) {
updateSettings { $0.lastVisualizationMode = mode.rawValue }
}
/// Updates reactivity
func setReactivity(_ value: Float) {
updateSettings { $0.reactivity = max(0.0, min(1.0, value)) }
}
/// Updates fullscreen state
func setFullscreen(_ isFullscreen: Bool) {
updateSettings { $0.isFullscreen = isFullscreen }
}
/// Updates window frame
func setWindowFrame(_ frame: CGRect) {
updateSettings { $0.windowFrame = CodableRect(from: frame) }
}
/// Updates input gain
func setInputGain(_ gain: Float) {
updateSettings { $0.inputGain = max(0.0, min(2.0, gain)) }
}
/// Updates FPS display setting
func setShowFPS(_ show: Bool) {
updateSettings { $0.showFPS = show }
}
/// Forces immediate save
func saveNow() {
saveWorkItem?.cancel()
performSave()
}
/// Resets to default settings
func resetToDefaults() {
settings = .default
saveNow()
}
// MARK: - Private Methods
/// Schedules a debounced save operation
private func scheduleSave() {
saveWorkItem?.cancel()
let workItem = DispatchWorkItem { [weak self] in
self?.performSave()
}
saveWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: workItem)
}
/// Performs the actual save operation
private func performSave() {
do {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try encoder.encode(settings)
// Save to UserDefaults
UserDefaults.standard.set(data, forKey: settingsKey)
// Also save to file for backup
if let url = settingsFileURL {
try data.write(to: url)
}
print("[SettingsManager] Settings saved successfully")
} catch {
print("[SettingsManager] Failed to save settings: \(error)")
}
}
/// Loads settings from storage
private static func loadSettings() -> AppSettings {
// Try UserDefaults first
if let data = UserDefaults.standard.data(forKey: "PsytranceVisualizerSettings") {
do {
var settings = try JSONDecoder().decode(AppSettings.self, from: data)
settings.validate()
print("[SettingsManager] Settings loaded from UserDefaults")
return settings
} catch {
print("[SettingsManager] Failed to decode settings from UserDefaults: \(error)")
}
}
// Try file backup
if let url = settingsFileURL,
let data = try? Data(contentsOf: url) {
do {
var settings = try JSONDecoder().decode(AppSettings.self, from: data)
settings.validate()
print("[SettingsManager] Settings loaded from file")
return settings
} catch {
print("[SettingsManager] Failed to decode settings from file: \(error)")
}
}
print("[SettingsManager] Using default settings")
return .default
}
/// URL for settings file backup
private static var settingsFileURL: URL? {
guard let appSupport = FileManager.default.urls(
for: .applicationSupportDirectory,
in: .userDomainMask
).first else {
return nil
}
let appDirectory = appSupport.appendingPathComponent("PsytranceVisualizer")
// Create directory if needed
try? FileManager.default.createDirectory(
at: appDirectory,
withIntermediateDirectories: true
)
return appDirectory.appendingPathComponent("settings.json")
}
/// Current visualization mode
var currentVisualizationMode: VisualizationMode {
VisualizationMode(rawValue: settings.lastVisualizationMode) ?? .fftClassic
}
}
+54
View File
@@ -14,6 +14,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
#if targetEnvironment(macCatalyst)
// Configure for macOS
configureMacOS()
#endif
return true return true
} }
@@ -33,10 +37,60 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidBecomeActive(_ application: UIApplication) { func applicationDidBecomeActive(_ application: UIApplication) {
// Resume game if needed // Resume game if needed
} }
#if targetEnvironment(macCatalyst)
// MARK: - macOS Configuration
private func configureMacOS() {
// Set minimum window size for macOS
UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.forEach { windowScene in
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 400, height: 600)
windowScene.sizeRestrictions?.maximumSize = CGSize(width: 600, height: 900)
}
}
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)
// Remove unnecessary menus for a game
builder.remove(menu: .format)
builder.remove(menu: .edit)
// Add Game menu
let pauseCommand = UIKeyCommand(
title: "Pause",
action: #selector(handlePauseCommand),
input: "p",
modifierFlags: .command
)
let restartCommand = UIKeyCommand(
title: "Neustart",
action: #selector(handleRestartCommand),
input: "r",
modifierFlags: .command
)
let gameMenu = UIMenu(
title: "Spiel",
children: [pauseCommand, restartCommand]
)
builder.insertSibling(gameMenu, afterMenu: .file)
}
@objc private func handlePauseCommand() {
NotificationCenter.default.post(name: .pauseGame, object: nil)
}
@objc private func handleRestartCommand() {
NotificationCenter.default.post(name: .restartGame, object: nil)
}
#endif
} }
// MARK: - Notification Names // MARK: - Notification Names
extension Notification.Name { extension Notification.Name {
static let pauseGame = Notification.Name("pauseGame") static let pauseGame = Notification.Name("pauseGame")
static let resumeGame = Notification.Name("resumeGame") static let resumeGame = Notification.Name("resumeGame")
static let restartGame = Notification.Name("restartGame")
} }
@@ -36,6 +36,10 @@ class GameViewController: UIViewController {
// Setup notification observers // Setup notification observers
setupNotificationObservers() setupNotificationObservers()
#if targetEnvironment(macCatalyst)
setupMacCatalyst()
#endif
} }
private func setupNotificationObservers() { private func setupNotificationObservers() {
@@ -45,6 +49,13 @@ class GameViewController: UIViewController {
name: .pauseGame, name: .pauseGame,
object: nil object: nil
) )
NotificationCenter.default.addObserver(
self,
selector: #selector(handleRestartNotification),
name: .restartGame,
object: nil
)
} }
@objc private func handlePauseNotification() { @objc private func handlePauseNotification() {
@@ -57,12 +68,46 @@ class GameViewController: UIViewController {
// This is just a notification that the app is going to background // This is just a notification that the app is going to background
} }
@objc private func handleRestartNotification() {
guard let skView = self.view as? SKView else { return }
let menuScene = MenuScene(size: skView.bounds.size)
menuScene.scaleMode = .aspectFill
let transition = SKTransition.fade(withDuration: 0.5)
skView.presentScene(menuScene, transition: transition)
}
#if targetEnvironment(macCatalyst)
private func setupMacCatalyst() {
// Configure window appearance for macOS
if let windowScene = view.window?.windowScene {
windowScene.title = "Rollkoffer Simulator"
// Set window style
if let titlebar = windowScene.titlebar {
titlebar.titleVisibility = .visible
titlebar.toolbarStyle = .unified
}
}
}
// Enable keyboard input
override var canBecomeFirstResponder: Bool {
return true
}
#endif
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
#if targetEnvironment(macCatalyst)
return .all
#else
if UIDevice.current.userInterfaceIdiom == .phone { if UIDevice.current.userInterfaceIdiom == .phone {
return .portrait return .portrait
} else { } else {
return .all return .all
} }
#endif
} }
override var prefersStatusBarHidden: Bool { override var prefersStatusBarHidden: Bool {
+4
View File
@@ -50,5 +50,9 @@
<string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string> <string>UIInterfaceOrientationLandscapeRight</string>
</array> </array>
<key>LSMinimumSystemVersion</key>
<string>13.0</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright 2024 Ingo K. All rights reserved.</string>
</dict> </dict>
</plist> </plist>
@@ -381,6 +381,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Info.plist; INFOPLIST_FILE = Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
@@ -393,9 +394,12 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.ingok.RollkofferSimulator; PRODUCT_BUNDLE_IDENTIFIER = com.ingok.RollkofferSimulator;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2"; TARGETED_DEVICE_FAMILY = "1,2";
@@ -409,6 +413,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Info.plist; INFOPLIST_FILE = Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
@@ -421,9 +426,12 @@
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
); );
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.0; MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.ingok.RollkofferSimulator; PRODUCT_BUNDLE_IDENTIFIER = com.ingok.RollkofferSimulator;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0; SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2"; TARGETED_DEVICE_FAMILY = "1,2";
@@ -223,6 +223,27 @@ class GameOverScene: SKScene {
} }
} }
// MARK: - Keyboard Handling (macOS)
#if targetEnvironment(macCatalyst)
override var canBecomeFirstResponder: Bool { true }
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else {
super.pressesBegan(presses, with: event)
return
}
switch key.keyCode {
case .keyboardSpacebar, .keyboardReturnOrEnter:
retryGame()
case .keyboardEscape:
returnToMenu()
default:
super.pressesBegan(presses, with: event)
}
}
#endif
private func retryGame() { private func retryGame() {
let pressDown = SKAction.scale(to: 0.9, duration: 0.1) let pressDown = SKAction.scale(to: 0.9, duration: 0.1)
let pressUp = SKAction.scale(to: 1.0, duration: 0.1) let pressUp = SKAction.scale(to: 1.0, duration: 0.1)
@@ -318,6 +318,29 @@ class GameScene: SKScene {
isDragging = false isDragging = false
} }
// MARK: - Keyboard Handling (macOS)
#if targetEnvironment(macCatalyst)
override var canBecomeFirstResponder: Bool { true }
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else {
super.pressesBegan(presses, with: event)
return
}
switch key.keyCode {
case .keyboardEscape:
togglePause()
case .keyboardSpacebar:
if gameState.currentState == .paused {
resumeGame()
}
default:
super.pressesBegan(presses, with: event)
}
}
#endif
// MARK: - Pause Handling // MARK: - Pause Handling
private func togglePause() { private func togglePause() {
if gameState.currentState == .playing { if gameState.currentState == .playing {
@@ -245,6 +245,25 @@ class MenuScene: SKScene {
} }
} }
// MARK: - Keyboard Handling (macOS)
#if targetEnvironment(macCatalyst)
override var canBecomeFirstResponder: Bool { true }
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else {
super.pressesBegan(presses, with: event)
return
}
switch key.keyCode {
case .keyboardSpacebar, .keyboardReturnOrEnter:
startGame()
default:
super.pressesBegan(presses, with: event)
}
}
#endif
private func startGame() { private func startGame() {
// Button press effect // Button press effect
let pressDown = SKAction.scale(to: 0.9, duration: 0.1) let pressDown = SKAction.scale(to: 0.9, duration: 0.1)
@@ -280,6 +280,27 @@ class VictoryScene: SKScene {
} }
} }
// MARK: - Keyboard Handling (macOS)
#if targetEnvironment(macCatalyst)
override var canBecomeFirstResponder: Bool { true }
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else {
super.pressesBegan(presses, with: event)
return
}
switch key.keyCode {
case .keyboardSpacebar, .keyboardReturnOrEnter:
playAgain()
case .keyboardEscape:
returnToMenu()
default:
super.pressesBegan(presses, with: event)
}
}
#endif
private func playAgain() { private func playAgain() {
let pressDown = SKAction.scale(to: 0.9, duration: 0.1) let pressDown = SKAction.scale(to: 0.9, duration: 0.1)
let pressUp = SKAction.scale(to: 1.0, duration: 0.1) let pressUp = SKAction.scale(to: 1.0, duration: 0.1)
+26
View File
@@ -0,0 +1,26 @@
# Configuration (contains secrets)
config.php
# Cache files
weather_cache.json
active_viewers.json
# IDE
.idea/
.vscode/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Logs
*.log
logs/
# Vendor (if using composer)
# vendor/
# Uploads (user content)
uploads/
+240
View File
@@ -0,0 +1,240 @@
# Integration Guide für Aurora Livecam Erweiterungen
## Übersicht der neuen Dateien
```
aurora-livecam/
├── SettingsManager.php # Admin-Einstellungen Klasse
├── settings.json # Einstellungen Datei
├── js/
│ ├── timelapse-controls.js # Timelapse mit Slider
│ ├── video-player.js # Tagesvideos im Player
│ └── admin-settings.js # Admin AJAX
├── css/
│ └── player-controls.css # Styles für Controls
└── INTEGRATION.md # Diese Anleitung
```
## Änderungen in index.php
### 1. Am Anfang der Datei (nach den requires)
```php
<?php
// ... bestehende requires ...
// NEU: Settings Manager einbinden
require_once 'SettingsManager.php';
$settingsManager = new SettingsManager();
// AJAX-Handler für Settings (VOR session_start!)
$settingsManager->handleAjax();
```
### 2. Im HEAD-Bereich (CSS einbinden)
```html
<link rel="stylesheet" href="css/player-controls.css">
```
### 3. Vor </body> (JavaScript einbinden)
```html
<script src="js/timelapse-controls.js"></script>
<script src="js/video-player.js"></script>
<?php if ($adminManager->isAdmin()): ?>
<script src="js/admin-settings.js"></script>
<?php endif; ?>
```
### 4. Video-Container anpassen
Ersetze den bestehenden video-container:
```html
<div class="video-container">
<?php echo $webcamManager->displayWebcam(); ?>
<!-- Timelapse Overlay -->
<div id="timelapse-viewer" style="display: none;">
<img id="timelapse-image" src="" alt="Timelapse">
</div>
<!-- NEU: Daily Video Player (wird dynamisch befüllt) -->
</div>
<!-- NEU: Timelapse Controls (außerhalb des Containers) -->
<div id="timelapse-controls"></div>
```
### 5. Zuschauer-Anzeige konditionell machen
Ersetze die Viewer-Stat Anzeige:
```php
<?php
$viewerCount = $viewerCounter->getInitialCount();
$showViewers = $settingsManager->shouldShowViewers($viewerCount);
?>
<?php if ($showViewers): ?>
<div class="info-badge viewer-stat">
<span class="live-dot"></span>
<strong id="viewer-count-display"><?php echo $viewerCount; ?></strong>
<span>Zuschauer</span>
</div>
<?php endif; ?>
```
### 6. Kalender Links anpassen
In der `VisualCalendarManager::displayVisualCalendar()` Methode:
```php
// Für Tagesvideos
$playInPlayer = $settingsManager->shouldPlayInPlayer();
$allowDownload = $settingsManager->shouldAllowDownload();
if ($playInPlayer) {
// Im Player abspielen
$output .= '<a href="#" onclick="DailyVideoPlayer.playVideo(\'' . $video['path'] . '\', ' . ($allowDownload ? 'true' : 'false') . '); return false;" class="play-link">';
$output .= '▶️ Abspielen';
$output .= '</a>';
}
if ($allowDownload) {
// Download Link
$output .= '<a href="?download_specific_video=..." class="download-link">⬇️ Download</a>';
}
```
### 7. Admin-Panel erweitern
Füge im Admin-Bereich hinzu:
```php
<?php if ($adminManager->isAdmin()): ?>
<section id="admin" class="section">
<div class="container">
<h2>Admin-Bereich</h2>
<!-- NEU: Settings Panel -->
<div id="admin-settings-panel">
<h3>⚙️ Anzeige-Einstellungen</h3>
<div class="settings-group">
<h4>👥 Zuschauer-Anzeige</h4>
<div class="setting-row">
<span class="setting-label">Zuschauer-Anzahl anzeigen</span>
<div class="setting-input">
<label class="toggle-switch">
<input type="checkbox" id="setting-viewer-enabled"
<?php echo $settingsManager->get('viewer_display.enabled') ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</label>
</div>
</div>
<div class="setting-row">
<span class="setting-label">Mindestanzahl für Anzeige</span>
<div class="setting-input">
<input type="number" id="setting-min-viewers" class="number-input"
min="1" max="100"
value="<?php echo $settingsManager->get('viewer_display.min_viewers'); ?>">
</div>
</div>
</div>
<div class="settings-group">
<h4>🎬 Video-Modus</h4>
<div class="setting-row">
<span class="setting-label">Videos im Player abspielen</span>
<div class="setting-input">
<label class="toggle-switch">
<input type="checkbox" id="setting-play-in-player"
<?php echo $settingsManager->get('video_mode.play_in_player') ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</label>
</div>
</div>
<div class="setting-row">
<span class="setting-label">Download erlauben</span>
<div class="setting-input">
<label class="toggle-switch">
<input type="checkbox" id="setting-allow-download"
<?php echo $settingsManager->get('video_mode.allow_download') ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</label>
</div>
</div>
</div>
</div>
<!-- Bestehender Admin-Content -->
<?php echo $adminManager->displayAdminContent(); ?>
</div>
</section>
<?php endif; ?>
```
### 8. Timelapse Button Event anpassen
Im bestehenden JavaScript:
```javascript
timelapseButton.addEventListener('click', function(e) {
e.preventDefault();
if (timelapseViewer.style.display === 'none') {
// NEU: TimelapseController verwenden
TimelapseController.init(imageFiles);
TimelapseController.show();
timelapseButton.textContent = 'Zurück zur Live-Webcam';
} else {
TimelapseController.backToLive();
}
});
```
### 9. Viewer Heartbeat anpassen
Im JavaScript für den Viewer-Counter:
```javascript
function updateViewerCount() {
fetch(window.location.href, {
method: 'POST',
body: new URLSearchParams({action: 'viewer_heartbeat'})
})
.then(r => r.json())
.then(data => {
const display = document.getElementById('viewer-count-display');
const container = document.querySelector('.viewer-stat');
if (data.count && display) {
display.textContent = data.count;
// Mindestanzahl prüfen (aus Settings)
const minViewers = window.minViewersToShow || 1;
if (container) {
container.style.display = data.count >= minViewers ? 'inline-flex' : 'none';
}
}
});
}
```
## Fertig!
Nach diesen Änderungen hast du:
- ✅ Timelapse mit Slider und 1x/10x/100x Geschwindigkeit
- ✅ Rückwärts-Spulen im Timelapse
- ✅ Tagesvideos im Player abspielen statt nur Download
- ✅ "Zurück zu Live" Button
- ✅ Admin-Einstellungen für Zuschauer-Anzeige
- ✅ Mindestanzahl für Zuschauer-Anzeige
- ✅ Video-Modus wählbar (Player/Download)
- ✅ Alles ohne Seiten-Reload
+396
View File
@@ -0,0 +1,396 @@
<?php
/**
* SettingsManager - Verwaltet Admin-Einstellungen
* Speichert in settings.json, lädt ohne Reload
*/
class SettingsManager {
private $settingsFile;
private $settings = [];
public function __construct($file = null) {
$this->settingsFile = $file ?: (__DIR__ . '/settings.json');
$this->load();
}
private function load() {
if (file_exists($this->settingsFile)) {
$content = file_get_contents($this->settingsFile);
$this->settings = json_decode($content, true) ?? $this->getDefaults();
} else {
$this->settings = $this->getDefaults();
$this->save();
}
}
private function getDefaults() {
return [
'viewer_display' => [
'enabled' => true,
'min_viewers' => 1,
'update_interval' => 5 // Sekunden
],
'video_mode' => [
'play_in_player' => true,
'allow_download' => true
],
'timelapse' => [
'default_speed' => 1,
'available_speeds' => [1, 10, 100]
],
// Punkt 2: UI-Anzeige Features
'ui_display' => [
'show_recommendation_banner' => true,
'show_qr_code' => true,
'show_social_media' => true,
'show_patrouille_suisse' => true
],
// Punkt 3: Zoom & Timelapse
'zoom_timelapse' => [
'show_zoom_controls' => true,
'max_zoom_level' => 4.0,
'timelapse_reverse_enabled' => true,
'weekly_timelapse_enabled' => true // Wochenzeitraffer Button
],
// Auto-Screenshot für Galerie
'auto_screenshot' => [
'enabled' => false,
'interval_minutes' => 10,
'max_images' => 144, // 24h bei 10min Intervall
'save_to_gallery' => true
],
// Email-Sharing
'sharing' => [
'email_enabled' => false,
'share_link_expiry_hours' => 24
],
// Punkt 5: Content Management
'content' => [
'guestbook_enabled' => true,
'gallery_enabled' => true,
'ai_events_enabled' => true,
'max_guestbook_entries' => 50
],
// Punkt 6: Technische Settings
'technical' => [
'viewer_update_interval' => 5, // Sekunden
'session_timeout' => 30 // Sekunden
],
// Punkt 7: Theme & Design
'theme' => [
'default_theme' => 'theme-legacy',
'show_theme_switcher' => false
],
// Punkt 8: SEO & Meta
'seo' => [
'custom_title' => '',
'meta_description' => '',
'meta_keywords' => ''
],
// Weather Widget
'weather' => [
'enabled' => true,
'api_key' => '',
'location' => 'Oberdürnten,CH',
'lat' => '47.2833',
'lon' => '8.7167',
'update_interval' => 5, // Minuten
'units' => 'metric' // metric (Celsius) oder imperial (Fahrenheit)
],
// SaaS Features - alle aktivierbar/deaktivierbar
'saas_features' => [
// Multi-Tenant
'multi_tenant_enabled' => false, // Aktiviert DB-basierte Tenant-Verwaltung
'customer_management_enabled' => false,
// Onboarding
'self_registration_enabled' => false,
'email_verification_required' => true,
'trial_enabled' => true,
'trial_days' => 14,
// Billing
'billing_enabled' => false,
'stripe_enabled' => false,
'free_plan_available' => true,
// Dashboard
'tenant_dashboard_enabled' => false,
'analytics_enabled' => false,
'custom_domain_enabled' => false,
'custom_branding_enabled' => false,
// Landing
'landing_page_enabled' => false,
'demo_mode_enabled' => false,
// Limits (Default für Free-Plan)
'default_max_viewers' => 50,
'default_storage_mb' => 500,
'default_retention_days' => 7
],
'last_updated' => null,
'updated_by' => null
];
}
public function get($key = null) {
if ($key === null) return $this->settings;
$keys = explode('.', $key);
$value = $this->settings;
foreach ($keys as $k) {
if (!isset($value[$k])) return null;
$value = $value[$k];
}
return $value;
}
public function set($key, $value) {
$keys = explode('.', $key);
$ref = &$this->settings;
foreach ($keys as $i => $k) {
if ($i === count($keys) - 1) {
$ref[$k] = $value;
} else {
if (!isset($ref[$k])) $ref[$k] = [];
$ref = &$ref[$k];
}
}
$this->settings['last_updated'] = date('Y-m-d H:i:s');
return $this->save();
}
private function save() {
$payload = json_encode($this->settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if ($payload === false) {
return false;
}
return file_put_contents($this->settingsFile, $payload, LOCK_EX) !== false;
}
// Für AJAX-Anfragen
public function handleAjax() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') return;
if (!isset($_POST['settings_action'])) return;
header('Content-Type: application/json');
switch ($_POST['settings_action']) {
case 'get':
echo json_encode(['success' => true, 'settings' => $this->settings]);
exit;
case 'update':
$key = $_POST['key'] ?? null;
$value = $_POST['value'] ?? null;
// Boolean-Werte konvertieren
if ($value === 'true') $value = true;
if ($value === 'false') $value = false;
if (is_numeric($value)) $value = intval($value);
if ($key && $this->set($key, $value)) {
echo json_encode(['success' => true, 'message' => 'Einstellung gespeichert']);
} else {
echo json_encode([
'success' => false,
'message' => 'Fehler beim Speichern. Bitte Dateirechte prüfen.'
]);
}
exit;
}
}
// Viewer-Anzeige prüfen
public function shouldShowViewers($currentCount) {
if (!$this->get('viewer_display.enabled')) return false;
return $currentCount >= $this->get('viewer_display.min_viewers');
}
// Video-Modus prüfen
public function shouldPlayInPlayer() {
return $this->get('video_mode.play_in_player') === true;
}
public function shouldAllowDownload() {
return $this->get('video_mode.allow_download') === true;
}
// UI Display Helper
public function shouldShowRecommendationBanner() {
return $this->get('ui_display.show_recommendation_banner') === true;
}
public function shouldShowQRCode() {
return $this->get('ui_display.show_qr_code') === true;
}
public function shouldShowSocialMedia() {
return $this->get('ui_display.show_social_media') === true;
}
public function shouldShowPatrouillesuisse() {
return $this->get('ui_display.show_patrouille_suisse') === true;
}
// Content Management Helper
public function isGuestbookEnabled() {
return $this->get('content.guestbook_enabled') === true;
}
public function isGalleryEnabled() {
return $this->get('content.gallery_enabled') === true;
}
public function isAIEventsEnabled() {
return $this->get('content.ai_events_enabled') === true;
}
public function getMaxGuestbookEntries() {
return $this->get('content.max_guestbook_entries') ?? 50;
}
// Theme Helper
public function getDefaultTheme() {
return $this->get('theme.default_theme') ?? 'theme-legacy';
}
public function shouldShowThemeSwitcher() {
return $this->get('theme.show_theme_switcher') === true;
}
// Technical Helper
public function getViewerUpdateInterval() {
return $this->get('technical.viewer_update_interval') ?? 5;
}
public function getSessionTimeout() {
return $this->get('technical.session_timeout') ?? 30;
}
// Zoom & Timelapse Helper
public function shouldShowZoomControls() {
return $this->get('zoom_timelapse.show_zoom_controls') === true;
}
public function getMaxZoomLevel() {
return $this->get('zoom_timelapse.max_zoom_level') ?? 4.0;
}
public function isTimelapseReverseEnabled() {
return $this->get('zoom_timelapse.timelapse_reverse_enabled') === true;
}
public function isWeeklyTimelapseEnabled() {
return $this->get('zoom_timelapse.weekly_timelapse_enabled') !== false;
}
// Auto-Screenshot Helper
public function isAutoScreenshotEnabled() {
return $this->get('auto_screenshot.enabled') === true;
}
public function getAutoScreenshotInterval() {
return $this->get('auto_screenshot.interval_minutes') ?? 10;
}
public function getAutoScreenshotMaxImages() {
return $this->get('auto_screenshot.max_images') ?? 144;
}
// Sharing Helper
public function isEmailSharingEnabled() {
return $this->get('sharing.email_enabled') === true;
}
public function getShareLinkExpiryHours() {
return $this->get('sharing.share_link_expiry_hours') ?? 24;
}
// SEO Helper
public function getCustomTitle() {
$title = $this->get('seo.custom_title');
return !empty($title) ? $title : null;
}
public function getMetaDescription() {
return $this->get('seo.meta_description') ?? '';
}
public function getMetaKeywords() {
return $this->get('seo.meta_keywords') ?? '';
}
// Weather Helper
public function isWeatherEnabled() {
return $this->get('weather.enabled') === true;
}
public function getWeatherApiKey() {
return $this->get('weather.api_key') ?? '';
}
public function getWeatherLocation() {
return $this->get('weather.location') ?? 'Oberdürnten,CH';
}
public function getWeatherCoords() {
return [
'lat' => $this->get('weather.lat') ?? '47.2833',
'lon' => $this->get('weather.lon') ?? '8.7167'
];
}
public function getWeatherUpdateInterval() {
return $this->get('weather.update_interval') ?? 5;
}
public function getWeatherUnits() {
return $this->get('weather.units') ?? 'metric';
}
// SaaS Feature Helper
public function isMultiTenantEnabled() {
return $this->get('saas_features.multi_tenant_enabled') === true;
}
public function isSelfRegistrationEnabled() {
return $this->get('saas_features.self_registration_enabled') === true;
}
public function isBillingEnabled() {
return $this->get('saas_features.billing_enabled') === true;
}
public function isStripeEnabled() {
return $this->get('saas_features.stripe_enabled') === true;
}
public function isTenantDashboardEnabled() {
return $this->get('saas_features.tenant_dashboard_enabled') === true;
}
public function isAnalyticsEnabled() {
return $this->get('saas_features.analytics_enabled') === true;
}
public function isCustomDomainEnabled() {
return $this->get('saas_features.custom_domain_enabled') === true;
}
public function isCustomBrandingEnabled() {
return $this->get('saas_features.custom_branding_enabled') === true;
}
public function isLandingPageEnabled() {
return $this->get('saas_features.landing_page_enabled') === true;
}
public function getTrialDays() {
return $this->get('saas_features.trial_days') ?? 14;
}
public function getDefaultMaxViewers() {
return $this->get('saas_features.default_max_viewers') ?? 50;
}
}
+225
View File
@@ -0,0 +1,225 @@
<?php
/**
* WeatherManager - Holt und cached Wetterdaten von Open-Meteo (kostenlos!)
* Keine API Key nötig!
*/
class WeatherManager {
private $settingsManager;
private $cacheFile = 'weather_cache.json';
private $cacheTime = 300; // 5 Minuten in Sekunden
public function __construct($settingsManager) {
$this->settingsManager = $settingsManager;
}
/**
* Holt aktuelle Wetterdaten (cached)
*/
public function getCurrentWeather() {
// Prüfe ob Weather aktiviert ist
if (!$this->settingsManager->isWeatherEnabled()) {
return null;
}
// Prüfe Cache
$cached = $this->getCache();
if ($cached !== null) {
return $cached;
}
// Hole frische Daten von API (Open-Meteo)
$coords = $this->settingsManager->getWeatherCoords();
// Open-Meteo API URL - komplett kostenlos, kein API Key!
$url = "https://api.open-meteo.com/v1/forecast?" . http_build_query([
'latitude' => $coords['lat'],
'longitude' => $coords['lon'],
'current' => 'temperature_2m,relative_humidity_2m,precipitation,weather_code,wind_speed_10m,wind_direction_10m,pressure_msl,cloud_cover',
'timezone' => 'Europe/Zurich'
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 || !$response) {
return ['error' => 'API Fehler'];
}
$data = json_decode($response, true);
if (!$data || !isset($data['current'])) {
return ['error' => 'Ungültige API Antwort'];
}
$current = $data['current'];
// Formatiere Daten
$weather = [
'temp' => round($current['temperature_2m'], 1),
'feels_like' => round($current['temperature_2m'], 1), // Open-Meteo hat keine "feels like"
'humidity' => $current['relative_humidity_2m'],
'pressure' => round($current['pressure_msl'], 0),
'wind_speed' => round($current['wind_speed_10m'], 1), // Schon in km/h!
'wind_deg' => $current['wind_direction_10m'],
'wind_direction' => $this->getWindDirection($current['wind_direction_10m']),
'clouds' => $current['cloud_cover'] ?? 0,
'description' => $this->getWeatherDescription($current['weather_code']),
'icon' => $this->getWeatherIcon($current['weather_code']),
'rain_1h' => $current['precipitation'] ?? 0,
'snow_1h' => 0, // Open-Meteo gibt Niederschlag gesamt
'location' => $this->settingsManager->getWeatherLocation(),
'timestamp' => time()
];
// Cache speichern
$this->saveCache($weather);
return $weather;
}
/**
* Wandelt WMO Weather Code in Beschreibung um
* https://open-meteo.com/en/docs
*/
private function getWeatherDescription($code) {
$descriptions = [
0 => 'Klar',
1 => 'Überwiegend klar',
2 => 'Teilweise bewölkt',
3 => 'Bewölkt',
45 => 'Neblig',
48 => 'Nebel mit Reifablagerung',
51 => 'Leichter Nieselregen',
53 => 'Mäßiger Nieselregen',
55 => 'Dichter Nieselregen',
61 => 'Leichter Regen',
63 => 'Mäßiger Regen',
65 => 'Starker Regen',
71 => 'Leichter Schneefall',
73 => 'Mäßiger Schneefall',
75 => 'Starker Schneefall',
77 => 'Schneegraupeln',
80 => 'Leichte Regenschauer',
81 => 'Mäßige Regenschauer',
82 => 'Starke Regenschauer',
85 => 'Leichte Schneeschauer',
86 => 'Starke Schneeschauer',
95 => 'Gewitter',
96 => 'Gewitter mit leichtem Hagel',
99 => 'Gewitter mit starkem Hagel'
];
return $descriptions[$code] ?? 'Unbekannt';
}
/**
* Wandelt WMO Weather Code in Icon-Code um (OpenWeatherMap kompatibel)
*/
private function getWeatherIcon($code) {
if ($code == 0) return '01d'; // Klar
if ($code >= 1 && $code <= 2) return '02d'; // Teilweise bewölkt
if ($code == 3) return '04d'; // Bewölkt
if ($code >= 45 && $code <= 48) return '50d'; // Nebel
if ($code >= 51 && $code <= 55) return '09d'; // Nieselregen
if ($code >= 61 && $code <= 65) return '10d'; // Regen
if ($code >= 71 && $code <= 77) return '13d'; // Schnee
if ($code >= 80 && $code <= 82) return '09d'; // Regenschauer
if ($code >= 85 && $code <= 86) return '13d'; // Schneeschauer
if ($code >= 95 && $code <= 99) return '11d'; // Gewitter
return '01d'; // Default
}
/**
* Wandelt Windrichtung (Grad) in Kompassrichtung um
*/
private function getWindDirection($deg) {
$directions = ['N', 'NNO', 'NO', 'ONO', 'O', 'OSO', 'SO', 'SSO', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'];
$index = round($deg / 22.5) % 16;
return $directions[$index];
}
/**
* Holt Daten aus Cache (wenn noch gültig)
*/
private function getCache() {
if (!file_exists($this->cacheFile)) {
return null;
}
$content = file_get_contents($this->cacheFile);
$data = json_decode($content, true);
if (!$data || !isset($data['timestamp'])) {
return null;
}
// Fehler nicht aus Cache zurückgeben (z.B. alter "API Key fehlt" Error)
if (isset($data['error'])) {
@unlink($this->cacheFile); // Cache mit Fehler löschen
return null;
}
// Update-Intervall aus Settings holen (in Minuten)
$updateInterval = $this->settingsManager->getWeatherUpdateInterval() * 60; // Minuten -> Sekunden
// Prüfe ob Cache noch gültig
if (time() - $data['timestamp'] < $updateInterval) {
return $data;
}
return null;
}
/**
* Speichert Daten im Cache (nur wenn kein Fehler)
*/
private function saveCache($data) {
// Fehler nicht cachen
if (isset($data['error'])) {
return;
}
$json = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents($this->cacheFile, $json, LOCK_EX);
}
/**
* Gibt Wetter-Icon-Emoji zurück
*/
public function getWeatherEmoji($iconCode) {
$map = [
'01d' => '☀️', '01n' => '🌙',
'02d' => '⛅', '02n' => '☁️',
'03d' => '☁️', '03n' => '☁️',
'04d' => '☁️', '04n' => '☁️',
'09d' => '🌧️', '09n' => '🌧️',
'10d' => '🌦️', '10n' => '🌧️',
'11d' => '⛈️', '11n' => '⛈️',
'13d' => '❄️', '13n' => '❄️',
'50d' => '🌫️', '50n' => '🌫️'
];
return $map[$iconCode] ?? '🌤️';
}
/**
* AJAX Handler für Wetter-Updates
*/
public function handleAjax() {
if ($_SERVER['REQUEST_METHOD'] !== 'GET') return;
if (!isset($_GET['weather_action'])) return;
header('Content-Type: application/json');
if ($_GET['weather_action'] === 'get') {
$weather = $this->getCurrentWeather();
echo json_encode(['success' => true, 'data' => $weather]);
exit;
}
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
/**
* Auto-Screenshot API
*
* Kann als Cron-Job aufgerufen werden:
* Beispiel: 0,10,20,30,40,50 * * * * curl -s http://localhost/api/auto-screenshot.php?key=YOUR_SECRET_KEY
*
* Oder via Webhook/Timer
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
header('Content-Type: application/json');
$settingsManager = new SettingsManager();
// Prüfe ob Feature aktiviert
if (!$settingsManager->isAutoScreenshotEnabled()) {
echo json_encode(['success' => false, 'error' => 'Auto-Screenshot deaktiviert']);
exit;
}
// Optionale API-Key Validierung
$configFile = dirname(__DIR__) . '/config.php';
if (file_exists($configFile)) {
$config = require $configFile;
$apiKey = $config['auto_screenshot_key'] ?? '';
if (!empty($apiKey) && ($_GET['key'] ?? '') !== $apiKey) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'Ungültiger API-Key']);
exit;
}
}
// Galerie-Verzeichnis erstellen
$galleryDir = dirname(__DIR__) . '/gallery/auto/';
if (!is_dir($galleryDir)) {
mkdir($galleryDir, 0755, true);
}
// Screenshot-Dateiname
$filename = 'auto_' . date('Y-m-d_H-i-s') . '.jpg';
$filepath = $galleryDir . $filename;
// Video-Stream URL
$streamUrl = 'test_video.m3u8';
$logoPath = dirname(__DIR__) . '/logo.png';
// FFmpeg-Befehl zum Erstellen des Screenshots
$command = sprintf(
'ffmpeg -i %s -vframes 1 -q:v 2 %s 2>&1',
escapeshellarg($streamUrl),
escapeshellarg($filepath)
);
exec($command, $output, $returnVar);
if ($returnVar !== 0 || !file_exists($filepath)) {
echo json_encode([
'success' => false,
'error' => 'Screenshot fehlgeschlagen',
'command' => $command,
'output' => implode("\n", $output)
]);
exit;
}
// Alte Screenshots aufräumen (max. Anzahl einhalten)
$maxImages = $settingsManager->getAutoScreenshotMaxImages();
$existingFiles = glob($galleryDir . 'auto_*.jpg');
rsort($existingFiles); // Neueste zuerst
if (count($existingFiles) > $maxImages) {
$filesToDelete = array_slice($existingFiles, $maxImages);
foreach ($filesToDelete as $file) {
@unlink($file);
}
}
// Metadaten speichern
$metaFile = $galleryDir . 'metadata.json';
$metadata = [];
if (file_exists($metaFile)) {
$metadata = json_decode(file_get_contents($metaFile), true) ?? [];
}
$metadata[$filename] = [
'created_at' => date('Y-m-d H:i:s'),
'timestamp' => time(),
'size' => filesize($filepath)
];
// Nur die letzten maxImages behalten
$metadata = array_slice($metadata, -$maxImages, null, true);
file_put_contents($metaFile, json_encode($metadata, JSON_PRETTY_PRINT));
echo json_encode([
'success' => true,
'file' => $filename,
'path' => '/gallery/auto/' . $filename,
'total_images' => count(glob($galleryDir . 'auto_*.jpg'))
]);
+97
View File
@@ -0,0 +1,97 @@
<?php
/**
* Gallery API
*
* GET /api/gallery.php - Liste alle Galerie-Bilder
* GET /api/gallery.php?date=2024-01-30 - Bilder eines bestimmten Datums
* GET /api/gallery.php?from=2024-01-01&to=2024-01-31 - Bilder in einem Zeitraum
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$settingsManager = new SettingsManager();
$galleryDir = dirname(__DIR__) . '/gallery/auto/';
// Prüfe ob Galerie existiert
if (!is_dir($galleryDir)) {
echo json_encode(['success' => true, 'images' => [], 'total' => 0]);
exit;
}
// Parameter
$date = $_GET['date'] ?? null;
$from = $_GET['from'] ?? null;
$to = $_GET['to'] ?? null;
$limit = min(100, (int)($_GET['limit'] ?? 50));
$offset = max(0, (int)($_GET['offset'] ?? 0));
// Alle Bilder holen
$allFiles = glob($galleryDir . 'auto_*.jpg');
rsort($allFiles); // Neueste zuerst
$images = [];
foreach ($allFiles as $file) {
$filename = basename($file);
// Extrahiere Datum aus Dateinamen: auto_2024-01-30_14-30-00.jpg
if (preg_match('/auto_(\d{4}-\d{2}-\d{2})_(\d{2}-\d{2}-\d{2})\.jpg/', $filename, $matches)) {
$fileDate = $matches[1];
$fileTime = str_replace('-', ':', $matches[2]);
// Datumsfilter
if ($date !== null && $fileDate !== $date) {
continue;
}
if ($from !== null && $fileDate < $from) {
continue;
}
if ($to !== null && $fileDate > $to) {
continue;
}
$images[] = [
'filename' => $filename,
'path' => '/gallery/auto/' . $filename,
'date' => $fileDate,
'time' => $fileTime,
'datetime' => $fileDate . ' ' . $fileTime,
'timestamp' => strtotime($fileDate . ' ' . $fileTime),
'size' => filesize($file)
];
}
}
$total = count($images);
// Pagination
$images = array_slice($images, $offset, $limit);
// Verfügbare Daten (für Kalender/Filter)
$availableDates = [];
foreach (glob($galleryDir . 'auto_*.jpg') as $file) {
if (preg_match('/auto_(\d{4}-\d{2}-\d{2})/', basename($file), $m)) {
$availableDates[$m[1]] = ($availableDates[$m[1]] ?? 0) + 1;
}
}
krsort($availableDates);
echo json_encode([
'success' => true,
'images' => $images,
'total' => $total,
'offset' => $offset,
'limit' => $limit,
'available_dates' => $availableDates,
'filters' => [
'date' => $date,
'from' => $from,
'to' => $to
]
]);
+315
View File
@@ -0,0 +1,315 @@
<?php
/**
* Share API - Teilen von Bildern/Videos per E-Mail
*
* POST /api/share.php
* Body: { email: "friend@example.com", type: "video|image", path: "/videos/...", message: "Schau dir das an!" }
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
$settingsManager = new SettingsManager();
// Prüfe ob Feature aktiviert
if (!$settingsManager->isEmailSharingEnabled()) {
echo json_encode(['success' => false, 'error' => 'E-Mail-Sharing ist deaktiviert']);
exit;
}
// Config laden
$configFile = dirname(__DIR__) . '/config.php';
$config = file_exists($configFile) ? require $configFile : [];
$mailConfig = $config['mail'] ?? [];
if (empty($mailConfig['host']) || empty($mailConfig['username'])) {
echo json_encode(['success' => false, 'error' => 'E-Mail-Server nicht konfiguriert']);
exit;
}
// === GET: Share-Link generieren ===
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['generate'])) {
$path = $_GET['path'] ?? '';
$type = $_GET['type'] ?? 'video';
if (empty($path)) {
echo json_encode(['success' => false, 'error' => 'Kein Pfad angegeben']);
exit;
}
// Token generieren
$expiryHours = $settingsManager->getShareLinkExpiryHours();
$expiry = time() + ($expiryHours * 3600);
$token = hash_hmac('sha256', $path . $expiry, session_id() . 'share_secret');
// Share-Link speichern
$shareDir = dirname(__DIR__) . '/data/shares/';
if (!is_dir($shareDir)) {
mkdir($shareDir, 0755, true);
}
$shareId = bin2hex(random_bytes(16));
$shareData = [
'id' => $shareId,
'path' => $path,
'type' => $type,
'token' => $token,
'expiry' => $expiry,
'created_at' => date('Y-m-d H:i:s')
];
file_put_contents($shareDir . $shareId . '.json', json_encode($shareData));
// URL generieren
$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
. '://' . $_SERVER['HTTP_HOST'];
$shareUrl = $baseUrl . '/api/share.php?view=' . $shareId;
echo json_encode([
'success' => true,
'share_url' => $shareUrl,
'share_id' => $shareId,
'expires_at' => date('Y-m-d H:i:s', $expiry)
]);
exit;
}
// === GET: Share-Link anzeigen ===
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['view'])) {
$shareId = preg_replace('/[^a-f0-9]/', '', $_GET['view']);
$shareFile = dirname(__DIR__) . '/data/shares/' . $shareId . '.json';
if (!file_exists($shareFile)) {
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><title>Link ungültig</title></head><body style="font-family:sans-serif;text-align:center;padding:50px;"><h1>❌ Link nicht gefunden</h1><p>Dieser Share-Link existiert nicht oder wurde bereits gelöscht.</p></body></html>';
exit;
}
$shareData = json_decode(file_get_contents($shareFile), true);
// Ablauf prüfen
if (time() > $shareData['expiry']) {
@unlink($shareFile);
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><title>Link abgelaufen</title></head><body style="font-family:sans-serif;text-align:center;padding:50px;"><h1>⏰ Link abgelaufen</h1><p>Dieser Share-Link ist abgelaufen. Bitte fordere einen neuen Link an.</p></body></html>';
exit;
}
// Datei existiert?
$filePath = dirname(__DIR__) . $shareData['path'];
if (!file_exists($filePath)) {
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><title>Datei nicht gefunden</title></head><body style="font-family:sans-serif;text-align:center;padding:50px;"><h1>📭 Datei nicht gefunden</h1><p>Die geteilte Datei existiert nicht mehr.</p></body></html>';
exit;
}
// Redirect zur Datei oder HTML-Seite mit eingebettetem Player
$isVideo = in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['mp4', 'webm', 'mov']);
$isImage = in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png', 'gif', 'webp']);
$siteName = $config['app']['name'] ?? 'Aurora Livecam';
$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
. '://' . $_SERVER['HTTP_HOST'];
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Geteilte ' . ($isVideo ? 'Video' : 'Bild') . ' - ' . htmlspecialchars($siteName) . '</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
border-radius: 16px;
padding: 30px;
max-width: 900px;
width: 100%;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
h1 { font-size: 1.5rem; margin-bottom: 20px; color: #333; }
video, img {
width: 100%;
max-height: 70vh;
object-fit: contain;
border-radius: 8px;
background: #000;
}
.download-btn {
display: inline-block;
margin-top: 20px;
padding: 12px 30px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
text-decoration: none;
border-radius: 8px;
font-weight: 600;
}
.download-btn:hover { opacity: 0.9; }
.footer {
margin-top: 20px;
color: rgba(255,255,255,0.8);
font-size: 0.9rem;
}
.footer a { color: white; }
</style>
</head>
<body>
<div class="container">
<h1>📤 Geteilte' . ($isVideo ? 's Video' : 's Bild') . '</h1>';
if ($isVideo) {
echo '<video controls autoplay><source src="' . htmlspecialchars($shareData['path']) . '" type="video/mp4">Ihr Browser unterstützt kein Video.</video>';
} else {
echo '<img src="' . htmlspecialchars($shareData['path']) . '" alt="Geteiltes Bild">';
}
echo '
<a href="' . htmlspecialchars($shareData['path']) . '" download class="download-btn">⬇️ Herunterladen</a>
</div>
<div class="footer">
Geteilt von <a href="' . $baseUrl . '">' . htmlspecialchars($siteName) . '</a>
</div>
</body>
</html>';
exit;
}
// === POST: E-Mail senden ===
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Nur POST erlaubt']);
exit;
}
// JSON-Body parsen
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
$input = $_POST;
}
$email = filter_var($input['email'] ?? '', FILTER_VALIDATE_EMAIL);
$path = $input['path'] ?? '';
$type = $input['type'] ?? 'video';
$message = htmlspecialchars($input['message'] ?? '');
$senderName = htmlspecialchars($input['sender_name'] ?? 'Ein Freund');
if (!$email) {
echo json_encode(['success' => false, 'error' => 'Ungültige E-Mail-Adresse']);
exit;
}
if (empty($path)) {
echo json_encode(['success' => false, 'error' => 'Kein Pfad angegeben']);
exit;
}
// Share-Link generieren
$expiryHours = $settingsManager->getShareLinkExpiryHours();
$expiry = time() + ($expiryHours * 3600);
$shareDir = dirname(__DIR__) . '/data/shares/';
if (!is_dir($shareDir)) {
mkdir($shareDir, 0755, true);
}
$shareId = bin2hex(random_bytes(16));
$shareData = [
'id' => $shareId,
'path' => $path,
'type' => $type,
'expiry' => $expiry,
'created_at' => date('Y-m-d H:i:s'),
'shared_to' => $email
];
file_put_contents($shareDir . $shareId . '.json', json_encode($shareData));
$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
. '://' . $_SERVER['HTTP_HOST'];
$shareUrl = $baseUrl . '/api/share.php?view=' . $shareId;
$siteName = $config['app']['name'] ?? 'Aurora Livecam';
// E-Mail senden
try {
$mail = new PHPMailer(true);
// SMTP Konfiguration
$mail->isSMTP();
$mail->Host = $mailConfig['host'];
$mail->SMTPAuth = true;
$mail->Username = $mailConfig['username'];
$mail->Password = $mailConfig['password'];
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $mailConfig['port'] ?? 587;
$mail->CharSet = 'UTF-8';
// Absender/Empfänger
$mail->setFrom($mailConfig['from_address'], $mailConfig['from_name'] ?? $siteName);
$mail->addAddress($email);
// Inhalt
$mail->isHTML(true);
$mail->Subject = $senderName . ' hat ' . ($type === 'video' ? 'ein Video' : 'ein Bild') . ' mit dir geteilt';
$mail->Body = '
<div style="font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 30px; border-radius: 12px 12px 0 0; text-align: center;">
<h1 style="color: white; margin: 0; font-size: 24px;">📤 ' . htmlspecialchars($siteName) . '</h1>
</div>
<div style="background: #f7f7f7; padding: 30px; border-radius: 0 0 12px 12px;">
<p style="font-size: 18px; color: #333; margin-bottom: 20px;">
<strong>' . htmlspecialchars($senderName) . '</strong> hat ' . ($type === 'video' ? 'ein Video' : 'ein Bild') . ' mit dir geteilt!
</p>
' . (!empty($message) ? '<div style="background: white; padding: 15px; border-radius: 8px; border-left: 4px solid #667eea; margin-bottom: 20px;"><em>"' . nl2br($message) . '"</em></div>' : '') . '
<a href="' . htmlspecialchars($shareUrl) . '" style="display: inline-block; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 15px 30px; text-decoration: none; border-radius: 8px; font-weight: 600; font-size: 16px;">
▶️ Jetzt ansehen
</a>
<p style="margin-top: 20px; color: #888; font-size: 12px;">
Dieser Link ist ' . $expiryHours . ' Stunden gültig.
</p>
</div>
</div>';
$mail->AltBody = $senderName . ' hat ' . ($type === 'video' ? 'ein Video' : 'ein Bild') . ' mit dir geteilt: ' . $shareUrl;
$mail->send();
echo json_encode([
'success' => true,
'message' => 'E-Mail wurde gesendet',
'share_url' => $shareUrl
]);
} catch (Exception $e) {
error_log('Share email error: ' . $e->getMessage());
echo json_encode([
'success' => false,
'error' => 'E-Mail konnte nicht gesendet werden',
'share_url' => $shareUrl // URL trotzdem zurückgeben
]);
}
+56
View File
@@ -0,0 +1,56 @@
<?php
/**
* Stripe Webhook Endpoint
*
* URL: /api/stripe-webhook.php
* Konfigurieren Sie diesen Endpoint in Ihrem Stripe Dashboard
*/
// Keine Session, keine Ausgabe vor JSON
error_reporting(0);
ini_set('display_errors', 0);
require_once dirname(__DIR__) . '/vendor/autoload.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Billing\WebhookHandler;
// Nur POST erlaubt
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
// Payload lesen
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '';
if (empty($payload)) {
http_response_code(400);
echo json_encode(['error' => 'Empty payload']);
exit;
}
// Webhook verarbeiten
try {
$handler = new WebhookHandler();
$result = $handler->handle($payload, $signature);
if ($result['success']) {
http_response_code(200);
} else {
http_response_code(400);
}
header('Content-Type: application/json');
echo json_encode($result);
} catch (\Exception $e) {
error_log('Stripe Webhook Error: ' . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Internal server error']);
}
+192
View File
@@ -0,0 +1,192 @@
<?php
/**
* Video Search API
*
* Suche nach Videos nach Datum und Uhrzeit
*
* GET /api/video-search.php?date=2024-01-30
* GET /api/video-search.php?date=2024-01-30&time=14:30
* GET /api/video-search.php?from=2024-01-01&to=2024-01-31
* GET /api/video-search.php?time_from=08:00&time_to=18:00
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$settingsManager = new SettingsManager();
$videoDir = dirname(__DIR__) . '/videos/';
$aiDir = dirname(__DIR__) . '/ai/';
// Parameter
$date = $_GET['date'] ?? null; // Format: YYYY-MM-DD
$time = $_GET['time'] ?? null; // Format: HH:MM
$fromDate = $_GET['from'] ?? null;
$toDate = $_GET['to'] ?? null;
$timeFrom = $_GET['time_from'] ?? null;
$timeTo = $_GET['time_to'] ?? null;
$type = $_GET['type'] ?? 'all'; // all, daily, ai
$aiCategory = $_GET['ai_category'] ?? null;
$limit = min(100, (int)($_GET['limit'] ?? 50));
$results = [
'daily_videos' => [],
'ai_videos' => [],
'gallery_images' => []
];
// AI-Kategorien
$aiCategories = ['sunny', 'rainy', 'snowy', 'planes', 'birds', 'sunset', 'sunrise', 'rainbow'];
// === TAGESVIDEOS SUCHEN ===
if ($type === 'all' || $type === 'daily') {
$pattern = $videoDir . 'daily_video_*.mp4';
$dailyVideos = glob($pattern);
foreach ($dailyVideos as $video) {
$filename = basename($video);
// Extrahiere Datum aus Dateinamen: daily_video_YYYYMMDD_HHMMSS.mp4
if (preg_match('/daily_video_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})\.mp4/', $filename, $matches)) {
$videoDate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
$videoTime = $matches[4] . ':' . $matches[5];
$videoDateTime = $videoDate . ' ' . $videoTime . ':' . $matches[6];
// Datumsfilter
if ($date !== null && $videoDate !== $date) {
continue;
}
if ($fromDate !== null && $videoDate < $fromDate) {
continue;
}
if ($toDate !== null && $videoDate > $toDate) {
continue;
}
// Uhrzeitfilter
if ($timeFrom !== null && $videoTime < $timeFrom) {
continue;
}
if ($timeTo !== null && $videoTime > $timeTo) {
continue;
}
// Spezifische Uhrzeit (mit 30 Min Toleranz)
if ($time !== null) {
$searchMinutes = intval(substr($time, 0, 2)) * 60 + intval(substr($time, 3, 2));
$videoMinutes = intval($matches[4]) * 60 + intval($matches[5]);
if (abs($searchMinutes - $videoMinutes) > 30) {
continue;
}
}
$results['daily_videos'][] = [
'type' => 'daily',
'filename' => $filename,
'path' => '/videos/' . $filename,
'date' => $videoDate,
'time' => $videoTime,
'datetime' => $videoDateTime,
'timestamp' => strtotime($videoDateTime),
'size' => filesize($video),
'size_mb' => round(filesize($video) / (1024 * 1024), 2)
];
}
}
}
// === AI-VIDEOS SUCHEN ===
if ($type === 'all' || $type === 'ai') {
$searchCategories = $aiCategory ? [$aiCategory] : $aiCategories;
foreach ($searchCategories as $category) {
$categoryDir = $aiDir . $category . '/';
if (!is_dir($categoryDir)) continue;
$pattern = $categoryDir . $category . '_*.mp4';
$aiVideos = glob($pattern);
foreach ($aiVideos as $video) {
$filename = basename($video);
// Extrahiere Datum aus Dateinamen: category_YYYYMMDD_HHMMSS.mp4
if (preg_match('/' . $category . '_(\d{4})(\d{2})(\d{2})_?(\d{2})?(\d{2})?(\d{2})?\.mp4/', $filename, $matches)) {
$videoDate = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
$videoTime = isset($matches[4]) ? ($matches[4] . ':' . ($matches[5] ?? '00')) : '00:00';
$videoDateTime = $videoDate . ' ' . $videoTime;
// Datumsfilter
if ($date !== null && $videoDate !== $date) {
continue;
}
if ($fromDate !== null && $videoDate < $fromDate) {
continue;
}
if ($toDate !== null && $videoDate > $toDate) {
continue;
}
// Uhrzeitfilter
if ($timeFrom !== null && $videoTime < $timeFrom) {
continue;
}
if ($timeTo !== null && $videoTime > $timeTo) {
continue;
}
$results['ai_videos'][] = [
'type' => 'ai',
'category' => $category,
'filename' => $filename,
'path' => '/ai/' . $category . '/' . $filename,
'date' => $videoDate,
'time' => $videoTime,
'datetime' => $videoDateTime,
'timestamp' => strtotime($videoDateTime),
'size' => filesize($video),
'size_mb' => round(filesize($video) / (1024 * 1024), 2)
];
}
}
}
}
// Sortieren nach Datum/Zeit (neueste zuerst)
usort($results['daily_videos'], fn($a, $b) => $b['timestamp'] - $a['timestamp']);
usort($results['ai_videos'], fn($a, $b) => $b['timestamp'] - $a['timestamp']);
// Limit anwenden
$results['daily_videos'] = array_slice($results['daily_videos'], 0, $limit);
$results['ai_videos'] = array_slice($results['ai_videos'], 0, $limit);
// Statistiken
$results['stats'] = [
'total_daily' => count($results['daily_videos']),
'total_ai' => count($results['ai_videos']),
'total' => count($results['daily_videos']) + count($results['ai_videos'])
];
$results['filters'] = [
'date' => $date,
'time' => $time,
'from' => $fromDate,
'to' => $toDate,
'time_from' => $timeFrom,
'time_to' => $timeTo,
'type' => $type,
'ai_category' => $aiCategory
];
$results['success'] = true;
echo json_encode($results, JSON_PRETTY_PRINT);
+15
View File
@@ -0,0 +1,15 @@
<?php
// Clear PHP OPcache
if (function_exists('opcache_reset')) {
opcache_reset();
echo "OPcache cleared successfully!\n";
} else {
echo "OPcache not available\n";
}
// Clear realpath cache
clearstatcache(true);
echo "Realpath cache cleared!\n";
echo "\nNow reload the page with CTRL+F5 (hard refresh)\n";
?>
+59
View File
@@ -0,0 +1,59 @@
<?php
/**
* Aurora Livecam - Konfigurationsdatei
*
* Kopiere diese Datei zu config.php und passe die Werte an.
* WICHTIG: config.php niemals in Git committen!
*/
return [
// Datenbank-Konfiguration
'database' => [
'host' => 'localhost',
'port' => 3306,
'database' => 'aurora_livecam',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
],
// Anwendungs-Einstellungen
'app' => [
'name' => 'Aurora Livecam',
'url' => 'https://aurora-weather-livecam.com',
'debug' => false,
'timezone' => 'Europe/Zurich',
],
// Multi-Tenant Einstellungen
'tenant' => [
'default_subdomain_suffix' => '.aurora-livecam.com',
'allow_custom_domains' => true,
'trial_days' => 14,
],
// Stripe (für Billing)
'stripe' => [
'public_key' => '',
'secret_key' => '',
'webhook_secret' => '',
'currency' => 'chf',
],
// E-Mail Einstellungen (für Onboarding)
'mail' => [
'host' => 'smtp.example.com',
'port' => 587,
'username' => '',
'password' => '',
'from_address' => 'noreply@aurora-livecam.com',
'from_name' => 'Aurora Livecam',
],
// Sicherheit
'security' => [
'session_lifetime' => 7200, // 2 Stunden
'remember_me_days' => 30,
'password_min_length' => 8,
],
];
+274
View File
@@ -0,0 +1,274 @@
/* ========== TIMELAPSE CONTROLS ========== */
#timelapse-controls {
display: none;
margin-top: 15px;
}
.timelapse-control-bar {
display: flex;
align-items: center;
gap: 10px;
background: rgba(255, 255, 255, 0.95);
padding: 12px 20px;
border-radius: 50px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
flex-wrap: wrap;
justify-content: center;
}
.tl-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
width: 44px;
height: 44px;
border-radius: 50%;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 16px;
transition: all 0.3s ease;
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}
.tl-btn:hover {
transform: scale(1.1);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.5);
}
.tl-btn.active {
background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
}
.tl-slider-container {
flex: 1;
min-width: 200px;
max-width: 400px;
display: flex;
align-items: center;
gap: 15px;
}
#tl-slider {
flex: 1;
height: 8px;
border-radius: 4px;
background: #e0e0e0;
outline: none;
-webkit-appearance: none;
}
#tl-slider::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
cursor: pointer;
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
#tl-slider::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
cursor: pointer;
border: none;
}
#tl-time-display {
font-family: monospace;
font-size: 14px;
color: #333;
background: #f5f5f5;
padding: 6px 12px;
border-radius: 20px;
min-width: 140px;
text-align: center;
}
.tl-speed-btn {
width: auto !important;
padding: 0 20px !important;
border-radius: 22px !important;
font-weight: bold;
font-size: 14px;
}
.tl-back-btn {
width: auto !important;
padding: 0 20px !important;
border-radius: 22px !important;
background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%) !important;
gap: 8px;
}
/* ========== DAILY VIDEO PLAYER ========== */
#daily-video-player {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000;
z-index: 50;
}
#daily-video {
width: 100%;
height: 100%;
object-fit: contain;
}
.video-player-controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 15px;
z-index: 60;
}
/* ========== ADMIN SETTINGS PANEL ========== */
#admin-settings-panel {
background: white;
padding: 25px;
border-radius: 12px;
margin-bottom: 30px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
#admin-settings-panel h3 {
color: #667eea;
border-bottom: 2px solid #667eea;
padding-bottom: 10px;
margin-bottom: 20px;
}
.settings-group {
margin-bottom: 25px;
padding: 20px;
background: #f9f9f9;
border-radius: 8px;
}
.settings-group h4 {
margin-bottom: 15px;
color: #333;
}
.setting-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 0;
border-bottom: 1px solid #eee;
}
.setting-row:last-child {
border-bottom: none;
}
.setting-label {
font-weight: 500;
color: #555;
}
.setting-input {
display: flex;
align-items: center;
gap: 10px;
}
/* Toggle Switch */
.toggle-switch {
position: relative;
width: 50px;
height: 26px;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: 0.3s;
border-radius: 26px;
}
.toggle-slider:before {
position: absolute;
content: "";
height: 20px;
width: 20px;
left: 3px;
bottom: 3px;
background-color: white;
transition: 0.3s;
border-radius: 50%;
}
input:checked + .toggle-slider {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
input:checked + .toggle-slider:before {
transform: translateX(24px);
}
/* Number Input */
.number-input {
width: 70px;
padding: 8px;
border: 2px solid #ddd;
border-radius: 8px;
font-size: 16px;
text-align: center;
}
.number-input:focus {
border-color: #667eea;
outline: none;
}
/* ========== MOBILE RESPONSIVE ========== */
@media (max-width: 600px) {
.timelapse-control-bar {
padding: 10px 15px;
gap: 8px;
}
.tl-btn {
width: 38px;
height: 38px;
font-size: 14px;
}
.tl-slider-container {
width: 100%;
order: 10;
margin-top: 10px;
}
#tl-time-display {
font-size: 12px;
min-width: 120px;
}
.video-player-controls {
flex-direction: column;
bottom: 10px;
}
}
+75
View File
@@ -0,0 +1,75 @@
<?php
/**
* Dashboard API - Stats
*/
header('Content-Type: application/json');
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once dirname(__DIR__, 2) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__, 2) . '/src/bootstrap.php')) {
require_once dirname(__DIR__, 2) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Core\Database;
$auth = new AuthManager();
// Auth check
if (!$auth->isLoggedIn()) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
exit;
}
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$stats = [
'viewers_current' => 0,
'viewers_today' => 0,
'viewers_peak' => 0,
'stream_status' => 'unknown',
];
// Aktuelle Zuschauer aus Datei
$viewerFile = dirname(__DIR__, 2) . '/active_viewers.json';
if (file_exists($viewerFile)) {
$viewers = json_decode(file_get_contents($viewerFile), true);
$stats['viewers_current'] = count($viewers ?? []);
}
// DB Stats falls verfügbar
try {
$db = Database::getInstance();
if ($tenantId > 0) {
$todayStats = $db->fetchOne(
"SELECT SUM(viewer_count) as total, MAX(viewer_count) as peak
FROM viewer_stats
WHERE tenant_id = ? AND DATE(recorded_at) = CURDATE()",
[$tenantId]
);
if ($todayStats) {
$stats['viewers_today'] = (int)($todayStats['total'] ?? 0);
$stats['viewers_peak'] = (int)($todayStats['peak'] ?? 0);
}
$stream = $db->fetchOne(
"SELECT last_status FROM tenant_streams WHERE tenant_id = ? AND is_primary = 1",
[$tenantId]
);
$stats['stream_status'] = $stream['last_status'] ?? 'unknown';
}
} catch (\Exception $e) {
// DB nicht verfügbar - Stats bleiben auf Defaults
}
echo json_encode([
'success' => true,
'stats' => $stats,
'timestamp' => time(),
]);
@@ -0,0 +1,536 @@
/* Dashboard CSS */
:root {
--primary: #667eea;
--primary-dark: #5a67d8;
--secondary: #764ba2;
--accent: #f093fb;
--success: #48bb78;
--warning: #ed8936;
--danger: #f56565;
--dark: #1a202c;
--gray-900: #1a202c;
--gray-800: #2d3748;
--gray-700: #4a5568;
--gray-600: #718096;
--gray-500: #a0aec0;
--gray-400: #cbd5e0;
--gray-300: #e2e8f0;
--gray-200: #edf2f7;
--gray-100: #f7fafc;
--white: #ffffff;
--sidebar-width: 260px;
--header-height: 60px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background: var(--gray-100);
color: var(--gray-800);
line-height: 1.6;
}
/* Dashboard Container */
.dashboard-container {
display: flex;
min-height: 100vh;
}
/* Sidebar */
.sidebar {
width: var(--sidebar-width);
background: linear-gradient(180deg, var(--gray-900) 0%, var(--gray-800) 100%);
color: var(--white);
display: flex;
flex-direction: column;
position: fixed;
height: 100vh;
z-index: 100;
}
.sidebar-header {
padding: 1.5rem;
border-bottom: 1px solid var(--gray-700);
}
.sidebar-header h2 {
font-size: 1.25rem;
font-weight: 600;
margin-bottom: 0.25rem;
}
.role-badge {
font-size: 0.75rem;
background: var(--primary);
padding: 0.125rem 0.5rem;
border-radius: 9999px;
text-transform: capitalize;
}
/* Navigation */
.sidebar-nav {
flex: 1;
padding: 1rem 0;
overflow-y: auto;
}
.nav-item {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.75rem 1.5rem;
color: var(--gray-400);
text-decoration: none;
transition: all 0.2s;
}
.nav-item:hover {
background: var(--gray-700);
color: var(--white);
}
.nav-item.active {
background: linear-gradient(90deg, var(--primary) 0%, var(--secondary) 100%);
color: var(--white);
}
.nav-icon {
font-size: 1.25rem;
width: 1.5rem;
text-align: center;
}
.nav-divider {
height: 1px;
background: var(--gray-700);
margin: 1rem 0;
}
.nav-label {
display: block;
padding: 0.5rem 1.5rem;
font-size: 0.75rem;
text-transform: uppercase;
color: var(--gray-500);
letter-spacing: 0.05em;
}
.sidebar-footer {
border-top: 1px solid var(--gray-700);
padding: 0.5rem 0;
}
.nav-item.logout:hover {
background: var(--danger);
}
/* Main Content */
.main-content {
flex: 1;
margin-left: var(--sidebar-width);
min-height: 100vh;
}
.main-header {
height: var(--header-height);
background: var(--white);
border-bottom: 1px solid var(--gray-300);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 2rem;
position: sticky;
top: 0;
z-index: 50;
}
.main-header h1 {
font-size: 1.5rem;
font-weight: 600;
}
.user-info {
color: var(--gray-600);
font-size: 0.875rem;
}
.content-wrapper {
padding: 2rem;
}
/* Cards */
.card {
background: var(--white);
border-radius: 0.5rem;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
margin-bottom: 1.5rem;
}
.card-header {
padding: 1rem 1.5rem;
border-bottom: 1px solid var(--gray-200);
display: flex;
align-items: center;
justify-content: space-between;
}
.card-title {
font-size: 1rem;
font-weight: 600;
}
.card-body {
padding: 1.5rem;
}
/* Stats Grid */
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1.5rem;
margin-bottom: 2rem;
}
.stat-card {
background: var(--white);
border-radius: 0.5rem;
padding: 1.5rem;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.stat-icon {
font-size: 2rem;
margin-bottom: 0.5rem;
}
.stat-value {
font-size: 2rem;
font-weight: 700;
color: var(--gray-900);
}
.stat-label {
color: var(--gray-600);
font-size: 0.875rem;
}
.stat-change {
font-size: 0.875rem;
margin-top: 0.25rem;
}
.stat-change.positive { color: var(--success); }
.stat-change.negative { color: var(--danger); }
/* Forms */
.form-group {
margin-bottom: 1.5rem;
}
.form-label {
display: block;
font-weight: 500;
margin-bottom: 0.5rem;
color: var(--gray-700);
}
.form-input,
.form-select,
.form-textarea {
width: 100%;
padding: 0.75rem 1rem;
border: 1px solid var(--gray-300);
border-radius: 0.375rem;
font-size: 1rem;
transition: border-color 0.2s, box-shadow 0.2s;
}
.form-input:focus,
.form-select:focus,
.form-textarea:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}
.form-help {
font-size: 0.875rem;
color: var(--gray-500);
margin-top: 0.25rem;
}
.form-textarea {
min-height: 100px;
resize: vertical;
}
/* Buttons */
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 0.375rem;
font-size: 1rem;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
text-decoration: none;
}
.btn-primary {
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
color: var(--white);
}
.btn-primary:hover {
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
.btn-secondary {
background: var(--gray-200);
color: var(--gray-700);
}
.btn-secondary:hover {
background: var(--gray-300);
}
.btn-danger {
background: var(--danger);
color: var(--white);
}
.btn-success {
background: var(--success);
color: var(--white);
}
.btn-sm {
padding: 0.5rem 1rem;
font-size: 0.875rem;
}
/* Alerts */
.alert {
padding: 1rem 1.5rem;
border-radius: 0.375rem;
margin-bottom: 1.5rem;
}
.alert-success {
background: #c6f6d5;
color: #22543d;
border: 1px solid #9ae6b4;
}
.alert-error {
background: #fed7d7;
color: #742a2a;
border: 1px solid #feb2b2;
}
.alert-warning {
background: #feebc8;
color: #744210;
border: 1px solid #fbd38d;
}
.alert-info {
background: #bee3f8;
color: #2a4365;
border: 1px solid #90cdf4;
}
/* Tables */
.table {
width: 100%;
border-collapse: collapse;
}
.table th,
.table td {
padding: 1rem;
text-align: left;
border-bottom: 1px solid var(--gray-200);
}
.table th {
font-weight: 600;
color: var(--gray-600);
font-size: 0.875rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.table tbody tr:hover {
background: var(--gray-50);
}
/* Status Badges */
.badge {
display: inline-block;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-size: 0.75rem;
font-weight: 500;
}
.badge-success { background: #c6f6d5; color: #22543d; }
.badge-warning { background: #feebc8; color: #744210; }
.badge-danger { background: #fed7d7; color: #742a2a; }
.badge-info { background: #bee3f8; color: #2a4365; }
/* Grid */
.grid {
display: grid;
gap: 1.5rem;
}
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
/* Color Picker */
.color-picker-wrapper {
display: flex;
align-items: center;
gap: 1rem;
}
.color-picker {
width: 50px;
height: 40px;
border: none;
border-radius: 0.375rem;
cursor: pointer;
}
.color-value {
font-family: monospace;
color: var(--gray-600);
}
/* Preview Box */
.preview-box {
border: 2px dashed var(--gray-300);
border-radius: 0.5rem;
padding: 2rem;
text-align: center;
background: var(--gray-50);
}
/* Toggle Switch */
.toggle-wrapper {
display: flex;
align-items: center;
gap: 0.75rem;
}
.toggle {
position: relative;
width: 48px;
height: 24px;
}
.toggle input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: var(--gray-300);
border-radius: 24px;
transition: 0.3s;
}
.toggle-slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 3px;
bottom: 3px;
background: white;
border-radius: 50%;
transition: 0.3s;
}
.toggle input:checked + .toggle-slider {
background: var(--primary);
}
.toggle input:checked + .toggle-slider:before {
transform: translateX(24px);
}
/* Login Page */
.login-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
}
.login-box {
background: var(--white);
padding: 2.5rem;
border-radius: 1rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 100%;
max-width: 400px;
}
.login-title {
text-align: center;
margin-bottom: 2rem;
}
.login-title h1 {
font-size: 1.5rem;
margin-bottom: 0.5rem;
}
.login-title p {
color: var(--gray-500);
}
/* Responsive */
@media (max-width: 768px) {
.sidebar {
transform: translateX(-100%);
transition: transform 0.3s;
}
.sidebar.open {
transform: translateX(0);
}
.main-content {
margin-left: 0;
}
.stats-grid {
grid-template-columns: 1fr;
}
.grid-2,
.grid-3 {
grid-template-columns: 1fr;
}
}
@@ -0,0 +1,131 @@
/**
* Dashboard JavaScript
*/
document.addEventListener('DOMContentLoaded', function() {
// Auto-dismiss alerts after 5 seconds
const alerts = document.querySelectorAll('.alert');
alerts.forEach(alert => {
setTimeout(() => {
alert.style.transition = 'opacity 0.3s';
alert.style.opacity = '0';
setTimeout(() => alert.remove(), 300);
}, 5000);
});
// Mobile sidebar toggle
const sidebar = document.querySelector('.sidebar');
const mainContent = document.querySelector('.main-content');
if (window.innerWidth <= 768) {
// Add menu button
const menuBtn = document.createElement('button');
menuBtn.className = 'btn btn-secondary';
menuBtn.style.cssText = 'position: fixed; top: 10px; left: 10px; z-index: 200; padding: 0.5rem;';
menuBtn.innerHTML = '☰';
menuBtn.onclick = () => sidebar.classList.toggle('open');
document.body.appendChild(menuBtn);
// Close sidebar on content click
mainContent.addEventListener('click', () => {
sidebar.classList.remove('open');
});
}
// Color picker live preview
document.querySelectorAll('.color-picker').forEach(picker => {
picker.addEventListener('input', function() {
const wrapper = this.closest('.color-picker-wrapper');
if (wrapper) {
const valueDisplay = wrapper.querySelector('.color-value');
if (valueDisplay) {
valueDisplay.textContent = this.value;
}
}
});
});
// Form unsaved changes warning
const forms = document.querySelectorAll('form');
let formChanged = false;
forms.forEach(form => {
form.addEventListener('change', () => {
formChanged = true;
});
form.addEventListener('submit', () => {
formChanged = false;
});
});
window.addEventListener('beforeunload', (e) => {
if (formChanged) {
e.preventDefault();
e.returnValue = '';
}
});
// Stats refresh (every 30 seconds on overview page)
if (document.querySelector('.stats-grid')) {
setInterval(refreshStats, 30000);
}
});
/**
* Refresh stats via AJAX
*/
function refreshStats() {
fetch('/dashboard/api/stats.php')
.then(response => response.json())
.then(data => {
if (data.success) {
updateStatCard('viewers_current', data.stats.viewers_current);
updateStatCard('viewers_today', data.stats.viewers_today);
updateStatCard('viewers_peak', data.stats.viewers_peak);
}
})
.catch(err => console.log('Stats refresh failed:', err));
}
/**
* Update a stat card value
*/
function updateStatCard(id, value) {
const cards = document.querySelectorAll('.stat-card');
cards.forEach(card => {
const label = card.querySelector('.stat-label');
if (label) {
// Match by label text (simplified)
const valueEl = card.querySelector('.stat-value');
if (valueEl && typeof value !== 'undefined') {
valueEl.textContent = value;
}
}
});
}
/**
* Show notification toast
*/
function showNotification(message, type = 'info') {
const toast = document.createElement('div');
toast.className = `alert alert-${type}`;
toast.style.cssText = 'position: fixed; top: 20px; right: 20px; z-index: 1000; min-width: 300px;';
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.style.transition = 'opacity 0.3s';
toast.style.opacity = '0';
setTimeout(() => toast.remove(), 300);
}, 3000);
}
/**
* Confirm dangerous actions
*/
function confirmAction(message) {
return confirm(message || 'Sind Sie sicher?');
}
+282
View File
@@ -0,0 +1,282 @@
<?php
/**
* Dashboard - Abrechnung
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Billing\StripeService;
use AuroraLivecam\Billing\SubscriptionManager;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
$auth->requireLogin();
// Prüfe ob Billing aktiviert
if (!$settingsManager->isBillingEnabled()) {
header('Location: /dashboard/');
exit;
}
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$flashMessage = null;
$flashType = 'info';
$stripe = new StripeService();
$subscriptions = new SubscriptionManager();
// Aktuelle Subscription
$currentSub = null;
$plans = [];
$invoices = [];
$trialDays = 0;
try {
$currentSub = $subscriptions->getSubscription($tenantId);
$plans = $subscriptions->getPlans();
$invoices = $subscriptions->getInvoices($tenantId, 5);
$trialDays = $subscriptions->getTrialDaysRemaining($tenantId);
} catch (\Exception $e) {
$flashMessage = 'Fehler beim Laden der Abrechnungsdaten';
$flashType = 'error';
}
// Checkout starten
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['plan_id'])) {
$planId = (int)$_POST['plan_id'];
$plan = $subscriptions->getPlan($planId);
if ($plan && !empty($plan['stripe_price_id'])) {
$baseUrl = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
$session = $stripe->createCheckoutSession(
$tenantId,
$plan['stripe_price_id'],
$baseUrl . '/dashboard/billing.php?success=1',
$baseUrl . '/dashboard/billing.php?canceled=1'
);
if ($session && isset($session['url'])) {
header('Location: ' . $session['url']);
exit;
} else {
$flashMessage = 'Fehler beim Erstellen der Checkout-Session';
$flashType = 'error';
}
}
}
// Billing Portal öffnen
if (isset($_GET['portal'])) {
$baseUrl = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
$session = $stripe->createPortalSession($tenantId, $baseUrl . '/dashboard/billing.php');
if ($session && isset($session['url'])) {
header('Location: ' . $session['url']);
exit;
}
}
// Success/Cancel Messages
if (isset($_GET['success'])) {
$flashMessage = 'Zahlung erfolgreich! Ihr Abo ist jetzt aktiv.';
$flashType = 'success';
}
if (isset($_GET['canceled'])) {
$flashMessage = 'Checkout abgebrochen.';
$flashType = 'warning';
}
$pageTitle = 'Abrechnung';
$currentPage = 'billing';
ob_start();
?>
<!-- Aktueller Plan -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Aktueller Plan</h3>
<?php if ($currentSub): ?>
<span class="badge badge-<?php echo $currentSub['status'] === 'active' ? 'success' : ($currentSub['status'] === 'trialing' ? 'warning' : 'danger'); ?>">
<?php echo ucfirst($currentSub['status']); ?>
</span>
<?php endif; ?>
</div>
<div class="card-body">
<?php if ($currentSub): ?>
<div style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem;">
<div>
<h2 style="margin: 0; font-size: 1.75rem;"><?php echo htmlspecialchars($currentSub['plan_name'] ?? 'Free'); ?></h2>
<?php if ($currentSub['status'] === 'trialing' && $trialDays > 0): ?>
<p style="color: var(--warning); margin: 0.5rem 0 0 0;">
Trial endet in <?php echo $trialDays; ?> Tag<?php echo $trialDays !== 1 ? 'en' : ''; ?>
</p>
<?php elseif ($currentSub['current_period_end']): ?>
<p style="color: var(--gray-500); margin: 0.5rem 0 0 0;">
Nächste Abrechnung: <?php echo date('d.m.Y', strtotime($currentSub['current_period_end'])); ?>
</p>
<?php endif; ?>
</div>
<?php if ($stripe->isConfigured() && !empty($currentSub['stripe_customer_id'])): ?>
<a href="?portal=1" class="btn btn-secondary">
Abo verwalten
</a>
<?php endif; ?>
</div>
<?php if (!empty($currentSub['plan_features'])): ?>
<div style="margin-top: 1.5rem; padding-top: 1.5rem; border-top: 1px solid var(--gray-200);">
<h4 style="font-size: 0.875rem; color: var(--gray-500); margin-bottom: 0.75rem;">Enthaltene Features:</h4>
<div style="display: flex; flex-wrap: wrap; gap: 0.5rem;">
<?php foreach ($currentSub['plan_features'] as $feature => $value): ?>
<?php if ($value): ?>
<span class="badge badge-info">
<?php
$labels = [
'max_viewers' => 'Max. Zuschauer: ' . ($value === -1 ? '∞' : $value),
'storage_gb' => 'Speicher: ' . $value . ' GB',
'custom_domain' => 'Custom Domain',
'weather_widget' => 'Wetter-Widget',
'timelapse' => 'Timelapse',
'analytics' => 'Analytics',
'branding' => 'Custom Branding',
'priority_support' => 'Priority Support',
];
echo $labels[$feature] ?? ucfirst(str_replace('_', ' ', $feature));
?>
</span>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php else: ?>
<p style="color: var(--gray-500);">Kein aktives Abo</p>
<?php endif; ?>
</div>
</div>
<!-- Verfügbare Pläne -->
<?php if (!empty($plans)): ?>
<div class="card">
<div class="card-header">
<h3 class="card-title">Verfügbare Pläne</h3>
</div>
<div class="card-body">
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1.5rem;">
<?php foreach ($plans as $plan): ?>
<?php $isCurrent = $currentSub && $currentSub['plan_id'] == $plan['id']; ?>
<div style="border: 2px solid <?php echo $isCurrent ? 'var(--primary)' : 'var(--gray-200)'; ?>; border-radius: 0.75rem; padding: 1.5rem; <?php echo $isCurrent ? 'background: rgba(102,126,234,0.05);' : ''; ?>">
<h4 style="margin: 0 0 0.5rem 0;"><?php echo htmlspecialchars($plan['name']); ?></h4>
<div style="font-size: 2rem; font-weight: 700; color: var(--gray-900);">
<?php if ($plan['price_monthly'] > 0): ?>
CHF <?php echo number_format($plan['price_monthly'], 0); ?>
<span style="font-size: 1rem; font-weight: 400; color: var(--gray-500);">/Monat</span>
<?php else: ?>
Kostenlos
<?php endif; ?>
</div>
<?php if (!empty($plan['features'])): ?>
<ul style="list-style: none; padding: 0; margin: 1rem 0; font-size: 0.875rem;">
<?php foreach ($plan['features'] as $feature => $value): ?>
<?php if ($value): ?>
<li style="padding: 0.25rem 0; color: var(--gray-600);">
<?php
$labels = [
'max_viewers' => 'Bis ' . ($value === -1 ? 'unbegrenzt' : $value) . ' Zuschauer',
'storage_gb' => $value . ' GB Speicher',
'custom_domain' => 'Eigene Domain',
'weather_widget' => 'Wetter-Widget',
'timelapse' => 'Timelapse',
'analytics' => 'Analytics',
'branding' => 'Custom Branding',
'priority_support' => 'Priority Support',
];
echo $labels[$feature] ?? ucfirst(str_replace('_', ' ', $feature));
?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php if ($isCurrent): ?>
<button class="btn btn-secondary" style="width: 100%;" disabled>Aktueller Plan</button>
<?php elseif ($plan['price_monthly'] > 0 && $stripe->isConfigured()): ?>
<form method="POST" action="">
<input type="hidden" name="plan_id" value="<?php echo $plan['id']; ?>">
<button type="submit" class="btn btn-primary" style="width: 100%;">
Upgrade
</button>
</form>
<?php elseif ($plan['price_monthly'] == 0): ?>
<button class="btn btn-secondary" style="width: 100%;" disabled>Free Plan</button>
<?php else: ?>
<button class="btn btn-secondary" style="width: 100%;" disabled>Stripe nicht konfiguriert</button>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
<!-- Rechnungen -->
<?php if (!empty($invoices)): ?>
<div class="card">
<div class="card-header">
<h3 class="card-title">Rechnungen</h3>
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Datum</th>
<th>Betrag</th>
<th>Status</th>
<th>PDF</th>
</tr>
</thead>
<tbody>
<?php foreach ($invoices as $invoice): ?>
<tr>
<td><?php echo date('d.m.Y', strtotime($invoice['created_at'])); ?></td>
<td><?php echo $invoice['currency']; ?> <?php echo number_format($invoice['amount'], 2); ?></td>
<td>
<span class="badge badge-<?php echo $invoice['status'] === 'paid' ? 'success' : 'warning'; ?>">
<?php echo ucfirst($invoice['status']); ?>
</span>
</td>
<td>
<?php if ($invoice['invoice_pdf_url']): ?>
<a href="<?php echo htmlspecialchars($invoice['invoice_pdf_url']); ?>" target="_blank" class="btn btn-sm btn-secondary">
Download
</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
<?php if (!$stripe->isConfigured()): ?>
<div class="alert alert-warning">
<strong>Hinweis:</strong> Stripe ist noch nicht konfiguriert. Bitte fügen Sie Ihre Stripe API-Keys in config.php hinzu.
</div>
<?php endif; ?>
<?php
$content = ob_get_clean();
include __DIR__ . '/templates/layout.php';
+230
View File
@@ -0,0 +1,230 @@
<?php
/**
* Dashboard - Branding Einstellungen
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Core\Database;
use AuroraLivecam\Tenant\TenantManager;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
$auth->requireLogin();
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$flashMessage = null;
$flashType = 'info';
// Branding-Daten laden
$branding = [
'site_name' => '',
'site_name_full' => '',
'tagline' => '',
'primary_color' => '#667eea',
'secondary_color' => '#764ba2',
'accent_color' => '#f093fb',
'welcome_text_de' => '',
'welcome_text_en' => '',
'footer_text' => '',
'custom_css' => '',
];
try {
$db = Database::getInstance();
if ($tenantId > 0) {
$tenantManager = new TenantManager($db);
$dbBranding = $tenantManager->getBranding($tenantId);
if ($dbBranding) {
$branding = array_merge($branding, $dbBranding);
}
}
} catch (\Exception $e) {
// DB nicht verfügbar
}
// Formular verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$newBranding = [
'site_name' => trim($_POST['site_name'] ?? ''),
'site_name_full' => trim($_POST['site_name_full'] ?? ''),
'tagline' => trim($_POST['tagline'] ?? ''),
'primary_color' => $_POST['primary_color'] ?? '#667eea',
'secondary_color' => $_POST['secondary_color'] ?? '#764ba2',
'accent_color' => $_POST['accent_color'] ?? '#f093fb',
'welcome_text_de' => trim($_POST['welcome_text_de'] ?? ''),
'welcome_text_en' => trim($_POST['welcome_text_en'] ?? ''),
'footer_text' => trim($_POST['footer_text'] ?? ''),
'custom_css' => trim($_POST['custom_css'] ?? ''),
];
try {
$db = Database::getInstance();
if ($tenantId > 0) {
$tenantManager = new TenantManager($db);
$tenantManager->updateBranding($tenantId, $newBranding);
$flashMessage = 'Branding gespeichert!';
$flashType = 'success';
$branding = array_merge($branding, $newBranding);
} else {
$flashMessage = 'Branding kann im Legacy-Modus nicht gespeichert werden.';
$flashType = 'warning';
}
} catch (\Exception $e) {
$flashMessage = 'Fehler beim Speichern: ' . $e->getMessage();
$flashType = 'error';
}
}
$pageTitle = 'Branding';
$currentPage = 'branding';
ob_start();
?>
<form method="POST" action="">
<div class="grid grid-2">
<!-- Grundeinstellungen -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Grundeinstellungen</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="form-label" for="site_name">Site Name (kurz)</label>
<input type="text" id="site_name" name="site_name" class="form-input"
value="<?php echo htmlspecialchars($branding['site_name']); ?>"
placeholder="MeineCam">
</div>
<div class="form-group">
<label class="form-label" for="site_name_full">Site Name (vollständig)</label>
<input type="text" id="site_name_full" name="site_name_full" class="form-input"
value="<?php echo htmlspecialchars($branding['site_name_full']); ?>"
placeholder="Meine Wetter Livecam">
</div>
<div class="form-group">
<label class="form-label" for="tagline">Tagline / Slogan</label>
<input type="text" id="tagline" name="tagline" class="form-input"
value="<?php echo htmlspecialchars($branding['tagline']); ?>"
placeholder="Ihre Live-Webcam 24/7">
</div>
</div>
</div>
<!-- Farben -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Farben</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="form-label">Primärfarbe</label>
<div class="color-picker-wrapper">
<input type="color" name="primary_color" class="color-picker"
value="<?php echo htmlspecialchars($branding['primary_color']); ?>">
<span class="color-value"><?php echo htmlspecialchars($branding['primary_color']); ?></span>
</div>
</div>
<div class="form-group">
<label class="form-label">Sekundärfarbe</label>
<div class="color-picker-wrapper">
<input type="color" name="secondary_color" class="color-picker"
value="<?php echo htmlspecialchars($branding['secondary_color']); ?>">
<span class="color-value"><?php echo htmlspecialchars($branding['secondary_color']); ?></span>
</div>
</div>
<div class="form-group">
<label class="form-label">Akzentfarbe</label>
<div class="color-picker-wrapper">
<input type="color" name="accent_color" class="color-picker"
value="<?php echo htmlspecialchars($branding['accent_color']); ?>">
<span class="color-value"><?php echo htmlspecialchars($branding['accent_color']); ?></span>
</div>
</div>
<!-- Vorschau -->
<div style="margin-top: 1rem; padding: 1rem; border-radius: 0.5rem;
background: linear-gradient(135deg, <?php echo htmlspecialchars($branding['primary_color']); ?> 0%, <?php echo htmlspecialchars($branding['secondary_color']); ?> 100%);">
<span style="color: white; font-weight: bold;">Farbvorschau</span>
</div>
</div>
</div>
</div>
<!-- Texte -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Willkommenstexte</h3>
</div>
<div class="card-body">
<div class="grid grid-2">
<div class="form-group">
<label class="form-label" for="welcome_text_de">Willkommenstext (Deutsch)</label>
<textarea id="welcome_text_de" name="welcome_text_de" class="form-textarea"
placeholder="Willkommen bei unserer Livecam..."><?php echo htmlspecialchars($branding['welcome_text_de']); ?></textarea>
</div>
<div class="form-group">
<label class="form-label" for="welcome_text_en">Welcome Text (English)</label>
<textarea id="welcome_text_en" name="welcome_text_en" class="form-textarea"
placeholder="Welcome to our livecam..."><?php echo htmlspecialchars($branding['welcome_text_en']); ?></textarea>
</div>
</div>
<div class="form-group">
<label class="form-label" for="footer_text">Footer Text</label>
<input type="text" id="footer_text" name="footer_text" class="form-input"
value="<?php echo htmlspecialchars($branding['footer_text']); ?>"
placeholder="© 2024 Ihre Livecam">
</div>
</div>
</div>
<!-- Custom CSS -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Eigenes CSS</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="form-label" for="custom_css">Custom CSS (optional)</label>
<textarea id="custom_css" name="custom_css" class="form-textarea"
style="font-family: monospace; min-height: 150px;"
placeholder="/* Eigene CSS-Regeln hier */"><?php echo htmlspecialchars($branding['custom_css']); ?></textarea>
<p class="form-help">Fortgeschrittene Benutzer können hier eigene CSS-Regeln hinzufügen.</p>
</div>
</div>
</div>
<div style="margin-top: 1.5rem;">
<button type="submit" class="btn btn-primary">
Branding speichern
</button>
</div>
</form>
<script>
// Color picker update
document.querySelectorAll('.color-picker').forEach(picker => {
picker.addEventListener('input', (e) => {
e.target.parentNode.querySelector('.color-value').textContent = e.target.value;
});
});
</script>
<?php
$content = ob_get_clean();
include __DIR__ . '/templates/layout.php';
+147
View File
@@ -0,0 +1,147 @@
<?php
/**
* Dashboard - Übersicht
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Core\Database;
use AuroraLivecam\Core\TenantResolver;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
// Login erforderlich
$auth->requireLogin();
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
// Stats laden
$stats = [
'viewers_current' => 0,
'viewers_today' => 0,
'viewers_peak' => 0,
'stream_status' => 'unknown',
];
// Versuche Stats aus DB zu laden
try {
$db = Database::getInstance();
if ($tenantId > 0) {
// Aktuelle Zuschauer (vereinfacht)
$viewerFile = dirname(__DIR__) . '/active_viewers.json';
if (file_exists($viewerFile)) {
$viewers = json_decode(file_get_contents($viewerFile), true);
$stats['viewers_current'] = count($viewers ?? []);
}
// Heute Stats
$todayStats = $db->fetchOne(
"SELECT SUM(viewer_count) as total, MAX(viewer_count) as peak
FROM viewer_stats
WHERE tenant_id = ? AND DATE(recorded_at) = CURDATE()",
[$tenantId]
);
if ($todayStats) {
$stats['viewers_today'] = $todayStats['total'] ?? 0;
$stats['viewers_peak'] = $todayStats['peak'] ?? 0;
}
// Stream Status
$stream = $db->fetchOne(
"SELECT last_status FROM tenant_streams WHERE tenant_id = ? AND is_primary = 1",
[$tenantId]
);
$stats['stream_status'] = $stream['last_status'] ?? 'unknown';
}
} catch (\Exception $e) {
// DB nicht verfügbar - Legacy-Modus
$viewerFile = dirname(__DIR__) . '/active_viewers.json';
if (file_exists($viewerFile)) {
$viewers = json_decode(file_get_contents($viewerFile), true);
$stats['viewers_current'] = count($viewers ?? []);
}
}
// Page Setup
$pageTitle = 'Übersicht';
$currentPage = 'overview';
ob_start();
?>
<!-- Stats Grid -->
<div class="stats-grid">
<div class="stat-card">
<div class="stat-icon">👥</div>
<div class="stat-value"><?php echo $stats['viewers_current']; ?></div>
<div class="stat-label">Aktuelle Zuschauer</div>
</div>
<div class="stat-card">
<div class="stat-icon">📊</div>
<div class="stat-value"><?php echo $stats['viewers_today']; ?></div>
<div class="stat-label">Zuschauer heute</div>
</div>
<div class="stat-card">
<div class="stat-icon">🏆</div>
<div class="stat-value"><?php echo $stats['viewers_peak']; ?></div>
<div class="stat-label">Peak heute</div>
</div>
<div class="stat-card">
<div class="stat-icon">
<?php echo $stats['stream_status'] === 'online' ? '🟢' : ($stats['stream_status'] === 'offline' ? '🔴' : '⚪'); ?>
</div>
<div class="stat-value" style="font-size: 1.25rem; text-transform: capitalize;">
<?php echo $stats['stream_status'] === 'online' ? 'Online' : ($stats['stream_status'] === 'offline' ? 'Offline' : 'Unbekannt'); ?>
</div>
<div class="stat-label">Stream Status</div>
</div>
</div>
<!-- Quick Actions -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Schnellzugriff</h3>
</div>
<div class="card-body">
<div class="grid grid-3">
<a href="/dashboard/stream.php" class="btn btn-secondary">
📹 Stream bearbeiten
</a>
<a href="/dashboard/branding.php" class="btn btn-secondary">
🎨 Branding anpassen
</a>
<a href="/dashboard/settings.php" class="btn btn-secondary">
⚙️ Einstellungen
</a>
</div>
</div>
</div>
<!-- Recent Activity (Platzhalter) -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Letzte Aktivitäten</h3>
</div>
<div class="card-body">
<p style="color: var(--gray-500); text-align: center; padding: 2rem;">
Aktivitäten werden hier angezeigt, sobald Analytics aktiviert ist.
</p>
</div>
</div>
<?php
$content = ob_get_clean();
include __DIR__ . '/templates/layout.php';
+102
View File
@@ -0,0 +1,102 @@
<?php
/**
* Dashboard Login
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
$settingsManager = new SettingsManager();
// Prüfe ob Dashboard aktiviert ist
if (!$settingsManager->isTenantDashboardEnabled() && !$settingsManager->isMultiTenantEnabled()) {
// Fallback auf Legacy-Admin
header('Location: /?admin=1');
exit;
}
$auth = new AuthManager();
// Bereits eingeloggt?
if ($auth->isLoggedIn()) {
header('Location: /dashboard/');
exit;
}
$error = '';
// Login verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$remember = isset($_POST['remember']);
if ($auth->login($email, $password, $remember)) {
header('Location: /dashboard/');
exit;
} else {
$error = 'Ungültige Anmeldedaten';
}
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Dashboard</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
</head>
<body>
<div class="login-container">
<div class="login-box">
<div class="login-title">
<h1>Dashboard Login</h1>
<p>Melden Sie sich an, um fortzufahren</p>
</div>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="POST" action="">
<div class="form-group">
<label class="form-label" for="email">E-Mail / Benutzername</label>
<input type="text" id="email" name="email" class="form-input"
value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>"
required autofocus>
</div>
<div class="form-group">
<label class="form-label" for="password">Passwort</label>
<input type="password" id="password" name="password" class="form-input" required>
</div>
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="remember">
<span class="toggle-slider"></span>
</span>
<span>Angemeldet bleiben</span>
</label>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">
Anmelden
</button>
</form>
<p style="text-align: center; margin-top: 1.5rem; color: var(--gray-500);">
<a href="/" style="color: var(--primary);">Zurück zur Livecam</a>
</p>
</div>
</div>
</body>
</html>
+18
View File
@@ -0,0 +1,18 @@
<?php
/**
* Dashboard Logout
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
$auth = new AuthManager();
$auth->logout();
header('Location: /dashboard/login.php');
exit;
+271
View File
@@ -0,0 +1,271 @@
<?php
/**
* Dashboard - Einstellungen
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Tenant\TenantSettingsManager;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
$auth->requireLogin();
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$flashMessage = null;
$flashType = 'info';
// Tenant-Settings laden
try {
$tenantSettings = new TenantSettingsManager($tenantId);
} catch (\Exception $e) {
$tenantSettings = null;
}
// Einstellungen für das Template
$settings = [
'viewer_display_enabled' => $settingsManager->get('viewer_display.enabled') ?? true,
'viewer_min' => $settingsManager->get('viewer_display.min_viewers') ?? 1,
'weather_enabled' => $settingsManager->get('weather.enabled') ?? true,
'weather_location' => $settingsManager->get('weather.location') ?? 'Zürich,CH',
'weather_lat' => $settingsManager->get('weather.lat') ?? '47.3769',
'weather_lon' => $settingsManager->get('weather.lon') ?? '8.5417',
'guestbook_enabled' => $settingsManager->get('content.guestbook_enabled') ?? true,
'gallery_enabled' => $settingsManager->get('content.gallery_enabled') ?? true,
'ai_events_enabled' => $settingsManager->get('content.ai_events_enabled') ?? true,
'show_qr_code' => $settingsManager->get('ui_display.show_qr_code') ?? true,
'show_social_media' => $settingsManager->get('ui_display.show_social_media') ?? true,
'timelapse_reverse' => $settingsManager->get('zoom_timelapse.timelapse_reverse_enabled') ?? true,
'max_zoom' => $settingsManager->get('zoom_timelapse.max_zoom_level') ?? 4.0,
];
// Formular verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$updates = [
'viewer_display.enabled' => isset($_POST['viewer_display_enabled']),
'viewer_display.min_viewers' => (int)($_POST['viewer_min'] ?? 1),
'weather.enabled' => isset($_POST['weather_enabled']),
'weather.location' => trim($_POST['weather_location'] ?? ''),
'weather.lat' => trim($_POST['weather_lat'] ?? ''),
'weather.lon' => trim($_POST['weather_lon'] ?? ''),
'content.guestbook_enabled' => isset($_POST['guestbook_enabled']),
'content.gallery_enabled' => isset($_POST['gallery_enabled']),
'content.ai_events_enabled' => isset($_POST['ai_events_enabled']),
'ui_display.show_qr_code' => isset($_POST['show_qr_code']),
'ui_display.show_social_media' => isset($_POST['show_social_media']),
'zoom_timelapse.timelapse_reverse_enabled' => isset($_POST['timelapse_reverse']),
'zoom_timelapse.max_zoom_level' => (float)($_POST['max_zoom'] ?? 4.0),
];
$success = true;
foreach ($updates as $key => $value) {
if (!$settingsManager->set($key, $value)) {
$success = false;
}
}
if ($success) {
$flashMessage = 'Einstellungen gespeichert!';
$flashType = 'success';
// Reload settings
$settings = [
'viewer_display_enabled' => $updates['viewer_display.enabled'],
'viewer_min' => $updates['viewer_display.min_viewers'],
'weather_enabled' => $updates['weather.enabled'],
'weather_location' => $updates['weather.location'],
'weather_lat' => $updates['weather.lat'],
'weather_lon' => $updates['weather.lon'],
'guestbook_enabled' => $updates['content.guestbook_enabled'],
'gallery_enabled' => $updates['content.gallery_enabled'],
'ai_events_enabled' => $updates['content.ai_events_enabled'],
'show_qr_code' => $updates['ui_display.show_qr_code'],
'show_social_media' => $updates['ui_display.show_social_media'],
'timelapse_reverse' => $updates['zoom_timelapse.timelapse_reverse_enabled'],
'max_zoom' => $updates['zoom_timelapse.max_zoom_level'],
];
} else {
$flashMessage = 'Fehler beim Speichern einiger Einstellungen.';
$flashType = 'error';
}
}
$pageTitle = 'Einstellungen';
$currentPage = 'settings';
ob_start();
?>
<form method="POST" action="">
<div class="grid grid-2">
<!-- Viewer-Anzeige -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Zuschauer-Anzeige</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="viewer_display_enabled"
<?php echo $settings['viewer_display_enabled'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>Zuschauer-Anzahl anzeigen</span>
</label>
</div>
<div class="form-group">
<label class="form-label" for="viewer_min">Mindestanzahl für Anzeige</label>
<input type="number" id="viewer_min" name="viewer_min" class="form-input"
value="<?php echo (int)$settings['viewer_min']; ?>" min="0" max="100">
<p class="form-help">Zuschauer werden erst ab dieser Anzahl angezeigt</p>
</div>
</div>
</div>
<!-- Wetter-Widget -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Wetter-Widget</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="weather_enabled"
<?php echo $settings['weather_enabled'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>Wetter-Widget aktivieren</span>
</label>
</div>
<div class="form-group">
<label class="form-label" for="weather_location">Standort-Name</label>
<input type="text" id="weather_location" name="weather_location" class="form-input"
value="<?php echo htmlspecialchars($settings['weather_location']); ?>">
</div>
<div class="grid grid-2">
<div class="form-group">
<label class="form-label" for="weather_lat">Breitengrad</label>
<input type="text" id="weather_lat" name="weather_lat" class="form-input"
value="<?php echo htmlspecialchars($settings['weather_lat']); ?>">
</div>
<div class="form-group">
<label class="form-label" for="weather_lon">Längengrad</label>
<input type="text" id="weather_lon" name="weather_lon" class="form-input"
value="<?php echo htmlspecialchars($settings['weather_lon']); ?>">
</div>
</div>
</div>
</div>
<!-- Content -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Inhalte</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="guestbook_enabled"
<?php echo $settings['guestbook_enabled'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>Gästebuch aktivieren</span>
</label>
</div>
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="gallery_enabled"
<?php echo $settings['gallery_enabled'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>Galerie aktivieren</span>
</label>
</div>
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="ai_events_enabled"
<?php echo $settings['ai_events_enabled'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>AI-Events aktivieren</span>
</label>
</div>
</div>
</div>
<!-- UI -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Oberfläche</h3>
</div>
<div class="card-body">
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="show_qr_code"
<?php echo $settings['show_qr_code'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>QR-Code anzeigen</span>
</label>
</div>
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="show_social_media"
<?php echo $settings['show_social_media'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>Social Media Links anzeigen</span>
</label>
</div>
<div class="form-group">
<label class="toggle-wrapper">
<span class="toggle">
<input type="checkbox" name="timelapse_reverse"
<?php echo $settings['timelapse_reverse'] ? 'checked' : ''; ?>>
<span class="toggle-slider"></span>
</span>
<span>Timelapse Rückwärts erlauben</span>
</label>
</div>
<div class="form-group">
<label class="form-label" for="max_zoom">Maximaler Zoom</label>
<input type="number" id="max_zoom" name="max_zoom" class="form-input"
value="<?php echo (float)$settings['max_zoom']; ?>" min="1" max="10" step="0.5">
</div>
</div>
</div>
</div>
<div style="margin-top: 1.5rem;">
<button type="submit" class="btn btn-primary">
Einstellungen speichern
</button>
</div>
</form>
<?php
$content = ob_get_clean();
include __DIR__ . '/templates/layout.php';
+183
View File
@@ -0,0 +1,183 @@
<?php
/**
* Dashboard - Stream Einstellungen
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Core\Database;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
$auth->requireLogin();
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$flashMessage = null;
$flashType = 'info';
// Stream-Daten laden
$stream = [
'stream_url' => '',
'stream_type' => 'hls',
'is_active' => true,
'last_status' => 'unknown',
];
try {
$db = Database::getInstance();
if ($tenantId > 0) {
$dbStream = $db->fetchOne(
"SELECT * FROM tenant_streams WHERE tenant_id = ? AND is_primary = 1",
[$tenantId]
);
if ($dbStream) {
$stream = $dbStream;
}
}
} catch (\Exception $e) {
// DB nicht verfügbar
}
// Formular verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$streamUrl = trim($_POST['stream_url'] ?? '');
$streamType = $_POST['stream_type'] ?? 'hls';
if (empty($streamUrl)) {
$flashMessage = 'Bitte geben Sie eine Stream-URL ein.';
$flashType = 'error';
} else {
try {
$db = Database::getInstance();
if ($tenantId > 0) {
// Prüfe ob Stream existiert
$existing = $db->fetchOne(
"SELECT id FROM tenant_streams WHERE tenant_id = ? AND is_primary = 1",
[$tenantId]
);
if ($existing) {
$db->update('tenant_streams', [
'stream_url' => $streamUrl,
'stream_type' => $streamType,
], 'id = ?', [$existing['id']]);
} else {
$db->insert('tenant_streams', [
'tenant_id' => $tenantId,
'stream_url' => $streamUrl,
'stream_type' => $streamType,
'is_primary' => 1,
]);
}
$flashMessage = 'Stream-Einstellungen gespeichert!';
$flashType = 'success';
// Reload stream data
$stream['stream_url'] = $streamUrl;
$stream['stream_type'] = $streamType;
} else {
$flashMessage = 'Stream-Einstellungen können im Legacy-Modus nicht gespeichert werden.';
$flashType = 'warning';
}
} catch (\Exception $e) {
$flashMessage = 'Fehler beim Speichern: ' . $e->getMessage();
$flashType = 'error';
}
}
}
$pageTitle = 'Stream Einstellungen';
$currentPage = 'stream';
ob_start();
?>
<div class="card">
<div class="card-header">
<h3 class="card-title">Stream Konfiguration</h3>
<span class="badge badge-<?php echo $stream['last_status'] === 'online' ? 'success' : ($stream['last_status'] === 'offline' ? 'danger' : 'info'); ?>">
<?php echo ucfirst($stream['last_status'] ?? 'Unbekannt'); ?>
</span>
</div>
<div class="card-body">
<form method="POST" action="">
<div class="form-group">
<label class="form-label" for="stream_url">Stream URL</label>
<input type="url" id="stream_url" name="stream_url" class="form-input"
value="<?php echo htmlspecialchars($stream['stream_url']); ?>"
placeholder="https://example.com/stream.m3u8">
<p class="form-help">Die URL zu Ihrem HLS-Stream (.m3u8) oder RTMP-Stream</p>
</div>
<div class="form-group">
<label class="form-label" for="stream_type">Stream Typ</label>
<select id="stream_type" name="stream_type" class="form-select">
<option value="hls" <?php echo ($stream['stream_type'] ?? 'hls') === 'hls' ? 'selected' : ''; ?>>
HLS (.m3u8)
</option>
<option value="rtmp" <?php echo ($stream['stream_type'] ?? '') === 'rtmp' ? 'selected' : ''; ?>>
RTMP
</option>
<option value="webrtc" <?php echo ($stream['stream_type'] ?? '') === 'webrtc' ? 'selected' : ''; ?>>
WebRTC
</option>
<option value="iframe" <?php echo ($stream['stream_type'] ?? '') === 'iframe' ? 'selected' : ''; ?>>
iFrame Embed
</option>
</select>
</div>
<button type="submit" class="btn btn-primary">
Speichern
</button>
</form>
</div>
</div>
<div class="card">
<div class="card-header">
<h3 class="card-title">Stream Vorschau</h3>
</div>
<div class="card-body">
<?php if (!empty($stream['stream_url'])): ?>
<div style="aspect-ratio: 16/9; background: #000; border-radius: 0.5rem; overflow: hidden;">
<video id="preview-player" controls style="width: 100%; height: 100%;">
<source src="<?php echo htmlspecialchars($stream['stream_url']); ?>" type="application/x-mpegURL">
</video>
</div>
<p class="form-help" style="margin-top: 1rem;">
Hinweis: Die Vorschau funktioniert nur mit HLS-Streams und wenn Ihr Browser HLS unterstützt.
</p>
<?php else: ?>
<div class="preview-box">
<p>Keine Stream-URL konfiguriert</p>
</div>
<?php endif; ?>
</div>
</div>
<div class="card">
<div class="card-header">
<h3 class="card-title">Stream Monitoring</h3>
</div>
<div class="card-body">
<p style="color: var(--gray-500);">
Stream-Monitoring zeigt automatische Verfügbarkeitsprüfungen an.
Diese Funktion wird demnächst verfügbar sein.
</p>
</div>
</div>
<?php
$content = ob_get_clean();
include __DIR__ . '/templates/layout.php';
@@ -0,0 +1,126 @@
<?php
/**
* Dashboard Layout Template
*
* Variablen:
* - $pageTitle: Seitentitel
* - $currentPage: Aktuelle Seite (für Navigation)
* - $content: Hauptinhalt
*/
$user = $auth->getUser();
$tenantName = $user['tenant_name'] ?? 'Dashboard';
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($pageTitle ?? 'Dashboard'); ?> - <?php echo htmlspecialchars($tenantName); ?></title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
</head>
<body>
<div class="dashboard-container">
<!-- Sidebar -->
<aside class="sidebar">
<div class="sidebar-header">
<h2><?php echo htmlspecialchars($tenantName); ?></h2>
<span class="role-badge"><?php echo htmlspecialchars($user['role'] ?? 'user'); ?></span>
</div>
<nav class="sidebar-nav">
<a href="/dashboard/" class="nav-item <?php echo ($currentPage ?? '') === 'overview' ? 'active' : ''; ?>">
<span class="nav-icon">📊</span>
<span>Übersicht</span>
</a>
<a href="/dashboard/stream.php" class="nav-item <?php echo ($currentPage ?? '') === 'stream' ? 'active' : ''; ?>">
<span class="nav-icon">📹</span>
<span>Stream</span>
</a>
<a href="/dashboard/branding.php" class="nav-item <?php echo ($currentPage ?? '') === 'branding' ? 'active' : ''; ?>">
<span class="nav-icon">🎨</span>
<span>Branding</span>
</a>
<a href="/dashboard/settings.php" class="nav-item <?php echo ($currentPage ?? '') === 'settings' ? 'active' : ''; ?>">
<span class="nav-icon">⚙️</span>
<span>Einstellungen</span>
</a>
<?php if ($settingsManager->isAnalyticsEnabled()): ?>
<a href="/dashboard/analytics.php" class="nav-item <?php echo ($currentPage ?? '') === 'analytics' ? 'active' : ''; ?>">
<span class="nav-icon">📈</span>
<span>Analytics</span>
</a>
<?php endif; ?>
<?php if ($settingsManager->isCustomDomainEnabled()): ?>
<a href="/dashboard/domains.php" class="nav-item <?php echo ($currentPage ?? '') === 'domains' ? 'active' : ''; ?>">
<span class="nav-icon">🌐</span>
<span>Domains</span>
</a>
<?php endif; ?>
<?php if ($settingsManager->isBillingEnabled()): ?>
<a href="/dashboard/billing.php" class="nav-item <?php echo ($currentPage ?? '') === 'billing' ? 'active' : ''; ?>">
<span class="nav-icon">💳</span>
<span>Abrechnung</span>
</a>
<?php endif; ?>
<?php if ($auth->isSuperAdmin()): ?>
<div class="nav-divider"></div>
<span class="nav-label">Admin</span>
<a href="/dashboard/admin/tenants.php" class="nav-item <?php echo ($currentPage ?? '') === 'admin-tenants' ? 'active' : ''; ?>">
<span class="nav-icon">👥</span>
<span>Kunden</span>
</a>
<a href="/dashboard/admin/plans.php" class="nav-item <?php echo ($currentPage ?? '') === 'admin-plans' ? 'active' : ''; ?>">
<span class="nav-icon">📋</span>
<span>Pläne</span>
</a>
<?php endif; ?>
</nav>
<div class="sidebar-footer">
<a href="/" class="nav-item" target="_blank">
<span class="nav-icon">🔗</span>
<span>Zur Livecam</span>
</a>
<a href="/dashboard/logout.php" class="nav-item logout">
<span class="nav-icon">🚪</span>
<span>Abmelden</span>
</a>
</div>
</aside>
<!-- Main Content -->
<main class="main-content">
<header class="main-header">
<h1><?php echo htmlspecialchars($pageTitle ?? 'Dashboard'); ?></h1>
<div class="header-actions">
<span class="user-info">
<?php echo htmlspecialchars($user['email'] ?? ''); ?>
</span>
</div>
</header>
<div class="content-wrapper">
<?php if (isset($flashMessage)): ?>
<div class="alert alert-<?php echo $flashType ?? 'info'; ?>">
<?php echo htmlspecialchars($flashMessage); ?>
</div>
<?php endif; ?>
<?php echo $content ?? ''; ?>
</div>
</main>
</div>
<script src="/dashboard/assets/dashboard.js"></script>
</body>
</html>
+205
View File
@@ -0,0 +1,205 @@
-- Aurora Livecam - Multi-Tenant SaaS Schema
-- Version: 1.0.0
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- --------------------------------------------------------
-- Subscription Plans
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `plans` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`slug` VARCHAR(50) UNIQUE NOT NULL,
`stripe_price_id` VARCHAR(100) NULL,
`price_monthly` DECIMAL(10,2) DEFAULT 0.00,
`price_yearly` DECIMAL(10,2) DEFAULT 0.00,
`features` JSON NULL COMMENT '{"max_viewers": 100, "storage_gb": 5, "custom_domain": true}',
`is_active` TINYINT(1) DEFAULT 1,
`sort_order` INT DEFAULT 0,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Default Plans
INSERT INTO `plans` (`name`, `slug`, `price_monthly`, `price_yearly`, `features`, `sort_order`) VALUES
('Free', 'free', 0.00, 0.00, '{"max_viewers": 10, "storage_gb": 0.5, "custom_domain": false, "weather_widget": true, "timelapse": false, "analytics": false, "branding": false}', 1),
('Basic', 'basic', 19.00, 190.00, '{"max_viewers": 50, "storage_gb": 5, "custom_domain": false, "weather_widget": true, "timelapse": true, "analytics": true, "branding": false}', 2),
('Professional', 'professional', 49.00, 490.00, '{"max_viewers": 200, "storage_gb": 20, "custom_domain": true, "weather_widget": true, "timelapse": true, "analytics": true, "branding": true}', 3),
('Enterprise', 'enterprise', 149.00, 1490.00, '{"max_viewers": -1, "storage_gb": 100, "custom_domain": true, "weather_widget": true, "timelapse": true, "analytics": true, "branding": true, "priority_support": true}', 4);
-- --------------------------------------------------------
-- Tenants (Customers)
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tenants` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`uuid` VARCHAR(36) UNIQUE NOT NULL,
`name` VARCHAR(255) NOT NULL,
`slug` VARCHAR(100) UNIQUE NOT NULL COMMENT 'URL-safe identifier, e.g. aurora, seecam',
`email` VARCHAR(255) NOT NULL,
`status` ENUM('trial', 'active', 'suspended', 'cancelled') DEFAULT 'trial',
`plan_id` INT UNSIGNED NULL,
`trial_ends_at` TIMESTAMP NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`plan_id`) REFERENCES `plans`(`id`) ON DELETE SET NULL,
INDEX `idx_status` (`status`),
INDEX `idx_slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Tenant Domains
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tenant_domains` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NOT NULL,
`domain` VARCHAR(255) UNIQUE NOT NULL,
`is_primary` TINYINT(1) DEFAULT 0,
`ssl_status` ENUM('pending', 'active', 'failed') DEFAULT 'pending',
`verified_at` TIMESTAMP NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE,
INDEX `idx_domain` (`domain`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Tenant Settings (replaces settings.json per tenant)
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tenant_settings` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NOT NULL,
`setting_key` VARCHAR(255) NOT NULL,
`setting_value` TEXT NULL,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `uk_tenant_key` (`tenant_id`, `setting_key`),
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Tenant Branding
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tenant_branding` (
`tenant_id` INT UNSIGNED PRIMARY KEY,
`site_name` VARCHAR(255) NULL,
`site_name_full` VARCHAR(255) NULL,
`tagline` VARCHAR(255) NULL,
`logo_path` VARCHAR(500) NULL,
`favicon_path` VARCHAR(500) NULL,
`primary_color` VARCHAR(7) DEFAULT '#667eea',
`secondary_color` VARCHAR(7) DEFAULT '#764ba2',
`accent_color` VARCHAR(7) DEFAULT '#f093fb',
`welcome_text_de` TEXT NULL,
`welcome_text_en` TEXT NULL,
`footer_text` TEXT NULL,
`custom_css` TEXT NULL,
`custom_js` TEXT NULL,
`social_facebook` VARCHAR(255) NULL,
`social_instagram` VARCHAR(255) NULL,
`social_youtube` VARCHAR(255) NULL,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Tenant Streams
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tenant_streams` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(255) DEFAULT 'Main Stream',
`stream_url` VARCHAR(500) NOT NULL,
`stream_type` ENUM('hls', 'rtmp', 'webrtc', 'iframe') DEFAULT 'hls',
`is_active` TINYINT(1) DEFAULT 1,
`is_primary` TINYINT(1) DEFAULT 1,
`last_check_at` TIMESTAMP NULL,
`last_status` ENUM('online', 'offline', 'error') NULL,
`error_message` VARCHAR(500) NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Users
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NULL COMMENT 'NULL = Super Admin',
`email` VARCHAR(255) UNIQUE NOT NULL,
`password_hash` VARCHAR(255) NOT NULL,
`name` VARCHAR(255) NULL,
`role` ENUM('super_admin', 'tenant_admin', 'tenant_user') NOT NULL DEFAULT 'tenant_user',
`email_verified_at` TIMESTAMP NULL,
`last_login_at` TIMESTAMP NULL,
`remember_token` VARCHAR(100) NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE,
INDEX `idx_email` (`email`),
INDEX `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Subscriptions
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `subscriptions` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NOT NULL,
`plan_id` INT UNSIGNED NOT NULL,
`stripe_subscription_id` VARCHAR(100) NULL,
`stripe_customer_id` VARCHAR(100) NULL,
`status` ENUM('trialing', 'active', 'past_due', 'canceled', 'unpaid', 'incomplete') DEFAULT 'trialing',
`current_period_start` TIMESTAMP NULL,
`current_period_end` TIMESTAMP NULL,
`canceled_at` TIMESTAMP NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`plan_id`) REFERENCES `plans`(`id`),
INDEX `idx_tenant` (`tenant_id`),
INDEX `idx_stripe_sub` (`stripe_subscription_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Invoices (Stripe cache)
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `invoices` (
`id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NOT NULL,
`stripe_invoice_id` VARCHAR(100) UNIQUE NULL,
`amount` DECIMAL(10,2) NOT NULL,
`currency` VARCHAR(3) DEFAULT 'CHF',
`status` VARCHAR(50) NULL,
`paid_at` TIMESTAMP NULL,
`invoice_pdf_url` VARCHAR(500) NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Viewer Statistics
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `viewer_stats` (
`id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`tenant_id` INT UNSIGNED NOT NULL,
`recorded_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`viewer_count` INT DEFAULT 0,
`unique_sessions` INT DEFAULT 0,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE,
INDEX `idx_tenant_time` (`tenant_id`, `recorded_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
-- Onboarding Progress
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tenant_onboarding` (
`tenant_id` INT UNSIGNED PRIMARY KEY,
`current_step` INT DEFAULT 1,
`stream_verified` TINYINT(1) DEFAULT 0,
`branding_configured` TINYINT(1) DEFAULT 0,
`payment_configured` TINYINT(1) DEFAULT 0,
`completed_at` TIMESTAMP NULL,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SET FOREIGN_KEY_CHECKS = 1;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+721
View File
@@ -0,0 +1,721 @@
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require __DIR__ . '/vendor/autoload.php';
require_once 'SettingsManager.php';
// SettingsManager initialisieren
$settingsManager = new SettingsManager();
// AJAX-Handler für Settings (MUSS ganz am Anfang sein!)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['settings_action'])) {
header('Content-Type: application/json');
switch ($_POST['settings_action']) {
case 'get':
echo json_encode(['success' => true, 'settings' => $settingsManager->get()]);
exit;
case 'update':
$key = $_POST['key'] ?? null;
$value = $_POST['value'] ?? null;
if ($value === 'true') $value = true;
if ($value === 'false') $value = false;
if (is_numeric($value)) $value = intval($value);
if ($key && $settingsManager->set($key, $value)) {
echo json_encode(['success' => true, 'message' => 'Gespeichert']);
} else {
echo json_encode(['success' => false, 'message' => 'Fehler']);
}
exit;
}
}
if (isset($_GET['download_video'])) {
$videoDir = './videos/';
$latestVideo = null;
$latestTime = 0;
foreach (glob($videoDir . '*.mp4') as $video) {
$mtime = filemtime($video);
if ($mtime > $latestTime) { $latestTime = $mtime; $latestVideo = $video; }
}
if ($latestVideo) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($latestVideo).'"');
header('Content-Length: ' . filesize($latestVideo));
readfile($latestVideo);
exit;
}
echo "Kein Video gefunden.";
exit;
}
$oldDomains = ['www.aurora-wetter-lifecam.ch', 'www.aurora-wetter-livecam.ch'];
$newDomain = 'www.aurora-weather-livecam.com';
if (in_array($_SERVER['HTTP_HOST'] ?? '', $oldDomains)) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $protocol . '://' . $newDomain . $_SERVER['REQUEST_URI']);
exit;
}
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 0);
$imageDir = "./image";
$imageFiles = glob("$imageDir/screenshot_*.jpg");
if ($imageFiles) rsort($imageFiles);
$imageFilesJson = json_encode($imageFiles ?: []);
class ViewerCounter {
private $file = 'active_viewers.json';
private $timeout = 30;
public function handleHeartbeat() {
$ip = md5($_SERVER['REMOTE_ADDR'] . ($_SERVER['HTTP_USER_AGENT'] ?? ''));
$now = time();
$viewers = file_exists($this->file) ? json_decode(file_get_contents($this->file), true) ?? [] : [];
$viewers[$ip] = $now;
$active = [];
foreach ($viewers as $u => $t) { if ($now - $t < $this->timeout) $active[$u] = $t; }
file_put_contents($this->file, json_encode($active));
header('Content-Type: application/json');
echo json_encode(['count' => count($active)]);
exit;
}
public function getInitialCount() {
if (file_exists($this->file)) {
return max(1, count(json_decode(file_get_contents($this->file), true) ?? []));
}
return 1;
}
}
$viewerCounter = new ViewerCounter();
class WebcamManager {
private $videoSrc = 'test_video.m3u8';
public function displayWebcam() {
return '<video id="webcam-player" autoplay muted playsinline></video>';
}
public function displayStreamStats() {
return '<div class="info-badge tech-stat" id="bitrate-display" style="display:none;">
<i class="fas fa-tachometer-alt"></i> <span id="bitrate-value">0.00</span> MBit/s
</div>';
}
public function getImageFiles() {
$f = glob("image/screenshot_*.jpg");
if ($f) rsort($f);
return json_encode($f ?: []);
}
public function getJavaScript() {
return "
document.addEventListener('DOMContentLoaded', function () {
var video = document.getElementById('webcam-player');
var videoSrc = '{$this->videoSrc}';
if(video && typeof Hls !== 'undefined' && Hls.isSupported()) {
var hls = new Hls();
hls.loadSource(videoSrc);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, function () { video.play().catch(()=>{}); });
} else if (video) {
video.src = videoSrc;
video.play().catch(()=>{});
}
});";
}
}
class VisualCalendarManager {
private $videoDir, $settingsManager;
private $months = [1=>'Jan',2=>'Feb',3=>'Mär',4=>'Apr',5=>'Mai',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Okt',11=>'Nov',12=>'Dez'];
public function __construct($videoDir = './videos/', $sm = null) {
$this->videoDir = $videoDir;
$this->settingsManager = $sm;
}
public function hasVideosForDate($y, $m, $d) {
return count(glob($this->videoDir . sprintf("daily_video_%04d%02d%02d_*.mp4", $y, $m, $d))) > 0;
}
public function getVideosForDate($y, $m, $d) {
$vids = [];
foreach (glob($this->videoDir . sprintf("daily_video_%04d%02d%02d_*.mp4", $y, $m, $d)) as $v) {
$vids[] = ['path' => $v, 'name' => basename($v), 'size' => filesize($v), 'time' => date('H:i', filemtime($v))];
}
return $vids;
}
public function displayVisualCalendar() {
$cy = isset($_GET['cal_year']) ? intval($_GET['cal_year']) : date('Y');
$cm = isset($_GET['cal_month']) ? intval($_GET['cal_month']) : date('n');
$sd = isset($_GET['cal_day']) ? intval($_GET['cal_day']) : null;
$pip = $this->settingsManager ? $this->settingsManager->get('video_mode.play_in_player') : true;
$dl = $this->settingsManager ? $this->settingsManager->get('video_mode.allow_download') : true;
$o = '<div class="calendar-box">';
$o .= '<div class="cal-nav"><button onclick="chgM('.$cy.','.($cm-1).')">&laquo;</button><span>'.$this->months[$cm].' '.$cy.'</span><button onclick="chgM('.$cy.','.($cm+1).')">&raquo;</button></div>';
$o .= '<div class="cal-grid">';
foreach(['Mo','Di','Mi','Do','Fr','Sa','So'] as $wd) $o .= '<div class="cal-hd">'.$wd.'</div>';
$fd = mktime(0,0,0,$cm,1,$cy);
$dim = date('t', $fd);
$dow = date('N', $fd) - 1;
for ($i=0; $i<$dow; $i++) $o .= '<div class="cal-day empty"></div>';
for ($d=1; $d<=$dim; $d++) {
$hv = $this->hasVideosForDate($cy,$cm,$d);
$sel = $sd==$d;
$td = ($cy==date('Y') && $cm==date('n') && $d==date('j'));
$cls = 'cal-day' . ($hv?' has-vid':'') . ($sel?' sel':'') . ($td?' today':'');
$o .= '<div class="'.$cls.'" onclick="selD('.$cy.','.$cm.','.$d.')"><span>'.$d.'</span>'.($hv?'<small>📹</small>':'').'</div>';
}
$o .= '</div>';
if ($sd) {
$vids = $this->getVideosForDate($cy,$cm,$sd);
$o .= '<div class="day-vids"><h4>📅 '.sprintf('%02d.%02d.%04d',$sd,$cm,$cy).'</h4>';
if ($vids) {
$o .= '<ul>';
foreach ($vids as $v) {
$sz = round($v['size']/1024/1024,1);
$tk = hash_hmac('sha256', $v['path'], session_id());
$o .= '<li><span>🕐 '.$v['time'].'</span><span>'.$sz.' MB</span><span class="vid-btns">';
if ($pip) $o .= '<a href="#" onclick="playVid(\''.htmlspecialchars($v['path']).'\');return false;" class="btn-play">▶️</a>';
if ($dl) $o .= '<a href="?download_specific_video='.urlencode($v['path']).'&token='.$tk.'" class="btn-dl">⬇️</a>';
$o .= '</span></li>';
}
$o .= '</ul>';
} else {
$o .= '<p>Keine Videos.</p>';
}
$o .= '</div>';
}
$o .= '</div>';
return $o;
}
}
class GuestbookManager {
private $entries = [], $file = 'guestbook.json';
public function __construct() { if (file_exists($this->file)) $this->entries = json_decode(file_get_contents($this->file), true) ?? []; }
public function handleFormSubmission() {
if (isset($_POST['guestbook'],$_POST['guest-name'],$_POST['guest-message'])) {
$this->entries[] = ['name'=>htmlspecialchars($_POST['guest-name']),'message'=>htmlspecialchars($_POST['guest-message']),'date'=>date('Y-m-d H:i:s')];
file_put_contents($this->file, json_encode($this->entries));
}
}
public function deleteEntry($i) { if (isset($this->entries[$i])) { unset($this->entries[$i]); $this->entries = array_values($this->entries); file_put_contents($this->file, json_encode($this->entries)); return true; } return false; }
public function displayForm() { return '<form method="post"><input type="hidden" name="guestbook" value="1"><label>Name:</label><input name="guest-name" required><label>Nachricht:</label><textarea name="guest-message" required></textarea><button type="submit">Senden</button></form>'; }
public function displayEntries($admin=false) {
$o = '<div class="gb-entries">';
foreach ($this->entries as $i=>$e) {
$o .= '<div class="gb-entry"><h4>'.$e['name'].'</h4><p>'.$e['message'].'</p><small>'.$e['date'].'</small>';
if ($admin) $o .= '<form method="post" style="display:inline"><input type="hidden" name="action" value="delete_guestbook"><input type="hidden" name="delete_entry" value="'.$i.'"><button class="del-btn">X</button></form>';
$o .= '</div>';
}
return $o.'</div>';
}
}
class ContactManager {
private $file = 'feedbacks.json';
public function displayForm() { return '<form method="post" id="contact-form"><input type="hidden" name="contact" value="1"><label>Name:</label><input name="name" required><label>E-Mail:</label><input type="email" name="email" required><label>Nachricht:</label><textarea name="message" required></textarea><button type="submit">Senden</button></form><div id="contact-fb"></div>'; }
public function handleSubmission($n,$e,$m) {
if (!$n||!$e||!$m) return ['success'=>false,'message'=>'Alle Felder ausfüllen'];
$fb = ['name'=>htmlspecialchars($n),'email'=>filter_var($e,FILTER_SANITIZE_EMAIL),'message'=>htmlspecialchars($m),'date'=>date('Y-m-d H:i:s'),'ip'=>$_SERVER['REMOTE_ADDR']??''];
$all = file_exists($this->file) ? json_decode(file_get_contents($this->file),true) : [];
$all[] = $fb;
file_put_contents($this->file, json_encode($all, JSON_PRETTY_PRINT));
return ['success'=>true,'message'=>'Nachricht gesendet!'];
}
public function deleteFeedback($i) { $all = json_decode(file_get_contents($this->file),true); if (isset($all[$i])) { unset($all[$i]); file_put_contents($this->file, json_encode(array_values($all),JSON_PRETTY_PRINT)); return true; } return false; }
}
class AdminManager {
public function isAdmin() { return isset($_SESSION['admin']) && $_SESSION['admin'] === true; }
public function handleLogin($u,$p) { if ($u==='admin' && $p==='sonne4000$$$$Q') { $_SESSION['admin']=true; return true; } return false; }
public function displayLoginForm() { return '<form method="post"><input type="hidden" name="admin-login" value="1"><label>User:</label><input name="username" required><label>Pass:</label><input type="password" name="password" required><button type="submit">Login</button></form>'; }
public function displayAdminContent() {
global $settingsManager;
$o = '<div class="admin-panel">';
$o .= '<h3>⚙️ Einstellungen</h3>';
$o .= '<div class="setting"><label>Zuschauer anzeigen</label><input type="checkbox" id="s-viewer" '.($settingsManager->get('viewer_display.enabled')?'checked':'').'></div>';
$o .= '<div class="setting"><label>Mindestanzahl</label><input type="number" id="s-min" value="'.$settingsManager->get('viewer_display.min_viewers').'" min="1" max="100"></div>';
$o .= '<div class="setting"><label>Im Player abspielen</label><input type="checkbox" id="s-play" '.($settingsManager->get('video_mode.play_in_player')?'checked':'').'></div>';
$o .= '<div class="setting"><label>Download erlauben</label><input type="checkbox" id="s-dl" '.($settingsManager->get('video_mode.allow_download')?'checked':'').'></div>';
$o .= '</div>';
$o .= '<div class="admin-panel"><h3>📩 Nachrichten</h3>';
$msgs = file_exists('feedbacks.json') ? json_decode(file_get_contents('feedbacks.json'),true) : [];
foreach ($msgs as $i=>$m) {
$o .= '<div class="msg"><strong>'.$m['name'].'</strong> ('.$m['email'].')<p>'.$m['message'].'</p><small>'.$m['date'].'</small>';
$o .= '<form method="post" style="display:inline"><input type="hidden" name="action" value="delete_feedback"><input type="hidden" name="delete_index" value="'.$i.'"><button class="del-btn">X</button></form></div>';
}
if (!$msgs) $o .= '<p>Keine Nachrichten.</p>';
$o .= '</div>';
return $o;
}
public function displayGalleryImages() {
$o = '<div class="gallery">';
foreach (glob("uploads/*.{jpg,jpeg,png,gif}",GLOB_BRACE) as $f) $o .= '<img src="'.$f.'" onclick="openImg(this.src)">';
return $o.'</div>';
}
}
class VideoArchiveManager {
private $dir;
public function __construct($d='./videos/') { $this->dir = $d; }
public function handleSpecificVideoDownload() {
if (isset($_GET['download_specific_video'],$_GET['token'])) {
$p = $_GET['download_specific_video'];
if (!hash_equals(hash_hmac('sha256',$p,session_id()), $_GET['token'])) { echo "Invalid"; exit; }
$rp = realpath($p);
$rd = realpath($this->dir);
if ($rp && strpos($rp,$rd)===0 && file_exists($rp)) {
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename="'.basename($rp).'"');
header('Content-Length: '.filesize($rp));
readfile($rp);
exit;
}
echo "Not found"; exit;
}
}
}
$webcamManager = new WebcamManager();
$guestbookManager = new GuestbookManager();
$contactManager = new ContactManager();
$adminManager = new AdminManager();
$videoArchiveManager = new VideoArchiveManager('./videos/');
$videoArchiveManager->handleSpecificVideoDownload();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action']) && $_POST['action'] === 'viewer_heartbeat') $viewerCounter->handleHeartbeat();
if (isset($_POST['guestbook'])) { $guestbookManager->handleFormSubmission(); header("Location: ".$_SERVER['PHP_SELF']."#guestbook"); exit; }
if (isset($_POST['contact'])) {
$r = $contactManager->handleSubmission($_POST['name'],$_POST['email'],$_POST['message']);
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { header('Content-Type: application/json'); echo json_encode($r); exit; }
header('Location: '.$_SERVER['PHP_SELF'].'#kontakt'); exit;
}
if (isset($_POST['admin-login'])) { $adminManager->handleLogin($_POST['username'],$_POST['password']); header('Location: '.$_SERVER['PHP_SELF'].'#admin'); exit; }
if ($adminManager->isAdmin()) {
if (isset($_POST['action']) && $_POST['action']==='delete_guestbook') { $guestbookManager->deleteEntry(intval($_POST['delete_entry'])); header("Location: ".$_SERVER['PHP_SELF']."#guestbook"); exit; }
if (isset($_POST['action']) && $_POST['action']==='delete_feedback') { $contactManager->deleteFeedback(intval($_POST['delete_index'])); header("Location: ".$_SERVER['PHP_SELF']."#admin"); exit; }
}
}
$vc = $viewerCounter->getInitialCount();
$sv = $settingsManager->get('viewer_display.enabled') && $vc >= $settingsManager->get('viewer_display.min_viewers');
$mv = $settingsManager->get('viewer_display.min_viewers');
?><!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=5,user-scalable=yes">
<title>Aurora Livecam</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:Arial,sans-serif;background:#f0f0f0;color:#333;line-height:1.6}
.container{max-width:1100px;margin:0 auto;padding:0 15px}
.section{padding:50px 0;background:#fff;margin-bottom:15px}
.section h2{text-align:center;margin-bottom:25px;font-size:28px}
header{background:#fff;padding:12px 0;position:sticky;top:0;z-index:100;box-shadow:0 2px 8px rgba(0,0,0,0.1)}
.header-inner{display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:10px}
.logo img{height:45px}
nav ul{list-style:none;display:flex;flex-wrap:wrap;gap:5px}
nav a{text-decoration:none;color:#333;padding:8px 14px;border-radius:5px;font-weight:bold;transition:.3s}
nav a:hover{background:#4CAF50;color:#fff}
.hero{text-align:center;padding:40px 15px;background:linear-gradient(135deg,#667eea,#764ba2);color:#fff}
.hero h1{font-size:2em;margin-bottom:10px}
.video-box{max-width:900px;margin:0 auto 20px}
.video-wrap{position:relative;padding-bottom:56.25%;background:#000;border-radius:10px;overflow:hidden}
.video-wrap video,.video-wrap img,.video-wrap #dvp{position:absolute;top:0;left:0;width:100%;height:100%;object-fit:contain}
#tlv,#dvp{display:none;background:#000}
#dvp video{width:100%;height:100%}
.zoom-btns{position:absolute;bottom:15px;right:15px;display:flex;gap:8px;z-index:100}
.zoom-btns button{width:44px;height:44px;border:none;border-radius:50%;background:rgba(255,255,255,.95);font-size:20px;cursor:pointer;box-shadow:0 2px 8px rgba(0,0,0,.3);transition:.2s}
.zoom-btns button:hover{transform:scale(1.1);background:#fff}
.info-bar{display:flex;justify-content:center;gap:15px;margin:15px 0;flex-wrap:wrap}
.badge{background:#fff;padding:8px 18px;border-radius:25px;font-weight:bold;display:flex;align-items:center;gap:8px;box-shadow:0 2px 8px rgba(0,0,0,.1)}
.badge.live{background:#fff5f5;color:#d32f2f}
.dot{width:8px;height:8px;background:#f44;border-radius:50%;animation:pulse 2s infinite}
@keyframes pulse{0%,100%{box-shadow:0 0 0 0 rgba(244,67,54,.6)}50%{box-shadow:0 0 0 8px transparent}}
.btns{display:flex;justify-content:center;gap:10px;flex-wrap:wrap;margin:15px 0}
.btn{padding:10px 20px;background:linear-gradient(135deg,#4CAF50,#45a049);color:#fff;border:none;border-radius:6px;font-weight:bold;cursor:pointer;text-decoration:none;transition:.3s}
.btn:hover{transform:translateY(-2px);box-shadow:0 4px 12px rgba(76,175,80,.4)}
.btn.purple{background:linear-gradient(135deg,#667eea,#764ba2)}
#tl-ctrl{display:none;background:#fff;padding:12px 20px;border-radius:30px;margin:15px auto;max-width:700px;box-shadow:0 3px 10px rgba(0,0,0,.1)}
.tl-bar{display:flex;align-items:center;gap:12px;flex-wrap:wrap;justify-content:center}
.tl-btn{width:40px;height:40px;border:none;border-radius:50%;background:linear-gradient(135deg,#667eea,#764ba2);color:#fff;cursor:pointer;font-size:14px}
.tl-btn.on{background:linear-gradient(135deg,#4CAF50,#45a049)}
.tl-btn.wide{width:auto;padding:0 15px;border-radius:20px}
#tl-slider{flex:1;min-width:120px;max-width:250px}
#tl-time{font-family:monospace;background:#f5f5f5;padding:6px 12px;border-radius:15px}
#back-live{display:none}
.calendar-box{max-width:700px;margin:0 auto;background:#fff;border-radius:10px;padding:20px;box-shadow:0 3px 15px rgba(0,0,0,.1)}
.cal-nav{display:flex;justify-content:space-between;align-items:center;background:linear-gradient(135deg,#667eea,#764ba2);color:#fff;padding:12px 15px;border-radius:8px;margin-bottom:15px}
.cal-nav button{background:rgba(255,255,255,.2);border:none;color:#fff;padding:8px 15px;border-radius:5px;font-size:18px;cursor:pointer}
.cal-grid{display:grid;grid-template-columns:repeat(7,1fr);gap:5px}
.cal-hd{text-align:center;font-weight:bold;padding:8px;background:#f5f5f5;border-radius:4px;font-size:12px}
.cal-day{aspect-ratio:1;display:flex;flex-direction:column;align-items:center;justify-content:center;background:#fff;border:2px solid #e0e0e0;border-radius:8px;cursor:pointer;transition:.2s;position:relative;font-size:14px}
.cal-day:hover:not(.empty){transform:scale(1.05);border-color:#667eea}
.cal-day.empty{background:transparent;border:none;cursor:default}
.cal-day.has-vid{background:linear-gradient(135deg,#e3f2fd,#bbdefb);border-color:#2196F3}
.cal-day.sel{background:linear-gradient(135deg,#667eea,#764ba2);color:#fff;transform:scale(1.08)}
.cal-day.today{border:2px solid #4CAF50}
.cal-day small{position:absolute;bottom:2px;right:2px;font-size:10px}
.day-vids{background:#f9f9f9;border-radius:8px;padding:15px;margin-top:15px}
.day-vids h4{margin-bottom:10px;border-bottom:2px solid #667eea;padding-bottom:8px}
.day-vids ul{list-style:none}
.day-vids li{display:flex;justify-content:space-between;align-items:center;padding:10px;background:#fff;margin-bottom:8px;border-radius:6px;flex-wrap:wrap;gap:8px}
.vid-btns{display:flex;gap:8px}
.btn-play,.btn-dl{padding:6px 12px;border-radius:15px;text-decoration:none;color:#fff;font-size:13px}
.btn-play{background:linear-gradient(135deg,#667eea,#764ba2)}
.btn-dl{background:linear-gradient(135deg,#4CAF50,#45a049)}
form{display:grid;gap:12px;background:#f9f9f9;padding:20px;border-radius:8px;max-width:500px;margin:0 auto}
input,textarea{width:100%;padding:10px;border:2px solid #ddd;border-radius:6px;font-size:15px}
input:focus,textarea:focus{border-color:#667eea;outline:none}
button[type=submit]{padding:10px 20px;background:linear-gradient(135deg,#4CAF50,#45a049);color:#fff;border:none;border-radius:6px;font-weight:bold;cursor:pointer}
.gb-entries{max-width:600px;margin:20px auto 0}
.gb-entry{background:#fff;border-left:4px solid #4CAF50;padding:15px;margin-bottom:10px;border-radius:6px;box-shadow:0 2px 6px rgba(0,0,0,.08)}
.gb-entry h4{margin-bottom:5px}
.gb-entry small{color:#888}
.gallery{display:flex;gap:10px;overflow-x:auto;padding:10px 0}
.gallery img{width:200px;height:140px;object-fit:cover;border-radius:8px;cursor:pointer;flex-shrink:0}
.admin-panel{background:#fff;padding:20px;border-radius:10px;margin-bottom:20px}
.admin-panel h3{margin-bottom:15px;border-bottom:2px solid #667eea;padding-bottom:8px}
.setting{display:flex;justify-content:space-between;align-items:center;padding:10px 0;border-bottom:1px solid #eee}
.setting:last-child{border-bottom:none}
.setting input[type=checkbox]{width:20px;height:20px}
.setting input[type=number]{width:60px;padding:5px;text-align:center}
.msg{background:#f9f9f9;padding:12px;border-left:3px solid #667eea;margin-bottom:8px;border-radius:4px}
.del-btn{background:#f44;color:#fff;border:none;padding:4px 10px;border-radius:4px;cursor:pointer}
footer{background:#333;color:#fff;padding:30px 0;text-align:center}
footer a{color:#fff;margin:0 10px}
.modal{display:none;position:fixed;z-index:1000;left:0;top:0;width:100%;height:100%;background:rgba(0,0,0,.9);align-items:center;justify-content:center}
.modal img{max-width:95%;max-height:90%}
.modal .close{position:absolute;top:15px;right:25px;color:#fff;font-size:35px;cursor:pointer}
@media(max-width:600px){
.header-inner{flex-direction:column}
nav ul{justify-content:center}
.hero h1{font-size:1.5em}
.btns{flex-direction:column}
.btn{width:100%}
.tl-bar{flex-direction:column}
#tl-slider{width:100%;max-width:none}
}
</style>
</head>
<body>
<header>
<div class="container header-inner">
<div class="logo"><img src="logo.png" alt="Logo"></div>
<nav><ul>
<li><a href="#cam">Webcam</a></li>
<li><a href="#archive">Archiv</a></li>
<li><a href="#guestbook">Gästebuch</a></li>
<li><a href="#kontakt">Kontakt</a></li>
<?php if($adminManager->isAdmin()): ?><li><a href="#admin">Admin</a></li><?php endif; ?>
</ul></nav>
</div>
</header>
<section class="hero">
<h1>Aurora Wetter Livecam</h1>
<p>Faszinierende Ausblicke aus dem Zürcher Oberland</p>
</section>
<section id="cam" class="section">
<div class="container">
<div class="video-box">
<div class="video-wrap" id="vw">
<?php echo $webcamManager->displayWebcam(); ?>
<div id="tlv"><img id="tl-img"><div id="tl-overlay" style="position:absolute;top:10px;left:10px;background:rgba(0,0,0,.7);color:#fff;padding:6px 12px;border-radius:4px;font-family:monospace"></div></div>
<div id="dvp"><video id="dv" controls playsinline></video></div>
<div class="zoom-btns">
<button onclick="zoom(-1)"></button>
<button onclick="zoom(0)"></button>
<button onclick="zoom(1)">+</button>
</div>
</div>
</div>
<div id="tl-ctrl">
<div class="tl-bar">
<button class="tl-btn" id="tl-play"><i class="fas fa-play"></i></button>
<button class="tl-btn" id="tl-rev"><i class="fas fa-backward"></i></button>
<input type="range" id="tl-slider" min="0" value="0">
<span id="tl-time">--:--:--</span>
<button class="tl-btn wide" id="tl-spd">1x</button>
<button class="tl-btn wide on" id="tl-back"><i class="fas fa-video"></i> Live</button>
</div>
</div>
<button class="btn purple" id="back-live" onclick="toLive()"><i class="fas fa-video"></i> Zurück zu Live</button>
<div class="info-bar">
<?php echo $webcamManager->displayStreamStats(); ?>
<?php if($sv): ?><div class="badge live"><span class="dot"></span><strong id="vc"><?php echo $vc; ?></strong> Zuschauer</div><?php endif; ?>
</div>
<div class="btns">
<a href="?action=snapshot" class="btn">📷 Snapshot</a>
<button class="btn" id="tl-btn">🎬 Zeitraffer</button>
<a href="?download_video=1" class="btn">⬇️ Tagesvideo</a>
</div>
</div>
</section>
<section id="archive" class="section">
<div class="container">
<h2>📅 Videoarchiv</h2>
<?php $cal = new VisualCalendarManager('./videos/', $settingsManager); echo $cal->displayVisualCalendar(); ?>
</div>
</section>
<section id="guestbook" class="section">
<div class="container">
<h2>Gästebuch</h2>
<?php echo $guestbookManager->displayForm(); echo $guestbookManager->displayEntries($adminManager->isAdmin()); ?>
</div>
</section>
<section id="kontakt" class="section">
<div class="container">
<h2>Kontakt</h2>
<?php echo $contactManager->displayForm(); ?>
</div>
</section>
<section id="gallery" class="section">
<div class="container">
<h2>Galerie</h2>
<?php echo $adminManager->displayGalleryImages(); ?>
</div>
</section>
<?php if($adminManager->isAdmin()): ?>
<section id="admin" class="section">
<div class="container">
<h2>⚙️ Admin</h2>
<?php echo $adminManager->displayAdminContent(); ?>
</div>
</section>
<?php else: ?>
<section id="admin" class="section">
<div class="container">
<h2>Admin Login</h2>
<?php echo $adminManager->displayLoginForm(); ?>
</div>
</section>
<?php endif; ?>
<footer>
<a href="#cam">Webcam</a>
<a href="#archive">Archiv</a>
<a href="#kontakt">Kontakt</a>
<p style="margin-top:15px">&copy; 2024 Aurora Livecam</p>
</footer>
<div class="modal" id="modal" onclick="this.style.display='none'">
<span class="close">&times;</span>
<img id="modal-img">
</div>
<script>
<?php echo $webcamManager->getJavaScript(); ?>
let zoomLvl=1;
function zoom(d){
if(d===0) zoomLvl=1;
else zoomLvl=Math.max(1,Math.min(4,zoomLvl+d*0.5));
// Alle Video-Elemente in allen Modi
const targets=['#webcam-player','#tl-img','#dv'];
targets.forEach(sel=>{
const el=document.querySelector(sel);
if(el){
el.style.transform='scale('+zoomLvl+')';
el.style.transformOrigin='center center';
el.style.transition='transform 0.2s ease';
}
});
// Zoom-Level Anzeige
showZoomLevel();
}
function showZoomLevel(){
let ind=document.getElementById('zoom-ind');
if(!ind){
ind=document.createElement('div');
ind.id='zoom-ind';
ind.style.cssText='position:absolute;top:15px;left:15px;background:rgba(0,0,0,0.7);color:#fff;padding:8px 14px;border-radius:20px;font-weight:bold;z-index:100;transition:opacity 0.3s';
document.getElementById('vw').appendChild(ind);
}
ind.textContent='🔍 '+Math.round(zoomLvl*100)+'%';
ind.style.opacity='1';
clearTimeout(ind.hideTimer);
ind.hideTimer=setTimeout(()=>{ind.style.opacity='0';},1500);
}
const TL={
imgs:<?php echo $imageFilesJson; ?>,
idx:0,playing:false,rev:false,spd:1,spds:[1,10,100],iv:null,
init(){
document.getElementById('tl-play').onclick=()=>this.toggle();
document.getElementById('tl-rev').onclick=()=>this.toggleRev();
document.getElementById('tl-spd').onclick=()=>this.cycleSpd();
document.getElementById('tl-back').onclick=()=>toLive();
document.getElementById('tl-slider').max=this.imgs.length-1;
document.getElementById('tl-slider').oninput=e=>this.seek(+e.target.value);
},
show(){
document.getElementById('webcam-player').style.display='none';
document.getElementById('dvp').style.display='none';
document.getElementById('tlv').style.display='block';
document.getElementById('tl-ctrl').style.display='block';
document.getElementById('back-live').style.display='none';
this.idx=0;this.frame();
},
toggle(){
this.playing=!this.playing;
document.getElementById('tl-play').innerHTML=this.playing?'<i class="fas fa-pause"></i>':'<i class="fas fa-play"></i>';
if(this.playing)this.play();else this.stop();
},
toggleRev(){this.rev=!this.rev;document.getElementById('tl-rev').classList.toggle('on',this.rev);},
cycleSpd(){const i=this.spds.indexOf(this.spd);this.spd=this.spds[(i+1)%this.spds.length];document.getElementById('tl-spd').textContent=this.spd+'x';if(this.playing){this.stop();this.play();}},
play(){this.iv=setInterval(()=>this.next(),200/this.spd);},
stop(){clearInterval(this.iv);},
next(){this.idx+=this.rev?-1:1;if(this.idx<0)this.idx=this.imgs.length-1;if(this.idx>=this.imgs.length)this.idx=0;this.frame();},
seek(i){this.idx=i;this.frame();},
frame(){
const img=this.imgs[this.idx];if(!img)return;
document.getElementById('tl-img').src=img;
document.getElementById('tl-slider').value=this.idx;
const m=img.match(/(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})/);
if(m){const t=m[3]+'.'+m[2]+'.'+m[1]+' '+m[4]+':'+m[5]+':'+m[6];document.getElementById('tl-time').textContent=t;document.getElementById('tl-overlay').textContent=t;}
}
};
function playVid(p){
document.getElementById('webcam-player').style.display='none';
document.getElementById('tlv').style.display='none';
document.getElementById('tl-ctrl').style.display='none';
document.getElementById('dvp').style.display='block';
document.getElementById('back-live').style.display='block';
const v=document.getElementById('dv');v.src=p;v.play();
document.getElementById('cam').scrollIntoView({behavior:'smooth'});
}
function toLive(){
TL.stop();TL.playing=false;
document.getElementById('tl-play').innerHTML='<i class="fas fa-play"></i>';
document.getElementById('tlv').style.display='none';
document.getElementById('tl-ctrl').style.display='none';
document.getElementById('dvp').style.display='none';
document.getElementById('back-live').style.display='none';
document.getElementById('webcam-player').style.display='block';
document.getElementById('tl-btn').textContent='🎬 Zeitraffer';
document.getElementById('dv').pause();document.getElementById('dv').src='';
zoomLvl=1;zoom(0);
}
function chgM(y,m){if(m<1){m=12;y--;}if(m>12){m=1;y++;}location.href='?cal_year='+y+'&cal_month='+m+'#archive';}
function selD(y,m,d){location.href='?cal_year='+y+'&cal_month='+m+'&cal_day='+d+'#archive';}
function openImg(s){document.getElementById('modal-img').src=s;document.getElementById('modal').style.display='flex';}
function updV(){
fetch(location.href,{method:'POST',body:new URLSearchParams({action:'viewer_heartbeat'})})
.then(r=>r.json()).then(d=>{const e=document.getElementById('vc');if(e&&d.count)e.textContent=d.count;});
}
<?php if($adminManager->isAdmin()): ?>
function saveSetting(key, value) {
const formData = new FormData();
formData.append('settings_action', 'update');
formData.append('key', key);
formData.append('value', value);
fetch(window.location.pathname, {
method: 'POST',
body: formData
})
.then(r => r.json())
.then(data => {
const toast = document.createElement('div');
toast.innerHTML = data.success ? '✓ Gespeichert' : '✗ Fehler: ' + (data.message || '');
toast.style.cssText = 'position:fixed;top:20px;right:20px;padding:15px 25px;border-radius:8px;background:' +
(data.success ? '#4CAF50' : '#f44336') + ';color:#fff;font-weight:bold;z-index:9999;box-shadow:0 4px 12px rgba(0,0,0,0.3);';
document.body.appendChild(toast);
setTimeout(() => { toast.style.opacity = '0'; toast.style.transition = 'opacity 0.3s'; }, 1500);
setTimeout(() => toast.remove(), 2000);
})
.catch(err => {
console.error('Settings save error:', err);
alert('Fehler beim Speichern: ' + err.message);
});
}
// Settings Event-Handler nach DOM-Load binden
document.addEventListener('DOMContentLoaded', function() {
const sViewer = document.getElementById('s-viewer');
const sMin = document.getElementById('s-min');
const sPlay = document.getElementById('s-play');
const sDl = document.getElementById('s-dl');
if (sViewer) sViewer.addEventListener('change', function() {
saveSetting('viewer_display.enabled', this.checked ? 'true' : 'false');
});
if (sMin) sMin.addEventListener('change', function() {
saveSetting('viewer_display.min_viewers', this.value);
});
if (sPlay) sPlay.addEventListener('change', function() {
saveSetting('video_mode.play_in_player', this.checked ? 'true' : 'false');
});
if (sDl) sDl.addEventListener('change', function() {
saveSetting('video_mode.allow_download', this.checked ? 'true' : 'false');
});
});
<?php endif; ?>
document.addEventListener('DOMContentLoaded',()=>{
TL.init();
document.getElementById('tl-btn').onclick=()=>{
if(document.getElementById('tlv').style.display==='block'){toLive();}
else{TL.show();document.getElementById('tl-btn').textContent='↩️ Zurück zu Live';}
};
setTimeout(updV,2000);setInterval(updV,10000);
});
</script>
</body>
</html>
+140
View File
@@ -0,0 +1,140 @@
/**
* Admin Settings Manager - AJAX ohne Reload
*/
const AdminSettings = {
settings: {},
init: function() {
this.loadSettings();
this.setupEventListeners();
},
loadSettings: function() {
fetch(window.location.href, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'settings_action=get'
})
.then(r => r.json())
.then(data => {
if (data.success) {
this.settings = data.settings;
this.updateUI();
}
})
.catch(err => console.error('Settings load error:', err));
},
updateSetting: function(key, value) {
fetch(window.location.href, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: `settings_action=update&key=${encodeURIComponent(key)}&value=${encodeURIComponent(value)}`
})
.then(r => r.json())
.then(data => {
if (data.success) {
this.showNotification('✓ Einstellung gespeichert', 'success');
// Sofort UI aktualisieren
this.applySettingImmediately(key, value);
} else {
this.showNotification('✗ Fehler beim Speichern', 'error');
}
})
.catch(err => {
console.error('Settings update error:', err);
this.showNotification('✗ Netzwerkfehler', 'error');
});
},
applySettingImmediately: function(key, value) {
// Sofortige Anwendung ohne Reload
switch(key) {
case 'viewer_display.enabled':
const viewerEl = document.querySelector('.viewer-stat');
if (viewerEl) {
viewerEl.style.display = value === true || value === 'true' ? 'inline-flex' : 'none';
}
break;
case 'viewer_display.min_viewers':
// Wird beim nächsten Heartbeat angewendet
window.minViewersToShow = parseInt(value);
break;
}
},
updateUI: function() {
// Checkbox für Zuschauer-Anzeige
const viewerEnabled = document.getElementById('setting-viewer-enabled');
if (viewerEnabled) {
viewerEnabled.checked = this.settings.viewer_display?.enabled ?? true;
}
// Mindestanzahl
const minViewers = document.getElementById('setting-min-viewers');
if (minViewers) {
minViewers.value = this.settings.viewer_display?.min_viewers ?? 1;
}
// Video-Modus
const playInPlayer = document.getElementById('setting-play-in-player');
if (playInPlayer) {
playInPlayer.checked = this.settings.video_mode?.play_in_player ?? true;
}
const allowDownload = document.getElementById('setting-allow-download');
if (allowDownload) {
allowDownload.checked = this.settings.video_mode?.allow_download ?? true;
}
},
setupEventListeners: function() {
// Zuschauer-Anzeige Toggle
document.getElementById('setting-viewer-enabled')?.addEventListener('change', (e) => {
this.updateSetting('viewer_display.enabled', e.target.checked);
});
// Mindestanzahl Zuschauer
document.getElementById('setting-min-viewers')?.addEventListener('change', (e) => {
this.updateSetting('viewer_display.min_viewers', e.target.value);
});
// Video im Player abspielen
document.getElementById('setting-play-in-player')?.addEventListener('change', (e) => {
this.updateSetting('video_mode.play_in_player', e.target.checked);
});
// Download erlauben
document.getElementById('setting-allow-download')?.addEventListener('change', (e) => {
this.updateSetting('video_mode.allow_download', e.target.checked);
});
},
showNotification: function(message, type) {
const notification = document.createElement('div');
notification.className = `admin-notification ${type}`;
notification.textContent = message;
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 15px 25px;
border-radius: 8px;
background: ${type === 'success' ? '#4CAF50' : '#f44336'};
color: white;
font-weight: bold;
z-index: 10000;
animation: slideIn 0.3s ease;
`;
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 3000);
}
};
// Initialisierung nur im Admin-Bereich
document.addEventListener('DOMContentLoaded', function() {
if (document.getElementById('admin-settings-panel')) {
AdminSettings.init();
}
});
+167
View File
@@ -0,0 +1,167 @@
/**
* Timelapse Controller mit Slider, Geschwindigkeit und Rückwärts
*/
const TimelapseController = {
imageFiles: [],
currentIndex: 0,
isPlaying: false,
isReverse: false,
speed: 1,
availableSpeeds: [1, 10, 100],
intervalId: null,
baseInterval: 200, // ms bei 1x
init: function(imageFilesArray) {
this.imageFiles = imageFilesArray;
this.setupControls();
this.updateSlider();
},
setupControls: function() {
const container = document.getElementById('timelapse-controls');
if (!container) return;
container.innerHTML = `
<div class="timelapse-control-bar">
<button id="tl-play-pause" class="tl-btn" title="Play/Pause">
<i class="fas fa-play"></i>
</button>
<button id="tl-reverse" class="tl-btn" title="Rückwärts">
<i class="fas fa-backward"></i>
</button>
<div class="tl-slider-container">
<input type="range" id="tl-slider" min="0" max="100" value="0">
<span id="tl-time-display">00:00:00</span>
</div>
<div class="tl-speed-container">
<button id="tl-speed" class="tl-btn tl-speed-btn">1x</button>
</div>
<button id="tl-back-live" class="tl-btn tl-back-btn" title="Zurück zu Live">
<i class="fas fa-video"></i> Live
</button>
</div>
`;
// Event Listeners
document.getElementById('tl-play-pause').onclick = () => this.togglePlay();
document.getElementById('tl-reverse').onclick = () => this.toggleReverse();
document.getElementById('tl-speed').onclick = () => this.cycleSpeed();
document.getElementById('tl-back-live').onclick = () => this.backToLive();
const slider = document.getElementById('tl-slider');
slider.max = this.imageFiles.length - 1;
slider.oninput = (e) => this.seekTo(parseInt(e.target.value));
},
togglePlay: function() {
this.isPlaying = !this.isPlaying;
const btn = document.getElementById('tl-play-pause');
btn.innerHTML = this.isPlaying ? '<i class="fas fa-pause"></i>' : '<i class="fas fa-play"></i>';
if (this.isPlaying) {
this.startPlayback();
} else {
this.stopPlayback();
}
},
toggleReverse: function() {
this.isReverse = !this.isReverse;
const btn = document.getElementById('tl-reverse');
btn.classList.toggle('active', this.isReverse);
btn.innerHTML = this.isReverse ?
'<i class="fas fa-forward"></i>' :
'<i class="fas fa-backward"></i>';
},
cycleSpeed: function() {
const idx = this.availableSpeeds.indexOf(this.speed);
this.speed = this.availableSpeeds[(idx + 1) % this.availableSpeeds.length];
document.getElementById('tl-speed').textContent = this.speed + 'x';
if (this.isPlaying) {
this.stopPlayback();
this.startPlayback();
}
},
startPlayback: function() {
const interval = this.baseInterval / this.speed;
this.intervalId = setInterval(() => this.nextFrame(), interval);
},
stopPlayback: function() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
},
nextFrame: function() {
if (this.isReverse) {
this.currentIndex--;
if (this.currentIndex < 0) this.currentIndex = this.imageFiles.length - 1;
} else {
this.currentIndex++;
if (this.currentIndex >= this.imageFiles.length) this.currentIndex = 0;
}
this.showFrame(this.currentIndex);
},
seekTo: function(index) {
this.currentIndex = index;
this.showFrame(index);
},
showFrame: function(index) {
const img = document.getElementById('timelapse-image');
if (img && this.imageFiles[index]) {
img.src = this.imageFiles[index];
}
this.updateSlider();
this.updateTimeDisplay();
},
updateSlider: function() {
const slider = document.getElementById('tl-slider');
if (slider) slider.value = this.currentIndex;
},
updateTimeDisplay: function() {
const display = document.getElementById('tl-time-display');
if (!display || !this.imageFiles[this.currentIndex]) return;
const filename = this.imageFiles[this.currentIndex];
const match = filename.match(/(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})/);
if (match) {
const [_, y, m, d, h, min, s] = match;
display.textContent = `${d}.${m}.${y} ${h}:${min}:${s}`;
}
},
backToLive: function() {
this.stopPlayback();
this.isPlaying = false;
// Live-Video wieder anzeigen
document.getElementById('timelapse-viewer').style.display = 'none';
document.getElementById('webcam-player').style.display = 'block';
document.getElementById('timelapse-button').textContent = 'Wochenzeitraffer';
// Controls verstecken
const controls = document.getElementById('timelapse-controls');
if (controls) controls.style.display = 'none';
},
show: function() {
document.getElementById('timelapse-viewer').style.display = 'block';
document.getElementById('webcam-player').style.display = 'none';
document.getElementById('daily-video-player').style.display = 'none';
const controls = document.getElementById('timelapse-controls');
if (controls) controls.style.display = 'block';
this.currentIndex = 0;
this.showFrame(0);
}
};
+108
View File
@@ -0,0 +1,108 @@
/**
* Daily Video Player - Spielt Tagesvideos im Hauptfenster ab
*/
const DailyVideoPlayer = {
currentVideo: null,
videoElement: null,
init: function() {
this.createPlayerElement();
this.setupEventListeners();
},
createPlayerElement: function() {
// Player-Container erstellen falls nicht vorhanden
if (document.getElementById('daily-video-player')) return;
const container = document.createElement('div');
container.id = 'daily-video-player';
container.style.display = 'none';
container.innerHTML = `
<video id="daily-video" controls playsinline>
<source src="" type="video/mp4">
</video>
<div class="video-player-controls">
<button id="dvp-back-live" class="tl-btn tl-back-btn">
<i class="fas fa-video"></i> Zurück zu Live
</button>
<a id="dvp-download" class="button" style="display:none;">
<i class="fas fa-download"></i> Download
</a>
</div>
`;
// Nach dem Webcam-Player einfügen
const videoContainer = document.querySelector('.video-container');
if (videoContainer) {
videoContainer.appendChild(container);
}
this.videoElement = document.getElementById('daily-video');
},
setupEventListeners: function() {
document.getElementById('dvp-back-live')?.addEventListener('click', () => this.backToLive());
// Video-Ende Event
this.videoElement?.addEventListener('ended', () => {
// Optional: Automatisch zurück zu Live
});
},
playVideo: function(videoPath, allowDownload = true) {
this.currentVideo = videoPath;
// Andere Player verstecken
document.getElementById('webcam-player').style.display = 'none';
document.getElementById('timelapse-viewer').style.display = 'none';
document.getElementById('timelapse-controls')?.style.display = 'none';
// Diesen Player anzeigen
const player = document.getElementById('daily-video-player');
player.style.display = 'block';
// Video laden
this.videoElement.src = videoPath;
this.videoElement.load();
this.videoElement.play();
// Download-Button
const downloadBtn = document.getElementById('dvp-download');
if (allowDownload && downloadBtn) {
downloadBtn.style.display = 'inline-block';
downloadBtn.href = videoPath;
downloadBtn.download = videoPath.split('/').pop();
} else if (downloadBtn) {
downloadBtn.style.display = 'none';
}
},
backToLive: function() {
// Video stoppen
if (this.videoElement) {
this.videoElement.pause();
this.videoElement.src = '';
}
// Player verstecken
document.getElementById('daily-video-player').style.display = 'none';
// Live-Stream anzeigen
document.getElementById('webcam-player').style.display = 'block';
},
// Wird vom Kalender aufgerufen
handleCalendarClick: function(videoPath, playInPlayer, allowDownload) {
if (playInPlayer) {
this.playVideo(videoPath, allowDownload);
} else {
// Nur Download
window.location.href = videoPath;
}
}
};
// Initialisierung
document.addEventListener('DOMContentLoaded', function() {
DailyVideoPlayer.init();
});
+215
View File
@@ -0,0 +1,215 @@
/**
* Video Zoom & Pan Controller
* Zoomt auf Wrapper-Layer statt direkt auf Video-Elemente
*/
(() => {
const config = window.zoomConfig || {};
if (!config.enabled) return;
let currentZoom = 1;
let panX = 0;
let panY = 0;
let isDragging = false;
let lastX = 0;
let lastY = 0;
const minZoom = Number(config.minZoom || 1);
const maxZoom = Number(config.maxZoom || 4);
const slider = document.getElementById('zoom-range');
const valueEl = document.getElementById('zoom-value');
// Wrapper-IDs für jeden Modus
const wrapperIds = ['live-video-wrapper', 'timelapse-wrapper', 'daily-video-wrapper'];
// Finde den aktuell sichtbaren Wrapper
function getActiveWrapper() {
// Prüfe daily-video-player
const dailyPlayer = document.getElementById('daily-video-player');
if (dailyPlayer && dailyPlayer.style.display !== 'none') {
return document.getElementById('daily-video-wrapper');
}
// Prüfe timelapse-viewer
const timelapseViewer = document.getElementById('timelapse-viewer');
if (timelapseViewer && timelapseViewer.style.display !== 'none') {
return document.getElementById('timelapse-wrapper');
}
// Fallback: Live-Video
return document.getElementById('live-video-wrapper');
}
// Wende Transform auf ALLE Wrapper an (damit beim Wechsel der Zoom erhalten bleibt)
function applyTransform() {
// Bei Zoom 1x: Kein Pan
if (currentZoom <= 1) {
panX = 0;
panY = 0;
}
// Pan begrenzen basierend auf Zoom
const maxPan = (currentZoom - 1) * 50;
panX = Math.max(-maxPan, Math.min(maxPan, panX));
panY = Math.max(-maxPan, Math.min(maxPan, panY));
// Transform auf alle Wrapper anwenden
wrapperIds.forEach(id => {
const wrapper = document.getElementById(id);
if (wrapper) {
wrapper.style.transform = `scale(${currentZoom}) translate(${panX}%, ${panY}%)`;
wrapper.style.transition = isDragging ? 'none' : 'transform 0.15s ease-out';
}
});
// UI Update
if (valueEl) valueEl.textContent = `${currentZoom.toFixed(1)}x`;
if (slider) slider.value = currentZoom;
// Cursor Update
updateCursor();
}
function updateCursor() {
const container = document.querySelector('.video-container');
if (container) {
if (currentZoom > 1) {
container.classList.add('zoomed');
} else {
container.classList.remove('zoomed');
}
}
}
// Zoom setzen
function setZoom(value) {
currentZoom = Math.max(minZoom, Math.min(maxZoom, value));
applyTransform();
}
// Zoom anpassen
function adjustZoom(delta) {
setZoom(currentZoom + delta);
}
// Zoom zurücksetzen
function resetZoom() {
currentZoom = 1;
panX = 0;
panY = 0;
applyTransform();
}
// Mouse Events für Pan
function setupPanEvents() {
const container = document.querySelector('.video-container');
if (!container) return;
// Mousedown - Start dragging
container.addEventListener('mousedown', (e) => {
if (currentZoom <= 1) return;
// Ignoriere Klicks auf Controls
if (e.target.closest('.zoom-controls, button, a')) return;
isDragging = true;
lastX = e.clientX;
lastY = e.clientY;
e.preventDefault();
});
// Mousemove - Dragging
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const deltaX = e.clientX - lastX;
const deltaY = e.clientY - lastY;
// Sensitivität basierend auf Zoom
const sensitivity = 0.15 / currentZoom;
panX += deltaX * sensitivity;
panY += deltaY * sensitivity;
lastX = e.clientX;
lastY = e.clientY;
applyTransform();
});
// Mouseup - Stop dragging
document.addEventListener('mouseup', () => {
isDragging = false;
});
// Mouse leave
document.addEventListener('mouseleave', () => {
isDragging = false;
});
// Touch Events für Mobile
container.addEventListener('touchstart', (e) => {
if (currentZoom <= 1 || e.touches.length !== 1) return;
if (e.target.closest('.zoom-controls, button, a')) return;
isDragging = true;
lastX = e.touches[0].clientX;
lastY = e.touches[0].clientY;
}, { passive: true });
container.addEventListener('touchmove', (e) => {
if (!isDragging || e.touches.length !== 1) return;
const deltaX = e.touches[0].clientX - lastX;
const deltaY = e.touches[0].clientY - lastY;
const sensitivity = 0.15 / currentZoom;
panX += deltaX * sensitivity;
panY += deltaY * sensitivity;
lastX = e.touches[0].clientX;
lastY = e.touches[0].clientY;
applyTransform();
}, { passive: true });
container.addEventListener('touchend', () => {
isDragging = false;
});
// Doppelklick zum Zurücksetzen
container.addEventListener('dblclick', (e) => {
if (e.target.closest('.zoom-controls, button, a')) return;
resetZoom();
});
}
// Slider Setup
function setupSlider() {
if (!slider) return;
slider.min = minZoom;
slider.max = maxZoom;
slider.step = 0.5;
slider.value = 1;
slider.addEventListener('input', (e) => {
setZoom(Number(e.target.value));
});
}
// Globale Funktionen
window.adjustZoom = adjustZoom;
window.resetZoom = resetZoom;
window.setZoom = setZoom;
// Initialisierung
document.addEventListener('DOMContentLoaded', () => {
setupSlider();
setupPanEvents();
// Initial State
currentZoom = 1;
applyTransform();
console.log('Video Zoom & Pan initialized');
});
})();
+422
View File
@@ -0,0 +1,422 @@
<?php
/**
* Landing Page - Marketing Seite
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
$settingsManager = new SettingsManager();
// Prüfe ob Landing Page aktiviert
if (!$settingsManager->isLandingPageEnabled()) {
header('Location: /');
exit;
}
$trialDays = $settingsManager->getTrialDays();
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aurora Livecam - Ihre Webcam als Service</title>
<meta name="description" content="Erstellen Sie Ihre eigene Live-Webcam in wenigen Minuten. Wetter-Widget, Timelapse, Analytics und mehr. Jetzt kostenlos testen!">
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
:root {
--gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #1a202c;
}
/* Header */
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
background: rgba(255,255,255,0.95);
backdrop-filter: blur(10px);
z-index: 100;
border-bottom: 1px solid #e2e8f0;
}
.header-inner {
max-width: 1200px;
margin: 0 auto;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 1.5rem;
font-weight: 700;
background: var(--gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-decoration: none;
}
.nav-links {
display: flex;
gap: 2rem;
align-items: center;
}
.nav-links a {
color: #4a5568;
text-decoration: none;
font-weight: 500;
transition: color 0.2s;
}
.nav-links a:hover {
color: #667eea;
}
/* Hero */
.hero {
padding: 8rem 2rem 6rem;
background: var(--gradient);
color: white;
text-align: center;
}
.hero h1 {
font-size: 3rem;
font-weight: 800;
margin-bottom: 1.5rem;
max-width: 800px;
margin-left: auto;
margin-right: auto;
}
.hero p {
font-size: 1.25rem;
opacity: 0.9;
max-width: 600px;
margin: 0 auto 2rem;
}
.hero-buttons {
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
}
.btn-hero {
padding: 1rem 2rem;
border-radius: 0.5rem;
font-size: 1.1rem;
font-weight: 600;
text-decoration: none;
transition: transform 0.2s, box-shadow 0.2s;
}
.btn-hero-primary {
background: white;
color: #667eea;
}
.btn-hero-secondary {
background: rgba(255,255,255,0.2);
color: white;
border: 2px solid rgba(255,255,255,0.5);
}
.btn-hero:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
.trial-badge {
display: inline-block;
background: rgba(255,255,255,0.2);
padding: 0.5rem 1rem;
border-radius: 2rem;
margin-top: 2rem;
font-size: 0.9rem;
}
/* Features */
.features {
padding: 6rem 2rem;
background: #f7fafc;
}
.section-title {
text-align: center;
margin-bottom: 4rem;
}
.section-title h2 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
.section-title p {
color: #718096;
font-size: 1.1rem;
}
.features-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
max-width: 1200px;
margin: 0 auto;
}
.feature-card {
background: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
transition: transform 0.2s, box-shadow 0.2s;
}
.feature-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.feature-icon {
font-size: 3rem;
margin-bottom: 1rem;
}
.feature-card h3 {
font-size: 1.25rem;
margin-bottom: 0.75rem;
}
.feature-card p {
color: #718096;
}
/* How it works */
.how-it-works {
padding: 6rem 2rem;
max-width: 1000px;
margin: 0 auto;
}
.steps {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 2rem;
margin-top: 3rem;
}
.step {
text-align: center;
}
.step-number {
width: 60px;
height: 60px;
background: var(--gradient);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.5rem;
font-weight: 700;
margin: 0 auto 1rem;
}
.step h4 {
margin-bottom: 0.5rem;
}
.step p {
color: #718096;
font-size: 0.9rem;
}
/* CTA */
.cta {
padding: 6rem 2rem;
background: var(--gradient);
color: white;
text-align: center;
}
.cta h2 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
.cta p {
font-size: 1.1rem;
opacity: 0.9;
margin-bottom: 2rem;
}
/* Footer */
.footer {
background: #1a202c;
color: #a0aec0;
padding: 3rem 2rem;
}
.footer-inner {
max-width: 1200px;
margin: 0 auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 2rem;
}
.footer-links a {
color: #a0aec0;
text-decoration: none;
margin-right: 1.5rem;
}
.footer-links a:hover {
color: white;
}
/* Responsive */
@media (max-width: 768px) {
.hero h1 { font-size: 2rem; }
.nav-links { display: none; }
.features-grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<!-- Header -->
<header class="header">
<div class="header-inner">
<a href="/" class="logo">Aurora Livecam</a>
<nav class="nav-links">
<a href="#features">Features</a>
<a href="/landing/pricing.php">Preise</a>
<a href="/dashboard/login.php">Login</a>
<a href="/onboarding/register.php" class="btn btn-primary btn-sm">Kostenlos starten</a>
</nav>
</div>
</header>
<!-- Hero -->
<section class="hero">
<h1>Ihre Webcam als Service - in 5 Minuten online</h1>
<p>Erstellen Sie Ihre eigene Live-Webcam-Website mit Wetter-Widget, Timelapse, Analytics und mehr. Keine Programmierkenntnisse erforderlich.</p>
<div class="hero-buttons">
<a href="/onboarding/register.php" class="btn-hero btn-hero-primary">
Jetzt starten
</a>
<a href="#features" class="btn-hero btn-hero-secondary">
Features ansehen
</a>
</div>
<div class="trial-badge">
<?php echo $trialDays; ?> Tage kostenlos testen - Keine Kreditkarte erforderlich
</div>
</section>
<!-- Features -->
<section class="features" id="features">
<div class="section-title">
<h2>Alles was Sie brauchen</h2>
<p>Professionelle Features für Ihre Live-Webcam</p>
</div>
<div class="features-grid">
<div class="feature-card">
<div class="feature-icon">📹</div>
<h3>Live-Streaming</h3>
<p>HLS, RTMP oder WebRTC - verbinden Sie jeden Stream in Sekunden. Automatische Qualitätsanpassung inklusive.</p>
</div>
<div class="feature-card">
<div class="feature-icon">🌤️</div>
<h3>Wetter-Widget</h3>
<p>Zeigen Sie Temperatur, Wind, Luftdruck und mehr an. Kostenlose Open-Meteo Integration ohne API-Key.</p>
</div>
<div class="feature-card">
<div class="feature-icon">⏱️</div>
<h3>Timelapse</h3>
<p>Automatische Zeitraffer-Erstellung. Scrubben Sie durch den ganzen Tag mit variabler Geschwindigkeit.</p>
</div>
<div class="feature-card">
<div class="feature-icon">🔍</div>
<h3>Zoom & Pan</h3>
<p>Lassen Sie Besucher in Ihren Stream hineinzoomen. Unterstützt Touch-Gesten und Maus-Steuerung.</p>
</div>
<div class="feature-card">
<div class="feature-icon">📊</div>
<h3>Analytics</h3>
<p>Sehen Sie wer Ihre Webcam besucht. Echtzeit-Zuschauerzähler und detaillierte Statistiken.</p>
</div>
<div class="feature-card">
<div class="feature-icon">🎨</div>
<h3>Custom Branding</h3>
<p>Ihr Logo, Ihre Farben, Ihre Domain. Machen Sie die Webcam zu Ihrer eigenen.</p>
</div>
</div>
</section>
<!-- How it works -->
<section class="how-it-works">
<div class="section-title">
<h2>So einfach geht's</h2>
<p>In 3 Schritten zur eigenen Livecam</p>
</div>
<div class="steps">
<div class="step">
<div class="step-number">1</div>
<h4>Registrieren</h4>
<p>Erstellen Sie in 30 Sekunden Ihr kostenloses Konto.</p>
</div>
<div class="step">
<div class="step-number">2</div>
<h4>Stream verbinden</h4>
<p>Fügen Sie Ihre Stream-URL ein. Wir unterstützen alle gängigen Formate.</p>
</div>
<div class="step">
<div class="step-number">3</div>
<h4>Anpassen & Teilen</h4>
<p>Personalisieren Sie Ihre Seite und teilen Sie den Link.</p>
</div>
</div>
</section>
<!-- CTA -->
<section class="cta">
<h2>Bereit loszulegen?</h2>
<p><?php echo $trialDays; ?> Tage kostenlos testen - keine Kreditkarte erforderlich</p>
<a href="/onboarding/register.php" class="btn-hero btn-hero-primary">
Jetzt kostenlos starten
</a>
</section>
<!-- Footer -->
<footer class="footer">
<div class="footer-inner">
<div>
© <?php echo date('Y'); ?> Aurora Livecam. Alle Rechte vorbehalten.
</div>
<div class="footer-links">
<a href="/terms">AGB</a>
<a href="/privacy">Datenschutz</a>
<a href="/imprint">Impressum</a>
<a href="mailto:support@aurora-livecam.com">Kontakt</a>
</div>
</div>
</footer>
</body>
</html>
+497
View File
@@ -0,0 +1,497 @@
<?php
/**
* Landing Page - Preise
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Billing\SubscriptionManager;
$settingsManager = new SettingsManager();
// Pläne laden
$plans = [];
try {
$subscriptions = new SubscriptionManager();
$plans = $subscriptions->getPlans();
} catch (\Exception $e) {
// Fallback-Pläne
$plans = [
['name' => 'Free', 'slug' => 'free', 'price_monthly' => 0, 'features' => ['max_viewers' => 10, 'weather_widget' => true]],
['name' => 'Basic', 'slug' => 'basic', 'price_monthly' => 19, 'features' => ['max_viewers' => 50, 'weather_widget' => true, 'timelapse' => true, 'analytics' => true]],
['name' => 'Professional', 'slug' => 'professional', 'price_monthly' => 49, 'features' => ['max_viewers' => 200, 'custom_domain' => true, 'weather_widget' => true, 'timelapse' => true, 'analytics' => true, 'branding' => true]],
['name' => 'Enterprise', 'slug' => 'enterprise', 'price_monthly' => 149, 'features' => ['max_viewers' => -1, 'custom_domain' => true, 'weather_widget' => true, 'timelapse' => true, 'analytics' => true, 'branding' => true, 'priority_support' => true]],
];
}
$trialDays = $settingsManager->getTrialDays();
// Feature-Labels
$featureLabels = [
'max_viewers' => 'Gleichzeitige Zuschauer',
'storage_gb' => 'Speicherplatz',
'custom_domain' => 'Eigene Domain',
'weather_widget' => 'Wetter-Widget',
'timelapse' => 'Timelapse',
'analytics' => 'Analytics & Statistiken',
'branding' => 'Custom Branding',
'priority_support' => 'Priority Support',
];
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Preise - Aurora Livecam</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
:root {
--gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #1a202c;
background: #f7fafc;
}
.header {
background: white;
border-bottom: 1px solid #e2e8f0;
padding: 1rem 2rem;
}
.header-inner {
max-width: 1200px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 1.5rem;
font-weight: 700;
background: var(--gradient);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-decoration: none;
}
.nav-links a {
color: #4a5568;
text-decoration: none;
margin-left: 1.5rem;
}
.page-header {
text-align: center;
padding: 4rem 2rem;
background: var(--gradient);
color: white;
}
.page-header h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
.page-header p {
font-size: 1.1rem;
opacity: 0.9;
}
.pricing-toggle {
display: flex;
justify-content: center;
gap: 1rem;
margin-top: 2rem;
align-items: center;
}
.pricing-toggle span {
font-size: 0.9rem;
}
.pricing-toggle .active {
font-weight: 600;
}
.toggle-switch {
width: 60px;
height: 30px;
background: rgba(255,255,255,0.3);
border-radius: 15px;
position: relative;
cursor: pointer;
}
.toggle-switch::after {
content: '';
position: absolute;
width: 26px;
height: 26px;
background: white;
border-radius: 50%;
top: 2px;
left: 2px;
transition: 0.3s;
}
.toggle-switch.yearly::after {
left: 32px;
}
.save-badge {
background: #48bb78;
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
font-size: 0.75rem;
font-weight: 600;
}
.pricing-container {
max-width: 1200px;
margin: -3rem auto 4rem;
padding: 0 2rem;
}
.pricing-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.5rem;
}
.pricing-card {
background: white;
border-radius: 1rem;
padding: 2rem;
box-shadow: 0 10px 40px rgba(0,0,0,0.1);
position: relative;
display: flex;
flex-direction: column;
}
.pricing-card.featured {
border: 2px solid #667eea;
transform: scale(1.05);
}
.pricing-card.featured::before {
content: 'Beliebt';
position: absolute;
top: -12px;
left: 50%;
transform: translateX(-50%);
background: var(--gradient);
color: white;
padding: 0.25rem 1rem;
border-radius: 1rem;
font-size: 0.75rem;
font-weight: 600;
}
.pricing-card h3 {
font-size: 1.25rem;
margin-bottom: 0.5rem;
}
.pricing-card .price {
font-size: 3rem;
font-weight: 800;
margin: 1rem 0;
}
.pricing-card .price span {
font-size: 1rem;
font-weight: 400;
color: #718096;
}
.pricing-card .price-yearly {
display: none;
}
.yearly-mode .price-monthly { display: none; }
.yearly-mode .price-yearly { display: block; }
.pricing-card ul {
list-style: none;
flex: 1;
margin: 1.5rem 0;
}
.pricing-card li {
padding: 0.5rem 0;
color: #4a5568;
display: flex;
align-items: center;
gap: 0.5rem;
}
.pricing-card li.included::before {
content: '✓';
color: #48bb78;
font-weight: bold;
}
.pricing-card li.not-included {
color: #a0aec0;
text-decoration: line-through;
}
.pricing-card li.not-included::before {
content: '✗';
color: #e53e3e;
}
.pricing-card .btn {
width: 100%;
padding: 1rem;
border: none;
border-radius: 0.5rem;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
text-decoration: none;
text-align: center;
transition: all 0.2s;
}
.pricing-card .btn-primary {
background: var(--gradient);
color: white;
}
.pricing-card .btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
.pricing-card .btn-secondary {
background: #e2e8f0;
color: #4a5568;
}
.faq {
max-width: 800px;
margin: 0 auto 4rem;
padding: 0 2rem;
}
.faq h2 {
text-align: center;
margin-bottom: 2rem;
}
.faq-item {
background: white;
border-radius: 0.5rem;
margin-bottom: 1rem;
overflow: hidden;
}
.faq-question {
padding: 1.25rem;
font-weight: 600;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.faq-answer {
padding: 0 1.25rem 1.25rem;
color: #718096;
display: none;
}
.faq-item.open .faq-answer {
display: block;
}
.footer {
background: #1a202c;
color: #a0aec0;
padding: 2rem;
text-align: center;
}
@media (max-width: 768px) {
.pricing-card.featured {
transform: none;
}
.pricing-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<header class="header">
<div class="header-inner">
<a href="/landing/" class="logo">Aurora Livecam</a>
<nav class="nav-links">
<a href="/landing/">Home</a>
<a href="/dashboard/login.php">Login</a>
<a href="/onboarding/register.php" class="btn btn-primary btn-sm">Kostenlos starten</a>
</nav>
</div>
</header>
<section class="page-header">
<h1>Einfache, transparente Preise</h1>
<p><?php echo $trialDays; ?> Tage kostenlos testen - jederzeit kündbar</p>
<div class="pricing-toggle">
<span class="monthly-label active">Monatlich</span>
<div class="toggle-switch" id="billing-toggle"></div>
<span class="yearly-label">Jährlich</span>
<span class="save-badge">2 Monate gratis</span>
</div>
</section>
<div class="pricing-container" id="pricing-container">
<div class="pricing-grid">
<?php foreach ($plans as $index => $plan): ?>
<?php $isFeatured = $plan['slug'] === 'professional'; ?>
<div class="pricing-card <?php echo $isFeatured ? 'featured' : ''; ?>">
<h3><?php echo htmlspecialchars($plan['name']); ?></h3>
<div class="price price-monthly">
<?php if ($plan['price_monthly'] > 0): ?>
CHF <?php echo number_format($plan['price_monthly'], 0); ?><span>/Monat</span>
<?php else: ?>
Kostenlos
<?php endif; ?>
</div>
<div class="price price-yearly">
<?php if (isset($plan['price_yearly']) && $plan['price_yearly'] > 0): ?>
CHF <?php echo number_format($plan['price_yearly'] / 12, 0); ?><span>/Monat</span>
<div style="font-size: 0.875rem; color: #718096;">
CHF <?php echo number_format($plan['price_yearly'], 0); ?> jährlich
</div>
<?php elseif ($plan['price_monthly'] > 0): ?>
CHF <?php echo number_format($plan['price_monthly'] * 10 / 12, 0); ?><span>/Monat</span>
<div style="font-size: 0.875rem; color: #718096;">
CHF <?php echo number_format($plan['price_monthly'] * 10, 0); ?> jährlich
</div>
<?php else: ?>
Kostenlos
<?php endif; ?>
</div>
<ul>
<?php
$features = is_array($plan['features']) ? $plan['features'] : json_decode($plan['features'], true) ?? [];
$allFeatures = ['max_viewers', 'weather_widget', 'timelapse', 'analytics', 'custom_domain', 'branding', 'priority_support'];
foreach ($allFeatures as $feature):
$hasFeature = !empty($features[$feature]);
$value = $features[$feature] ?? null;
?>
<li class="<?php echo $hasFeature ? 'included' : 'not-included'; ?>">
<?php
if ($feature === 'max_viewers' && $value) {
echo $value === -1 ? 'Unbegrenzte Zuschauer' : "Bis $value Zuschauer";
} elseif ($feature === 'storage_gb' && $value) {
echo "$value GB Speicher";
} else {
echo $featureLabels[$feature] ?? ucfirst(str_replace('_', ' ', $feature));
}
?>
</li>
<?php endforeach; ?>
</ul>
<a href="/onboarding/register.php?plan=<?php echo $plan['slug']; ?>"
class="btn <?php echo $isFeatured || $plan['price_monthly'] > 0 ? 'btn-primary' : 'btn-secondary'; ?>">
<?php echo $plan['price_monthly'] > 0 ? 'Jetzt starten' : 'Kostenlos starten'; ?>
</a>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- FAQ -->
<section class="faq">
<h2>Häufige Fragen</h2>
<div class="faq-item">
<div class="faq-question">
Kann ich jederzeit wechseln oder kündigen?
<span>+</span>
</div>
<div class="faq-answer">
Ja! Sie können Ihren Plan jederzeit upgraden oder downgraden. Bei einer Kündigung bleibt Ihr Zugang bis zum Ende der Abrechnungsperiode aktiv.
</div>
</div>
<div class="faq-item">
<div class="faq-question">
Was passiert nach dem Trial?
<span>+</span>
</div>
<div class="faq-answer">
Nach Ablauf der <?php echo $trialDays; ?> Tage werden Sie automatisch auf den kostenlosen Plan umgestellt, sofern Sie kein Abo abschliessen. Keine Sorge, Ihre Daten bleiben erhalten.
</div>
</div>
<div class="faq-item">
<div class="faq-question">
Welche Zahlungsmethoden werden akzeptiert?
<span>+</span>
</div>
<div class="faq-answer">
Wir akzeptieren alle gängigen Kreditkarten (Visa, Mastercard, American Express) sowie TWINT und Banküberweisung bei Jahresabos.
</div>
</div>
<div class="faq-item">
<div class="faq-question">
Brauche ich technisches Wissen?
<span>+</span>
</div>
<div class="faq-answer">
Nein! Unser Onboarding-Wizard führt Sie Schritt für Schritt durch die Einrichtung. Sie benötigen lediglich eine Stream-URL (HLS/m3u8) von Ihrem Kamera-Anbieter.
</div>
</div>
</section>
<footer class="footer">
© <?php echo date('Y'); ?> Aurora Livecam. Alle Rechte vorbehalten.
</footer>
<script>
// Billing toggle
const toggle = document.getElementById('billing-toggle');
const container = document.getElementById('pricing-container');
toggle.addEventListener('click', () => {
toggle.classList.toggle('yearly');
container.classList.toggle('yearly-mode');
document.querySelector('.monthly-label').classList.toggle('active');
document.querySelector('.yearly-label').classList.toggle('active');
});
// FAQ accordion
document.querySelectorAll('.faq-question').forEach(q => {
q.addEventListener('click', () => {
q.parentElement.classList.toggle('open');
q.querySelector('span').textContent = q.parentElement.classList.contains('open') ? '' : '+';
});
});
</script>
</body>
</html>
+253
View File
@@ -0,0 +1,253 @@
<?php
/**
* Onboarding - Branding (Schritt 4)
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Onboarding\OnboardingManager;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
if (!$auth->isLoggedIn()) {
header('Location: /onboarding/register.php');
exit;
}
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$error = '';
$branding = [
'site_name' => $user['tenant_name'] ?? '',
'tagline' => '',
'primary_color' => '#667eea',
'secondary_color' => '#764ba2',
];
// Formular verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$branding = [
'site_name' => trim($_POST['site_name'] ?? ''),
'site_name_full' => trim($_POST['site_name'] ?? ''),
'tagline' => trim($_POST['tagline'] ?? ''),
'primary_color' => $_POST['primary_color'] ?? '#667eea',
'secondary_color' => $_POST['secondary_color'] ?? '#764ba2',
];
try {
$onboarding = new OnboardingManager();
$result = $onboarding->saveBranding($tenantId, $branding);
if ($result['success']) {
header('Location: /onboarding/complete.php');
exit;
} else {
$error = $result['error'] ?? 'Fehler beim Speichern';
}
} catch (\Exception $e) {
$error = 'Fehler: ' . $e->getMessage();
}
}
// Skip
if (isset($_GET['skip'])) {
header('Location: /onboarding/complete.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Branding - Aurora Livecam</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
.onboarding-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
padding: 2rem;
}
.onboarding-box {
background: var(--white);
padding: 2.5rem;
border-radius: 1rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 100%;
max-width: 600px;
}
.progress-steps {
display: flex;
justify-content: center;
gap: 0.5rem;
margin-bottom: 1.5rem;
}
.step {
width: 12px;
height: 12px;
border-radius: 50%;
background: var(--gray-300);
}
.step.active { background: var(--primary); }
.step.completed { background: var(--success); }
.onboarding-header {
text-align: center;
margin-bottom: 2rem;
}
.onboarding-header h1 {
font-size: 1.5rem;
margin-bottom: 0.5rem;
}
.color-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.preview-card {
margin-top: 1.5rem;
border-radius: 0.75rem;
overflow: hidden;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.preview-header {
padding: 1.5rem;
color: white;
text-align: center;
}
.preview-header h3 {
margin: 0;
font-size: 1.25rem;
}
.preview-header p {
margin: 0.5rem 0 0 0;
opacity: 0.9;
font-size: 0.875rem;
}
.preview-body {
padding: 1rem;
background: var(--gray-100);
text-align: center;
font-size: 0.875rem;
color: var(--gray-500);
}
.skip-link {
display: block;
text-align: center;
margin-top: 1.5rem;
color: var(--gray-500);
font-size: 0.875rem;
}
</style>
</head>
<body>
<div class="onboarding-container">
<div class="onboarding-box">
<div class="progress-steps">
<div class="step completed"></div>
<div class="step completed"></div>
<div class="step completed"></div>
<div class="step active"></div>
</div>
<div class="onboarding-header">
<h1>🎨 Branding</h1>
<p style="color: var(--gray-500);">Personalisieren Sie Ihre Livecam</p>
</div>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="POST" action="">
<div class="form-group">
<label class="form-label" for="site_name">Name Ihrer Livecam</label>
<input type="text" id="site_name" name="site_name" class="form-input"
value="<?php echo htmlspecialchars($branding['site_name']); ?>"
placeholder="z.B. Berghütte Webcam">
</div>
<div class="form-group">
<label class="form-label" for="tagline">Slogan / Beschreibung</label>
<input type="text" id="tagline" name="tagline" class="form-input"
value="<?php echo htmlspecialchars($branding['tagline']); ?>"
placeholder="z.B. Live aus den Schweizer Alpen">
</div>
<div class="color-row">
<div class="form-group">
<label class="form-label">Primärfarbe</label>
<div class="color-picker-wrapper">
<input type="color" name="primary_color" id="primary_color" class="color-picker"
value="<?php echo htmlspecialchars($branding['primary_color']); ?>">
<span class="color-value"><?php echo htmlspecialchars($branding['primary_color']); ?></span>
</div>
</div>
<div class="form-group">
<label class="form-label">Sekundärfarbe</label>
<div class="color-picker-wrapper">
<input type="color" name="secondary_color" id="secondary_color" class="color-picker"
value="<?php echo htmlspecialchars($branding['secondary_color']); ?>">
<span class="color-value"><?php echo htmlspecialchars($branding['secondary_color']); ?></span>
</div>
</div>
</div>
<!-- Live Preview -->
<div class="preview-card">
<div class="preview-header" id="preview-header" style="background: linear-gradient(135deg, <?php echo htmlspecialchars($branding['primary_color']); ?> 0%, <?php echo htmlspecialchars($branding['secondary_color']); ?> 100%);">
<h3 id="preview-name"><?php echo htmlspecialchars($branding['site_name'] ?: 'Ihre Livecam'); ?></h3>
<p id="preview-tagline"><?php echo htmlspecialchars($branding['tagline'] ?: 'Ihr Slogan hier'); ?></p>
</div>
<div class="preview-body">
Live-Vorschau
</div>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%; margin-top: 1.5rem;">
Speichern & abschliessen
</button>
</form>
<a href="?skip=1" class="skip-link">
Später anpassen
</a>
</div>
</div>
<script>
// Live preview updates
document.getElementById('site_name').addEventListener('input', (e) => {
document.getElementById('preview-name').textContent = e.target.value || 'Ihre Livecam';
});
document.getElementById('tagline').addEventListener('input', (e) => {
document.getElementById('preview-tagline').textContent = e.target.value || 'Ihr Slogan hier';
});
document.getElementById('primary_color').addEventListener('input', updateColors);
document.getElementById('secondary_color').addEventListener('input', updateColors);
function updateColors() {
const primary = document.getElementById('primary_color').value;
const secondary = document.getElementById('secondary_color').value;
document.getElementById('preview-header').style.background =
`linear-gradient(135deg, ${primary} 0%, ${secondary} 100%)`;
document.querySelectorAll('.color-value')[0].textContent = primary;
document.querySelectorAll('.color-value')[1].textContent = secondary;
}
</script>
</body>
</html>
+237
View File
@@ -0,0 +1,237 @@
<?php
/**
* Onboarding - Abgeschlossen
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Onboarding\OnboardingManager;
use AuroraLivecam\Core\Database;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
if (!$auth->isLoggedIn()) {
header('Location: /onboarding/register.php');
exit;
}
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
// Onboarding abschliessen
try {
$onboarding = new OnboardingManager();
$onboarding->complete($tenantId);
} catch (\Exception $e) {
// Ignorieren wenn DB nicht verfügbar
}
// Tenant-Info laden
$tenantSlug = 'demo';
$subdomain = '';
try {
$db = Database::getInstance();
$tenant = $db->fetchOne("SELECT slug FROM tenants WHERE id = ?", [$tenantId]);
if ($tenant) {
$tenantSlug = $tenant['slug'];
$subdomain = $tenantSlug . '.aurora-livecam.com';
}
} catch (\Exception $e) {
// Fallback
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fertig! - Aurora Livecam</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
.complete-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
padding: 2rem;
}
.complete-box {
background: var(--white);
padding: 3rem;
border-radius: 1rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 100%;
max-width: 600px;
text-align: center;
}
.complete-icon {
font-size: 5rem;
margin-bottom: 1.5rem;
animation: bounce 0.5s ease;
}
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
.complete-box h1 {
font-size: 2rem;
margin-bottom: 1rem;
color: var(--success);
}
.complete-box p {
color: var(--gray-600);
margin-bottom: 2rem;
font-size: 1.1rem;
}
.url-box {
background: var(--gray-100);
border-radius: 0.5rem;
padding: 1rem;
margin-bottom: 2rem;
}
.url-box label {
display: block;
font-size: 0.875rem;
color: var(--gray-500);
margin-bottom: 0.5rem;
}
.url-box .url {
font-family: monospace;
font-size: 1rem;
color: var(--primary);
word-break: break-all;
}
.action-buttons {
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
}
.next-steps {
margin-top: 2.5rem;
text-align: left;
background: var(--gray-50);
border-radius: 0.5rem;
padding: 1.5rem;
}
.next-steps h3 {
font-size: 1rem;
margin-bottom: 1rem;
color: var(--gray-700);
}
.next-steps ul {
list-style: none;
padding: 0;
margin: 0;
}
.next-steps li {
padding: 0.5rem 0;
padding-left: 1.5rem;
position: relative;
color: var(--gray-600);
}
.next-steps li::before {
content: '→';
position: absolute;
left: 0;
color: var(--primary);
}
.confetti {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
overflow: hidden;
z-index: 1000;
}
.confetti-piece {
position: absolute;
width: 10px;
height: 10px;
background: var(--primary);
animation: confetti-fall 3s ease-out forwards;
}
@keyframes confetti-fall {
0% { transform: translateY(-100px) rotate(0deg); opacity: 1; }
100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}
</style>
</head>
<body>
<div class="confetti" id="confetti"></div>
<div class="complete-container">
<div class="complete-box">
<div class="complete-icon">🎉</div>
<h1>Herzlichen Glückwunsch!</h1>
<p>Ihre Livecam ist jetzt eingerichtet und bereit.</p>
<?php if ($subdomain): ?>
<div class="url-box">
<label>Ihre Livecam-Adresse:</label>
<div class="url">https://<?php echo htmlspecialchars($subdomain); ?></div>
</div>
<?php endif; ?>
<div class="action-buttons">
<a href="/dashboard/" class="btn btn-primary">
Zum Dashboard
</a>
<a href="/" class="btn btn-secondary" target="_blank">
Livecam ansehen
</a>
</div>
<div class="next-steps">
<h3>Nächste Schritte</h3>
<ul>
<li>Stream-URL im Dashboard anpassen (falls noch nicht geschehen)</li>
<li>Logo und Farben im Branding-Bereich hochladen</li>
<li>Wetter-Widget konfigurieren</li>
<li>Eigene Domain verbinden (optional)</li>
<?php if ($settingsManager->isBillingEnabled()): ?>
<li>Abo auswählen für mehr Funktionen</li>
<?php endif; ?>
</ul>
</div>
</div>
</div>
<script>
// Confetti Animation
function createConfetti() {
const container = document.getElementById('confetti');
const colors = ['#667eea', '#764ba2', '#f093fb', '#48bb78', '#ed8936'];
for (let i = 0; i < 50; i++) {
const piece = document.createElement('div');
piece.className = 'confetti-piece';
piece.style.left = Math.random() * 100 + '%';
piece.style.background = colors[Math.floor(Math.random() * colors.length)];
piece.style.animationDelay = Math.random() * 2 + 's';
piece.style.width = (Math.random() * 10 + 5) + 'px';
piece.style.height = piece.style.width;
container.appendChild(piece);
}
// Cleanup after animation
setTimeout(() => {
container.innerHTML = '';
}, 5000);
}
createConfetti();
</script>
</body>
</html>
+265
View File
@@ -0,0 +1,265 @@
<?php
/**
* Onboarding - Registrierung
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Onboarding\OnboardingManager;
use AuroraLivecam\Auth\AuthManager;
$settingsManager = new SettingsManager();
// Prüfe ob Self-Registration aktiviert ist
if (!$settingsManager->isSelfRegistrationEnabled()) {
header('Location: /');
exit;
}
$auth = new AuthManager();
// Bereits eingeloggt?
if ($auth->isLoggedIn()) {
header('Location: /dashboard/');
exit;
}
$errors = [];
$formData = [];
$success = false;
// Formular verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$formData = [
'name' => trim($_POST['name'] ?? ''),
'company_name' => trim($_POST['company_name'] ?? ''),
'email' => trim($_POST['email'] ?? ''),
'password' => $_POST['password'] ?? '',
'password_confirm' => $_POST['password_confirm'] ?? '',
'stream_url' => trim($_POST['stream_url'] ?? ''),
'accept_terms' => isset($_POST['accept_terms']),
];
try {
$onboarding = new OnboardingManager();
$result = $onboarding->register($formData);
if ($result['success']) {
// Session starten und User einloggen
$auth->login($formData['email'], $formData['password']);
// Zur nächsten Seite weiterleiten
if ($onboarding->requiresEmailVerification()) {
// Token für Demo-Zwecke in Session speichern
$_SESSION['verification_token'] = $result['verification_token'];
header('Location: /onboarding/verify.php');
} else {
header('Location: /onboarding/stream.php');
}
exit;
} else {
$errors = $result['errors'];
}
} catch (\Exception $e) {
$errors['general'] = 'Registrierung fehlgeschlagen: ' . $e->getMessage();
}
}
$trialDays = $settingsManager->getTrialDays();
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registrierung - Aurora Livecam</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
.register-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
padding: 2rem;
}
.register-box {
background: var(--white);
padding: 2.5rem;
border-radius: 1rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 100%;
max-width: 500px;
}
.register-header {
text-align: center;
margin-bottom: 2rem;
}
.register-header h1 {
font-size: 1.75rem;
margin-bottom: 0.5rem;
}
.register-header p {
color: var(--gray-500);
}
.trial-badge {
display: inline-block;
background: linear-gradient(135deg, var(--success) 0%, #38a169 100%);
color: white;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-size: 0.875rem;
margin-top: 0.5rem;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.error-text {
color: var(--danger);
font-size: 0.875rem;
margin-top: 0.25rem;
}
.input-error {
border-color: var(--danger) !important;
}
.terms-text {
font-size: 0.875rem;
color: var(--gray-600);
}
.terms-text a {
color: var(--primary);
}
.divider {
display: flex;
align-items: center;
margin: 1.5rem 0;
color: var(--gray-400);
}
.divider::before,
.divider::after {
content: '';
flex: 1;
height: 1px;
background: var(--gray-300);
}
.divider span {
padding: 0 1rem;
font-size: 0.875rem;
}
@media (max-width: 500px) {
.form-row {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="register-container">
<div class="register-box">
<div class="register-header">
<h1>Jetzt starten</h1>
<p>Erstellen Sie Ihre eigene Live-Webcam</p>
<span class="trial-badge"><?php echo $trialDays; ?> Tage kostenlos testen</span>
</div>
<?php if (!empty($errors['general'])): ?>
<div class="alert alert-error"><?php echo htmlspecialchars($errors['general']); ?></div>
<?php endif; ?>
<form method="POST" action="" novalidate>
<div class="form-row">
<div class="form-group">
<label class="form-label" for="name">Ihr Name *</label>
<input type="text" id="name" name="name" class="form-input <?php echo isset($errors['name']) ? 'input-error' : ''; ?>"
value="<?php echo htmlspecialchars($formData['name'] ?? ''); ?>" required>
<?php if (isset($errors['name'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['name']); ?></p>
<?php endif; ?>
</div>
<div class="form-group">
<label class="form-label" for="company_name">Webcam / Firma *</label>
<input type="text" id="company_name" name="company_name" class="form-input <?php echo isset($errors['company_name']) ? 'input-error' : ''; ?>"
value="<?php echo htmlspecialchars($formData['company_name'] ?? ''); ?>"
placeholder="z.B. Berghütte Webcam" required>
<?php if (isset($errors['company_name'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['company_name']); ?></p>
<?php endif; ?>
</div>
</div>
<div class="form-group">
<label class="form-label" for="email">E-Mail-Adresse *</label>
<input type="email" id="email" name="email" class="form-input <?php echo isset($errors['email']) ? 'input-error' : ''; ?>"
value="<?php echo htmlspecialchars($formData['email'] ?? ''); ?>" required>
<?php if (isset($errors['email'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['email']); ?></p>
<?php endif; ?>
</div>
<div class="form-row">
<div class="form-group">
<label class="form-label" for="password">Passwort *</label>
<input type="password" id="password" name="password" class="form-input <?php echo isset($errors['password']) ? 'input-error' : ''; ?>"
minlength="8" required>
<?php if (isset($errors['password'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['password']); ?></p>
<?php endif; ?>
</div>
<div class="form-group">
<label class="form-label" for="password_confirm">Passwort bestätigen *</label>
<input type="password" id="password_confirm" name="password_confirm" class="form-input <?php echo isset($errors['password_confirm']) ? 'input-error' : ''; ?>"
required>
<?php if (isset($errors['password_confirm'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['password_confirm']); ?></p>
<?php endif; ?>
</div>
</div>
<div class="divider"><span>Optional</span></div>
<div class="form-group">
<label class="form-label" for="stream_url">Stream-URL</label>
<input type="url" id="stream_url" name="stream_url" class="form-input <?php echo isset($errors['stream_url']) ? 'input-error' : ''; ?>"
value="<?php echo htmlspecialchars($formData['stream_url'] ?? ''); ?>"
placeholder="https://example.com/stream.m3u8">
<p class="form-help">Sie können die Stream-URL auch später im Dashboard hinzufügen</p>
<?php if (isset($errors['stream_url'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['stream_url']); ?></p>
<?php endif; ?>
</div>
<div class="form-group">
<label class="toggle-wrapper">
<input type="checkbox" name="accept_terms" <?php echo !empty($formData['accept_terms']) ? 'checked' : ''; ?> required>
<span class="terms-text">
Ich akzeptiere die <a href="/terms" target="_blank">AGB</a> und
<a href="/privacy" target="_blank">Datenschutzerklärung</a> *
</span>
</label>
<?php if (isset($errors['accept_terms'])): ?>
<p class="error-text"><?php echo htmlspecialchars($errors['accept_terms']); ?></p>
<?php endif; ?>
</div>
<button type="submit" class="btn btn-primary" style="width: 100%; margin-top: 1rem;">
Kostenlos registrieren
</button>
</form>
<p style="text-align: center; margin-top: 1.5rem; color: var(--gray-500);">
Bereits registriert?
<a href="/dashboard/login.php" style="color: var(--primary);">Anmelden</a>
</p>
</div>
</div>
</body>
</html>
+265
View File
@@ -0,0 +1,265 @@
<?php
/**
* Onboarding - Stream Konfiguration (Schritt 3)
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Onboarding\OnboardingManager;
use AuroraLivecam\Onboarding\StreamValidator;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
// Login prüfen
if (!$auth->isLoggedIn()) {
header('Location: /onboarding/register.php');
exit;
}
$user = $auth->getUser();
$tenantId = $user['tenant_id'] ?? 0;
$error = '';
$streamUrl = '';
$streamType = 'hls';
$validationResult = null;
// Formular verarbeiten
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$streamUrl = trim($_POST['stream_url'] ?? '');
$streamType = $_POST['stream_type'] ?? 'hls';
if (empty($streamUrl)) {
$error = 'Bitte geben Sie eine Stream-URL ein';
} else {
try {
// Stream validieren
$validator = new StreamValidator();
$validationResult = $validator->validate($streamUrl);
if ($validationResult['valid']) {
// Speichern
$onboarding = new OnboardingManager();
$result = $onboarding->saveStream($tenantId, $streamUrl, $streamType);
if ($result['success']) {
header('Location: /onboarding/branding.php');
exit;
} else {
$error = $result['error'];
}
} else {
$error = $validationResult['error'] ?? 'Stream-URL konnte nicht validiert werden';
}
} catch (\Exception $e) {
$error = 'Fehler: ' . $e->getMessage();
}
}
}
// Skip erlauben
if (isset($_GET['skip'])) {
header('Location: /onboarding/branding.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stream einrichten - Aurora Livecam</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
.onboarding-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
padding: 2rem;
}
.onboarding-box {
background: var(--white);
padding: 2.5rem;
border-radius: 1rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 100%;
max-width: 600px;
}
.onboarding-header {
text-align: center;
margin-bottom: 2rem;
}
.onboarding-header h1 {
font-size: 1.5rem;
margin-bottom: 0.5rem;
}
.progress-steps {
display: flex;
justify-content: center;
gap: 0.5rem;
margin-bottom: 1.5rem;
}
.step {
width: 12px;
height: 12px;
border-radius: 50%;
background: var(--gray-300);
}
.step.active { background: var(--primary); }
.step.completed { background: var(--success); }
.validation-result {
margin-top: 1rem;
padding: 1rem;
border-radius: 0.5rem;
}
.validation-success {
background: #c6f6d5;
border: 1px solid #9ae6b4;
}
.validation-error {
background: #fed7d7;
border: 1px solid #feb2b2;
}
.validation-details {
font-size: 0.875rem;
margin-top: 0.5rem;
color: var(--gray-600);
}
.stream-types {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 1rem;
margin-bottom: 1.5rem;
}
.stream-type-card {
border: 2px solid var(--gray-200);
border-radius: 0.5rem;
padding: 1rem;
cursor: pointer;
transition: all 0.2s;
}
.stream-type-card:hover {
border-color: var(--primary);
}
.stream-type-card.selected {
border-color: var(--primary);
background: rgba(102, 126, 234, 0.05);
}
.stream-type-card input {
display: none;
}
.stream-type-card h4 {
margin: 0 0 0.25rem 0;
font-size: 1rem;
}
.stream-type-card p {
margin: 0;
font-size: 0.75rem;
color: var(--gray-500);
}
.skip-link {
display: block;
text-align: center;
margin-top: 1.5rem;
color: var(--gray-500);
font-size: 0.875rem;
}
</style>
</head>
<body>
<div class="onboarding-container">
<div class="onboarding-box">
<div class="progress-steps">
<div class="step completed"></div>
<div class="step completed"></div>
<div class="step active"></div>
<div class="step"></div>
</div>
<div class="onboarding-header">
<h1>📹 Stream einrichten</h1>
<p style="color: var(--gray-500);">Verbinden Sie Ihre Webcam oder Ihren Stream</p>
</div>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="POST" action="" id="stream-form">
<div class="form-group">
<label class="form-label">Stream-Typ wählen</label>
<div class="stream-types">
<label class="stream-type-card <?php echo $streamType === 'hls' ? 'selected' : ''; ?>">
<input type="radio" name="stream_type" value="hls" <?php echo $streamType === 'hls' ? 'checked' : ''; ?>>
<h4>🎬 HLS Stream</h4>
<p>.m3u8 Playlist (empfohlen)</p>
</label>
<label class="stream-type-card <?php echo $streamType === 'rtmp' ? 'selected' : ''; ?>">
<input type="radio" name="stream_type" value="rtmp" <?php echo $streamType === 'rtmp' ? 'checked' : ''; ?>>
<h4>📡 RTMP</h4>
<p>Real-Time Messaging Protocol</p>
</label>
<label class="stream-type-card <?php echo $streamType === 'iframe' ? 'selected' : ''; ?>">
<input type="radio" name="stream_type" value="iframe" <?php echo $streamType === 'iframe' ? 'checked' : ''; ?>>
<h4>🖼️ Embed</h4>
<p>YouTube, Vimeo, Twitch</p>
</label>
<label class="stream-type-card <?php echo $streamType === 'webrtc' ? 'selected' : ''; ?>">
<input type="radio" name="stream_type" value="webrtc" <?php echo $streamType === 'webrtc' ? 'checked' : ''; ?>>
<h4> WebRTC</h4>
<p>Ultra-niedrige Latenz</p>
</label>
</div>
</div>
<div class="form-group">
<label class="form-label" for="stream_url">Stream-URL</label>
<input type="url" id="stream_url" name="stream_url" class="form-input"
value="<?php echo htmlspecialchars($streamUrl); ?>"
placeholder="https://example.com/stream.m3u8" required>
<p class="form-help">Die vollständige URL zu Ihrem Stream</p>
</div>
<?php if ($validationResult): ?>
<div class="validation-result <?php echo $validationResult['valid'] ? 'validation-success' : 'validation-error'; ?>">
<strong><?php echo $validationResult['valid'] ? '✓ Stream erreichbar' : '✗ Stream nicht erreichbar'; ?></strong>
<?php if (!empty($validationResult['details'])): ?>
<div class="validation-details">
<?php if (isset($validationResult['details']['detected_type'])): ?>
Erkannter Typ: <?php echo htmlspecialchars($validationResult['details']['detected_type']); ?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<button type="submit" class="btn btn-primary" style="width: 100%; margin-top: 1.5rem;">
Stream testen & weiter
</button>
</form>
<a href="?skip=1" class="skip-link">
Später einrichten
</a>
</div>
</div>
<script>
document.querySelectorAll('.stream-type-card').forEach(card => {
card.addEventListener('click', () => {
document.querySelectorAll('.stream-type-card').forEach(c => c.classList.remove('selected'));
card.classList.add('selected');
});
});
</script>
</body>
</html>
+214
View File
@@ -0,0 +1,214 @@
<?php
/**
* Onboarding - E-Mail Verifizierung
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/SettingsManager.php';
if (file_exists(dirname(__DIR__) . '/src/bootstrap.php')) {
require_once dirname(__DIR__) . '/src/bootstrap.php';
}
use AuroraLivecam\Auth\AuthManager;
use AuroraLivecam\Onboarding\OnboardingManager;
$settingsManager = new SettingsManager();
$auth = new AuthManager();
// Login prüfen
if (!$auth->isLoggedIn()) {
header('Location: /onboarding/register.php');
exit;
}
$user = $auth->getUser();
$message = '';
$error = '';
$verified = false;
// Token aus URL verarbeiten
if (isset($_GET['token'])) {
try {
$onboarding = new OnboardingManager();
$result = $onboarding->verifyEmail($_GET['token']);
if ($result['success']) {
$verified = true;
$message = 'E-Mail erfolgreich verifiziert!';
} else {
$error = $result['error'];
}
} catch (\Exception $e) {
$error = 'Verifikation fehlgeschlagen';
}
}
// E-Mail erneut senden
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['resend'])) {
try {
$onboarding = new OnboardingManager();
$result = $onboarding->resendVerification($user['id']);
if ($result['success']) {
$_SESSION['verification_token'] = $result['token'];
$message = 'Verifikations-E-Mail wurde erneut gesendet!';
} else {
$error = $result['error'];
}
} catch (\Exception $e) {
$error = 'Fehler beim Senden';
}
}
// Demo: Token anzeigen (in Produktion würde eine E-Mail gesendet)
$demoToken = $_SESSION['verification_token'] ?? null;
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>E-Mail verifizieren - Aurora Livecam</title>
<link rel="stylesheet" href="/dashboard/assets/dashboard.css">
<style>
.verify-container {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
padding: 2rem;
}
.verify-box {
background: var(--white);
padding: 2.5rem;
border-radius: 1rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
width: 100%;
max-width: 500px;
text-align: center;
}
.verify-icon {
font-size: 4rem;
margin-bottom: 1rem;
}
.verify-box h1 {
font-size: 1.5rem;
margin-bottom: 1rem;
}
.verify-box p {
color: var(--gray-600);
margin-bottom: 1.5rem;
}
.email-highlight {
font-weight: 600;
color: var(--gray-800);
}
.demo-box {
background: var(--gray-100);
border: 1px dashed var(--gray-300);
border-radius: 0.5rem;
padding: 1rem;
margin: 1.5rem 0;
text-align: left;
}
.demo-box h4 {
font-size: 0.875rem;
color: var(--warning);
margin-bottom: 0.5rem;
}
.demo-link {
word-break: break-all;
font-family: monospace;
font-size: 0.75rem;
background: white;
padding: 0.5rem;
border-radius: 0.25rem;
display: block;
margin-top: 0.5rem;
}
.progress-steps {
display: flex;
justify-content: center;
gap: 0.5rem;
margin-bottom: 2rem;
}
.step {
width: 12px;
height: 12px;
border-radius: 50%;
background: var(--gray-300);
}
.step.active {
background: var(--primary);
}
.step.completed {
background: var(--success);
}
</style>
</head>
<body>
<div class="verify-container">
<div class="verify-box">
<div class="progress-steps">
<div class="step completed"></div>
<div class="step active"></div>
<div class="step"></div>
<div class="step"></div>
</div>
<?php if ($verified): ?>
<div class="verify-icon"></div>
<h1>E-Mail verifiziert!</h1>
<p>Ihre E-Mail-Adresse wurde erfolgreich bestätigt.</p>
<a href="/onboarding/stream.php" class="btn btn-primary" style="width: 100%;">
Weiter zur Stream-Konfiguration
</a>
<?php else: ?>
<div class="verify-icon">📧</div>
<h1>E-Mail bestätigen</h1>
<p>
Wir haben eine Bestätigungs-E-Mail an<br>
<span class="email-highlight"><?php echo htmlspecialchars($user['email'] ?? ''); ?></span><br>
gesendet.
</p>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($demoToken): ?>
<div class="demo-box">
<h4>⚠️ Demo-Modus</h4>
<p style="font-size: 0.875rem; margin: 0;">In der Produktion würde eine E-Mail gesendet. Für Demo-Zwecke:</p>
<a href="/onboarding/verify.php?token=<?php echo urlencode($demoToken); ?>" class="demo-link">
Klicken Sie hier um zu verifizieren
</a>
</div>
<?php endif; ?>
<p style="margin-top: 1.5rem; color: var(--gray-500); font-size: 0.875rem;">
Keine E-Mail erhalten?
</p>
<form method="POST" action="" style="display: inline;">
<button type="submit" name="resend" class="btn btn-secondary">
Erneut senden
</button>
</form>
<?php endif; ?>
<p style="margin-top: 2rem;">
<a href="/dashboard/logout.php" style="color: var(--gray-500); font-size: 0.875rem;">
Abmelden
</a>
</p>
</div>
</div>
</body>
</html>
+71
View File
@@ -0,0 +1,71 @@
{
"viewer_display": {
"enabled": true,
"min_viewers": 1,
"update_interval": 5
},
"video_mode": {
"play_in_player": true,
"allow_download": true
},
"timelapse": {
"default_speed": 1,
"available_speeds": [
1,
10,
100
]
},
"ui_display": {
"show_recommendation_banner": true,
"show_qr_code": true,
"show_social_media": true,
"show_patrouille_suisse": true
},
"zoom_timelapse": {
"show_zoom_controls": true,
"max_zoom_level": 4.0,
"timelapse_reverse_enabled": true,
"weekly_timelapse_enabled": true
},
"auto_screenshot": {
"enabled": false,
"interval_minutes": 10,
"max_images": 144,
"save_to_gallery": true
},
"sharing": {
"email_enabled": false,
"share_link_expiry_hours": 24
},
"content": {
"guestbook_enabled": true,
"gallery_enabled": true,
"ai_events_enabled": true,
"max_guestbook_entries": 50
},
"technical": {
"viewer_update_interval": 5,
"session_timeout": 30
},
"theme": {
"default_theme": "theme-legacy",
"show_theme_switcher": false
},
"seo": {
"custom_title": "",
"meta_description": "",
"meta_keywords": ""
},
"weather": {
"enabled": true,
"api_key": "",
"location": "Oberdürnten,CH",
"lat": "47.2833",
"lon": "8.7167",
"update_interval": 5,
"units": "metric"
},
"last_updated": null,
"updated_by": null
}

Some files were not shown because too many files have changed in this diff Show More