From 646945ca76cb771894656176e3b66d49e0beec06 Mon Sep 17 00:00:00 2001 From: Joseph Mattiello Date: Thu, 24 Oct 2024 15:45:31 -0400 Subject: [PATCH] cleanup consoles game view, support mocks Signed-off-by: Joseph Mattiello --- .../PVSwiftUI/Consoles/ConsoleGamesView.swift | 157 ++++++------------ 1 file changed, 53 insertions(+), 104 deletions(-) diff --git a/PVUI/Sources/PVSwiftUI/Consoles/ConsoleGamesView.swift b/PVUI/Sources/PVSwiftUI/Consoles/ConsoleGamesView.swift index 0e6c976c17..4923842781 100644 --- a/PVUI/Sources/PVSwiftUI/Consoles/ConsoleGamesView.swift +++ b/PVUI/Sources/PVSwiftUI/Consoles/ConsoleGamesView.swift @@ -177,56 +177,16 @@ struct ConsoleGamesView: SwiftUI.View, GameContextMenuDelegate { // Show mock games in simulator let fakeGames = PVGame.mockGenerate(systemID: console.identifier) if viewModel.viewGamesAsGrid { - let columns = [GridItem(.adaptive(minimum: calculateGridItemSize()), spacing: 2)] - LazyVGrid(columns: columns, spacing: 2) { - ForEach(fakeGames, id: \.self) { game in - GameItemView(game: game, constrainHeight: false) { - // No action needed for fake games - } - } - } + showGamesGrid(fakeGames) } else { - LazyVStack(spacing: 8) { - ForEach(fakeGames, id: \.self) { game in - GameItemView(game: game, constrainHeight: false) { - // No action needed for fake games - } - } - } + showGamesList(fakeGames) } } else { // Show real games if viewModel.viewGamesAsGrid { - let columns = [GridItem(.adaptive(minimum: calculateGridItemSize()), spacing: 2)] - LazyVGrid(columns: columns, spacing: 2) { - ForEach(filteredAndSortedGames(), id: \.self) { game in - GameItemView(game: game, constrainHeight: false) { - loadGame(game) - } - .contextMenu { GameContextMenu(game: game, rootDelegate: rootDelegate, contextMenuDelegate: self) } - } - } - .gesture( - MagnificationGesture() - .onChanged { value in - let delta = value / lastScale - lastScale = value - adjustZoom(delta: delta) - } - .onEnded { _ in - lastScale = 1.0 - saveScale() - } - ) + showGamesGrid(filteredAndSortedGames()) } else { - LazyVStack(spacing: 8) { - ForEach(filteredAndSortedGames(), id: \.self) { game in - GameItemView(game: game, constrainHeight: false) { - loadGame(game) - } - .contextMenu { GameContextMenu(game: game, rootDelegate: rootDelegate, contextMenuDelegate: self) } - } - } + showGamesList(filteredAndSortedGames()) } } @@ -288,22 +248,60 @@ struct ConsoleGamesView: SwiftUI.View, GameContextMenuDelegate { } } - // MARK: - Zoom Helpers + private func showGamesGrid(_ games: [PVGame]) -> some View { + let columns = [GridItem(.adaptive(minimum: calculateGridItemSize()), spacing: 10)] + return LazyVGrid(columns: columns, spacing: 10) { + ForEach(games, id: \.self) { game in + GameItemView(game: game, constrainHeight: false) { + loadGame(game) + } + .contextMenu { GameContextMenu(game: game, rootDelegate: rootDelegate, contextMenuDelegate: self) } + } + } + .padding(.horizontal, 10) // Add horizontal padding to prevent overflow + } + + private func showGamesGrid(_ games: Results) -> some View { + let columns = [GridItem(.adaptive(minimum: calculateGridItemSize()), spacing: 10)] + return LazyVGrid(columns: columns, spacing: 10) { + ForEach(games, id: \.self) { game in + GameItemView(game: game, constrainHeight: false) { + loadGame(game) + } + .contextMenu { GameContextMenu(game: game, rootDelegate: rootDelegate, contextMenuDelegate: self) } + } + } + .padding(.horizontal, 10) // Add horizontal padding to prevent overflow + } - private func calculateGridItemSize() -> CGFloat { - let baseSize: CGFloat = 100 // Adjust this base size as needed - return baseSize * scale + private func showGamesList(_ games: [PVGame]) -> some View { + LazyVStack(spacing: 8) { + ForEach(games, id: \.self) { game in + GameItemView(game: game, constrainHeight: false) { + loadGame(game) + } + .contextMenu { GameContextMenu(game: game, rootDelegate: rootDelegate, contextMenuDelegate: self) } + } + } } - private func adjustZoom(delta: CGFloat) { - let minScale: CGFloat = 0.5 - let maxScale: CGFloat = 1.5 - let newScale = scale * delta - scale = min(max(newScale, minScale), maxScale) + private func showGamesList(_ games: Results) -> some View { + LazyVStack(spacing: 8) { + ForEach(games, id: \.self) { game in + GameItemView(game: game, constrainHeight: false) { + loadGame(game) + } + .contextMenu { GameContextMenu(game: game, rootDelegate: rootDelegate, contextMenuDelegate: self) } + } + } } - private func saveScale() { - Defaults[.gameLibraryScale] = Float(scale) + private func calculateGridItemSize() -> CGFloat { + // Calculate grid item size based on the number of items per row + let numberOfItemsPerRow: CGFloat = 3 // Adjust this number as needed + let totalSpacing: CGFloat = 10 * (numberOfItemsPerRow - 1) + let availableWidth = UIScreen.main.bounds.width - totalSpacing - 20 // Account for padding + return availableWidth / numberOfItemsPerRow } /// MARK: Rename @@ -392,55 +390,6 @@ struct ConsoleGamesView_Previews: PreviewProvider { } } -struct GridZoomStages -{ - static var zoomStages: [Int] - { -#if os(tvOS) - return [1, 2, 4, 8, 16] -#else - if UIDevice.current.userInterfaceIdiom == .pad - { - if UIDevice.current.orientation.isLandscape - { - return [4, 6, 10, 14, 18] - } - else - { - return [4, 6, 8, 10, 12] - } - } - else - { - if UIDevice.current.orientation.isLandscape - { - return [4, 6, 8, 9] - } - else - { - return [1, 2, 4, 6, 8] - } - } -#endif - } - - static func getZoomStage(at index: Int) -> Int - { - if index >= zoomStages.count - { - return zoomStages.last! - } - else if index < 0 - { - return zoomStages.first! - } - else - { - return zoomStages[index] - } - } -} - // New BiosesView struct BiosesView: View { let console: PVSystem