2021-03-31 08:28:40 +02:00
|
|
|
//
|
|
|
|
// SearchViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/31.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
2021-03-31 08:48:34 +02:00
|
|
|
import MastodonSDK
|
|
|
|
import UIKit
|
2021-03-31 14:56:11 +02:00
|
|
|
import OSLog
|
2021-03-31 08:28:40 +02:00
|
|
|
|
|
|
|
final class SearchViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-03-31 08:48:34 +02:00
|
|
|
// input
|
2021-03-31 13:29:54 +02:00
|
|
|
let context: AppContext
|
|
|
|
|
|
|
|
// output
|
|
|
|
let searchText = CurrentValueSubject<String, Never>("")
|
|
|
|
|
|
|
|
var recommendHashTags = [Mastodon.Entity.Tag]()
|
|
|
|
var recommendAccounts = [Mastodon.Entity.Account]()
|
2021-03-31 08:28:40 +02:00
|
|
|
|
|
|
|
init(context: AppContext) {
|
|
|
|
self.context = context
|
2021-04-01 05:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func requestRecommendData() {
|
2021-03-31 14:56:11 +02:00
|
|
|
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else {
|
|
|
|
return
|
|
|
|
}
|
2021-04-01 05:49:38 +02:00
|
|
|
let trendsAPI = context.apiService.recommendTrends(domain: activeMastodonAuthenticationBox.domain, query: Mastodon.API.Trends.Query(limit: 5))
|
2021-03-31 14:56:11 +02:00
|
|
|
|
2021-04-01 05:49:38 +02:00
|
|
|
let accountsAPI = context.apiService.recommendAccount(domain: activeMastodonAuthenticationBox.domain, query: nil, mastodonAuthenticationBox: activeMastodonAuthenticationBox)
|
|
|
|
Publishers.Zip(trendsAPI,accountsAPI)
|
2021-03-31 14:56:11 +02:00
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
2021-04-01 05:49:38 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: zip request fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
2021-03-31 14:56:11 +02:00
|
|
|
case .finished:
|
2021-04-01 05:49:38 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: zip request success", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-03-31 14:56:11 +02:00
|
|
|
break
|
|
|
|
}
|
2021-04-01 05:49:38 +02:00
|
|
|
} receiveValue: { [weak self] (tags, accounts) in
|
2021-03-31 14:56:11 +02:00
|
|
|
guard let self = self else { return }
|
|
|
|
self.recommendAccounts = accounts.value
|
2021-04-01 05:49:38 +02:00
|
|
|
self.recommendHashTags = tags.value
|
2021-03-31 14:56:11 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-03-31 08:28:40 +02:00
|
|
|
}
|
|
|
|
}
|