# HG changeset patch # User Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> # Date 1612207172 -3600 # Node ID 16f8514cc5e67c36f0a4038ce35a7c3a1a4950dd # Parent cd6177f15ded8bd239f4d2c2080d65e4f0721128 Implement insider transactions diff -r cd6177f15ded -r 16f8514cc5e6 LazyBear.xcodeproj/project.pbxproj --- a/LazyBear.xcodeproj/project.pbxproj Mon Feb 01 19:15:26 2021 +0100 +++ b/LazyBear.xcodeproj/project.pbxproj Mon Feb 01 20:19:32 2021 +0100 @@ -13,6 +13,9 @@ 9520F0B225C712D000692610 /* LineChartShape.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9520F0B125C712D000692610 /* LineChartShape.swift */; }; 9520F0B525C7131300692610 /* LineChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9520F0B425C7131300692610 /* LineChart.swift */; }; 95363CDC25C8741900B74131 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CDB25C8741900B74131 /* Request.swift */; }; + 95363CE225C87CF000B74131 /* InsiderTransactionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CE125C87CF000B74131 /* InsiderTransactionModel.swift */; }; + 95363CE625C87FEC00B74131 /* InsiderTransactions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CE525C87FEC00B74131 /* InsiderTransactions.swift */; }; + 95363CEA25C8858800B74131 /* TransactionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95363CE925C8858800B74131 /* TransactionRow.swift */; }; 954D992525A2123B001F7F60 /* HistoricalPricesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */; }; 954DDF0425C456E800848A4B /* QuoteModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954DDF0325C456E800848A4B /* QuoteModel.swift */; }; 95612C512598D48200F7698F /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95612C4F2598D48200F7698F /* SearchBar.swift */; }; @@ -55,6 +58,9 @@ 9520F0B125C712D000692610 /* LineChartShape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LineChartShape.swift; path = lazybear/Views/LineChartShape.swift; sourceTree = SOURCE_ROOT; }; 9520F0B425C7131300692610 /* LineChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LineChart.swift; path = lazybear/Views/LineChart.swift; sourceTree = SOURCE_ROOT; }; 95363CDB25C8741900B74131 /* Request.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Request.swift; path = lazybear/Network/Request.swift; sourceTree = SOURCE_ROOT; }; + 95363CE125C87CF000B74131 /* InsiderTransactionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransactionModel.swift; path = lazybear/Models/InsiderTransactionModel.swift; sourceTree = SOURCE_ROOT; }; + 95363CE525C87FEC00B74131 /* InsiderTransactions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransactions.swift; path = lazybear/Views/InsiderTransactions.swift; sourceTree = SOURCE_ROOT; }; + 95363CE925C8858800B74131 /* TransactionRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TransactionRow.swift; path = lazybear/Views/TransactionRow.swift; sourceTree = SOURCE_ROOT; }; 954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HistoricalPricesModel.swift; path = lazybear/Models/HistoricalPricesModel.swift; sourceTree = SOURCE_ROOT; }; 954DDF0325C456E800848A4B /* QuoteModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = QuoteModel.swift; path = lazybear/Models/QuoteModel.swift; sourceTree = SOURCE_ROOT; }; 95612C4F2598D48200F7698F /* SearchBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = ""; }; @@ -128,6 +134,7 @@ 954D992425A2123B001F7F60 /* HistoricalPricesModel.swift */, 95FE646A25C30B880052832E /* ApiModel.swift */, 954DDF0325C456E800848A4B /* QuoteModel.swift */, + 95363CE125C87CF000B74131 /* InsiderTransactionModel.swift */, ); path = Models; sourceTree = ""; @@ -153,6 +160,8 @@ 95F6C30825BAF7C2003CF389 /* DateSelection.swift */, 9520F0B125C712D000692610 /* LineChartShape.swift */, 9520F0B425C7131300692610 /* LineChart.swift */, + 95363CE525C87FEC00B74131 /* InsiderTransactions.swift */, + 95363CE925C8858800B74131 /* TransactionRow.swift */, ); path = Views; sourceTree = ""; @@ -306,7 +315,9 @@ 9520F0B525C7131300692610 /* LineChart.swift in Sources */, 9597CE0425C1DFE7004DDFED /* LogoPlaceholder.swift in Sources */, 95FE646B25C30B880052832E /* ApiModel.swift in Sources */, + 95363CE625C87FEC00B74131 /* InsiderTransactions.swift in Sources */, 95F6C30525BAF599003CF389 /* CompanyHeader.swift in Sources */, + 95363CEA25C8858800B74131 /* TransactionRow.swift in Sources */, 95612C512598D48200F7698F /* SearchBar.swift in Sources */, 95E411BE25BEEA6C00A9C23F /* WatchlistRow.swift in Sources */, 95AD892425C5D8A200BCE8E4 /* AddWatchlist.swift in Sources */, @@ -330,6 +341,7 @@ 95E411A725BEE03000A9C23F /* Watchlist.swift in Sources */, 95825AFC25C7255600465409 /* NormalizeData.swift in Sources */, 95363CDC25C8741900B74131 /* Request.swift in Sources */, + 95363CE225C87CF000B74131 /* InsiderTransactionModel.swift in Sources */, 95F7CAF625ADC7B7009E0E7C /* LazyBear.xcdatamodeld in Sources */, 95FE646825C2DC580052832E /* WatchlistCompany+CoreDataProperties.swift in Sources */, ); diff -r cd6177f15ded -r 16f8514cc5e6 LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Jobs/NormalizeData.swift --- a/lazybear/Jobs/NormalizeData.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Jobs/NormalizeData.swift Mon Feb 01 20:19:32 2021 +0100 @@ -5,7 +5,7 @@ // Created by Dennis Concepción Martín on 31/1/21. // -import SwiftUI +import Foundation func normalize(_ data: [Double]) -> [DataPoint] { var normalData = [DataPoint]() diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Jobs/ReadJson.swift --- a/lazybear/Jobs/ReadJson.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Jobs/ReadJson.swift Mon Feb 01 20:19:32 2021 +0100 @@ -5,7 +5,7 @@ // Created by Dennis Concepción Martín on 31/12/20. // -import SwiftUI +import Foundation // With this function I parse the local JSON file to read it and create a list with its items. let companiesData: [CompanyModel] = load("companies.json") diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Models/ApiModel.swift --- a/lazybear/Models/ApiModel.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Models/ApiModel.swift Mon Feb 01 20:19:32 2021 +0100 @@ -5,7 +5,7 @@ // Created by Dennis Concepción Martín on 28/1/21. // -import SwiftUI +import Foundation struct ApiModel { var key: String? diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Models/CompanyModel.swift --- a/lazybear/Models/CompanyModel.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Models/CompanyModel.swift Mon Feb 01 20:19:32 2021 +0100 @@ -5,7 +5,7 @@ // Created by Dennis Concepción Martín on 31/12/20. // -import SwiftUI +import Foundation struct CompanyModel: Hashable, Codable { var symbol: String diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Models/HistoricalPricesModel.swift --- a/lazybear/Models/HistoricalPricesModel.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Models/HistoricalPricesModel.swift Mon Feb 01 20:19:32 2021 +0100 @@ -5,7 +5,7 @@ // Created by Dennis Concepción Martín on 3/1/21. // -import SwiftUI +import Foundation struct HistoricalPricesModel: Codable { var date: String diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Models/InsiderTransactionModel.swift --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Models/InsiderTransactionModel.swift Mon Feb 01 20:19:32 2021 +0100 @@ -0,0 +1,22 @@ +// +// InsiderTransactions.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 1/2/21. +// + +import Foundation + +struct InsiderTransactionModel: Codable, Hashable { + var conversionOrExercisePrice: Float? + var directIndirect: String? + var filingDate: String? + var fullName: String? + var postShares: Int? + var reportedTitle: String? + var transactionCode: String? + var transactionDate: String? + var transactionPrice: Float? + var transactionShares: Int? + var transactionValue: Double? +} diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Models/QuoteModel.swift --- a/lazybear/Models/QuoteModel.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Models/QuoteModel.swift Mon Feb 01 20:19:32 2021 +0100 @@ -5,7 +5,7 @@ // Created by Dennis Concepción Martín on 29/1/21. // -import SwiftUI +import Foundation struct QuoteModel: Codable { var latestPrice: Float diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Views/Company.swift --- a/lazybear/Views/Company.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Views/Company.swift Mon Feb 01 20:19:32 2021 +0100 @@ -19,8 +19,11 @@ ScrollView { VStack(alignment: .leading) { Stock(name: name, symbol: symbol, lineChartHeight: geo.size.height*0.2) - .environment(\.managedObjectContext, persistenceController.container.viewContext) + .padding(.bottom) + + InsiderTransactions(symbol: symbol) } + .environment(\.managedObjectContext, persistenceController.container.viewContext) } } } diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Views/InsiderTransactions.swift --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Views/InsiderTransactions.swift Mon Feb 01 20:19:32 2021 +0100 @@ -0,0 +1,50 @@ +// +// Insiders.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 1/2/21. +// + +import SwiftUI + +struct InsiderTransactions: View { + var symbol: String + @EnvironmentObject var apiAccess: ApiAccess + + // <--------- API Job ---------> + @State private var url = String() { + didSet { request(url: url, model: [InsiderTransactionModel].self) { self.data = $0 } }} + + @State private var data = [InsiderTransactionModel]() + // <--------- API Job ---------> + + var body: some View { + VStack(alignment: .leading) { + Text("Insider transactions") + .font(.title) + .fontWeight(.semibold) + .padding([.leading, .bottom]) + + ForEach(data, id: \.self) { data in + TransactionRow(data: data) + } + } + .onAppear { getUrl() } + } + + private func getUrl() { + // 1 -> Sandbox / 2 -> Production + let baseUrl = apiAccess.results[1].url ?? "" + let token = apiAccess.results[1].key ?? "" + let path = "/stable/stock/\(symbol)/insider-transactions?token=" + + self.url = baseUrl + path + token + print(url) + } +} + +struct InsiderTransactions_Previews: PreviewProvider { + static var previews: some View { + InsiderTransactions(symbol: "aapl") + } +} diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Views/Stock.swift --- a/lazybear/Views/Stock.swift Mon Feb 01 19:15:26 2021 +0100 +++ b/lazybear/Views/Stock.swift Mon Feb 01 20:19:32 2021 +0100 @@ -72,7 +72,6 @@ let path = "/stable/stock/\(symbol)/chart/\(range)?chartCloseOnly=true&includeToday=false&token=" self.url = baseUrl + path + token - print(self.url) } private func colorLineChart(prices: [Double]) -> Bool { diff -r cd6177f15ded -r 16f8514cc5e6 lazybear/Views/TransactionRow.swift --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Views/TransactionRow.swift Mon Feb 01 20:19:32 2021 +0100 @@ -0,0 +1,50 @@ +// +// TransactionRow.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 1/2/21. +// + +import SwiftUI + +struct TransactionRow: View { + @State var data: InsiderTransactionModel + + var body: some View { + VStack { + HStack(alignment: .top) { + VStack(alignment: .leading) { + Text(data.fullName ?? "-".capitalized) + .fontWeight(.semibold) + + Text(data.transactionDate ?? "-") + .font(.subheadline) + } + Spacer() + Text("$\(data.transactionValue ?? 0, specifier: "%.2f")") + .fontWeight(.semibold) + } + .padding([.leading, .trailing]) + + Divider() + } + } +} + +struct TransactionRow_Previews: PreviewProvider { + static var previews: some View { + TransactionRow(data: InsiderTransactionModel( + conversionOrExercisePrice: 100, + directIndirect: "D", + filingDate: "2020-10-09", + fullName: "Tim", + postShares: 100, + reportedTitle: "Some reported title", + transactionCode: "B", + transactionDate: "2020-10-09", + transactionPrice: 50, + transactionShares: 1000, + transactionValue: 50000) + ) + } +}