refactor(view, model, viewmodel): 优化代码写法
优化代码写法 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
189bfc0bb8
commit
449fa1b8e9
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */; };
|
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */; };
|
||||||
240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* ContentView.swift */; };
|
240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */; };
|
||||||
240EDC432998A3BA00A46AC9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC422998A3BA00A46AC9 /* Assets.xcassets */; };
|
240EDC432998A3BA00A46AC9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC422998A3BA00A46AC9 /* Assets.xcassets */; };
|
||||||
240EDC462998A3BA00A46AC9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */; };
|
240EDC462998A3BA00A46AC9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */; };
|
||||||
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F22998EAD6000CE9DA /* MemoryGame.swift */; };
|
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F22998EAD6000CE9DA /* MemoryGame.swift */; };
|
||||||
@ -18,7 +18,7 @@
|
|||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
240EDC3B2998A3B900A46AC9 /* Memorize.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Memorize.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
240EDC3B2998A3B900A46AC9 /* Memorize.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Memorize.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeApp.swift; sourceTree = "<group>"; };
|
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemorizeApp.swift; sourceTree = "<group>"; };
|
||||||
240EDC402998A3B900A46AC9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGameView.swift; sourceTree = "<group>"; };
|
||||||
240EDC422998A3BA00A46AC9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
240EDC422998A3BA00A46AC9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||||
240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
240EDC452998A3BA00A46AC9 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
||||||
245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = "<group>"; };
|
245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = "<group>"; };
|
||||||
@ -56,7 +56,7 @@
|
|||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
|
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
|
||||||
240EDC402998A3B900A46AC9 /* ContentView.swift */,
|
240EDC402998A3B900A46AC9 /* EmojiMemoryGameView.swift */,
|
||||||
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
|
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
|
||||||
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
|
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
|
||||||
240EDC422998A3BA00A46AC9 /* Assets.xcassets */,
|
240EDC422998A3BA00A46AC9 /* Assets.xcassets */,
|
||||||
@ -144,7 +144,7 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
|
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
|
||||||
240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */,
|
240EDC412998A3B900A46AC9 /* EmojiMemoryGameView.swift in Sources */,
|
||||||
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
|
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
|
||||||
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
|
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -8,22 +8,23 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
// viewmodel
|
// viewmodel
|
||||||
class EmojiMemoryGame: ObservableObject{
|
class EmojiMemoryGame: ObservableObject {
|
||||||
|
typealias Card = MemoryGame<String>.Card
|
||||||
static let emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"]
|
static let emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"]
|
||||||
|
|
||||||
static func createMemoryGame() -> MemoryGame<String> {
|
static func createMemoryGame() -> MemoryGame<String> {
|
||||||
MemoryGame<String>(numberOfPairsOfCards: 5) { pairIndex in EmojiMemoryGame.emojis[pairIndex] }
|
MemoryGame<String>(numberOfPairsOfCards: emojis.count) { pairIndex in EmojiMemoryGame.emojis[pairIndex] }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Published private var model: MemoryGame<String> = createMemoryGame()
|
@Published private var model = createMemoryGame()
|
||||||
|
|
||||||
var cards: [MemoryGame<String>.Card] {
|
var cards: [Card] {
|
||||||
return model.cards
|
return model.cards
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Intent(s)
|
// MARK: - Intent(s)
|
||||||
|
|
||||||
func choose(_ card: MemoryGame<String>.Card) {
|
func choose(_ card: Card) {
|
||||||
model.choose(card)
|
model.choose(card)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// ContentView.swift
|
// EmojiMemoryGameView.swift
|
||||||
// Memorize
|
// Memorize
|
||||||
//
|
//
|
||||||
// Created by ching on 2023/2/12.
|
// Created by ching on 2023/2/12.
|
||||||
@ -7,15 +7,15 @@
|
|||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ContentView: View {
|
struct EmojiMemoryGameView: View {
|
||||||
@ObservedObject var viewModel: EmojiMemoryGame
|
@ObservedObject var game: EmojiMemoryGame
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ScrollView {
|
ScrollView {
|
||||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) {
|
LazyVGrid(columns: [GridItem(.adaptive(minimum: 65))]) {
|
||||||
ForEach(viewModel.cards) {
|
ForEach(game.cards) {
|
||||||
card in CardView(card: card).aspectRatio(2 / 3, contentMode: .fit)
|
card in CardView(card: card).aspectRatio(2 / 3, contentMode: .fit)
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
viewModel.choose(card)
|
game.choose(card)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,11 +26,11 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct CardView: View {
|
struct CardView: View {
|
||||||
let card: MemoryGame<String>.Card
|
let card: EmojiMemoryGame.Card
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ZStack {
|
ZStack {
|
||||||
let shape = RoundedRectangle(cornerRadius: 20.0)
|
let shape = RoundedRectangle(cornerRadius: 20.0)
|
||||||
if card.isFaceUp {
|
if card.isFaceUp && !card.isMatched {
|
||||||
shape.fill().foregroundColor(.white)
|
shape.fill().foregroundColor(.white)
|
||||||
shape.strokeBorder(lineWidth: 3)
|
shape.strokeBorder(lineWidth: 3)
|
||||||
Text(card.content).font(.largeTitle)
|
Text(card.content).font(.largeTitle)
|
||||||
@ -46,9 +46,9 @@ struct CardView: View {
|
|||||||
struct ContentView_Previews: PreviewProvider {
|
struct ContentView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
let game = EmojiMemoryGame()
|
let game = EmojiMemoryGame()
|
||||||
ContentView(viewModel: game)
|
EmojiMemoryGameView(game: game)
|
||||||
.preferredColorScheme(.dark)
|
.preferredColorScheme(.dark)
|
||||||
ContentView(viewModel: game)
|
EmojiMemoryGameView(game: game)
|
||||||
.preferredColorScheme(.light)
|
.preferredColorScheme(.light)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -12,7 +12,7 @@ struct MemorizeApp: App {
|
|||||||
let game = EmojiMemoryGame()
|
let game = EmojiMemoryGame()
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView(viewModel: game)
|
EmojiMemoryGameView(game: game)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,10 @@ import Foundation
|
|||||||
// model
|
// model
|
||||||
struct MemoryGame<CardContent> where CardContent: Equatable {
|
struct MemoryGame<CardContent> where CardContent: Equatable {
|
||||||
private(set) var cards: [Card]
|
private(set) var cards: [Card]
|
||||||
private var indexOfTheOneAndOnlyFaceUpCard: Int?
|
private var indexOfTheOneAndOnlyFaceUpCard: Int? {
|
||||||
|
get { cards.indices.filter { cards[$0].isFaceUp }.oneAndOnly }
|
||||||
|
set { cards.indices.forEach { cards[$0].isFaceUp = ($0 == newValue) } }
|
||||||
|
}
|
||||||
|
|
||||||
mutating func choose(_ card: Card) {
|
mutating func choose(_ card: Card) {
|
||||||
if let chosenIndex = cards.firstIndex(where: { $0.id == card.id }), !cards[chosenIndex].isFaceUp, !cards[chosenIndex].isMatched {
|
if let chosenIndex = cards.firstIndex(where: { $0.id == card.id }), !cards[chosenIndex].isFaceUp, !cards[chosenIndex].isMatched {
|
||||||
@ -19,16 +22,10 @@ struct MemoryGame<CardContent> where CardContent: Equatable {
|
|||||||
cards[chosenIndex].isMatched = true
|
cards[chosenIndex].isMatched = true
|
||||||
cards[potentialMatchIndex].isMatched = true
|
cards[potentialMatchIndex].isMatched = true
|
||||||
}
|
}
|
||||||
indexOfTheOneAndOnlyFaceUpCard = nil
|
cards[chosenIndex].isFaceUp.toggle()
|
||||||
} else {
|
} else {
|
||||||
for index in cards.indices {
|
|
||||||
if cards[index].isFaceUp {
|
|
||||||
cards[index].isFaceUp.toggle()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
indexOfTheOneAndOnlyFaceUpCard = chosenIndex
|
indexOfTheOneAndOnlyFaceUpCard = chosenIndex
|
||||||
}
|
}
|
||||||
cards[chosenIndex].isFaceUp.toggle()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,8 +39,8 @@ struct MemoryGame<CardContent> where CardContent: Equatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) {
|
init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) {
|
||||||
cards = [Card]()
|
cards = []
|
||||||
//
|
|
||||||
for pairIndex in 0 ..< numberOfPairsOfCards {
|
for pairIndex in 0 ..< numberOfPairsOfCards {
|
||||||
let content: CardContent = createCardContent(pairIndex)
|
let content: CardContent = createCardContent(pairIndex)
|
||||||
cards.append(Card(content: content, id: pairIndex * 2))
|
cards.append(Card(content: content, id: pairIndex * 2))
|
||||||
@ -52,9 +49,19 @@ struct MemoryGame<CardContent> where CardContent: Equatable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Card: Identifiable {
|
struct Card: Identifiable {
|
||||||
var isFaceUp: Bool = false
|
var isFaceUp = false
|
||||||
var isMatched: Bool = false
|
var isMatched = false
|
||||||
var content: CardContent
|
let content: CardContent
|
||||||
var id: Int
|
let id: Int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Array {
|
||||||
|
var oneAndOnly: Element? {
|
||||||
|
if count == 1 {
|
||||||
|
return first
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user