-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from yumemi-inc/feature/uikit
- Loading branch information
Showing
7 changed files
with
332 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# UITableView | ||
|
||
[UITableView](https://developer.apple.com/documentation/uikit/uitableview)を使ってみましょう | ||
|
||
## 課題 | ||
|
||
- 呼び出しAPIの`List ver`を使用する | ||
|
||
- 天気リスト画面を作成する | ||
|
||
- 1行には天気アイコン、都市名、最低・最高気温を表示する | ||
- 行選択で、詳細画面へ遷移する | ||
|
||
### エクストラ | ||
|
||
- [pull to refresh](https://developer.apple.com/design/human-interface-guidelines/ios/controls/refresh-content-controls/ | ||
)機能を追加する |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// SeedRandomNumberGenerator.swift | ||
// | ||
// | ||
// Created by 古宮 伸久 on 2022/04/08. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SeedRandomNumberGenerator: RandomNumberGenerator { | ||
init(seed: Int) { | ||
// Set the random seed | ||
srand48(seed) | ||
} | ||
|
||
func next() -> UInt64 { | ||
UInt64(drand48() * Double(UInt64.max)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// YumemiWeatherList.swift | ||
// | ||
// | ||
// Created by 古宮 伸久 on 2022/04/04. | ||
// | ||
|
||
import Foundation | ||
|
||
struct AreaRequest: Decodable { | ||
let areas: [String] | ||
let date: Date | ||
} | ||
|
||
struct AreaResponse: Codable { | ||
let area: Area | ||
let info: Response | ||
} | ||
|
||
public enum Area: String, CaseIterable, Codable { | ||
case Sapporo | ||
case Sendai | ||
case Niigata | ||
case Kanazawa | ||
case Tokyo | ||
case Nagoya | ||
case Osaka | ||
case Hiroshima | ||
case Kochi | ||
case Fukuoka | ||
case Kagoshima | ||
case Naha | ||
} | ||
|
||
public extension YumemiWeather { | ||
|
||
/// 擬似 天気予報一覧API Json ver | ||
/// - Parameter jsonString: 地域と日付を含むJson文字列 | ||
/// example: | ||
/// { | ||
/// "areas": ["Tokyo"], | ||
/// "date": "2020-04-01T12:00:00+09:00" | ||
/// } | ||
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する | ||
/// - Returns: Json文字列 | ||
/// example: [{area: Tokyo, info: {"max_temp":25,"date":"2020-04-01T12:00:00+09:00","min_temp":7,"weather":"cloudy"}}] | ||
static func fetchWeatherList(_ jsonString: String) throws -> String { | ||
guard let requestData = jsonString.data(using: .utf8), | ||
let request = try? decoder.decode(AreaRequest.self, from: requestData) else { | ||
throw YumemiWeatherError.invalidParameterError | ||
} | ||
|
||
if Int.random(in: 0...4) == 4 { | ||
throw YumemiWeatherError.unknownError | ||
} | ||
|
||
let areas = request.areas.isEmpty ? Area.allCases : request.areas.compactMap { Area(rawValue: $0) } | ||
let response = areas.map { area -> AreaResponse in | ||
var hasher = Hasher() | ||
hasher.combine(area) | ||
hasher.combine(request.date) | ||
return AreaResponse(area: area, info: makeRandomResponse(date: request.date, seed: hasher.finalize())) | ||
} | ||
let responseData = try encoder.encode(response) | ||
|
||
return String(data: responseData, encoding: .utf8)! | ||
} | ||
|
||
/// 擬似 天気予報一覧API Sync ver | ||
/// - Parameter jsonString: 地域と日付を含むJson文字列 | ||
/// example: | ||
/// { | ||
/// "areas": ["Tokyo"], | ||
/// "date": "2020-04-01T12:00:00+09:00" | ||
/// } | ||
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する | ||
/// - Returns: Json文字列 | ||
static func syncFetchWeatherList(_ jsonString: String) throws -> String { | ||
Thread.sleep(forTimeInterval: apiDuration) | ||
return try self.fetchWeatherList(jsonString) | ||
} | ||
|
||
/// 擬似 天気予報一覧API Callback ver | ||
/// - Parameters: | ||
/// - jsonString: 地域と日付を含むJson文字列 | ||
/// example: | ||
/// { | ||
/// "areas": ["Tokyo"], | ||
/// "date": "2020-04-01T12:00:00+09:00" | ||
/// } | ||
/// - completion: 完了コールバック | ||
static func callbackFetchWeatherList(_ jsonString: String, completion: @escaping (Result<String, YumemiWeatherError>) -> Void) { | ||
DispatchQueue.global().asyncAfter(deadline: .now() + apiDuration) { | ||
do { | ||
let response = try fetchWeatherList(jsonString) | ||
completion(Result.success(response)) | ||
} | ||
catch let error where error is YumemiWeatherError { | ||
completion(Result.failure(error as! YumemiWeatherError)) | ||
} | ||
catch { | ||
fatalError() | ||
} | ||
} | ||
} | ||
|
||
/// 擬似 天気予報一覧API Async ver | ||
/// - Parameter jsonString: 地域と日付を含むJson文字列 | ||
/// example: | ||
/// { | ||
/// "areas": ["Tokyo"], | ||
/// "date": "2020-04-01T12:00:00+09:00" | ||
/// } | ||
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する | ||
/// - Returns: Json文字列 | ||
@available(iOS 13, macOS 10.15, *) | ||
static func asyncFetchWeatherList(_ jsonString: String) async throws -> String { | ||
return try await withCheckedThrowingContinuation { continuation in | ||
callbackFetchWeatherList(jsonString) { result in | ||
continuation.resume(with: result) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.