diff --git a/src/App/ViewModels/GameGroupViewModel.swift b/src/App/ViewModels/GameGroupViewModel.swift index 79e3bc8..7fb27f2 100644 --- a/src/App/ViewModels/GameGroupViewModel.swift +++ b/src/App/ViewModels/GameGroupViewModel.swift @@ -14,6 +14,15 @@ enum GameGroupType: Hashable { } } + var isValidType: Bool { + switch self { + case .type, .weakness: + return true + default: + return false + } + } + var isType: Bool { if case .type = self { return true diff --git a/src/App/ViewModels/GameViewModel.swift b/src/App/ViewModels/GameViewModel.swift index 93bda9f..11c6664 100644 --- a/src/App/ViewModels/GameViewModel.swift +++ b/src/App/ViewModels/GameViewModel.swift @@ -281,15 +281,17 @@ final class GameViewModel: ObservableObject { .sorted() case .weakness: groups = baseMonsters - .reduce(into: [:]) { (result: inout [Attack: [GameItemViewModel]], next: GameItemViewModel) in + .reduce(into: [:]) { (result: inout [Attack?: [GameItemViewModel]], next: GameItemViewModel) in guard let weaknesses = next.weaknesses else { return } + var added: Bool = false if weaknesses.contains(where: { $0.1.fire >= .effective }) { if let items = result[.fire] { result[.fire] = items + [next] } else { result[.fire] = [next] } + added = true } if weaknesses.contains(where: { $0.1.water >= .effective }) { if let items = result[.water] { @@ -297,6 +299,7 @@ final class GameViewModel: ObservableObject { } else { result[.water] = [next] } + added = true } if weaknesses.contains(where: { $0.1.thunder >= .effective }) { if let items = result[.thunder] { @@ -304,6 +307,7 @@ final class GameViewModel: ObservableObject { } else { result[.thunder] = [next] } + added = true } if weaknesses.contains(where: { $0.1.ice >= .effective }) { if let items = result[.ice] { @@ -311,6 +315,7 @@ final class GameViewModel: ObservableObject { } else { result[.ice] = [next] } + added = true } if weaknesses.contains(where: { $0.1.dragon >= .effective }) { if let items = result[.dragon] { @@ -318,11 +323,19 @@ final class GameViewModel: ObservableObject { } else { result[.dragon] = [next] } + added = true + } + if !added { + if let items = result[.none] { + result[.none] = items + [next] + } else { + result[.none] = [next] + } } } .map { element, baseItems in let items = baseItems.map { monster in - IdentifyHolder(monster, prefix: element.prefix) + IdentifyHolder(monster, prefix: element?.prefix ?? "n") } return GameGroupViewModel(gameID: gameID, type: .weakness(element: element), items: items) } diff --git a/src/App/Views/MonsterLists/MonsterList.swift b/src/App/Views/MonsterLists/MonsterList.swift index f10555b..94eda46 100644 --- a/src/App/Views/MonsterLists/MonsterList.swift +++ b/src/App/Views/MonsterLists/MonsterList.swift @@ -12,7 +12,7 @@ struct MonsterList: View { var body: some View { let items = viewModel.items - let isHeaderShow = items.count > 1 || items.first?.type.isType == true + let isHeaderShow = items.count > 1 || items.first?.type.isValidType == true List(items) { group in Section { ForEach(group.items) { item in diff --git a/src/App/Views/MonsterLists/MonsterListColumn.swift b/src/App/Views/MonsterLists/MonsterListColumn.swift index 1e6f6a4..42736d7 100644 --- a/src/App/Views/MonsterLists/MonsterListColumn.swift +++ b/src/App/Views/MonsterLists/MonsterListColumn.swift @@ -19,7 +19,7 @@ struct MonsterListColumn: View { var body: some View { let items = viewModel.items - let isHeaderShow = items.count > 1 || items.first?.type.isType == true + let isHeaderShow = items.count > 1 || items.first?.type.isValidType == true List(items, id: \.id, selection: $selectedMonsterID) { group in Section { ForEach(group.items) { item in