view LazyBear/Views/Profile/Helpers/RenameSheet.swift @ 389:db8bc3ed526a

Implementing add to watchlist feature from SearchView
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 25 Apr 2021 16:42:26 +0200
parents 79c39987aaa4
children
line wrap: on
line source

//
//  RenameSheet.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 24/4/21.
//

import SwiftUI

struct RenameSheet: View {
    var listName: String
    
    @State private var newListName: String = String()
    @Binding var showRenameAction: Bool
    @Binding var presentationMode: PresentationMode
    
    @Environment(\.managedObjectContext) private var moc
    @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
    var watchlistCompany: FetchedResults<WatchlistCompany>
    
    var body: some View {
        RoundedRectangle(cornerRadius: 15)
            .frame(width: 280, height: 180)
            .foregroundColor(Color(.secondarySystemBackground))
            .overlay(
                VStack {
                    Text("Rename your list")
                        .font(.headline)
                    
                    Text("Enter a name")
                        .font(.callout)
                    
                    Spacer()
                    TextField("Technologies, banks...", text: $newListName)
                        .padding(7)
                        .background(
                            Color(.systemBackground)
                                .cornerRadius(7)
                        )
                    
                    Divider()

                    HStack {
                        Spacer()
                        Button(action: {
                            UIApplication.shared.endEditing()
                            self.showRenameAction = false
                        }) {
                            Text("Cancel")
                                .fontWeight(.semibold)
                                .foregroundColor(.red)
                        }
                        

                        Spacer()
                        Divider()
                            
                        Spacer()
                        Button(action: { renameList(newListName) }) {
                            Text("Done")
                        }
                        Spacer()
                    }
                    .frame(height: 25)
                }
                .padding()
            )
        .background(
            BlurBackground(style: .systemMaterial)
               .clipShape(RoundedRectangle(cornerRadius: 15))
        )
    }
    
    private func renameList(_ newListName: String) {
        let selectedWatchlist = watchlistCompany.filter({ $0.watchlist == listName })
        for company in selectedWatchlist {
            company.watchlist = newListName
        }
        do {
            try moc.save()
            print("List updated")
            UIApplication.shared.endEditing()  // Dismiss Keyboard
            self.showRenameAction = false  // Dismiss action rename sheet
            self.$presentationMode.wrappedValue.dismiss()  // Dismiss Modal View
        } catch {
            print(error.localizedDescription)
        }
    }
}
// Dismiss Keyboard
extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

struct RenameSheet_Previews: PreviewProvider {
    @Environment(\.presentationMode) static var presentationMode
    
    static var previews: some View {
        RenameSheet(listName: "MyWatchlist", showRenameAction: .constant(true), presentationMode: presentationMode)
    }
}