Cleanup code structure, code smells, rename paramter, break out more granular managers
This commit is contained in:
@@ -15,6 +15,8 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
public class StatuePhotoGalleryController : ManagedBehaviour
|
||||
{
|
||||
public static StatuePhotoGalleryController Instance { get; private set; }
|
||||
|
||||
[Header("Gallery UI")]
|
||||
[SerializeField] private Transform gridContainer;
|
||||
[SerializeField] private PhotoGridItem gridItemPrefab;
|
||||
@@ -29,25 +31,33 @@ namespace Minigames.StatueDressup.Controllers
|
||||
[SerializeField] private Button nextPageButton;
|
||||
[SerializeField] private Text pageStatusText;
|
||||
|
||||
private AppleHills.Core.Settings.IStatueDressupSettings _settings;
|
||||
private int _currentPage;
|
||||
private List<string> _allPhotoIds = new List<string>();
|
||||
private Dictionary<string, PhotoGridItem> _activeGridItems = new Dictionary<string, PhotoGridItem>();
|
||||
private Dictionary<string, Texture2D> _thumbnailCache = new Dictionary<string, Texture2D>();
|
||||
private Dictionary<string, Texture2D> _fullPhotoCache = new Dictionary<string, Texture2D>(); // Cache full photos for enlargement
|
||||
private Queue<string> _thumbnailCacheOrder = new Queue<string>();
|
||||
private bool _isLoadingPage;
|
||||
private PhotoEnlargeController _enlargeController;
|
||||
private int currentPage;
|
||||
private List<string> allPhotoIds = new List<string>();
|
||||
private Dictionary<string, PhotoGridItem> activeGridItems = new Dictionary<string, PhotoGridItem>();
|
||||
private Dictionary<string, Texture2D> thumbnailCache = new Dictionary<string, Texture2D>();
|
||||
private Dictionary<string, Texture2D> fullPhotoCache = new Dictionary<string, Texture2D>(); // Cache full photos for enlargement
|
||||
private Queue<string> thumbnailCacheOrder = new Queue<string>();
|
||||
private bool isLoadingPage;
|
||||
private PhotoEnlargeController enlargeController;
|
||||
|
||||
internal override void OnManagedStart()
|
||||
{
|
||||
base.OnManagedStart();
|
||||
|
||||
// Get settings
|
||||
_settings = GameManager.GetSettingsObject<AppleHills.Core.Settings.IStatueDressupSettings>();
|
||||
// Singleton pattern
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Logging.Warning("[StatuePhotoGalleryController] Duplicate instance detected. Destroying duplicate.");
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
|
||||
var settings = StatueDressupSettings.Instance?.Settings;
|
||||
|
||||
// Initialize enlarge controller
|
||||
_enlargeController = new PhotoEnlargeController(backdrop, enlargedContainer, _settings?.GalleryAnimationDuration ?? 0.3f);
|
||||
enlargeController = new PhotoEnlargeController(backdrop, enlargedContainer, settings?.GalleryAnimationDuration ?? 0.3f);
|
||||
|
||||
// Setup page navigation buttons
|
||||
if (previousPageButton != null)
|
||||
@@ -76,10 +86,10 @@ namespace Minigames.StatueDressup.Controllers
|
||||
ClearGrid();
|
||||
|
||||
// Get all photo IDs
|
||||
_allPhotoIds = PhotoManager.GetAllPhotoIds(CaptureType.StatueMinigame);
|
||||
_currentPage = 0;
|
||||
allPhotoIds = PhotoManager.GetAllPhotoIds(CaptureType.StatueMinigame);
|
||||
currentPage = 0;
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Gallery refreshed: {_allPhotoIds.Count} photos");
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Gallery refreshed: {allPhotoIds.Count} photos");
|
||||
|
||||
// Display first page
|
||||
DisplayCurrentPage();
|
||||
@@ -90,18 +100,18 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
private void DisplayCurrentPage()
|
||||
{
|
||||
if (_isLoadingPage) return;
|
||||
if (isLoadingPage) return;
|
||||
|
||||
_isLoadingPage = true;
|
||||
isLoadingPage = true;
|
||||
|
||||
// Clear current grid
|
||||
ClearGrid();
|
||||
|
||||
// Get photos for current page
|
||||
int itemsPerPage = _settings?.GalleryItemsPerPage ?? 20;
|
||||
List<string> pagePhotoIds = PhotoManager.GetPhotoIdsPage(CaptureType.StatueMinigame, _currentPage, itemsPerPage);
|
||||
int itemsPerPage = StatueDressupSettings.Instance?.Settings?.GalleryItemsPerPage ?? 20;
|
||||
List<string> pagePhotoIds = PhotoManager.GetPhotoIdsPage(CaptureType.StatueMinigame, currentPage, itemsPerPage);
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Displaying page {_currentPage + 1}: {pagePhotoIds.Count} items");
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Displaying page {currentPage + 1}: {pagePhotoIds.Count} items");
|
||||
|
||||
// Spawn grid items for this page
|
||||
foreach (string photoId in pagePhotoIds)
|
||||
@@ -113,10 +123,10 @@ namespace Minigames.StatueDressup.Controllers
|
||||
UpdatePageButtons();
|
||||
|
||||
// Update status text
|
||||
int totalPages = Mathf.CeilToInt((float)_allPhotoIds.Count / itemsPerPage);
|
||||
UpdateStatusText($"Page {_currentPage + 1}/{totalPages} ({_allPhotoIds.Count} photos)");
|
||||
int totalPages = Mathf.CeilToInt((float)allPhotoIds.Count / itemsPerPage);
|
||||
UpdateStatusText($"Page {currentPage + 1}/{totalPages} ({allPhotoIds.Count} photos)");
|
||||
|
||||
_isLoadingPage = false;
|
||||
isLoadingPage = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -124,19 +134,19 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
private void UpdatePageButtons()
|
||||
{
|
||||
int itemsPerPage = _settings?.GalleryItemsPerPage ?? 20;
|
||||
int totalPages = Mathf.CeilToInt((float)_allPhotoIds.Count / itemsPerPage);
|
||||
int itemsPerPage = StatueDressupSettings.Instance?.Settings?.GalleryItemsPerPage ?? 20;
|
||||
int totalPages = Mathf.CeilToInt((float)allPhotoIds.Count / itemsPerPage);
|
||||
|
||||
// Enable/disable previous button
|
||||
if (previousPageButton != null)
|
||||
{
|
||||
previousPageButton.interactable = _currentPage > 0;
|
||||
previousPageButton.interactable = currentPage > 0;
|
||||
}
|
||||
|
||||
// Enable/disable next button
|
||||
if (nextPageButton != null)
|
||||
{
|
||||
nextPageButton.interactable = _currentPage < totalPages - 1;
|
||||
nextPageButton.interactable = currentPage < totalPages - 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,11 +155,11 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
private void OnPreviousPageClicked()
|
||||
{
|
||||
if (_currentPage > 0)
|
||||
if (currentPage > 0)
|
||||
{
|
||||
_currentPage--;
|
||||
currentPage--;
|
||||
DisplayCurrentPage();
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Navigated to previous page: {_currentPage}");
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Navigated to previous page: {currentPage}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,14 +168,14 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
private void OnNextPageClicked()
|
||||
{
|
||||
int itemsPerPage = _settings?.GalleryItemsPerPage ?? 20;
|
||||
int totalPages = Mathf.CeilToInt((float)_allPhotoIds.Count / itemsPerPage);
|
||||
int itemsPerPage = StatueDressupSettings.Instance?.Settings?.GalleryItemsPerPage ?? 20;
|
||||
int totalPages = Mathf.CeilToInt((float)allPhotoIds.Count / itemsPerPage);
|
||||
|
||||
if (_currentPage < totalPages - 1)
|
||||
if (currentPage < totalPages - 1)
|
||||
{
|
||||
_currentPage++;
|
||||
currentPage++;
|
||||
DisplayCurrentPage();
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Navigated to next page: {_currentPage}");
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Navigated to next page: {currentPage}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +184,7 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
private void SpawnGridItem(string photoId)
|
||||
{
|
||||
if (_activeGridItems.ContainsKey(photoId))
|
||||
if (activeGridItems.ContainsKey(photoId))
|
||||
{
|
||||
Logging.Warning($"[StatuePhotoGalleryController] Grid item already exists: {photoId}");
|
||||
return;
|
||||
@@ -183,7 +193,7 @@ namespace Minigames.StatueDressup.Controllers
|
||||
PhotoGridItem gridItem = Instantiate(gridItemPrefab, gridContainer);
|
||||
gridItem.Initialize(photoId, this);
|
||||
|
||||
_activeGridItems[photoId] = gridItem;
|
||||
activeGridItems[photoId] = gridItem;
|
||||
|
||||
// Load thumbnail asynchronously
|
||||
StartCoroutine(LoadThumbnailAsync(photoId, gridItem));
|
||||
@@ -195,7 +205,7 @@ namespace Minigames.StatueDressup.Controllers
|
||||
private IEnumerator LoadThumbnailAsync(string photoId, PhotoGridItem gridItem)
|
||||
{
|
||||
// Check cache first
|
||||
if (_thumbnailCache.TryGetValue(photoId, out Texture2D cachedThumbnail))
|
||||
if (thumbnailCache.TryGetValue(photoId, out Texture2D cachedThumbnail))
|
||||
{
|
||||
gridItem.SetThumbnail(cachedThumbnail);
|
||||
yield break;
|
||||
@@ -214,7 +224,7 @@ namespace Minigames.StatueDressup.Controllers
|
||||
}
|
||||
|
||||
// Create thumbnail
|
||||
int thumbSize = _settings?.GalleryThumbnailSize ?? 256;
|
||||
int thumbSize = StatueDressupSettings.Instance?.Settings?.GalleryThumbnailSize ?? 256;
|
||||
Texture2D thumbnail = PhotoManager.CreateThumbnail(fullPhoto, thumbSize);
|
||||
|
||||
// Destroy full photo immediately (we only need thumbnail)
|
||||
@@ -236,19 +246,19 @@ namespace Minigames.StatueDressup.Controllers
|
||||
private void CacheThumbnail(string photoId, Texture2D thumbnail)
|
||||
{
|
||||
// Add to cache
|
||||
_thumbnailCache[photoId] = thumbnail;
|
||||
_thumbnailCacheOrder.Enqueue(photoId);
|
||||
thumbnailCache[photoId] = thumbnail;
|
||||
thumbnailCacheOrder.Enqueue(photoId);
|
||||
|
||||
// Evict oldest if over limit
|
||||
int maxCached = _settings?.GalleryMaxCachedThumbnails ?? 50;
|
||||
while (_thumbnailCache.Count > maxCached && _thumbnailCacheOrder.Count > 0)
|
||||
int maxCached = StatueDressupSettings.Instance?.Settings?.GalleryMaxCachedThumbnails ?? 50;
|
||||
while (thumbnailCache.Count > maxCached && thumbnailCacheOrder.Count > 0)
|
||||
{
|
||||
string oldestId = _thumbnailCacheOrder.Dequeue();
|
||||
string oldestId = thumbnailCacheOrder.Dequeue();
|
||||
|
||||
if (_thumbnailCache.TryGetValue(oldestId, out Texture2D oldThumbnail))
|
||||
if (thumbnailCache.TryGetValue(oldestId, out Texture2D oldThumbnail))
|
||||
{
|
||||
Destroy(oldThumbnail);
|
||||
_thumbnailCache.Remove(oldestId);
|
||||
thumbnailCache.Remove(oldestId);
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Evicted thumbnail from cache: {oldestId}");
|
||||
}
|
||||
}
|
||||
@@ -259,28 +269,28 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
public void OnGridItemClicked(PhotoGridItem gridItem, string photoId)
|
||||
{
|
||||
if (_enlargeController == null)
|
||||
if (enlargeController == null)
|
||||
{
|
||||
Logging.Error("[StatuePhotoGalleryController] Enlarge controller not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
// If already enlarged, shrink it
|
||||
if (_enlargeController.IsPhotoEnlarged)
|
||||
if (enlargeController.IsPhotoEnlarged)
|
||||
{
|
||||
_enlargeController.ShrinkPhoto();
|
||||
enlargeController.ShrinkPhoto();
|
||||
return;
|
||||
}
|
||||
|
||||
Logging.Debug($"[StatuePhotoGalleryController] Enlarging photo: {photoId}");
|
||||
|
||||
float enlargedScale = _settings?.GalleryEnlargedScale ?? 2.5f;
|
||||
float enlargedScale = StatueDressupSettings.Instance?.Settings?.GalleryEnlargedScale ?? 2.5f;
|
||||
|
||||
// Check cache first
|
||||
if (_fullPhotoCache.TryGetValue(photoId, out Texture2D fullPhoto))
|
||||
if (fullPhotoCache.TryGetValue(photoId, out Texture2D fullPhoto))
|
||||
{
|
||||
// Use cached photo
|
||||
_enlargeController.EnlargePhoto(gridItem, enlargedPreviewPrefab != null ? enlargedPreviewPrefab : gridItem.gameObject, fullPhoto, enlargedScale);
|
||||
enlargeController.EnlargePhoto(gridItem, enlargedPreviewPrefab != null ? enlargedPreviewPrefab : gridItem.gameObject, fullPhoto, enlargedScale);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -294,12 +304,12 @@ namespace Minigames.StatueDressup.Controllers
|
||||
}
|
||||
|
||||
// Cache it (limited cache)
|
||||
if (_fullPhotoCache.Count < 10) // Keep only recent 10 full photos
|
||||
if (fullPhotoCache.Count < 10) // Keep only recent 10 full photos
|
||||
{
|
||||
_fullPhotoCache[photoId] = fullPhoto;
|
||||
fullPhotoCache[photoId] = fullPhoto;
|
||||
}
|
||||
|
||||
_enlargeController.EnlargePhoto(gridItem, enlargedPreviewPrefab != null ? enlargedPreviewPrefab : gridItem.gameObject, fullPhoto, enlargedScale);
|
||||
enlargeController.EnlargePhoto(gridItem, enlargedPreviewPrefab != null ? enlargedPreviewPrefab : gridItem.gameObject, fullPhoto, enlargedScale);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,20 +318,20 @@ namespace Minigames.StatueDressup.Controllers
|
||||
/// </summary>
|
||||
public void CleanupGallery()
|
||||
{
|
||||
if (_enlargeController != null)
|
||||
if (enlargeController != null)
|
||||
{
|
||||
_enlargeController.Cleanup();
|
||||
enlargeController.Cleanup();
|
||||
}
|
||||
|
||||
// Clean up cached full photos
|
||||
foreach (var photo in _fullPhotoCache.Values)
|
||||
foreach (var photo in fullPhotoCache.Values)
|
||||
{
|
||||
if (photo != null)
|
||||
{
|
||||
Destroy(photo);
|
||||
}
|
||||
}
|
||||
_fullPhotoCache.Clear();
|
||||
fullPhotoCache.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -341,12 +351,12 @@ namespace Minigames.StatueDressup.Controllers
|
||||
private void ClearGrid()
|
||||
{
|
||||
// Destroy grid items
|
||||
foreach (var gridItem in _activeGridItems.Values)
|
||||
foreach (var gridItem in activeGridItems.Values)
|
||||
{
|
||||
if (gridItem != null)
|
||||
Destroy(gridItem.gameObject);
|
||||
}
|
||||
_activeGridItems.Clear();
|
||||
activeGridItems.Clear();
|
||||
|
||||
Logging.Debug("[StatuePhotoGalleryController] Grid cleared");
|
||||
}
|
||||
@@ -359,13 +369,13 @@ namespace Minigames.StatueDressup.Controllers
|
||||
ClearGrid();
|
||||
|
||||
// Clear thumbnail cache
|
||||
foreach (var thumbnail in _thumbnailCache.Values)
|
||||
foreach (var thumbnail in thumbnailCache.Values)
|
||||
{
|
||||
if (thumbnail != null)
|
||||
Destroy(thumbnail);
|
||||
}
|
||||
_thumbnailCache.Clear();
|
||||
_thumbnailCacheOrder.Clear();
|
||||
thumbnailCache.Clear();
|
||||
thumbnailCacheOrder.Clear();
|
||||
|
||||
Logging.Debug("[StatuePhotoGalleryController] Gallery fully cleared");
|
||||
}
|
||||
@@ -374,7 +384,13 @@ namespace Minigames.StatueDressup.Controllers
|
||||
{
|
||||
base.OnManagedDestroy();
|
||||
|
||||
// Cleanup
|
||||
// Singleton cleanup
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
// Clean up cached textures
|
||||
ClearGallery();
|
||||
CleanupGallery();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user