This repository has been archived on 2026-08-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
melo-app/lib/utils/user_effekte.dart

83 lines
2.6 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:just_audio/just_audio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'farb_theme.dart';
/// Login-Effekte: Pro Person eigener Akzent, Willkommens-Sound und Begrüßung.
/// Wird nach erfolgreichem Login und beim App-Start (wenn Token vorhanden) ausgelöst.
class UserEffekt {
final Color akzent;
final String begruessung;
final String soundAsset;
const UserEffekt(this.akzent, this.begruessung, this.soundAsset);
/// Baka = königlich-rot + Fanfare 👑
static const baka = UserEffekt(
Color(0xFFCC0000),
'Willkommen zurück, Baka! 👑',
'assets/sounds/baka_fanfare.wav',
);
/// Tinker = türkis + heller Chime 🐱
static const tinker = UserEffekt(
Color(0xFF00B8A9),
'Tinker ist da! 🐱✨',
'assets/sounds/tinker_chime.wav',
);
static const standard = UserEffekt(
Color(0xFFCC0000),
'Willkommen! 🎵',
'assets/sounds/willkommen_ping.wav',
);
static UserEffekt fuer(String? user) {
final u = (user ?? '').toLowerCase().trim();
if (u == 'baka' || u == 'dustin') return baka;
if (u == 'tinker') return tinker;
return standard;
}
/// Key der per-User-Akzentfarbe in den SharedPreferences.
static String akzentKey(String? user) =>
'akzent_farbe_${(user ?? '').toLowerCase().trim()}';
/// '#RRGGBB' (oder 'RRGGBB') → Color; null bei ungültigem Format.
static Color? farbeAusHex(String? hex) {
if (hex == null) return null;
final h = hex.replaceFirst('#', '').trim();
if (h.length != 6) return null;
final v = int.tryParse(h, radix: 16);
if (v == null) return null;
return Color(0xFF000000 | v);
}
/// Setzt die Akzentfarbe (Thema wechselt live) und spielt den Sound ab.
/// Die in den Einstellungen gespeicherte Akzentfarbe
/// (`akzent_farbe_<user>`, F4) hat Vorrang vor der Standard-Farbe des Users.
static Future<void> anwenden(String? user) async {
var farbe = fuer(user).akzent;
try {
final p = await SharedPreferences.getInstance();
farbe = farbeAusHex(p.getString(akzentKey(user))) ?? farbe;
} catch (_) {
// Prefs-Fehler ignorieren — Standard-Akzent bleibt
}
MeloTheme.akzent = farbe;
try {
final p = AudioPlayer();
await p.setAsset(fuer(user).soundAsset);
unawaited(p.play());
// Player nach dem Abspielen sauber freigeben (kein Memory-Leak)
Future.delayed(const Duration(seconds: 4), () {
if (p.playing) p.stop();
p.dispose();
});
} catch (_) {
// Sound-Fehler ignorieren Akzent bleibt trotzdem aktiv
}
}
}