view LazyBear/Views/Profile/Helpers/TextfieldAlert.swift @ 425:4effac4733b0

Changing keys from API responses
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Wed, 16 Jun 2021 13:46:01 +0200
parents 5f21f7c23c5e
children
line wrap: on
line source

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

import SwiftUI

struct TextfieldAlert: View {
    var watchlistName: 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 watchlistCompanies: 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()
                    
                    
                    /*
                     Cancel and Done buttons
                     */
                    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))
        )
    }
    
    /*
     Rename watchlist name in Core Data
     */
    private func renameList(_ newWatchlistName: String) {
        let selectedWatchlist = watchlistCompanies.filter({ $0.watchlistName == watchlistName })
        for company in selectedWatchlist {
            company.watchlistName = newWatchlistName
        }
        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)
        }
    }
}
extension UIApplication {  /// Dismiss Keyboard Extension
    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 {
        TextfieldAlert(watchlistName: "MyWatchlist", showRenameAction: .constant(true), presentationMode: presentationMode)
    }
}