Skip to content

Commit

Permalink
completed mvvm structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmodom committed Mar 12, 2022
1 parent 281fc9b commit 4c0c3c8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
32 changes: 17 additions & 15 deletions Memorize/Memorize/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import SwiftUI

struct ContentView: View {
var emojis = ["✈️", "🚝", "🚌", "🚲", "🚘", "🚁", "🛵", "🏎", "🚃", "🛺", "⛵️", "🚤", "🚕", "🚚", "🛸", "🛶", "🚢", "🚡", "🛰", "🛻", "🚜", "🏍", "🚔", "🛴"]
@State var emojiCount = 4
@ObservedObject var viewModel: EmojiMemoryGame

var body: some View {
VStack{
ScrollView{
LazyVGrid(columns: [GridItem(.adaptive(minimum: 75))] ){
ForEach(emojis[0..<emojiCount], id:\.self, content:{ emoji in
CardView(isFaceUp:true,content: emoji).aspectRatio(2/3, contentMode: .fit)
})
ForEach(viewModel.cards){ card in
CardView(card: card)
.aspectRatio(2/3, contentMode: .fit)
.onTapGesture {
viewModel.choose(card)
}
}
}
}
.foregroundColor(.red)
Expand All @@ -34,7 +37,7 @@ struct ContentView: View {
.padding(.horizontal)
}
//plus.circle
var removeButton: some View{
/*var removeButton: some View{
Button(action: {
if emojiCount > 1{
emojiCount -= 1
Expand All @@ -52,41 +55,40 @@ struct ContentView: View {
}, label: {
Image(systemName: "plus.circle")
})
}
}*/
}


struct CardView: View{
@State var isFaceUp: Bool
var content: String
let card: MemoryGame<String>.Card
var body: some View{
ZStack{
let shape = RoundedRectangle(cornerRadius: 20.0)
if isFaceUp{
if card.isFacedUp{
shape
.fill()
.foregroundColor(.white)
shape
.strokeBorder(lineWidth: 3.0)

Text(content)
Text(card.content)
.font(.largeTitle)
.foregroundColor(.blue)
}else{
shape
.fill()
}

}.onTapGesture {
isFaceUp = !isFaceUp
}
}
}

struct ContentView_Previews: PreviewProvider {

static var previews: some View {
let game = EmojiMemoryGame()
//create for each color scheme to see the ui effects
ContentView().preferredColorScheme(.light)
ContentView().preferredColorScheme(.dark)
ContentView(viewModel: game).preferredColorScheme(.light)
ContentView(viewModel: game).preferredColorScheme(.dark)
}
}
14 changes: 9 additions & 5 deletions Memorize/Memorize/EmojiMemoryGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import SwiftUI


class EmojiMemoryGame {
class EmojiMemoryGame: ObservableObject {
/* private(set) var model: MemoryGame<String> = MemoryGame<String>(numberOfPairsOfCards: 4, createCardContent: { pairIndex in
return emojis[pairIndex]
})*/

private var model: MemoryGame<String> = createMemoryGame()


@Published private var model: MemoryGame<String> = createMemoryGame()
static let emojis = ["✈️", "🚝", "🚌", "🚲", "🚘", "🚁", "🛵", "🏎", "🚃", "🛺", "⛵️", "🚤", "🚕", "🚚", "🛸", "🛶", "🚢", "🚡", "🛰", "🛻", "🚜", "🏍", "🚔", "🛴"]



static func createMemoryGame() -> MemoryGame<String>{
return MemoryGame<String>(numberOfPairsOfCards: 4, createCardContent: { pairIndex in
emojis[pairIndex]
Expand All @@ -31,6 +29,12 @@ class EmojiMemoryGame {
return model.cards
}

// MARK: Intents
func choose(_ card: MemoryGame<String>.Card){
//objectWillChange.send()
model.choose(card)
}

}


3 changes: 2 additions & 1 deletion Memorize/Memorize/MemorizeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import SwiftUI

@main
struct MemorizeApp: App {
let game = EmojiMemoryGame()
var body: some Scene {
WindowGroup {
ContentView()
ContentView(viewModel: game)
}
}
}
26 changes: 18 additions & 8 deletions Memorize/Memorize/MemoryGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@ struct MemoryGame<CardContent>{
//TODO: add numberOfPairsOfCards * 2 to card array
for pairIndex in 0..<numberOfPairsOfCards{
let content = createCardContent(pairIndex)
cards.append(Card(content: content))
cards.append(Card(content: content))
cards.append(Card(id: pairIndex*2 ,content: content))
cards.append(Card(id: pairIndex*2+1,content: content))
}
}


func choose(_ card: Card){

func index(of card: Card) -> Int{
for index in 0..<cards.count{
if cards[index].id == card.id
{
return index
}
}
return -1 // TODO: fix this make it nice
}

mutating func choose(_ card: Card){
let chosenIndex = index(of: card)
cards[chosenIndex].isFacedUp.toggle()

print("\(cards)")
}



struct Card{
struct Card: Identifiable{
var id: Int
var isFacedUp: Bool = false
var isMatched: Bool = false
var content: CardContent
Expand Down

0 comments on commit 4c0c3c8

Please sign in to comment.