56: Bookworm, part 4

그루두·2024년 7월 2일
0

100 days of SwiftUI

목록 보기
64/108

100 days of swiftui: 56

challenge

  1. Right now it’s possible to select no title, author, or genre for books, which causes a problem for the detail view. Please fix this, either by forcing defaults, validating the form, or showing a default picture for unknown genres – you can choose.
  2. Modify ContentView so that books rated as 1 star are highlighted somehow, such as having their name shown in red.
  3. Add a new “date” attribute to the Book class, assigning Date.now to it so it gets the current date and time, then format that nicely somewhere in DetailView.

solution

1. 제목이 작성되지 않았을 때 새로운 책 저장을 막기

다른 속성 제외 책 제목은 필수로 작성해야 책을 저장할 수 있도록 설정했다.

            Section {
                Button("Save") {
                    let newBook = Book(title: title, author: author, genre: genre, rating: rating, review: review)
                    modelContext.insert(newBook)
                    dismiss()
                }
            }
            .disabled(title.isEmpty || (title.trimmingCharacters(in: .whitespacesAndNewlines)) == "")

깃헙 링크

2. 특정 조건에 맞추어 ContentView 나타내기

좋았던 책은 다시 찾아보고 싶을 것 같아서 별점이 1점인 책은 폰트 색을 회색으로 드러내고, 5점인 책은 폰트에 볼드 효과를 주어 눈에 띄게 설정했다.

                            VStack(alignment: .leading) {
                                Text(book.title)
                                    .font(.headline)
                                    .fontWeight(book.rating == 5 ? .bold : .regular)
                                    .foregroundStyle(book.rating == 1 ? .gray : .black)
                                Text(book.author)
                                    .font(.subheadline)
                            }

깃헙 링크

3. 속성 date 추가하고 DetailView에 나타내기

  1. Book class에 startDate, endDate를 추가한다.
  2. AddBookView에서 startDate, endDate를 입력받는 Picker를 추가한다.
  3. endDate를 받는 Picker의 선택 가능한 날짜를 제한한다. 시작한 날짜가 끝난 날짜의 이후일 수 없기에 설정했다.
  4. DetailView에서 날짜 정보를 나타낸다.


깃헙 링크

추가: 장르를 하드코딩으로 추가하기

전기, 역사, 언어 등 장르를 추가했다.

    let genres = ["Fantasy", "Horror", "Kids", "Mystery", "Poetry", "Romance", "Thriller", "Biography", "History", "Language", "Business", "Hobby", "Art", "Essay"]

깃헙 링크

profile
계속 해보자

0개의 댓글

관련 채용 정보