diff LazyBear/Views/Global Helpers/ExtensiveList.swift @ 387:c206bd0bdb4e

Implementing RenameSheet.swift
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sat, 24 Apr 2021 12:21:23 +0200
parents 5d7a66e2b39f
children 79c39987aaa4
line wrap: on
line diff
--- a/LazyBear/Views/Global Helpers/ExtensiveList.swift	Sat Apr 24 12:20:57 2021 +0200
+++ b/LazyBear/Views/Global Helpers/ExtensiveList.swift	Sat Apr 24 12:21:23 2021 +0200
@@ -15,45 +15,96 @@
     var addOnDelete: Bool
     
     @Environment(\.presentationMode) private var extensiveListPresent
-    @State var isEditMode: EditMode = .inactive
+    @State private var isEditMode: EditMode = .inactive
+    @State private var showRenameAction = false
+    @State private var showDeleteAlert = false
     
     var body: some View {
         NavigationView {
-            VStack {
-                if let list = list {
-                    List {
-                        ForEach(Array(list.keys.sorted()), id: \.self) { companySymbol in
-                                StockItem(symbol: companySymbol,
-                                          company: list[companySymbol]!,
-                                          intradayPrices: intradayPrices?[companySymbol],
-                                          orientation: .horizontal,
-                                          hidePriceView: self.isEditMode == .active  // Hide on EditMode
-                                )
+            ZStack {
+                VStack {
+                    if let list = list {
+                        List {
+                            ForEach(Array(list.keys.sorted()), id: \.self) { companySymbol in
+                                    StockItem(symbol: companySymbol,
+                                              company: list[companySymbol]!,
+                                              intradayPrices: intradayPrices?[companySymbol],
+                                              orientation: .horizontal,
+                                              hidePriceView: self.isEditMode == .active  // Hide on EditMode
+                                    )
+                                
+                            }
+                            .onDelete(perform: addOnDelete ? removeCompany: nil)
+                        }
+                    }
+                    
+                    if let latestCurrencies = latestCurrencies {
+                        List(Array(latestCurrencies.keys.sorted()), id: \.self) { currencySymbol in
+                            CurrencyListItem(currencySymbol: currencySymbol, currency: latestCurrencies[currencySymbol]!)
                             
                         }
-                        .onDelete(perform: addOnDelete ? removeCompany: nil)
                     }
                 }
                 
-                if let latestCurrencies = latestCurrencies {
-                    List(Array(latestCurrencies.keys.sorted()), id: \.self) { currencySymbol in
-                        CurrencyListItem(currencySymbol: currencySymbol, currency: latestCurrencies[currencySymbol]!)
-                        
-                    }
-                }
+                // Blur background
+                Color(.black)
+                    .edgesIgnoringSafeArea(.all)
+                    .opacity(showRenameAction ? 0.2: 0)
+                    .animation(.easeInOut)
+                    .onTapGesture { showRenameAction = false }
+                
+                // Show rename Action Sheet
+                RenameSheet(showAction: $showRenameAction)
+                    .offset(y: showRenameAction ? 0: 700)
+                    .animation(.easeInOut)
+            }
+            // Show delete list alert
+            .alert(isPresented: $showDeleteAlert) {
+                Alert(
+                    title: Text("Are you sure you want to delete this list?"),
+                    message: Text("This action can't be undo"),
+                    primaryButton: .destructive(Text("Delete")) {
+                        print("Deleting...")
+                    },
+                    secondaryButton: .cancel()
+                )
             }
             .navigationTitle(listName)
             .navigationBarTitleDisplayMode(.inline)
             .toolbar {
-                ToolbarItem(placement: .navigationBarTrailing) {
-                    Button(action: { extensiveListPresent.wrappedValue.dismiss() }) {
-                        Image(systemName: "multiply")
-                            .imageScale(.large)
+                ToolbarItem(placement: .cancellationAction) {
+                    if addOnDelete {
+                        EditButton()
+                    } else {
+                        Button(action: { extensiveListPresent.wrappedValue.dismiss() }) {
+                            Image(systemName: "multiply")
+                                .imageScale(.large)
+                        }
                     }
                 }
-                ToolbarItem(placement: .navigationBarLeading) {
+                ToolbarItem(placement: .navigationBarTrailing) {
                     if addOnDelete {
-                        EditButton()
+                        Menu {
+                            Section {
+                                Button(action: { self.showRenameAction = true }) {
+                                    Label("Rename list", systemImage: "square.and.pencil")
+                                }
+
+                                Button(action: { print("Add company") }) {
+                                    Label("Add company", systemImage: "plus")
+                                }
+                            }
+
+                            Section(header: Text("Secondary actions")) {
+                                Button(action: { self.showDeleteAlert = true }) {
+                                    Label("Delete list", systemImage: "trash")
+                                }
+                            }
+                        }
+                        label: {
+                            Label("Options", systemImage: "ellipsis.circle")
+                                .imageScale(.large)
+                        }
                     }
                 }
             }