Skip to content

Commit

Permalink
Ugh! Took forever to realize used wrong struct name in new screen. Ev…
Browse files Browse the repository at this point in the history
…erything is functional now so yay!
  • Loading branch information
storiwr committed Sep 3, 2020
1 parent 838ed74 commit 18879ce
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 47 deletions.
4 changes: 4 additions & 0 deletions CardStack.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DC6D320324FC31150020AD3E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DC6D320224FC31150020AD3E /* Assets.xcassets */; };
DC6D320624FC31150020AD3E /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DC6D320524FC31150020AD3E /* Preview Assets.xcassets */; };
DC6D320E24FE94120020AD3E /* CardDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC6D320D24FE94120020AD3E /* CardDataModel.swift */; };
DCB765982501771600D12594 /* NewCardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCB765972501771600D12594 /* NewCardView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -22,6 +23,7 @@
DC6D320524FC31150020AD3E /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
DC6D320724FC31150020AD3E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
DC6D320D24FE94120020AD3E /* CardDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CardDataModel.swift; sourceTree = "<group>"; };
DCB765972501771600D12594 /* NewCardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewCardView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -56,6 +58,7 @@
children = (
DC6D31FE24FC31130020AD3E /* CardStackApp.swift */,
DC6D320024FC31130020AD3E /* ContentView.swift */,
DCB765972501771600D12594 /* NewCardView.swift */,
DC6D320D24FE94120020AD3E /* CardDataModel.swift */,
DC6D320224FC31150020AD3E /* Assets.xcassets */,
DC6D320724FC31150020AD3E /* Info.plist */,
Expand Down Expand Up @@ -144,6 +147,7 @@
DC6D320124FC31130020AD3E /* ContentView.swift in Sources */,
DC6D320E24FE94120020AD3E /* CardDataModel.swift in Sources */,
DC6D31FF24FC31130020AD3E /* CardStackApp.swift in Sources */,
DCB765982501771600D12594 /* NewCardView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
130 changes: 83 additions & 47 deletions CardStack/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,104 @@

import SwiftUI

class CardData: ObservableObject {
@Published var name = "Hello Card"
@Published var cardBody = "I'm so happy to be here!"
struct CardInfo: Codable, Identifiable {
let id = UUID()
let name: String
let cardBody: String
// let amount: Int
}

struct ContentView: View {
@ObservedObject private var card = CardData()
@State private var showingSheet = false
var body: some View {
NavigationView {
List {
Text(card.name)
Text(card.cardBody)
}.navigationBarTitle("Cards")
.navigationBarItems(trailing:
Button("New"){
self.showingSheet.toggle()
} .sheet(isPresented: $showingSheet, content: {
NewView()
}))
class Cards: ObservableObject {
@Published var items: [CardInfo] {
didSet {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(items) {
UserDefaults.standard.set(encoded, forKey: "Items")
}
}
}
init() {
if let items = UserDefaults.standard.data(forKey: "Items") {
let decoder = JSONDecoder()
if let decoded = try? decoder.decode([CardInfo].self, from: items) {
self.items = decoded
return
}
}
}

self.items = []
}
}

struct DetailView: View {
@ObservedObject private var card = CardData()
struct ContentView: View {
@ObservedObject var cards = Cards()
@State private var showingNewCardView = false
var body: some View {
NavigationView {
List {
Text(card.cardBody)
Text(card.name)
}.navigationBarTitle(Text("Card Title"))
.navigationBarItems(trailing: EditButton())

}
ForEach(cards.items) { card in
VStack {
Text(card.name)
Text(card.cardBody)
}

}.onDelete(perform: removeItems)

}.navigationBarTitle("Cards")
.navigationBarItems(trailing:
Button(action: {
self.showingNewCardView = true
}) {
Image(systemName: "plus")
}
)
.sheet(isPresented: $showingNewCardView) {
NewCardView(cards: self.cards)
}
}
}
func removeItems(at offsets: IndexSet) {
cards.items.remove(atOffsets: offsets)
}
}

//struct DetailView: View {
// @ObservedObject private var card = CardInfo()
// var body: some View {
// NavigationView {
// List {
// Text(card.cardBody)
// Text(card.name)
// }.navigationBarTitle(Text("Card Title"))
// .navigationBarItems(trailing: EditButton())
//
// }
// }
//}

struct NewView: View {
@ObservedObject private var card = CardData()
@Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView {
List {
TextField("Title", text: $card.name)
TextField("Body", text: $card.cardBody)
}.navigationBarTitle(Text("NewCard"))
.navigationBarItems(trailing:
Button("Done") {
self.presentationMode.wrappedValue.dismiss()
}
)}
}
}
// struct NewView: View {
// @ObservedObject private var card = CardInfo()
// @Environment(\.presentationMode) var presentationMode
//
// var body: some View {
// NavigationView {
// List {
// TextField("Title", text: $card.name)
// TextField("Body", text: $card.cardBody)
// }.navigationBarTitle(Text("NewCard"))
// .navigationBarItems(trailing:
// Button("Done") {
// self.presentationMode.wrappedValue.dismiss()
// }
//
// )}
// }
// }


struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}

41 changes: 41 additions & 0 deletions CardStack/NewCardView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// NewCardView.swift
// CardStack
//
// Created by Stephen on 9/3/20.
//

import SwiftUI

struct NewCardView: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var cards: Cards
@State private var name = ""
@State private var cardBody = ""
// @State private var amount = ""

var body: some View {
NavigationView {
Form {
TextField("Title", text: $name)
TextField("Body", text: $cardBody)

}
.navigationBarTitle("New Card")
.navigationBarItems(trailing: Button("Done") {
// if let actualAmount = Int(self.amount) {
let item = CardInfo(name: self.name, cardBody: self.cardBody)
self.cards.items.append(item)
self.presentationMode.wrappedValue.dismiss()
// }
})
}
}
}


struct NewCardView_Previews: PreviewProvider {
static var previews: some View {
NewCardView(cards: Cards())
}
}

0 comments on commit 18879ce

Please sign in to comment.