-
Notifications
You must be signed in to change notification settings - Fork 5
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
StringCatalog対応やSwinftLint修正など #15
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import Foundation | |
|
||
final class APIModelData: ObservableObject { | ||
@Published var markdownContent: [String] = [] | ||
@Published var errorMessage: String = "不明なエラー" | ||
@Published var errorMessage: String = Strings.Other.unknown | ||
|
||
private var lastFetchDate: Date? | ||
private let fetchThresholdSeconds: TimeInterval = 3600 // 1hour | ||
|
@@ -17,76 +17,65 @@ final class APIModelData: ObservableObject { | |
] | ||
|
||
func fetchPostsIfNeeded() { | ||
let now: Date = .now | ||
if let lastFetchDate { | ||
guard now.timeIntervalSince(lastFetchDate) > fetchThresholdSeconds else { | ||
print("取得できませんでした。:\(now.timeIntervalSince(lastFetchDate))") | ||
return | ||
Task { | ||
let now: Date = .now | ||
if let lastFetchDate { | ||
guard now.timeIntervalSince(lastFetchDate) > fetchThresholdSeconds else { | ||
print("取得できませんでした。:\(now.timeIntervalSince(lastFetchDate))") | ||
return | ||
} | ||
} | ||
await fetchPosts() | ||
lastFetchDate = now | ||
} | ||
fetchPosts() | ||
lastFetchDate = now | ||
} | ||
|
||
private func fetchPosts() { | ||
@MainActor | ||
private func fetchPosts() async { | ||
markdownContent.removeAll() | ||
guard var url = URL(string: Url.osushiApi) else { return } | ||
url.append(queryItems: APIModelData.githubApiQuery) | ||
|
||
URLSession.shared.dataTask(with: url) { [weak self] data, response, error in | ||
guard let data, let self else { return } | ||
if let error { | ||
// TODO: エラー処理をここに記述 | ||
DispatchQueue.main.async { | ||
self.errorMessage = "API Request Failed: \(error.localizedDescription)" | ||
} | ||
return | ||
} | ||
do { | ||
let (data, _) = try await URLSession.shared.data(from: url) | ||
|
||
// APIエラーレスポンスのハンドリング | ||
if let rateLimitError = try? self.decoder.decode(RateLimitError.self, from: data) { | ||
DispatchQueue.main.async { | ||
self.errorMessage = rateLimitError.message | ||
} | ||
if let rateLimitError = try? decoder.decode(RateLimitError.self, from: data) { | ||
errorMessage = rateLimitError.message | ||
return | ||
} | ||
|
||
if let response = try? self.decoder.decode([Post].self, from: data) { | ||
DispatchQueue.main.async { | ||
if let response = try? decoder.decode([Post].self, from: data) { | ||
// TaskGroupを使用して並列処理する | ||
await withTaskGroup(of: Void.self) { group in | ||
Comment on lines
+49
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 将来的に、 |
||
for post in response { | ||
self.fetchDownloadUrl(post.downloadUrl) | ||
group.addTask { | ||
await self.fetchDownloadUrl(post.downloadUrl) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
} catch { | ||
errorMessage = "API Request Failed: \(error.localizedDescription)" | ||
} | ||
.resume() | ||
} | ||
|
||
private func fetchDownloadUrl(_ downloadUrl: String) { | ||
@MainActor | ||
private func fetchDownloadUrl(_ downloadUrl: String) async { | ||
if downloadUrl == Url.indexDownload { return } | ||
guard let url = URL(string: downloadUrl) else { | ||
fatalError("Invalid Url") | ||
} | ||
|
||
URLSession.shared.dataTask(with: url) { data, response, error in | ||
guard let data else { return } | ||
if let error { | ||
fatalError("API Request Failed: \(error.localizedDescription)") | ||
} | ||
|
||
if let markdownString = String(data: data, encoding: .utf8) { | ||
DispatchQueue.main.async { | ||
if markdownString.starts(with: " ") { return } | ||
self.markdownContent.append(markdownString) | ||
} | ||
do { | ||
let (data, _) = try await URLSession.shared.data(from: url) | ||
let markdownString = String(decoding: data, as: UTF8.self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちら、今まで使っていた |
||
guard markdownString.starts(with: " ") else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. もし |
||
markdownContent.append(markdownString) | ||
return | ||
} else { | ||
self.markdownContent.append("**エラー**") | ||
// TODO: アラートを表示 | ||
print("Error fetching markdown content: \(error?.localizedDescription ?? "Unknown error")") | ||
} | ||
} catch { | ||
fatalError("API Request Failed: \(error.localizedDescription)") | ||
} | ||
.resume() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{ | ||
"sourceLanguage" : "ja", | ||
"strings" : { | ||
"GitHub" : { | ||
|
||
}, | ||
"iOS Osushi" : { | ||
|
||
}, | ||
"iOS Osushi🍣" : { | ||
|
||
}, | ||
"MarkdownUI" : { | ||
|
||
}, | ||
"X" : { | ||
|
||
}, | ||
"お気に入り" : { | ||
|
||
}, | ||
"お気に入りボタン" : { | ||
|
||
}, | ||
"お気に入りボタンの説明" : { | ||
"extractionState" : "manual", | ||
"localizations" : { | ||
"ja" : { | ||
"stringUnit" : { | ||
"state" : "translated", | ||
"value" : "この記事をお気に入りすることができます🍣\nお気に入りした記事は下のタブから確認することができます!" | ||
} | ||
} | ||
} | ||
}, | ||
"お気に入りリストが空の場合のメッセージ" : { | ||
"localizations" : { | ||
"ja" : { | ||
"stringUnit" : { | ||
"state" : "translated", | ||
"value" : "お気に入りボタンをタップして追加してみよう🍣" | ||
} | ||
} | ||
} | ||
}, | ||
"お気に入り一覧" : { | ||
|
||
}, | ||
"コミュニティの説明" : { | ||
"localizations" : { | ||
"ja" : { | ||
"stringUnit" : { | ||
"state" : "translated", | ||
"value" : "iOS Osushi は、iOS 関連のニュースを定期的に配信するサイトです。" | ||
} | ||
} | ||
} | ||
}, | ||
"バージョン" : { | ||
|
||
}, | ||
"ホーム" : { | ||
|
||
}, | ||
"ライセンス" : { | ||
|
||
}, | ||
"不明" : { | ||
|
||
}, | ||
"投稿一覧" : { | ||
|
||
}, | ||
"詳細情報" : { | ||
|
||
}, | ||
"運営" : { | ||
|
||
}, | ||
"運営メンバー" : { | ||
|
||
} | ||
}, | ||
"version" : "1.0" | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
namespace 的な使い方をしたいということだと思うので |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
struct Strings { | ||
struct Home { | ||
static let home = String(localized: "ホーム") | ||
static let favorite = String(localized: "お気に入り") | ||
static let iosOsushi = String(localized: "iOS Osushi") | ||
} | ||
|
||
struct PostList { | ||
static let title = String(localized: "投稿一覧") | ||
static let tipsTitle = String(localized: "お気に入りボタン") | ||
static let tipsMessage = String(localized: "お気に入りボタンの説明") | ||
static let errorMessage = String(localized: "詳細情報") | ||
} | ||
|
||
|
||
struct FavoriteList { | ||
static let title = String(localized: "お気に入り一覧") | ||
static let emptyListTitle = String(localized: "お気に入り一覧") | ||
static let emptyListMessage = String(localized: "お気に入りリストが空の場合のメッセージ") | ||
} | ||
|
||
struct Infomation { | ||
static let title = String(localized: "iOS Osushi🍣") | ||
static let management = String(localized: "運営") | ||
static let license = String(localized: "ライセンス") | ||
static let version = String(localized: "バージョン") | ||
static let description = String(localized: "コミュニティの説明") | ||
static let staffListTitle = String(localized: "運営メンバー") | ||
static let x = String(localized: "X") | ||
static let gitHub = String(localized: "GitHub") | ||
} | ||
|
||
struct Other { | ||
static let unknown = String(localized: "不明") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最新の https://github.com/github/gitignore/blob/main/Swift.gitignore を参考にしつつ、Swift Package Manager、CocoaPods、Carthage、fastlane のいずれも、コメントアウトだけでなくて記述自体を削除してしまって良いかなと思います