Skip to content

Commit

Permalink
add a lot of pvgame isInvalidated checks
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Mattiello <[email protected]>
  • Loading branch information
JoeMatt committed Nov 22, 2024
1 parent 0bd2e53 commit 9dcde91
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public extension PVGame {

#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
var spotlightContentSet: CSSearchableItemAttributeSet {
guard !isInvalidated else {
return CSSearchableItemAttributeSet()
}

let systemName = self.systemName

var description = "\(systemName ?? "")"
Expand Down Expand Up @@ -90,11 +94,13 @@ public extension PVGame {
}

var spotlightUniqueIdentifier: String {
guard !self.isInvalidated else { return "invalid" }
return "org.provenance-emu.game.\(md5Hash)"
}
#endif

var spotlightActivity: NSUserActivity {
guard !self.isInvalidated else { return NSUserActivity() }
let activity = NSUserActivity(activityType: "org.provenance-emu.game.play")
activity.title = title
activity.userInfo = ["md5": md5Hash]
Expand Down
20 changes: 18 additions & 2 deletions PVUI/Sources/PVSwiftUI/Components/GameContextMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ struct GameContextMenu: SwiftUI.View {
} label: { Label("Move to System", systemImage: "folder.fill.badge.plus") }
if #available(iOS 15, tvOS 15, macOS 12, *) {
Button(role: .destructive) {
rootDelegate?.attemptToDelete(game: game)
Task.detached { @MainActor in
rootDelegate?.attemptToDelete(game: game)
}
} label: { Label("Delete", systemImage: "trash") }
} else {
Button {
rootDelegate?.attemptToDelete(game: game)
Task.detached { @MainActor in
rootDelegate?.attemptToDelete(game: game)
}
} label: { Label("Delete", systemImage: "trash") }
}
}
Expand All @@ -94,6 +98,7 @@ struct GameContextMenu: SwiftUI.View {
extension GameContextMenu {

func showMoreInfo(forGame game: PVGame) {
guard !game.isInvalidated else { return }
let moreInfoCollectionVC = GameMoreInfoViewController(game: game)
if let rootDelegate = rootDelegate as? UIViewController {

Expand All @@ -108,6 +113,7 @@ extension GameContextMenu {
}

func promptUserMD5CopiedToClipboard(forGame game: PVGame) {
guard !game.isInvalidated else { return }
// Get the MD5 of the game
let md5 = game.md5
// Copy to pasteboard
Expand All @@ -119,6 +125,7 @@ extension GameContextMenu {
}

func pasteArtwork(forGame game: PVGame) {
guard !game.isInvalidated else { return }
#if !os(tvOS)
DLOG("Attempting to paste artwork for game: \(game.title)")
let pasteboard = UIPasteboard.general
Expand Down Expand Up @@ -157,8 +164,15 @@ extension GameContextMenu {
}

func share(game: PVGame) {
guard !game.isInvalidated else { return }
#if !os(tvOS)
DLOG("Attempting to share game: \(game.title)")
#warning("TODO: Share button action")
self.rootDelegate?.showUnderConstructionAlert()
#else
DLOG("Sharing not supported on this platform")
rootDelegate?.showMessage("Sharing is not supported on this platform.", title: "Not Supported")
#endif
}

private func saveArtwork(image: UIImage, forGame game: PVGame) {
Expand Down Expand Up @@ -201,6 +215,7 @@ extension GameContextMenu {
}

private func clearCustomArtwork(forGame game: PVGame) {
guard !game.isInvalidated else { return }
DLOG("GameContextMenu: Attempting to clear custom artwork for game: \(game.title)")
do {
try RomDatabase.sharedInstance.writeTransaction {
Expand All @@ -218,6 +233,7 @@ extension GameContextMenu {
}

private func resetCorePreferences(forGame game: PVGame) {
guard !game.isInvalidated else { return }
let hasGamePreference = game.userPreferredCoreID != nil
let hasSystemPreference = game.system.userPreferredCoreID != nil

Expand Down
39 changes: 22 additions & 17 deletions PVUI/Sources/PVSwiftUI/Components/SystemPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,34 @@ struct SystemPickerView: View {
@Binding var isPresented: Bool

private var availableSystems: [PVSystem] {
PVEmulatorConfiguration.systems.filter { $0.identifier != game.systemIdentifier }
PVEmulatorConfiguration.systems.filter {
$0.identifier != game.systemIdentifier &&
!(AppState.shared.isAppStore && $0.appStoreDisabled)
}
}

var body: some View {
NavigationView {
List {
ForEach(availableSystems) { system in
Button {
moveGame(to: system)
isPresented = false
} label: {
SystemRowView(system: system)
if !availableSystems.isEmpty {
NavigationView {
List {
ForEach(availableSystems) { system in
Button {
moveGame(to: system)
isPresented = false
} label: {
SystemRowView(system: system)
}
}
}
.navigationTitle("Select System")
.navigationBarItems(trailing: Button("Cancel") {
isPresented = false
})
}.onAppear {
DLOG("Loading systems for game: \(game.title)")
let systemsList = PVEmulatorConfiguration.systems.map{ $0.identifier }.joined(separator: ", ")
ILOG("Systemslist: \(systemsList)")
}
.navigationTitle("Select System")
.navigationBarItems(trailing: Button("Cancel") {
isPresented = false
})
}.onAppear {
DLOG("Loading systems for game: \(game.title)")
let systemsList = PVEmulatorConfiguration.systems.map{ $0.identifier }.joined(separator: ", ")
ILOG("Systemslist: \(systemsList)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import SwiftUI

internal struct SystemMoveState: Identifiable {
var id: String { game.id }
var id: String {
guard !game.isInvalidated else { return "" }
return game.id
}
let game: PVGame
var isPresenting: Bool = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ extension ConsoleGamesView {
}

internal func currentSectionForGame(_ game: PVGame) -> HomeSectionType {
guard !game.isInvalidated else { return .allGames }
// If we're in favorites section, ONLY return favorites if the game is actually in favorites
if gamesViewModel.focusedSection == .favorites {
return gamesViewModel.favorites.contains(where: { $0.id == game.id }) ? .favorites : .allGames
Expand Down
136 changes: 73 additions & 63 deletions PVUI/Sources/PVSwiftUI/Consoles/ConsoleGamesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,10 @@ struct ConsoleGamesView: SwiftUI.View {
!gamesViewModel.favorites.filter("systemIdentifier == %@", console.identifier).isEmpty
}

private var favoritesArray: [PVGame] {
Array(gamesViewModel.favorites.filter("systemIdentifier == %@", console.identifier))
}

private var hasRecentlyPlayedGames: Bool {
!gamesViewModel.recentlyPlayedGames.isEmpty
}

private var recentlyPlayedGamesArray: [PVGame] {
gamesViewModel.recentlyPlayedGames.compactMap { $0.game }
}

private func loadGame(_ game: PVGame) {
Task.detached { @MainActor in
await rootDelegate?.root_load(game, sender: self, core: nil, saveState: nil)
Expand Down Expand Up @@ -211,8 +203,10 @@ struct ConsoleGamesView: SwiftUI.View {
constrainHeight: false,
sectionContext: .allGames,
isFocused: Binding(
get: { gamesViewModel.focusedItemInSection == game.id },
set: { if $0 { gamesViewModel.focusedItemInSection = game.id } }
get: { !game.isInvalidated &&
gamesViewModel.focusedSection == .allGames &&
gamesViewModel.focusedItemInSection == game.id },
set: { if $0 && !game.isInvalidated { gamesViewModel.focusedItemInSection = game.id} }
)
) {
Task.detached { @MainActor in
Expand All @@ -238,11 +232,12 @@ struct ConsoleGamesView: SwiftUI.View {
sectionContext: .allGames,
isFocused: Binding(
get: {
!game.isInvalidated &&
gamesViewModel.focusedSection == .allGames &&
gamesViewModel.focusedItemInSection == game.id
},
set: {
if $0 {
if $0 && !game.isInvalidated {
gamesViewModel.focusedSection = .allGames
gamesViewModel.focusedItemInSection = game.id
}
Expand Down Expand Up @@ -276,8 +271,12 @@ struct ConsoleGamesView: SwiftUI.View {
viewType: .row,
sectionContext: .allGames,
isFocused: Binding(
get: { gamesViewModel.focusedItemInSection == game.id },
set: { if $0 { gamesViewModel.focusedItemInSection = game.id } }
get: {
!game.isInvalidated &&
gamesViewModel.focusedSection == .allGames &&
gamesViewModel.focusedItemInSection == game.id
},
set: { if $0 && !game.isInvalidated { gamesViewModel.focusedItemInSection = game.id} }
)
) {
Task.detached { @MainActor in
Expand All @@ -300,8 +299,11 @@ struct ConsoleGamesView: SwiftUI.View {
viewType: .row,
sectionContext: .allGames,
isFocused: Binding(
get: { gamesViewModel.focusedItemInSection == game.id },
set: { if $0 { gamesViewModel.focusedItemInSection = game.id } }
get: {
!game.isInvalidated &&
gamesViewModel.focusedSection == .allGames &&
gamesViewModel.focusedItemInSection == game.id },
set: { if $0 && !game.isInvalidated { gamesViewModel.focusedItemInSection = game.id} }
))
{
loadGame(game)
Expand Down Expand Up @@ -495,66 +497,74 @@ extension ConsoleGamesView {

@ViewBuilder
private func gameItem(_ game: PVGame, section: HomeSectionType) -> some View {
GameItemView(
game: game,
constrainHeight: true,
viewType: .cell,
sectionContext: section,
isFocused: Binding(
get: {
gamesViewModel.focusedSection == section &&
gamesViewModel.focusedItemInSection == game.id
},
set: {
if $0 {
gamesViewModel.focusedSection = section
gamesViewModel.focusedItemInSection = game.id
if !game.isInvalidated {

GameItemView(
game: game,
constrainHeight: true,
viewType: .cell,
sectionContext: section,
isFocused: Binding(
get: {
!game.isInvalidated &&
gamesViewModel.focusedSection == section &&
gamesViewModel.focusedItemInSection == game.id
},
set: {
if $0 && !game.isInvalidated {
gamesViewModel.focusedSection = section
gamesViewModel.focusedItemInSection = game.id
}
}
)
) {
Task.detached { @MainActor in
await rootDelegate?.root_load(game, sender: self, core: nil, saveState: nil)
}
)
) {
Task.detached { @MainActor in
await rootDelegate?.root_load(game, sender: self, core: nil, saveState: nil)
}
}
.id(game.id)
.focusableIfAvailable()
.contextMenu {
GameContextMenu(
game: game,
rootDelegate: rootDelegate,
contextMenuDelegate: self
)
.id(game.id)
.focusableIfAvailable()
.contextMenu {
GameContextMenu(
game: game,
rootDelegate: rootDelegate,
contextMenuDelegate: self
)
}
}
}

@ViewBuilder
private func saveStateItem(_ saveState: PVSaveState) -> some View {
GameItemView(
game: saveState.game,
saveState: saveState,
constrainHeight: true,
viewType: .cell,
sectionContext: .recentSaveStates,
isFocused: Binding(
get: {
gamesViewModel.focusedSection == .recentSaveStates &&
gamesViewModel.focusedItemInSection == saveState.id
},
set: {
if $0 {
gamesViewModel.focusedSection = .recentSaveStates
gamesViewModel.focusedItemInSection = saveState.id
if !saveState.isInvalidated && !saveState.game.isInvalidated {
GameItemView(
game: saveState.game,
saveState: saveState,
constrainHeight: true,
viewType: .cell,
sectionContext: .recentSaveStates,
isFocused: Binding(
get: {
!saveState.isInvalidated &&
gamesViewModel.focusedSection == .recentSaveStates &&
gamesViewModel.focusedItemInSection == saveState.id
},
set: {
if $0 && !saveState.isInvalidated {
gamesViewModel.focusedSection = .recentSaveStates
gamesViewModel.focusedItemInSection = saveState.id
}
}
)
) {
Task.detached { @MainActor in
guard !saveState.isInvalidated, !saveState.game.isInvalidated else { return }
await rootDelegate?.root_load(saveState.game, sender: self, core: saveState.core, saveState: saveState)
}
)
) {
Task.detached { @MainActor in
await rootDelegate?.root_load(saveState.game, sender: self, core: saveState.core, saveState: saveState)
}
.id(saveState.id)
.focusableIfAvailable()
}
.id(saveState.id)
.focusableIfAvailable()
}
}

Expand Down
Loading

0 comments on commit 9dcde91

Please sign in to comment.