diff LazyBear/Views/Company/Helpers/InsiderList.swift @ 411:681fb377235e

Implementing insider transactions
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Mon, 07 Jun 2021 20:59:52 +0200
parents
children a7c9dd0c5822
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Views/Company/Helpers/InsiderList.swift	Mon Jun 07 20:59:52 2021 +0200
@@ -0,0 +1,85 @@
+//
+//  InsiderList.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 7/6/21.
+//
+
+import SwiftUI
+
+struct InsiderList: View {
+    var insiderSummary: [InsiderRosterModel]
+    var numberOfRows: Int
+    
+    @State private var showFullList = false
+    
+    var body: some View {
+        VStack(alignment: .leading) {
+            HStack {
+                Text("Top 10 insiders")
+                    .font(.title3)
+                    .fontWeight(.semibold)
+                
+                Spacer()
+                Button("See all", action: { showFullList = true })
+            }
+        
+            // Get total shares owned by the top 10 insiders
+            let totalPositions =  insiderSummary.map { $0.position }.reduce(0, +)
+            VStack(alignment: .leading, spacing: 20) {
+                ForEach(insiderSummary.prefix(numberOfRows), id: \.self) { insider in
+                    
+                    // Compute percentage of ownership for each insider
+                    let percentage = Double(insider.position) / Double(totalPositions)
+                    
+                    InsiderRow(percentageOfWidth: CGFloat(percentage), insiderRoster: insider)
+                }
+            }
+        }
+        .sheet(isPresented: $showFullList) {
+            InsiderFullList(insiderSummary: insiderSummary)
+        }
+    }
+}
+
+struct InsiderList_Previews: PreviewProvider {
+    static var previews: some View {
+        InsiderList(insiderSummary:
+                    [InsiderRosterModel(entityName: "Dennis Concepcion", position: 1234, reportDate: 1234567)],
+                    numberOfRows: 3)
+    }
+}
+
+struct InsiderFullList: View {
+    var insiderSummary: [InsiderRosterModel]
+    @Environment(\.presentationMode) private var presentationInsiderFullList
+    
+    var body: some View {
+        NavigationView {
+            ScrollView {
+                // Get total shares owned by the top 10 insiders
+                let totalPositions =  insiderSummary.map { $0.position }.reduce(0, +)
+                VStack(alignment: .leading, spacing: 20) {
+                    ForEach(insiderSummary, id: \.self) { insider in
+                        
+                        // Compute percentage of ownership for each insider
+                        let percentage = Double(insider.position) / Double(totalPositions)
+                        
+                        InsiderRow(percentageOfWidth: CGFloat(percentage), insiderRoster: insider)
+                    }
+                }
+                .padding()
+            }
+            .navigationTitle("Top 10 insiders")
+            .navigationBarTitleDisplayMode(.inline)
+            .toolbar {
+                ToolbarItem(placement: .navigationBarLeading) {
+                    Button(action: { presentationInsiderFullList.wrappedValue.dismiss() }) {
+                        Image(systemName: "multiply")
+                            .imageScale(.large)
+                    }
+                }
+            }
+        }
+    }
+}