-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView.swift
61 lines (53 loc) · 1.7 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// ContentView.swift
// HelloCoreData
//
// Created by Tilek on 5/12/22.
//
import SwiftUI
struct ContentView: View {
let coreDM: CoreDataManager
@State private var movieName: String = ""
@State private var movies: [Movie] = [Movie]()
@State private var needsRefresh: Bool = false
private func populateMovies() {
movies = coreDM.getAllMovies()
}
var body: some View {
NavigationView{
VStack{
TextField("Enter movie name", text: $movieName)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button("Save"){
coreDM.saveMovie(title: movieName)
populateMovies()
}
List{
ForEach(movies, id: \.self){ movie in
NavigationLink(
destination: MovieDetail(movie: movie, coreDM: coreDM, needsRefresh: $needsRefresh),
label: {
Text(movie.title ?? "")
})
}.onDelete(perform: { indexSet in
indexSet.forEach { index in
let movie = movies[index]
coreDM.deleteMovie(movie: movie)
populateMovies()
}
})
}.listStyle(PlainListStyle())
.accentColor(needsRefresh ? .white: .black)
Spacer()
} .padding()
.onAppear (perform: {
populateMovies()
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(coreDM: CoreDataManager())
}
}