comparison LazyBear/Views/Profile/Helpers/RenameListSheet.swift @ 428:8c58ce834d95

Bug fixes and change assets
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Fri, 18 Jun 2021 12:43:17 +0200
parents
children c78d5b5b3bda
comparison
equal deleted inserted replaced
427:e707dbfc3115 428:8c58ce834d95
1 //
2 // RenameListSheet.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 18/6/21.
6 //
7
8 import SwiftUI
9 import Introspect
10
11 struct RenameListSheet: View {
12 var oldWatchlistName: String
13 @State private var newWatchlistName = String()
14
15 @Environment(\.presentationMode) private var renameListSheetPresentationMode
16 @Environment(\.managedObjectContext) private var moc
17 @FetchRequest(entity: WatchlistCompany.entity(), sortDescriptors: [])
18 var watchlistCompanies: FetchedResults<WatchlistCompany>
19
20 var body: some View {
21 NavigationView {
22 Form {
23 let header = "Need ideas? Try my portfolio, or banks"
24 Section(header: Text(header)) {
25 TextField("Enter a name", text: $newWatchlistName)
26 .introspectTextField { textField in
27 textField.becomeFirstResponder()
28 }
29 }
30 }
31 .navigationTitle("Rename your watchlist")
32 .toolbar {
33 ToolbarItem(placement: .navigationBarLeading) {
34 Button(action: { renameListSheetPresentationMode.wrappedValue.dismiss() }) {
35 Image(systemName: "multiply")
36 }
37 }
38
39 ToolbarItem(placement: .navigationBarTrailing) {
40 if !newWatchlistName.isEmpty {
41 Button("Done", action: { renameWatchlist() })
42 }
43 }
44 }
45 }
46 }
47
48 /*
49 Rename watchlistName variable from each Core Data (WatchlistCompany) object
50 */
51 private func renameWatchlist() {
52 for watchlistCompany in watchlistCompanies {
53 watchlistCompany.watchlistName = newWatchlistName
54 }
55
56 do {
57 try moc.save()
58 renameListSheetPresentationMode.wrappedValue.dismiss()
59 } catch {
60 print(error.localizedDescription)
61 }
62 }
63 }
64
65 struct RenameListSheet_Previews: PreviewProvider {
66 static var previews: some View {
67 RenameListSheet(oldWatchlistName: "Old name")
68 }
69 }