From ada6d542f3efb6c5f01d7b376e0df432a7ae9ba3 Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Thu, 1 Apr 2021 11:49:38 +0800 Subject: [PATCH] fix: decode error --- .../CoreData.xcdatamodel/contents | 6 +- CoreDataStack/Entity/History.swift | 10 ++-- .../Scene/Search/SearchViewController.swift | 1 + Mastodon/Scene/Search/SearchViewModel.swift | 59 ++++++++++++------- .../MastodonSDK/API/Mastodon+API+Search.swift | 34 +++++------ .../API/Mastodon+API+Suggestions.swift | 2 +- .../MastodonSDK/API/Mastodon+API+Trends.swift | 2 +- .../MastodonSDK/API/Mastodon+API.swift | 3 + .../Entity/Mastodon+Entity+History.swift | 4 +- 9 files changed, 71 insertions(+), 50 deletions(-) diff --git a/CoreDataStack/CoreData.xcdatamodeld/CoreData.xcdatamodel/contents b/CoreDataStack/CoreData.xcdatamodeld/CoreData.xcdatamodel/contents index c9ebac45f..3c73a77bf 100644 --- a/CoreDataStack/CoreData.xcdatamodeld/CoreData.xcdatamodel/contents +++ b/CoreDataStack/CoreData.xcdatamodeld/CoreData.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -35,11 +35,11 @@ - + - + diff --git a/CoreDataStack/Entity/History.swift b/CoreDataStack/Entity/History.swift index 552e2a406..114879298 100644 --- a/CoreDataStack/Entity/History.swift +++ b/CoreDataStack/Entity/History.swift @@ -14,8 +14,8 @@ public final class History: NSManagedObject { @NSManaged public private(set) var createAt: Date @NSManaged public private(set) var day: Date - @NSManaged public private(set) var uses: Int - @NSManaged public private(set) var accounts: Int + @NSManaged public private(set) var uses: String + @NSManaged public private(set) var accounts: String // many-to-one relationship @NSManaged public private(set) var tag: Tag @@ -43,10 +43,10 @@ public extension History { public extension History { struct Property { public let day: Date - public let uses: Int - public let accounts: Int + public let uses: String + public let accounts: String - public init(day: Date, uses: Int, accounts: Int) { + public init(day: Date, uses: String, accounts: String) { self.day = day self.uses = uses self.accounts = accounts diff --git a/Mastodon/Scene/Search/SearchViewController.swift b/Mastodon/Scene/Search/SearchViewController.swift index 8d6139e17..f76f596c0 100644 --- a/Mastodon/Scene/Search/SearchViewController.swift +++ b/Mastodon/Scene/Search/SearchViewController.swift @@ -47,6 +47,7 @@ extension SearchViewController { searchBar.delegate = self navigationItem.titleView = searchBar navigationItem.hidesBackButton = true + viewModel.requestRecommendData() } } diff --git a/Mastodon/Scene/Search/SearchViewModel.swift b/Mastodon/Scene/Search/SearchViewModel.swift index 37e4344cd..160dc069d 100644 --- a/Mastodon/Scene/Search/SearchViewModel.swift +++ b/Mastodon/Scene/Search/SearchViewModel.swift @@ -26,39 +26,56 @@ final class SearchViewModel { init(context: AppContext) { self.context = context + } + + func requestRecommendData() { guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return } - context.apiService.recommendTrends(domain: activeMastodonAuthenticationBox.domain, query: Mastodon.API.Trends.Query(limit: 5)) + let trendsAPI = context.apiService.recommendTrends(domain: activeMastodonAuthenticationBox.domain, query: Mastodon.API.Trends.Query(limit: 5)) +// .sink { completion in +// switch completion { +// case .failure(let error): +// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendTrends fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) +// case .finished: +// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendTrends success", ((#file as NSString).lastPathComponent), #line, #function) +// break +// } +// +// } receiveValue: { [weak self] tags in +// guard let self = self else { return } +// self.recommendHashTags = tags.value +// } +// .store(in: &disposeBag) + + let accountsAPI = context.apiService.recommendAccount(domain: activeMastodonAuthenticationBox.domain, query: nil, mastodonAuthenticationBox: activeMastodonAuthenticationBox) +// .sink { completion in +// switch completion { +// case .failure(let error): +// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendAccount fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) +// case .finished: +// os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendAccount success", ((#file as NSString).lastPathComponent), #line, #function) +// break +// } +// } receiveValue: { [weak self] accounts in +// guard let self = self else { return } +// self.recommendAccounts = accounts.value +// } +// .store(in: &disposeBag) + Publishers.Zip(trendsAPI,accountsAPI) .sink { completion in switch completion { case .failure(let error): - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendTrends fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: zip request fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) case .finished: - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendTrends success", ((#file as NSString).lastPathComponent), #line, #function) + os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: zip request success", ((#file as NSString).lastPathComponent), #line, #function) break } - - } receiveValue: { [weak self] tags in + } receiveValue: { [weak self] (tags, accounts) in guard let self = self else { return } + self.recommendAccounts = accounts.value self.recommendHashTags = tags.value } .store(in: &disposeBag) - - context.apiService.recommendAccount(domain: activeMastodonAuthenticationBox.domain, query: nil, mastodonAuthenticationBox: activeMastodonAuthenticationBox) - .sink { completion in - switch completion { - case .failure(let error): - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendAccount fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription) - case .finished: - os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: recommendAccount success", ((#file as NSString).lastPathComponent), #line, #function) - break - } - } receiveValue: { [weak self] accounts in - guard let self = self else { return } - self.recommendAccounts = accounts.value - } - .store(in: &disposeBag) - } } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift index b157d0938..8f266437f 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift @@ -8,27 +8,27 @@ import Combine import Foundation -/// Search results -/// -/// Search for content in accounts, statuses and hashtags. -/// -/// Version history: -/// 2.4.1 - added, limit hardcoded to 5 -/// 2.8.0 - add type, limit, offset, min_id, max_id, account_id -/// 3.0.0 - add exclude_unreviewed param -/// # Reference -/// [Document](https://docs.joinmastodon.org/methods/search/) -/// - Parameters: -/// - session: `URLSession` -/// - domain: Mastodon instance domain. e.g. "example.com" -/// - query: search query -/// - Returns: `AnyPublisher` contains `Accounts,Hashtags,Status` nested in the response - extension Mastodon.API.Search { static func searchURL(domain: String) -> URL { - Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v2/search") + Mastodon.API.endpointV2URL(domain: domain).appendingPathComponent("search") } + /// Search results + /// + /// Search for content in accounts, statuses and hashtags. + /// + /// Version history: + /// 2.4.1 - added, limit hardcoded to 5 + /// 2.8.0 - add type, limit, offset, min_id, max_id, account_id + /// 3.0.0 - add exclude_unreviewed param + /// # Reference + /// [Document](https://docs.joinmastodon.org/methods/search/) + /// - Parameters: + /// - session: `URLSession` + /// - domain: Mastodon instance domain. e.g. "example.com" + /// - query: search query + /// - authorization: User token + /// - Returns: `AnyPublisher` contains `Accounts,Hashtags,Status` nested in the response public static func search( session: URLSession, domain: String, diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift index 5a966afbd..558089645 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift @@ -10,7 +10,7 @@ import Foundation extension Mastodon.API.Suggestions { static func suggestionsURL(domain: String) -> URL { - Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v1/suggestions") + Mastodon.API.endpointURL(domain: domain).appendingPathComponent("suggestions") } /// Follow suggestions diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift index e0f316434..385e3d756 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift @@ -10,7 +10,7 @@ import Foundation extension Mastodon.API.Trends { static func trendsURL(domain: String) -> URL { - Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v1/trends") + Mastodon.API.endpointURL(domain: domain).appendingPathComponent("trends") } /// Trending tags diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift index 644c86b91..ac960e710 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift @@ -50,6 +50,9 @@ extension Mastodon.API { if let date = fullDatePreciseISO8601Formatter.date(from: string) { return date } + if let timestamp = TimeInterval(string) { + return Date(timeIntervalSince1970: timestamp) + } } catch { // do nothing } diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+History.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+History.swift index 9e2f9efb2..9bf1a3a28 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+History.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+History.swift @@ -19,7 +19,7 @@ extension Mastodon.Entity { public struct History: Codable { /// UNIX timestamp on midnight of the given day public let day: Date - public let uses: Int - public let accounts: Int + public let uses: String + public let accounts: String } }