Mercurial > public > lazybear
changeset 162:6488392684cc
Implementing CoreData
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Wed, 17 Feb 2021 20:11:16 +0100 |
parents | 26d69711ea66 |
children | 9d519f230f26 |
files | LazyBear.xcodeproj/project.pbxproj LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate lazybear/Classes/WatchlistManager.swift lazybear/LazyBear.entitlements lazybear/LazyBearApp.swift lazybear/UI/Row.swift lazybear/UI/Watchlist.swift |
diffstat | 7 files changed, 76 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/LazyBear.xcodeproj/project.pbxproj Wed Feb 17 19:18:53 2021 +0100 +++ b/LazyBear.xcodeproj/project.pbxproj Wed Feb 17 20:11:16 2021 +0100 @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 95002580256D17D9008FFD28 /* StoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9500257F256D17D9008FFD28 /* StoreKit.framework */; }; + 950C0BB625DD9867006A49BC /* WatchlistManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950C0BB525DD9867006A49BC /* WatchlistManager.swift */; }; 951F589125DD86F200E051E0 /* TestAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951F589025DD86F200E051E0 /* TestAPI.swift */; }; 951F589A25DD8A8100E051E0 /* Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951F589925DD8A8100E051E0 /* Row.swift */; }; 951F589D25DD8A9C00E051E0 /* Watchlist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 951F589C25DD8A9C00E051E0 /* Watchlist.swift */; }; @@ -22,6 +23,7 @@ /* Begin PBXFileReference section */ 95002578256D1564008FFD28 /* Configuration.storekit */ = {isa = PBXFileReference; lastKnownFileType = text; path = Configuration.storekit; sourceTree = "<group>"; }; 9500257F256D17D9008FFD28 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; }; + 950C0BB525DD9867006A49BC /* WatchlistManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WatchlistManager.swift; path = lazybear/Classes/WatchlistManager.swift; sourceTree = SOURCE_ROOT; }; 951F589025DD86F200E051E0 /* TestAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TestAPI.swift; path = lazybear/Tests/TestAPI.swift; sourceTree = SOURCE_ROOT; }; 951F589925DD8A8100E051E0 /* Row.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Row.swift; path = lazybear/UI/Row.swift; sourceTree = SOURCE_ROOT; }; 951F589C25DD8A9C00E051E0 /* Watchlist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Watchlist.swift; path = lazybear/UI/Watchlist.swift; sourceTree = SOURCE_ROOT; }; @@ -60,6 +62,14 @@ name = Frameworks; sourceTree = "<group>"; }; + 950C0BB425DD9852006A49BC /* Classes */ = { + isa = PBXGroup; + children = ( + 950C0BB525DD9867006A49BC /* WatchlistManager.swift */, + ); + path = Classes; + sourceTree = "<group>"; + }; 951F588F25DD86E600E051E0 /* Tests */ = { isa = PBXGroup; children = ( @@ -108,6 +118,7 @@ 95B04EB225212369000AD27F /* LazyBearApp.swift */, 95B04EB425212369000AD27F /* ContentView.swift */, 951F589825DD8A1B00E051E0 /* UI */, + 950C0BB425DD9852006A49BC /* Classes */, 951F588F25DD86E600E051E0 /* Tests */, ); path = LazyBear; @@ -185,6 +196,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 950C0BB625DD9867006A49BC /* WatchlistManager.swift in Sources */, 95B04EB525212369000AD27F /* ContentView.swift in Sources */, 95B04EB325212369000AD27F /* LazyBearApp.swift in Sources */, 951F589125DD86F200E051E0 /* TestAPI.swift in Sources */,
Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Classes/WatchlistManager.swift Wed Feb 17 20:11:16 2021 +0100 @@ -0,0 +1,29 @@ +// +// WatchlistManager.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 17/2/21. +// + +import SwiftUI +/* +class WatchlistManager { + @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \WatchlistCompanies.name, ascending: true)]) + var companies: FetchedResults<WatchlistCompanies> + + // Add to watchlist + func addWatchlist(name: String, symbol: String) { + let watchlistCompanies = WatchlistCompanies(context: viewContext) + watchlistCompanies.name = name + watchlistCompanies.symbol = symbol + do { + try viewContext.save() + print("Company saved.") + } catch { + print(error.localizedDescription) + } + } + +} +*/
--- a/lazybear/LazyBear.entitlements Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/LazyBear.entitlements Wed Feb 17 20:11:16 2021 +0100 @@ -2,8 +2,6 @@ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> - <key>aps-environment</key> - <string>development</string> <key>com.apple.developer.icloud-container-identifiers</key> <array> <string>iCloud.dennis.LazyBear</string>
--- a/lazybear/LazyBearApp.swift Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/LazyBearApp.swift Wed Feb 17 20:11:16 2021 +0100 @@ -9,10 +9,12 @@ @main struct LazyBearApp: App { + let persistenceController = PersistenceController.shared var body: some Scene { WindowGroup { - TestAPI() + ContentView() + .environment(\.managedObjectContext, persistenceController.container.viewContext) } } }
--- a/lazybear/UI/Row.swift Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/UI/Row.swift Wed Feb 17 20:11:16 2021 +0100 @@ -8,11 +8,11 @@ import SwiftUI struct Row: View { - var baseText: String - var underText: String? + @State var baseText: String + @State var underText: String? - var leftView: AnyView? - var rightView: AnyView? + @State var leftView: AnyView? + @State var rightView: AnyView? var body: some View { HStack {
--- a/lazybear/UI/Watchlist.swift Wed Feb 17 19:18:53 2021 +0100 +++ b/lazybear/UI/Watchlist.swift Wed Feb 17 20:11:16 2021 +0100 @@ -8,8 +8,35 @@ import SwiftUI struct Watchlist: View { + @Environment(\.managedObjectContext) private var viewContext + @FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \WatchlistCompanies.name, ascending: true)]) + var companies: FetchedResults<WatchlistCompanies> + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + //let watchlistManager = WatchlistManager() + + + List(companies) { company in + let name = company.name ?? "Preview name" + let symbol = company.symbol ?? "Preview symbol" + Row(baseText: name, underText: symbol) + } + .onAppear { + //watchlistManager.addWatchlist(name: "Test", symbol: "Test") + } + } + + // Add to watchlist + private func addWatchlist(name: String, symbol: String) { + let watchlistCompanies = WatchlistCompanies(context: viewContext) + watchlistCompanies.name = name + watchlistCompanies.symbol = symbol + do { + try viewContext.save() + print("Company saved.") + } catch { + print(error.localizedDescription) + } } }