comparison LazyBear/Views/Profile/Helpers/WatchlistCreator.swift @ 393:0a4c399170c4

Implementing WatchlistCreator
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Sun, 02 May 2021 12:41:20 +0200
parents
children 5f21f7c23c5e
comparison
equal deleted inserted replaced
392:13f3578def61 393:0a4c399170c4
1 //
2 // WatchlistCreator.swift
3 // LazyBear
4 //
5 // Created by Dennis Concepción Martín on 30/4/21.
6 //
7
8 import SwiftUI
9
10 struct WatchlistCreator: View {
11 @ObservedObject var watchlistCreatorClass = WatchlistCreatorClass()
12 @State private var showSearchView = false
13 @State private var showCancelAlert = false
14 @State private var watchlistNameIsEmpty = false
15
16 @Environment(\.managedObjectContext) private var moc
17 @Environment(\.presentationMode) private var presentationMode
18
19 var body: some View {
20 NavigationView {
21 Form {
22 Section(header: Text("Watchlist name")) {
23 TextField("Technologies, banks", text: $watchlistCreatorClass.name)
24 }
25
26 Section(header: Text("Companies")) {
27 Button("Add companies", action: { showSearchView = true })
28
29 ForEach(watchlistCreatorClass.companies, id: \.self) { company in
30 HStack {
31 VStack(alignment: .leading) {
32 Text(company.symbol!.uppercased())
33 .fontWeight(.semibold)
34
35 Text(company.securityName!.capitalized)
36 .lineLimit(1)
37 }
38
39 Spacer()
40 Button(action: { remove(company) }, label: {
41 Image(systemName: "multiply.circle")
42 .imageScale(.large)
43 })
44 }
45 }
46 }
47 }
48 .navigationTitle("New watchlist")
49 .navigationBarTitleDisplayMode(.inline)
50 .toolbar {
51 ToolbarItem(placement: .navigationBarTrailing) {
52 Button("Done", action: { save() })
53 }
54
55 ToolbarItem(placement: .navigationBarLeading) {
56 Button("Cancel", action: { self.showCancelAlert = true })
57 }
58 }
59 // Show delete list alert
60 .alert(isPresented: $showCancelAlert) {
61 Alert(
62 title: Text("Your watchlist won't be saved"),
63 message: Text("This action can't be undo"),
64 primaryButton: .destructive(Text("Exit")) { presentationMode.wrappedValue.dismiss() },
65 secondaryButton: .cancel()
66 )
67 }
68 }
69 // Show alert when watchlist name is empty
70 .alert(isPresented: $watchlistNameIsEmpty) {
71 Alert(
72 title: Text("Give a name to your new watchlist"),
73 message: Text("Try My portfolio, Favourites, ..."),
74 dismissButton: .default(Text("Got it!"))
75 )
76 }
77 .sheet(isPresented: $showSearchView) {
78 WatchlistCreatorList(watchlistCreatorClass: watchlistCreatorClass)
79 .environment(\.managedObjectContext, self.moc)
80 }
81 }
82
83 /*
84 Search company in array and get the index -> Delete company at
85 this index from the array
86 */
87 private func remove(_ company: SearchResponse) {
88 let index = watchlistCreatorClass.companies.firstIndex(of: company)
89 watchlistCreatorClass.companies.remove(at: index!)
90 }
91
92 /*
93 Check if the watchlist name is empty when the user taps the Done button.
94 If it's not empty -> Save watchlist to Core Data
95 */
96 private func save() {
97 if watchlistCreatorClass.name.isEmpty {
98 watchlistNameIsEmpty = true
99 } else {
100 for company in watchlistCreatorClass.companies {
101 let watchlistCompany = WatchlistCompany(context: moc)
102 watchlistCompany.name = company.securityName ?? "-"
103 watchlistCompany.symbol = company.symbol!
104 watchlistCompany.watchlist = watchlistCreatorClass.name
105 }
106
107 do {
108 try moc.save()
109 print("Watchlist created")
110 presentationMode.wrappedValue.dismiss() // Dismiss view
111 } catch {
112 print(error.localizedDescription)
113 }
114 }
115 }
116 }
117
118 struct CreateNewWatchlist_Previews: PreviewProvider {
119 static var previews: some View {
120 WatchlistCreator()
121 }
122 }