Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

날씨앱 [STEP 2] hyujin #42

Open
wants to merge 7 commits into
base: rft_3_hyujin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions WeatherForecast/WeatherForecast.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
D49CA6042BD4D54D000C36AF /* ImageDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA6032BD4D54D000C36AF /* ImageDownloader.swift */; };
D49CA6062BD4FB21000C36AF /* ImageLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA6052BD4FB21000C36AF /* ImageLoader.swift */; };
D49CA6082BD74724000C36AF /* ImageCachable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA6072BD74724000C36AF /* ImageCachable.swift */; };
D49CA6172BDA56F8000C36AF /* WeatherDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA6162BDA56F8000C36AF /* WeatherDetailViewModel.swift */; };
D49CA6192BDA5B94000C36AF /* WeatherDetailLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA6182BDA5B94000C36AF /* WeatherDetailLabel.swift */; };
D49CA61B2BDA5D11000C36AF /* UIStackView+extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA61A2BDA5D11000C36AF /* UIStackView+extension.swift */; };
D49CA61D2BDA6A53000C36AF /* WeatherViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49CA61C2BDA6A53000C36AF /* WeatherViewModel.swift */; };
D4C5F2CD2BCCFD88008055B5 /* Date+extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C5F2CC2BCCFD88008055B5 /* Date+extension.swift */; };
D4C5F2D42BCF8F80008055B5 /* WeatherView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C5F2D32BCF8F80008055B5 /* WeatherView.swift */; };
D4C5F2D82BCF98C3008055B5 /* WeatherDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4C5F2D72BCF98C3008055B5 /* WeatherDetailView.swift */; };
Expand Down Expand Up @@ -70,6 +74,10 @@
D49CA6032BD4D54D000C36AF /* ImageDownloader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageDownloader.swift; sourceTree = "<group>"; };
D49CA6052BD4FB21000C36AF /* ImageLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageLoader.swift; sourceTree = "<group>"; };
D49CA6072BD74724000C36AF /* ImageCachable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageCachable.swift; sourceTree = "<group>"; };
D49CA6162BDA56F8000C36AF /* WeatherDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherDetailViewModel.swift; sourceTree = "<group>"; };
D49CA6182BDA5B94000C36AF /* WeatherDetailLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherDetailLabel.swift; sourceTree = "<group>"; };
D49CA61A2BDA5D11000C36AF /* UIStackView+extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIStackView+extension.swift"; sourceTree = "<group>"; };
D49CA61C2BDA6A53000C36AF /* WeatherViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherViewModel.swift; sourceTree = "<group>"; };
D4C5F2CC2BCCFD88008055B5 /* Date+extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+extension.swift"; sourceTree = "<group>"; };
D4C5F2D32BCF8F80008055B5 /* WeatherView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherView.swift; sourceTree = "<group>"; };
D4C5F2D72BCF98C3008055B5 /* WeatherDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherDetailView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -170,6 +178,7 @@
C7743D902B21C38100DF0D09 /* WeatherViewController.swift */,
D4C5F2D32BCF8F80008055B5 /* WeatherView.swift */,
C7743DA02B21C3B400DF0D09 /* WeatherTableViewCell.swift */,
D49CA61C2BDA6A53000C36AF /* WeatherViewModel.swift */,
);
path = Weather;
sourceTree = "<group>";
Expand All @@ -189,6 +198,8 @@
children = (
C7743DA22B21CA8500DF0D09 /* WeatherDetailViewController.swift */,
D4C5F2D72BCF98C3008055B5 /* WeatherDetailView.swift */,
D49CA6162BDA56F8000C36AF /* WeatherDetailViewModel.swift */,
D49CA6182BDA5B94000C36AF /* WeatherDetailLabel.swift */,
);
path = WeatherDetail;
sourceTree = "<group>";
Expand All @@ -198,6 +209,7 @@
children = (
D4C5F2CC2BCCFD88008055B5 /* Date+extension.swift */,
D4707EB12BCFB6E300742016 /* UIImageView+extension.swift */,
D49CA61A2BDA5D11000C36AF /* UIStackView+extension.swift */,
);
path = Extension;
sourceTree = "<group>";
Expand Down Expand Up @@ -303,6 +315,7 @@
buildActionMask = 2147483647;
files = (
D49CA5FE2BD4D1DB000C36AF /* sha256.swift in Sources */,
D49CA61B2BDA5D11000C36AF /* UIStackView+extension.swift in Sources */,
C7743DA12B21C3B400DF0D09 /* WeatherTableViewCell.swift in Sources */,
D49CA5D22BD22FEA000C36AF /* NetworkError.swift in Sources */,
D49CA5FB2BD4D0EF000C36AF /* ImageCacheManager.swift in Sources */,
Expand All @@ -311,11 +324,14 @@
D49CA5C32BD22372000C36AF /* TempFormatter.swift in Sources */,
D49CA6082BD74724000C36AF /* ImageCachable.swift in Sources */,
C7743D912B21C38100DF0D09 /* WeatherViewController.swift in Sources */,
D49CA61D2BDA6A53000C36AF /* WeatherViewModel.swift in Sources */,
C7743D8D2B21C38100DF0D09 /* AppDelegate.swift in Sources */,
D49CA6022BD4D2B7000C36AF /* DiskCache.swift in Sources */,
C7743DA32B21CA8600DF0D09 /* WeatherDetailViewController.swift in Sources */,
D49CA5D42BD23378000C36AF /* Util.swift in Sources */,
D49CA6192BDA5B94000C36AF /* WeatherDetailLabel.swift in Sources */,
C741F6702B58F00500A4DDC0 /* Weather.swift in Sources */,
D49CA6172BDA56F8000C36AF /* WeatherDetailViewModel.swift in Sources */,
D4707EB22BCFB6E300742016 /* UIImageView+extension.swift in Sources */,
C7743D8F2B21C38100DF0D09 /* SceneDelegate.swift in Sources */,
D4707EB02BCFACA900742016 /* TempUnit.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1530"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C7743D882B21C38100DF0D09"
BuildableName = "WeatherForecast.app"
BlueprintName = "WeatherForecast"
ReferencedContainer = "container:WeatherForecast.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO"
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D49CA5E72BD27F80000C36AF"
BuildableName = "WeatherForecastTests.xctest"
BlueprintName = "WeatherForecastTests"
ReferencedContainer = "container:WeatherForecast.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C7743D882B21C38100DF0D09"
BuildableName = "WeatherForecast.app"
BlueprintName = "WeatherForecast"
ReferencedContainer = "container:WeatherForecast.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C7743D882B21C38100DF0D09"
BuildableName = "WeatherForecast.app"
BlueprintName = "WeatherForecast"
ReferencedContainer = "container:WeatherForecast.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UIStackView+extension.swift
// WeatherForecast
//
// Created by Hong yujin on 4/25/24.
//

import UIKit

extension UIStackView {
convenience init(
arrangedSubviews views: [UIView],
alignment: UIStackView.Alignment,
axis: NSLayoutConstraint.Axis,
distribution: UIStackView.Distribution = .fill,
spacing: CGFloat
) {
self.init(arrangedSubviews: views)
self.alignment = alignment
self.axis = axis
self.distribution = distribution
self.spacing = spacing
}
}
23 changes: 12 additions & 11 deletions WeatherForecast/WeatherForecast/ImageCache/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ final class DiskCache: ImageCachable {

init(countLimit: Int = 50) {
self.countLimit = countLimit
self.directoryURL = try! fileManager.url(for: .cachesDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)

if let url = try? fileManager.url(for: .cachesDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true) {
directoryURL = url
} else {
directoryURL = URL(fileURLWithPath: "")
}
}

private func cacheFileURL(for key: String) -> URL {
Expand All @@ -41,19 +46,15 @@ final class DiskCache: ImageCachable {
}
}

func store(for key: String, image: UIImage) {
func store(for key: String, image: UIImage) throws {
guard let data = image.jpegData(compressionQuality: 0.5) else { return }
let fileURL = cacheFileURL(for: key)

if countStoredCaches() >= countLimit {
removeOldCache()
}

do {
try data.write(to: fileURL)
} catch {
print("Error writing image to disk: \(error)")
}

try data.write(to: fileURL)
}

private func countStoredCaches() -> Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import UIKit

protocol ImageCachable {
func value(for key: String) -> UIImage?
func store(for key: String, image: UIImage)
func store(for key: String, image: UIImage) throws
}
27 changes: 20 additions & 7 deletions WeatherForecast/WeatherForecast/ImageCache/ImageCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ enum CacheOption {
}

actor ImageCacheManager {

static let shared = ImageCacheManager()
private let memoryCache: MemoryCache = MemoryCache()
private let diskCache: DiskCache = DiskCache()
static let shared = ImageCacheManager(memoryCache: MemoryCache(), diskCache: DiskCache())
private let memoryCache: ImageCachable
private var diskCache: ImageCachable
private var option: CacheOption = .both

init(_ option: CacheOption = .both) {
init(memoryCache: ImageCachable,
diskCache: ImageCachable,
_ option: CacheOption = .both) {
self.memoryCache = memoryCache
self.diskCache = diskCache
self.option = option
}

Expand All @@ -48,13 +52,22 @@ actor ImageCacheManager {

func storeMemoryCache(for key: String, image: UIImage) {
if option == .both || option == .onlyMemory {
memoryCache.store(for: key, image: image)
do {
try memoryCache.store(for: key, image: image)
} catch {
print("Error writing image to disk: \(error)")
}
}
}

func storeDiskCache(for key: String, image: UIImage) {
if option == .both || option == .onlyDisk {
diskCache.store(for: key, image: image)

do {
try diskCache.store(for: key, image: image)
} catch {
print("Error writing image to disk: \(error)")
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions WeatherForecast/WeatherForecast/Model/TempFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ struct TempFormatter {
}

func temperatureFormat() -> String {
return "\(info.main.temp)\(tempUnit.expression)"
return String(format: "%0.2f%@", arguments: [celsiusFahrenheitConvert(temp: info.main.temp), tempUnit.expression])
}

func feelsLikeFormat() -> String {
return "\(info.main.feelsLike)\(tempUnit.expression)"
return String(format: "%0.2f%@", arguments: [celsiusFahrenheitConvert(temp: info.main.feelsLike), tempUnit.expression])
}

func maximumTemperatureFormat() -> String {
return "\(info.main.tempMax)\(tempUnit.expression)"
return String(format: "%0.2f%@", arguments: [celsiusFahrenheitConvert(temp: info.main.tempMax), tempUnit.expression])
}

func minimumTemperatureFormat() -> String {
return "\(info.main.tempMin)\(tempUnit.expression)"
return String(format: "%0.2f%@", arguments: [celsiusFahrenheitConvert(temp: info.main.tempMin), tempUnit.expression])
}


private func celsiusFahrenheitConvert(temp: Double) -> Double {
var convertValue = temp
if tempUnit == .imperial {
convertValue = (temp * 1.8) + 32
}

return convertValue
}
}
Loading