Skip to content

Commit

Permalink
Changes to compile with Marina
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuowei committed Jun 8, 2019
1 parent e34c33f commit 077c6f1
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 78 deletions.
10 changes: 6 additions & 4 deletions Landmarks/Landmarks/LandmarkDetail.swift
Expand Up @@ -5,7 +5,7 @@ Abstract:
A view showing the details for a landmark.
*/

import SwiftUI
// import SwiftUI

struct LandmarkDetail: View {
@EnvironmentObject var userData: UserData
Expand All @@ -17,9 +17,11 @@ struct LandmarkDetail: View {

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

CircleImage(image: landmark.image(forSize: 250))
.offset(x: 0, y: -130)
Expand All @@ -34,14 +36,14 @@ struct LandmarkDetail: View {
self.userData.landmarks[self.landmarkIndex]
.isFavorite.toggle()
}) {
if self.userData.landmarks[self.landmarkIndex]
/*if self.userData.landmarks[self.landmarkIndex]
.isFavorite {
Image(systemName: "star.fill")
.foregroundColor(Color.yellow)
} else {
} else {*/
Image(systemName: "star")
.foregroundColor(Color.gray)
}
//}
}
}

Expand Down
6 changes: 3 additions & 3 deletions Landmarks/Landmarks/LandmarkList.swift
Expand Up @@ -5,7 +5,7 @@ Abstract:
A view showing a list of landmarks.
*/

import SwiftUI
// import SwiftUI

struct LandmarkList: View {
@EnvironmentObject private var userData: UserData
Expand All @@ -18,12 +18,12 @@ struct LandmarkList: View {
}

ForEach(userData.landmarks) { landmark in
if !self.userData.showFavoritesOnly || landmark.isFavorite {
/*if !self.userData.showFavoritesOnly || landmark.isFavorite {*/
NavigationButton(
destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
//}
}
}
.navigationBarTitle(Text("Landmarks"), displayMode: .large)
Expand Down
59 changes: 4 additions & 55 deletions Landmarks/Landmarks/Models/Data.swift
Expand Up @@ -6,8 +6,8 @@ Helpers for loading images and data.
*/

import UIKit
import SwiftUI
import CoreLocation
// import SwiftUI
// import CoreLocation

let landmarkData: [Landmark] = load("landmarkData.json")

Expand All @@ -34,60 +34,9 @@ func load<T: Decodable>(_ filename: String, as type: T.Type = T.self) -> T {
}

final class ImageStore {
fileprivate typealias _ImageDictionary = [String: [Int: CGImage]]
fileprivate var images: _ImageDictionary = [:]

fileprivate static var originalSize = 250
fileprivate static var scale = 2

static var shared = ImageStore()

static let shared = ImageStore()
func image(name: String, size: Int) -> Image {
let index = _guaranteeInitialImage(name: name)

let sizedImage = images.values[index][size]
?? _sizeImage(images.values[index][ImageStore.originalSize]!, to: size * ImageStore.scale)
images.values[index][size] = sizedImage

return Image(sizedImage, scale: Length(ImageStore.scale), label: Text(verbatim: name))
}

fileprivate func _guaranteeInitialImage(name: String) -> _ImageDictionary.Index {
if let index = images.index(forKey: name) { return index }

guard
let url = Bundle.main.url(forResource: name, withExtension: "jpg"),
let imageSource = CGImageSourceCreateWithURL(url as NSURL, nil),
let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil)
else {
fatalError("Couldn't load image \(name).jpg from main bundle.")
}

images[name] = [ImageStore.originalSize: image]
return images.index(forKey: name)!
}

fileprivate func _sizeImage(_ image: CGImage, to size: Int) -> CGImage {
guard
let colorSpace = image.colorSpace,
let context = CGContext(
data: nil,
width: size, height: size,
bitsPerComponent: image.bitsPerComponent,
bytesPerRow: image.bytesPerRow,
space: colorSpace,
bitmapInfo: image.bitmapInfo.rawValue)
else {
fatalError("Couldn't create graphics context.")
}
context.interpolationQuality = .high
context.draw(image, in: CGRect(x: 0, y: 0, width: size, height: size))

if let sizedImage = context.makeImage() {
return sizedImage
} else {
fatalError("Couldn't resize image.")
}
return Image(url: name + ".jpg", label: Text(verbatim: name))
}
}

10 changes: 2 additions & 8 deletions Landmarks/Landmarks/Models/Landmark.swift
Expand Up @@ -5,8 +5,8 @@ Abstract:
The model for an individual landmark.
*/

import SwiftUI
import CoreLocation
// import SwiftUI
// import CoreLocation

struct Landmark: Hashable, Codable, Identifiable {
var id: Int
Expand All @@ -18,12 +18,6 @@ struct Landmark: Hashable, Codable, Identifiable {
var category: Category
var isFavorite: Bool

var locationCoordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: coordinates.latitude,
longitude: coordinates.longitude)
}

func image(forSize size: Int) -> Image {
ImageStore.shared.image(name: imageName, size: size)
}
Expand Down
4 changes: 2 additions & 2 deletions Landmarks/Landmarks/Models/UserData.swift
Expand Up @@ -5,8 +5,8 @@ Abstract:
A model object that stores app data.
*/

import Combine
import SwiftUI
// import Combine
// import SwiftUI

final class UserData: BindableObject {
let didChange = PassthroughSubject<UserData, Never>()
Expand Down
2 changes: 1 addition & 1 deletion Landmarks/Landmarks/SceneDelegate.swift
Expand Up @@ -6,7 +6,7 @@ The scene delegate.
*/

import UIKit
import SwiftUI
// import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

Expand Down
2 changes: 1 addition & 1 deletion Landmarks/Landmarks/Supporting Views/CircleImage.swift
Expand Up @@ -5,7 +5,7 @@ Abstract:
A view that clips an image to a circle and adds a stroke and shadow.
*/

import SwiftUI
// import SwiftUI

struct CircleImage: View {
var image: Image
Expand Down
6 changes: 3 additions & 3 deletions Landmarks/Landmarks/Supporting Views/LandmarkRow.swift
Expand Up @@ -5,7 +5,7 @@ Abstract:
A single row to be displayed in a list of landmarks.
*/

import SwiftUI
// import SwiftUI

struct LandmarkRow: View {
var landmark: Landmark
Expand All @@ -16,11 +16,11 @@ struct LandmarkRow: View {
Text(verbatim: landmark.name)
Spacer()

if landmark.isFavorite {
/*if landmark.isFavorite {
Image(systemName: "star.fill")
.imageScale(.medium)
.foregroundColor(.yellow)
}
}*/
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Landmarks/Landmarks/Supporting Views/MapView.swift
Expand Up @@ -5,7 +5,7 @@ Abstract:
A view that hosts an `MKMapView`.
*/

import SwiftUI
// import SwiftUI
import MapKit

struct MapView: UIViewRepresentable {
Expand Down

0 comments on commit 077c6f1

Please sign in to comment.