Playvoi/Playvoi.Client/Assets/ShiroginSDK/Runtime/Services/Implementations/UI/ThemeService.cs

123 lines
No EOL
4.2 KiB
C#

using ShiroginSDK.Runtime.Modules.Data.Scripts;
using ShiroginSDK.Runtime.Modules.Events;
using ShiroginSDK.Runtime.Modules.UI.Theme.Enums;
using ShiroginSDK.Runtime.Modules.UI.Theme.Scripts.SO;
using ShiroginSDK.Runtime.Services.Base;
using ShiroginSDK.Runtime.Services.Interfaces;
using ShiroginSDK.Runtime.Shared.Constants;
using UnityEngine;
namespace ShiroginSDK.Runtime.Services.Implementations.UI
{
/// <summary>
/// Manages theme switching and sprite retrieval for active UI skin.
/// Repository is injected via SDKConfig.
/// </summary>
public class ThemeService : ServiceBase, IThemeService
{
private ThemeProfile _currentProfile;
private IDataService _dataService;
private IEventService _eventService;
private ThemeRepository _repository;
public string CurrentThemeName => _currentProfile ? _currentProfile.ThemeName : SDKConstants.DefaultTheme;
// ----------------------------------------------------
// Repository Injection
// ----------------------------------------------------
public void SetRepository(ThemeRepository repo)
{
_repository = repo;
if (_repository == null)
{
Debug.LogWarning("[ThemeService] ⚠️ Repository not assigned. Theme system disabled.");
return;
}
var themeData = _dataService.Get<ThemeData>();
var savedTheme = themeData?.ActiveTheme ?? SDKConstants.DefaultTheme;
if (!SetTheme(savedTheme))
{
Debug.LogWarning($"[ThemeService] Saved theme '{savedTheme}' not found. Reverting to Default.");
SetTheme("Default");
}
else
{
Debug.Log($"[ThemeService] ✅ Loaded saved theme: {savedTheme}");
}
}
// ----------------------------------------------------
// Theme Management
// ----------------------------------------------------
public bool SetTheme(string themeName)
{
if (_repository == null)
{
Debug.LogError("[ThemeService] ❌ Repository not set. Cannot change theme.");
return false;
}
var profile = _repository.GetProfile(themeName);
if (profile == null)
{
Debug.LogWarning($"[ThemeService] ⚠️ Theme '{themeName}' not found in repository.");
return false;
}
_currentProfile = profile;
// 🔔 Notify systems via IEventService
_eventService?.Invoke(new ShiroginEvents.Theme.ThemeChanged(themeName));
// 💾 Save new active theme
var themeData = _dataService.Get<ThemeData>();
if (themeData != null)
{
themeData.ActiveTheme = themeName;
if (!themeData.UnlockedThemes.Contains(themeName))
themeData.UnlockedThemes.Add(themeName);
themeData.Save();
}
Debug.Log($"[ThemeService] 🌈 Active theme set to '{themeName}'");
return true;
}
// ----------------------------------------------------
// Sprite Retrieval
// ----------------------------------------------------
public Sprite GetSprite(ThemeAssetType type)
{
if (!_currentProfile)
{
Debug.LogWarning("[ThemeService] ⚠️ No active theme profile set.");
return null;
}
return _currentProfile.GetSprite(type);
}
// ----------------------------------------------------
// Lifecycle
// ----------------------------------------------------
protected override void OnInitialize()
{
_eventService = ServiceLocator.Get<IEventService>();
_dataService = ServiceLocator.Get<IDataService>();
Debug.Log("[ThemeService] ✅ Initialized.");
}
protected override void OnDispose()
{
_repository = null;
_currentProfile = null;
_eventService = null;
_dataService = null;
}
}
}