Skip to content

Commit

Permalink
Feat: Section 3.6. Update, 앱 완성
Browse files Browse the repository at this point in the history
### Motivation
- Section 6 Create a Favorite Button for Each Landmark

### Key Changes
-- Section 6 Create a Favorite Button for Each Landmark 예제 코드 및 주석 추가

### To Reviewers
- 샘플 앱 완성
  • Loading branch information
garlicvread committed Aug 11, 2022
1 parent 40a7a22 commit 8da4667
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Binary file not shown.
34 changes: 34 additions & 0 deletions tutorial/Views/Helpers/FavoriteButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// FavoriteButton.swift
// tutorial
//
// Created by 김제필 on 8/11/22.
//

import SwiftUI

struct FavoriteButton: View {
// 3.6.2. 버튼의 현재 상태를 저장할 변수 isSet을 선언하고 @Binding 프로퍼티 래퍼를 추가한다. ⬇️
@Binding var isSet: Bool

var body: some View {
// 3.6.3. 버튼 생성 -> 기본 형태 지정
Button {
isSet.toggle()
} label: {
Label("Toggle Favorite", systemImage: isSet ? "star.fill" : "star")
.labelStyle(.iconOnly)
.foregroundColor(isSet ? .yellow : .gray)
}
}
}

struct FavoriteButton_Previews: PreviewProvider {
static var previews: some View {
// 3.6.2. ⬆️ 변수 isSet을 프리뷰에 추가한다.
FavoriteButton(isSet: .constant(true))
}
}


// 3.6.3. 까지 도달했으면 LandmarkDetail.swift 파일로 이동
16 changes: 14 additions & 2 deletions tutorial/Views/Landmarks/LandmarkDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
import SwiftUI

struct LandmarkDetail: View {
// 3.6.5. computed property landmarkIndex를 생성하여 EnvironmentalObjet 변수인 modelData와 비교한다.
@EnvironmentObject var modelData: ModelData

var landmarkIndex: Int {
modelData.landmarks.firstIndex(where: { $0.id == landmark.id})!
}

// 2.7.6. Landmark 프로퍼티 추가. 프리뷰 코드로 이동 ⬇️
var landmark: Landmark

Expand All @@ -32,8 +39,13 @@ struct LandmarkDetail: View {

// 1.3.1., 1.3.2. VStack 추가
VStack(alignment: .leading) { // 1.3.5. alignment 옵션 추가
Text(landmark.name) // 2.7.8. 필요한 데이터 추가
.font(.title)
// 3.6.6. HStack 내로 landmark.name TextView를 이동하고 옆에 FavoriteButton 추가
HStack {
Text(landmark.name) // 2.7.8. 필요한 데이터 추가
.font(.title)

FavoriteButton(isSet: $modelData.landmarks[landmarkIndex].isFavorite)
}

// 1.3.6. HStack 추가
HStack {
Expand Down

0 comments on commit 8da4667

Please sign in to comment.