# HG changeset patch # User Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> # Date 1610994878 -3600 # Node ID 4de3f0d5f305f721f616cb1d3da76843a34197c5 # Parent d53c220dcfa41dccef70d5beb9be55b338eb9767 Update grid for iPad, add new charts, update README diff -r d53c220dcfa4 -r 4de3f0d5f305 LazyBear.xcodeproj/project.pbxproj --- a/LazyBear.xcodeproj/project.pbxproj Sun Jan 17 18:56:42 2021 +0100 +++ b/LazyBear.xcodeproj/project.pbxproj Mon Jan 18 19:34:38 2021 +0100 @@ -30,6 +30,8 @@ 958DF3D825A08F4E00D10D22 /* Stock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958DF3D725A08F4E00D10D22 /* Stock.swift */; }; 958DF3DB25A08F8600D10D22 /* Insiders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958DF3DA25A08F8600D10D22 /* Insiders.swift */; }; 958E472B25B1CA8B0048E770 /* FavCompanies.swift in Sources */ = {isa = PBXBuildFile; fileRef = 958E472A25B1CA8B0048E770 /* FavCompanies.swift */; }; + 959B940925B5F4BC00EEB802 /* LazyColumns.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959B940825B5F4BC00EEB802 /* LazyColumns.swift */; }; + 959B940C25B6058E00EEB802 /* StockStats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 959B940B25B6058E00EEB802 /* StockStats.swift */; }; 95A1ECAF25A36127001D4A21 /* InsiderTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A1ECAE25A36127001D4A21 /* InsiderTransaction.swift */; }; 95A1ECB225A36230001D4A21 /* InsiderTransactionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A1ECB125A36230001D4A21 /* InsiderTransactionModel.swift */; }; 95A1ECC525A37541001D4A21 /* TransactionRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A1ECC425A37541001D4A21 /* TransactionRow.swift */; }; @@ -75,6 +77,8 @@ 958DF3D725A08F4E00D10D22 /* Stock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Stock.swift; path = lazybear/Stock.swift; sourceTree = SOURCE_ROOT; }; 958DF3DA25A08F8600D10D22 /* Insiders.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Insiders.swift; path = lazybear/Insiders.swift; sourceTree = SOURCE_ROOT; }; 958E472A25B1CA8B0048E770 /* FavCompanies.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FavCompanies.swift; path = "lazybear/Supply views/FavCompanies.swift"; sourceTree = SOURCE_ROOT; }; + 959B940825B5F4BC00EEB802 /* LazyColumns.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LazyColumns.swift; path = lazybear/Functions/LazyColumns.swift; sourceTree = SOURCE_ROOT; }; + 959B940B25B6058E00EEB802 /* StockStats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StockStats.swift; sourceTree = ""; }; 95A1ECAE25A36127001D4A21 /* InsiderTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransaction.swift; path = lazybear/Functions/InsiderTransaction.swift; sourceTree = SOURCE_ROOT; }; 95A1ECB125A36230001D4A21 /* InsiderTransactionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsiderTransactionModel.swift; path = lazybear/Models/InsiderTransactionModel.swift; sourceTree = SOURCE_ROOT; }; 95A1ECC425A37541001D4A21 /* TransactionRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionRow.swift; sourceTree = ""; }; @@ -146,6 +150,7 @@ 950B79FB25B1CFD400E5DB5B /* StockCharts.swift */, 950B79FE25B1E68D00E5DB5B /* InsiderCharts.swift */, 950B7A0325B1E7E100E5DB5B /* TransactionList.swift */, + 959B940B25B6058E00EEB802 /* StockStats.swift */, ); name = "Supply views"; path = "lazybear/Supply views"; @@ -157,6 +162,7 @@ 95AB4A79259DCBAE0064C9C1 /* ReadJson.swift */, 954D998225A27571001F7F60 /* HistoricalPrices.swift */, 95A1ECAE25A36127001D4A21 /* InsiderTransaction.swift */, + 959B940825B5F4BC00EEB802 /* LazyColumns.swift */, ); path = Functions; sourceTree = ""; @@ -312,7 +318,9 @@ files = ( 95AB4A7A259DCBAE0064C9C1 /* ReadJson.swift in Sources */, 95DF5194259DDC45003790B2 /* AboutButton.swift in Sources */, + 959B940C25B6058E00EEB802 /* StockStats.swift in Sources */, 950B79F925B1CEA100E5DB5B /* PriceOverview.swift in Sources */, + 959B940925B5F4BC00EEB802 /* LazyColumns.swift in Sources */, 95612C512598D48200F7698F /* SearchBar.swift in Sources */, 95A1ECC525A37541001D4A21 /* TransactionRow.swift in Sources */, 950B79F625B1CB7A00E5DB5B /* CompanyList.swift in Sources */, diff -r d53c220dcfa4 -r 4de3f0d5f305 LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate Binary file LazyBear.xcodeproj/project.xcworkspace/xcuserdata/dennis.xcuserdatad/UserInterfaceState.xcuserstate has changed diff -r d53c220dcfa4 -r 4de3f0d5f305 README.md --- a/README.md Sun Jan 17 18:56:42 2021 +0100 +++ b/README.md Mon Jan 18 19:34:38 2021 +0100 @@ -21,6 +21,10 @@ ## Contact us Send me a DM [Twitter](https://twitter.com/dennisconcep) +## Open source frameworks used +[ChartView](https://github.com/AppPear/ChartView) +[swiftui-charts](https://github.com/spacenation/swiftui-charts) + ## License Repository under [MIT LICENSE](https://en.wikipedia.org/wiki/MIT_License). Check [LICENSE](LICENSE) for more details diff -r d53c220dcfa4 -r 4de3f0d5f305 lazybear/Functions/LazyColumns.swift --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Functions/LazyColumns.swift Mon Jan 18 19:34:38 2021 +0100 @@ -0,0 +1,24 @@ +// +// LazyColumns.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 18/1/21. +// + +import SwiftUI + +// Change number of columns LazyGrid depending on the device +func lazyColumns(verticalSizeClass: UserInterfaceSizeClass, horizontalSizeClass: UserInterfaceSizeClass, iphoneColumns: Int, ipadColumns: Int) -> [GridItem] { + var columns = [GridItem]() + if horizontalSizeClass == .compact && verticalSizeClass == .regular { + print("Running on iPhone") + columns = Array(repeating: .init(.flexible()), count: iphoneColumns) + } + else if horizontalSizeClass == .regular && verticalSizeClass == .regular { + print("Running on iPad") + columns = Array(repeating: .init(.flexible()), count: ipadColumns) + } + + return columns + +} diff -r d53c220dcfa4 -r 4de3f0d5f305 lazybear/Supply views/Companies.swift --- a/lazybear/Supply views/Companies.swift Sun Jan 17 18:56:42 2021 +0100 +++ b/lazybear/Supply views/Companies.swift Mon Jan 18 19:34:38 2021 +0100 @@ -18,7 +18,10 @@ var body: some View { ScrollView { - LazyVGrid(columns: columns(), spacing: 20) { + let columns = lazyColumns(verticalSizeClass: verticalSizeClass!, horizontalSizeClass: horizontalSizeClass!, + iphoneColumns: 2, ipadColumns: 4) + + LazyVGrid(columns: columns, spacing: 20) { ForEach((0...15), id: \.self) { index in NavigationLink(destination: Company(cik: ciks[index], symbol: symbols[index], name: names[index]) .navigationBarTitle(names[index].capitalized) @@ -40,22 +43,6 @@ .padding() } } - - // Change number of columns depend on the device - func columns() -> [GridItem] { - var columns = [GridItem]() - if horizontalSizeClass == .compact && verticalSizeClass == .regular { - print("Running on iPhone") - columns = Array(repeating: .init(.flexible()), count: 2) - } - else if horizontalSizeClass == .regular && verticalSizeClass == .regular { - print("Running on iPad") - columns = Array(repeating: .init(.flexible()), count: 4) - } - - return columns - - } } struct Companies_Previews: PreviewProvider { diff -r d53c220dcfa4 -r 4de3f0d5f305 lazybear/Supply views/StockCharts.swift --- a/lazybear/Supply views/StockCharts.swift Sun Jan 17 18:56:42 2021 +0100 +++ b/lazybear/Supply views/StockCharts.swift Mon Jan 18 19:34:38 2021 +0100 @@ -11,20 +11,59 @@ struct StockCharts: View { @State var historicalPrices: HistoricalPrices @State var width: CGFloat + @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass? + @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass? var body: some View { - let prices = historicalPrices.result.map { $0.close } // Get an array of the variable "Close" in the struct ScrollView { - // Stock price - LineChartView(data: prices, title: "Stock price", legend: "Last 20 days", form: CGSize(width: width, height: width/2), rateValue: nil) - .padding() + let columns = lazyColumns(verticalSizeClass: verticalSizeClass!, horizontalSizeClass: horizontalSizeClass!, + iphoneColumns: 1, ipadColumns: 2) - // Volume - let volume = historicalPrices.result.map { $0.volume } - BarChartView(data: ChartData(points: volume), title: "Volume", form: CGSize(width: width, height: width/1.5)) + LazyVGrid(columns: columns, spacing: 20) { + let normalSize = chartSize(chartType: "") + let specialSize = chartSize(chartType: "special") + + let prices = historicalPrices.result.map { $0.close } // Get an array of the variable "Close" in the struct + LineChartView(data: prices, title: "Stock price", legend: "Last month", form: normalSize, rateValue: nil) + .padding() + + let maxPrice = String(prices.max()!) + StockStats(data: maxPrice) + + let volume = historicalPrices.result.map { $0.volume } + BarChartView(data: ChartData(points: volume), title: "Volume", form: specialSize) + .padding() + + let maxVolume = String(volume.max()!) + StockStats(data: maxVolume) + + let change = historicalPrices.result.map { $0.changePercent*100 } + LineChartView(data: change, title: "Daily percentage change", legend: "Last month", form: normalSize, rateValue: nil) .padding() + + } } } + + func chartSize(chartType: String) -> CGSize { + var size = CGSize() + if horizontalSizeClass == .compact && verticalSizeClass == .regular { + print("Running on iPhone") + + if chartType == "special" { + size = CGSize(width: width, height: width/1.5) + } + else { + size = CGSize(width: width, height: width/2) + } + } + else { + print("Running on iPad") + size = CGSize(width: 360, height: 240) + } + + return size + } } struct ScrollCharts_Previews: PreviewProvider { diff -r d53c220dcfa4 -r 4de3f0d5f305 lazybear/Supply views/StockStats.swift --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lazybear/Supply views/StockStats.swift Mon Jan 18 19:34:38 2021 +0100 @@ -0,0 +1,26 @@ +// +// StockStats.swift +// LazyBear +// +// Created by Dennis Concepción Martín on 18/1/21. +// + +import SwiftUI + +struct StockStats: View { + @State var data: String + + var body: some View { + RoundedRectangle(cornerRadius: 25) + .foregroundColor(.white) + .overlay(Text(String(data))) + .frame(maxWidth: 360) + .padding() + } +} + +struct StockStats_Previews: PreviewProvider { + static var previews: some View { + StockStats(data: "500.00") + } +}