feat(view, viewmodel): 新增model 和对应的 viewmodel

新增model 和对应的 viewmodel

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2023-02-12 18:28:14 +08:00
parent 673adb8752
commit 87b6465e68
4 changed files with 65 additions and 39 deletions

View File

@ -11,6 +11,8 @@
240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 240EDC402998A3B900A46AC9 /* ContentView.swift */; };
240EDC432998A3BA00A46AC9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 240EDC422998A3BA00A46AC9 /* 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 */; };
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -19,6 +21,8 @@
240EDC402998A3B900A46AC9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; 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>"; };
245099F22998EAD6000CE9DA /* MemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryGame.swift; sourceTree = "<group>"; };
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiMemoryGame.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -53,6 +57,8 @@
children = (
240EDC3E2998A3B900A46AC9 /* MemorizeApp.swift */,
240EDC402998A3B900A46AC9 /* ContentView.swift */,
245099F22998EAD6000CE9DA /* MemoryGame.swift */,
245099F42998EC71000CE9DA /* EmojiMemoryGame.swift */,
240EDC422998A3BA00A46AC9 /* Assets.xcassets */,
240EDC442998A3BA00A46AC9 /* Preview Content */,
);
@ -137,8 +143,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
245099F32998EAD6000CE9DA /* MemoryGame.swift in Sources */,
240EDC412998A3B900A46AC9 /* ContentView.swift in Sources */,
240EDC3F2998A3B900A46AC9 /* MemorizeApp.swift in Sources */,
245099F52998EC71000CE9DA /* EmojiMemoryGame.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -20,36 +20,11 @@ struct ContentView: View {
}
}
.foregroundColor(/*@START_MENU_TOKEN@*/ .red/*@END_MENU_TOKEN@*/)
Spacer()
HStack{
remove
Spacer()
add
}
.padding(.horizontal)
.font(.largeTitle)
}
.padding(.horizontal)
}
var remove: some View {
Button(action: {
if emojiCount > 1{
emojiCount -= 1
}
}, label: {
Image(systemName: "minus.circle")
})
}
var add: some View {
Button(action: {
if emojiCount < emojis.count {
emojiCount += 1
}
}, label: {
Image(systemName: "plus.circle")
})
}
}
struct CardView: View {
@ -78,17 +53,6 @@ struct CardView: View {
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()

View File

@ -0,0 +1,23 @@
//
// EmojiMemoryGame.swift
// Memorize
//
// Created by ching on 2023/2/12.
//
import SwiftUI
// viewmodel
class EmojiMemoryGame {
static let emojis = ["🚌", "🚙", "🚗", "🚕", "🏎", "🚎", "🚓"]
static func createMemoryGame() -> MemoryGame<String> {
MemoryGame<String>(numberOfPairsOfCards: 5) { pairIndex in EmojiMemoryGame.emojis[pairIndex] }
}
private var model: MemoryGame<String> = createMemoryGame()
var cards: [MemoryGame<String>.Card] {
return model.cards
}
}

31
Memorize/MemoryGame.swift Normal file
View File

@ -0,0 +1,31 @@
//
// MemoryGame.swift
// Memorize
//
// Created by ching on 2023/2/12.
//
import Foundation
// model
struct MemoryGame<CardContent> {
private(set) var cards: [Card]
func choose(_ card: Card) {}
init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) {
cards = [Card]()
//
for pairIndex in 0 ..< numberOfPairsOfCards {
let content: CardContent = createCardContent(pairIndex)
cards.append(Card(content: content))
cards.append(Card(content: content))
}
}
struct Card {
var isFaceUp: Bool = false
var isMatched: Bool = false
var content: CardContent
}
}