Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Search/SearchViewModel.swift

55 lines
1.9 KiB
Swift
Raw Normal View History

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
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() {
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-04-01 05:49:38 +02:00
let accountsAPI = context.apiService.recommendAccount(domain: activeMastodonAuthenticationBox.domain, query: nil, mastodonAuthenticationBox: activeMastodonAuthenticationBox)
Publishers.Zip(trendsAPI,accountsAPI)
.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)
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)
break
}
2021-04-01 05:49:38 +02:00
} receiveValue: { [weak self] (tags, accounts) in
guard let self = self else { return }
self.recommendAccounts = accounts.value
2021-04-01 05:49:38 +02:00
self.recommendHashTags = tags.value
}
.store(in: &disposeBag)
2021-03-31 08:28:40 +02:00
}
}