Skip to content

Commit

Permalink
Add "SwiftUI Essentials" > "Handling User Input"
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieWangWei committed Jun 7, 2019
1 parent 93c8313 commit c604254
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 100 deletions.
108 changes: 56 additions & 52 deletions SwiftUI-Tutorials.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
objects = {

/* Begin PBXBuildFile section */
36A89B9622AACB3B0096F89A /* UserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36A89B9522AACB3B0096F89A /* UserData.swift */; };
36A89BA422AACDE40096F89A /* lakemcdonald.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9722AACDE30096F89A /* lakemcdonald.jpg */; };
36A89BA522AACDE40096F89A /* chincoteague.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9822AACDE30096F89A /* chincoteague.jpg */; };
36A89BA622AACDE40096F89A /* chilkoottrail.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9922AACDE30096F89A /* chilkoottrail.jpg */; };
36A89BA722AACDE40096F89A /* landmarkData.json in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9A22AACDE30096F89A /* landmarkData.json */; };
36A89BA822AACDE40096F89A /* icybay.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9B22AACDE30096F89A /* icybay.jpg */; };
36A89BA922AACDE40096F89A /* turtlerock.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9C22AACDE30096F89A /* turtlerock.jpg */; };
36A89BAA22AACDE40096F89A /* rainbowlake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9D22AACDE30096F89A /* rainbowlake.jpg */; };
36A89BAB22AACDE40096F89A /* charleyrivers.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9E22AACDE30096F89A /* charleyrivers.jpg */; };
36A89BAC22AACDE40096F89A /* twinlake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89B9F22AACDE30096F89A /* twinlake.jpg */; };
36A89BAD22AACDE40096F89A /* stmarylake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89BA022AACDE40096F89A /* stmarylake.jpg */; };
36A89BAE22AACDE40096F89A /* umbagog.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89BA122AACDE40096F89A /* umbagog.jpg */; };
36A89BAF22AACDE40096F89A /* silversalmoncreek.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89BA222AACDE40096F89A /* silversalmoncreek.jpg */; };
36A89BB022AACDE40096F89A /* hiddenlake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36A89BA322AACDE40096F89A /* hiddenlake.jpg */; };
36C193F722A8F28C00966915 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C193F622A8F28C00966915 /* AppDelegate.swift */; };
36C193F922A8F28C00966915 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C193F822A8F28C00966915 /* SceneDelegate.swift */; };
36C193FD22A8F29100966915 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 36C193FC22A8F29100966915 /* Assets.xcassets */; };
Expand All @@ -16,25 +30,26 @@
36C1941122A8F30800966915 /* CircleImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C1940E22A8F30800966915 /* CircleImage.swift */; };
36E2C6C522AA5B990037499F /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E2C6C322AA5B990037499F /* Data.swift */; };
36E2C6C622AA5B990037499F /* Landmark.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E2C6C422AA5B990037499F /* Landmark.swift */; };
36E2C6D522AA5BAE0037499F /* turtlerock.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6C822AA5BAE0037499F /* turtlerock.jpg */; };
36E2C6D622AA5BAE0037499F /* silversalmoncreek.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6C922AA5BAE0037499F /* silversalmoncreek.jpg */; };
36E2C6D722AA5BAE0037499F /* icybay.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6CA22AA5BAE0037499F /* icybay.jpg */; };
36E2C6D822AA5BAE0037499F /* lakemcdonald.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6CB22AA5BAE0037499F /* lakemcdonald.jpg */; };
36E2C6D922AA5BAE0037499F /* hiddenlake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6CC22AA5BAE0037499F /* hiddenlake.jpg */; };
36E2C6DA22AA5BAE0037499F /* twinlake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6CD22AA5BAE0037499F /* twinlake.jpg */; };
36E2C6DB22AA5BAE0037499F /* umbagog.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6CE22AA5BAE0037499F /* umbagog.jpg */; };
36E2C6DC22AA5BAE0037499F /* landmarkData.json in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6CF22AA5BAE0037499F /* landmarkData.json */; };
36E2C6DD22AA5BAE0037499F /* chincoteague.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6D022AA5BAE0037499F /* chincoteague.jpg */; };
36E2C6DE22AA5BAE0037499F /* yukon_charleyrivers.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6D122AA5BAE0037499F /* yukon_charleyrivers.jpg */; };
36E2C6DF22AA5BAE0037499F /* stmarylake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6D222AA5BAE0037499F /* stmarylake.jpg */; };
36E2C6E022AA5BAE0037499F /* chilkoottrail.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6D322AA5BAE0037499F /* chilkoottrail.jpg */; };
36E2C6E122AA5BAE0037499F /* rainbowlake.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 36E2C6D422AA5BAE0037499F /* rainbowlake.jpg */; };
36E2C6E822AA5C530037499F /* LandmarkDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E2C6E722AA5C530037499F /* LandmarkDetail.swift */; };
36E2C6EA22AA5E670037499F /* LandmarkRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E2C6E922AA5E670037499F /* LandmarkRow.swift */; };
36E2C6EC22AA60300037499F /* LandmarkList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E2C6EB22AA60300037499F /* LandmarkList.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
36A89B9522AACB3B0096F89A /* UserData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserData.swift; sourceTree = "<group>"; };
36A89B9722AACDE30096F89A /* lakemcdonald.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = lakemcdonald.jpg; sourceTree = "<group>"; };
36A89B9822AACDE30096F89A /* chincoteague.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = chincoteague.jpg; sourceTree = "<group>"; };
36A89B9922AACDE30096F89A /* chilkoottrail.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = chilkoottrail.jpg; sourceTree = "<group>"; };
36A89B9A22AACDE30096F89A /* landmarkData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = landmarkData.json; sourceTree = "<group>"; };
36A89B9B22AACDE30096F89A /* icybay.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = icybay.jpg; sourceTree = "<group>"; };
36A89B9C22AACDE30096F89A /* turtlerock.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = turtlerock.jpg; sourceTree = "<group>"; };
36A89B9D22AACDE30096F89A /* rainbowlake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = rainbowlake.jpg; sourceTree = "<group>"; };
36A89B9E22AACDE30096F89A /* charleyrivers.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = charleyrivers.jpg; sourceTree = "<group>"; };
36A89B9F22AACDE30096F89A /* twinlake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = twinlake.jpg; sourceTree = "<group>"; };
36A89BA022AACDE40096F89A /* stmarylake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = stmarylake.jpg; sourceTree = "<group>"; };
36A89BA122AACDE40096F89A /* umbagog.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = umbagog.jpg; sourceTree = "<group>"; };
36A89BA222AACDE40096F89A /* silversalmoncreek.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = silversalmoncreek.jpg; sourceTree = "<group>"; };
36A89BA322AACDE40096F89A /* hiddenlake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = hiddenlake.jpg; sourceTree = "<group>"; };
36C193F322A8F28C00966915 /* SwiftUI-Tutorials.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SwiftUI-Tutorials.app"; sourceTree = BUILT_PRODUCTS_DIR; };
36C193F622A8F28C00966915 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
36C193F822A8F28C00966915 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -46,19 +61,6 @@
36C1940E22A8F30800966915 /* CircleImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CircleImage.swift; sourceTree = "<group>"; };
36E2C6C322AA5B990037499F /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
36E2C6C422AA5B990037499F /* Landmark.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Landmark.swift; sourceTree = "<group>"; };
36E2C6C822AA5BAE0037499F /* turtlerock.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = turtlerock.jpg; sourceTree = "<group>"; };
36E2C6C922AA5BAE0037499F /* silversalmoncreek.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = silversalmoncreek.jpg; sourceTree = "<group>"; };
36E2C6CA22AA5BAE0037499F /* icybay.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = icybay.jpg; sourceTree = "<group>"; };
36E2C6CB22AA5BAE0037499F /* lakemcdonald.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = lakemcdonald.jpg; sourceTree = "<group>"; };
36E2C6CC22AA5BAE0037499F /* hiddenlake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = hiddenlake.jpg; sourceTree = "<group>"; };
36E2C6CD22AA5BAE0037499F /* twinlake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = twinlake.jpg; sourceTree = "<group>"; };
36E2C6CE22AA5BAE0037499F /* umbagog.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = umbagog.jpg; sourceTree = "<group>"; };
36E2C6CF22AA5BAE0037499F /* landmarkData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = landmarkData.json; sourceTree = "<group>"; };
36E2C6D022AA5BAE0037499F /* chincoteague.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = chincoteague.jpg; sourceTree = "<group>"; };
36E2C6D122AA5BAE0037499F /* yukon_charleyrivers.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = yukon_charleyrivers.jpg; sourceTree = "<group>"; };
36E2C6D222AA5BAE0037499F /* stmarylake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = stmarylake.jpg; sourceTree = "<group>"; };
36E2C6D322AA5BAE0037499F /* chilkoottrail.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = chilkoottrail.jpg; sourceTree = "<group>"; };
36E2C6D422AA5BAE0037499F /* rainbowlake.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = rainbowlake.jpg; sourceTree = "<group>"; };
36E2C6E722AA5C530037499F /* LandmarkDetail.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LandmarkDetail.swift; sourceTree = "<group>"; };
36E2C6E922AA5E670037499F /* LandmarkRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LandmarkRow.swift; sourceTree = "<group>"; };
36E2C6EB22AA60300037499F /* LandmarkList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LandmarkList.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -123,26 +125,27 @@
children = (
36E2C6C322AA5B990037499F /* Data.swift */,
36E2C6C422AA5B990037499F /* Landmark.swift */,
36A89B9522AACB3B0096F89A /* UserData.swift */,
);
path = Models;
sourceTree = "<group>";
};
36E2C6C722AA5BAE0037499F /* Resources */ = {
isa = PBXGroup;
children = (
36E2C6D322AA5BAE0037499F /* chilkoottrail.jpg */,
36E2C6D022AA5BAE0037499F /* chincoteague.jpg */,
36E2C6CC22AA5BAE0037499F /* hiddenlake.jpg */,
36E2C6CA22AA5BAE0037499F /* icybay.jpg */,
36E2C6CB22AA5BAE0037499F /* lakemcdonald.jpg */,
36E2C6CF22AA5BAE0037499F /* landmarkData.json */,
36E2C6D422AA5BAE0037499F /* rainbowlake.jpg */,
36E2C6C922AA5BAE0037499F /* silversalmoncreek.jpg */,
36E2C6D222AA5BAE0037499F /* stmarylake.jpg */,
36E2C6C822AA5BAE0037499F /* turtlerock.jpg */,
36E2C6CD22AA5BAE0037499F /* twinlake.jpg */,
36E2C6CE22AA5BAE0037499F /* umbagog.jpg */,
36E2C6D122AA5BAE0037499F /* yukon_charleyrivers.jpg */,
36A89B9E22AACDE30096F89A /* charleyrivers.jpg */,
36A89B9922AACDE30096F89A /* chilkoottrail.jpg */,
36A89B9822AACDE30096F89A /* chincoteague.jpg */,
36A89BA322AACDE40096F89A /* hiddenlake.jpg */,
36A89B9B22AACDE30096F89A /* icybay.jpg */,
36A89B9722AACDE30096F89A /* lakemcdonald.jpg */,
36A89B9A22AACDE30096F89A /* landmarkData.json */,
36A89B9D22AACDE30096F89A /* rainbowlake.jpg */,
36A89BA222AACDE40096F89A /* silversalmoncreek.jpg */,
36A89BA022AACDE40096F89A /* stmarylake.jpg */,
36A89B9C22AACDE30096F89A /* turtlerock.jpg */,
36A89B9F22AACDE30096F89A /* twinlake.jpg */,
36A89BA122AACDE40096F89A /* umbagog.jpg */,
);
path = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -214,22 +217,22 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
36E2C6D822AA5BAE0037499F /* lakemcdonald.jpg in Resources */,
36E2C6D622AA5BAE0037499F /* silversalmoncreek.jpg in Resources */,
36E2C6DC22AA5BAE0037499F /* landmarkData.json in Resources */,
36E2C6D522AA5BAE0037499F /* turtlerock.jpg in Resources */,
36A89BAF22AACDE40096F89A /* silversalmoncreek.jpg in Resources */,
36C1940322A8F29100966915 /* LaunchScreen.storyboard in Resources */,
36E2C6DB22AA5BAE0037499F /* umbagog.jpg in Resources */,
36E2C6DF22AA5BAE0037499F /* stmarylake.jpg in Resources */,
36E2C6DD22AA5BAE0037499F /* chincoteague.jpg in Resources */,
36A89BAA22AACDE40096F89A /* rainbowlake.jpg in Resources */,
36A89BAE22AACDE40096F89A /* umbagog.jpg in Resources */,
36A89BA622AACDE40096F89A /* chilkoottrail.jpg in Resources */,
36A89BAD22AACDE40096F89A /* stmarylake.jpg in Resources */,
36A89BB022AACDE40096F89A /* hiddenlake.jpg in Resources */,
36A89BA922AACDE40096F89A /* turtlerock.jpg in Resources */,
36A89BA822AACDE40096F89A /* icybay.jpg in Resources */,
36A89BA722AACDE40096F89A /* landmarkData.json in Resources */,
36A89BAC22AACDE40096F89A /* twinlake.jpg in Resources */,
36C1940022A8F29100966915 /* Preview Assets.xcassets in Resources */,
36E2C6D722AA5BAE0037499F /* icybay.jpg in Resources */,
36E2C6DA22AA5BAE0037499F /* twinlake.jpg in Resources */,
36E2C6D922AA5BAE0037499F /* hiddenlake.jpg in Resources */,
36A89BAB22AACDE40096F89A /* charleyrivers.jpg in Resources */,
36A89BA422AACDE40096F89A /* lakemcdonald.jpg in Resources */,
36C193FD22A8F29100966915 /* Assets.xcassets in Resources */,
36E2C6E122AA5BAE0037499F /* rainbowlake.jpg in Resources */,
36E2C6E022AA5BAE0037499F /* chilkoottrail.jpg in Resources */,
36E2C6DE22AA5BAE0037499F /* yukon_charleyrivers.jpg in Resources */,
36A89BA522AACDE40096F89A /* chincoteague.jpg in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -248,6 +251,7 @@
36C193F922A8F28C00966915 /* SceneDelegate.swift in Sources */,
36E2C6E822AA5C530037499F /* LandmarkDetail.swift in Sources */,
36E2C6EA22AA5E670037499F /* LandmarkRow.swift in Sources */,
36A89B9622AACB3B0096F89A /* UserData.swift in Sources */,
36C1941122A8F30800966915 /* CircleImage.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
40 changes: 32 additions & 8 deletions SwiftUI-Tutorials/LandmarkDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,65 @@
import SwiftUI

struct LandmarkDetail: View {
@EnvironmentObject var userData: UserData
var landmark: Landmark

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

var body: some View {
VStack {
MapView(coordinate: landmark.locationCoordinate)
.edgesIgnoringSafeArea(.top)
.frame(height: 300)

CircleImage(image: landmark.image(forSize: 250))
.offset(y: -130)
.offset(x: 0, y: -130)
.padding(.bottom, -130)

VStack(alignment: .leading) {
Text(landmark.name)
.font(.title)
HStack {
Text(verbatim: landmark.name)
.font(.title)

Button(action: {
self.userData.landmarks[self.landmarkIndex]
.isFavorite.toggle()
}) {
if self.userData.landmarks[self.landmarkIndex]
.isFavorite {
Image(systemName: "star.fill")
.foregroundColor(Color.yellow)
} else {
Image(systemName: "star")
.foregroundColor(Color.gray)
}
}
}

HStack(alignment: .top) {
Text(landmark.park)
Text(verbatim: landmark.park)
.font(.subheadline)
Spacer()
Text(landmark.state)
Text(verbatim: landmark.state)
.font(.subheadline)
}
}
.padding()

Spacer()
}
.navigationBarTitle(Text(landmark.name), displayMode: .inline)
}
}

#if DEBUG
struct LandmarkDetail_Previews : PreviewProvider {
struct LandmarkDetail_Preview: PreviewProvider {
static var previews: some View {
LandmarkDetail(landmark: landmarkData[0])
let userData = UserData()
return LandmarkDetail(landmark: userData.landmarks[0])
.environmentObject(userData)
}
}
#endif

26 changes: 19 additions & 7 deletions SwiftUI-Tutorials/LandmarkList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,39 @@

import SwiftUI

struct LandmarkList : View {
struct LandmarkList: View {
@EnvironmentObject private var userData: UserData

var body: some View {
NavigationView {
List(landmarkData) { landmark in
NavigationButton(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Show Favorites Only")
}

ForEach(userData.landmarks) { landmark in
if !self.userData.showFavoritesOnly || landmark.isFavorite {
NavigationButton(
destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
}
.navigationBarTitle(Text("Landmarks"))
}
.navigationBarTitle(Text("Landmarks"), displayMode: .large)
}
}
}

#if DEBUG
struct LandmarkList_Previews : PreviewProvider {
struct LandmarksList_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPhone SE", "iPhone XS Max"].identified(by: \.self)) { deviceName in
LandmarkList()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
.environmentObject(UserData())
}
}
#endif
Loading

0 comments on commit c604254

Please sign in to comment.