Skip to content

Commit

Permalink
Fix resolving conflicts failures
Browse files Browse the repository at this point in the history
  • Loading branch information
kantacky committed Feb 22, 2024
1 parent cb5e8bf commit ccee91d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 40 deletions.
2 changes: 1 addition & 1 deletion YumemiTraining/ForecastViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ForecastViewModel: ObservableObject {
do {
weather = try weatherClient.fetchThrowingWeather(area, date)
} catch {
self.handleError(error)
alertMessage = error.localizedDescription
}
}
}
41 changes: 5 additions & 36 deletions YumemiTraining/Weather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,16 @@ struct Weather: Equatable, Decodable {
var maxTemperature: Int
var minTemperature: Int

struct WeatherRequest {
var area: String
var date: Date
}

extension WeatherRequest: Encodable {
func encode() throws -> String {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.dateEncodingStrategy = .iso8601
encoder.outputFormatting = .prettyPrinted

let data = try encoder.encode(self)

guard let jsonString = String(data: data, encoding: .utf8) else {
throw WeatherError.encodeRequestError
}

return jsonString
struct WeatherRequest {
var area: String
var date: Date
}
}

typealias WeatherResponse = Weather

extension WeatherResponse: Decodable {
static func decode(jsonString: String) throws -> Self {
extension WeatherResponse {
static func decode(from jsonString: String) throws -> Self {
guard let data: Data = jsonString.data(using: .utf8) else {
throw WeatherError.decodeResponseError
}
Expand All @@ -52,18 +36,3 @@ extension WeatherResponse: Decodable {
return try decoder.decode(Weather.self, from: data)
}
}

enum WeatherError: LocalizedError {
case encodeRequestError
case decodeResponseError

var errorDescription: String? {
switch self {
case .encodeRequestError:
return "Failed to encode response"

case .decodeResponseError:
return "Failed to decode response"
}
}
}
2 changes: 1 addition & 1 deletion YumemiTraining/WeatherRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct WeatherRequest: Encodable {
func encode() throws -> String {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.dateEncodingStrategy = .formatted(.iso8601Full)
encoder.dateEncodingStrategy = .iso8601
encoder.outputFormatting = .sortedKeys

let data = try encoder.encode(self)
Expand Down
2 changes: 1 addition & 1 deletion YumemiTrainingTests/DecodeWeatherResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTest
final class DecodeWeatherResponseTests: XCTestCase {
func testDecodeWeatherResponse() throws {
// Given
guard let now = DateFormatter.iso8601Full.date(from: "2020-04-01T12:00:00+09:00") else {
guard let now = DateFormatter().date(from: "2020-04-01T12:00:00+09:00") else {
return
}
let response = """
Expand Down
2 changes: 1 addition & 1 deletion YumemiTrainingTests/EncodeWeatherRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTest
final class EncodeWeatherRequestTests: XCTestCase {
func testEncodeWeatherRequest() throws {
// Given
guard let now = DateFormatter.iso8601Full.date(from: "2020-04-01T12:00:00+09:00") else {
guard let now = DateFormatter().date(from: "2020-04-01T12:00:00+09:00") else {
return
}
let request = WeatherRequest(area: "Tokyo", date: now)
Expand Down

0 comments on commit ccee91d

Please sign in to comment.