2022-11-25 13:07:28 +01:00
|
|
|
//
|
|
|
|
// FollowedTagsViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Marcus Kida on 23.11.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
import MastodonCore
|
|
|
|
|
|
|
|
final class FollowedTagsViewModel: NSObject {
|
|
|
|
let logger = Logger(subsystem: String(describing: FollowedTagsViewModel.self), category: "ViewModel")
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
let fetchedResultsController: FollowedTagsFetchedResultController
|
|
|
|
|
|
|
|
private weak var tableView: UITableView?
|
2022-12-01 11:39:02 +01:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<Section, Item>?
|
|
|
|
|
2022-11-25 13:07:28 +01:00
|
|
|
// input
|
|
|
|
let context: AppContext
|
|
|
|
let authContext: AuthContext
|
|
|
|
|
2022-11-25 13:43:20 +01:00
|
|
|
// output
|
|
|
|
let presentHashtagTimeline = PassthroughSubject<HashtagTimelineViewModel, Never>()
|
|
|
|
|
2022-11-25 13:07:28 +01:00
|
|
|
init(context: AppContext, authContext: AuthContext) {
|
|
|
|
self.context = context
|
|
|
|
self.authContext = authContext
|
|
|
|
self.fetchedResultsController = FollowedTagsFetchedResultController(
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
|
|
|
domain: authContext.mastodonAuthenticationBox.domain,
|
|
|
|
user: authContext.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)!.user
|
|
|
|
)
|
2022-12-01 11:39:02 +01:00
|
|
|
|
2022-11-25 13:07:28 +01:00
|
|
|
super.init()
|
2022-12-01 11:39:02 +01:00
|
|
|
|
2022-11-25 13:07:28 +01:00
|
|
|
self.fetchedResultsController
|
|
|
|
.$records
|
|
|
|
.receive(on: DispatchQueue.main)
|
2022-12-01 11:39:02 +01:00
|
|
|
.sink { [weak self] records in
|
|
|
|
guard let self = self else { return }
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
snapshot.appendSections([.main])
|
|
|
|
snapshot.appendItems(records.map {.hashtag($0) })
|
|
|
|
self.diffableDataSource?.applySnapshot(snapshot, animated: true)
|
2022-11-25 13:07:28 +01:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FollowedTagsViewModel {
|
|
|
|
func setupTableView(_ tableView: UITableView) {
|
|
|
|
self.tableView = tableView
|
2022-12-01 11:39:02 +01:00
|
|
|
setupDiffableDataSource(tableView: tableView)
|
2022-11-25 13:07:28 +01:00
|
|
|
tableView.delegate = self
|
|
|
|
|
2022-12-01 11:39:02 +01:00
|
|
|
fetchFollowedTags()
|
2022-11-25 13:07:28 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 11:39:02 +01:00
|
|
|
func fetchFollowedTags() {
|
|
|
|
Task { @MainActor in
|
|
|
|
try await context.apiService.getFollowedTags(
|
|
|
|
domain: authContext.mastodonAuthenticationBox.domain,
|
|
|
|
query: Mastodon.API.Account.FollowedTagsQuery(limit: nil),
|
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
|
|
|
)
|
2022-11-25 13:07:28 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 11:39:02 +01:00
|
|
|
|
2022-11-25 13:07:28 +01:00
|
|
|
func followOrUnfollow(_ tag: Tag) {
|
|
|
|
Task { @MainActor in
|
|
|
|
switch tag.following {
|
|
|
|
case true:
|
|
|
|
_ = try? await context.apiService.unfollowTag(
|
|
|
|
for: tag.name,
|
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
|
|
|
)
|
|
|
|
case false:
|
|
|
|
_ = try? await context.apiService.followTag(
|
|
|
|
for: tag.name,
|
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
|
|
|
)
|
|
|
|
}
|
2022-12-01 11:39:02 +01:00
|
|
|
fetchFollowedTags()
|
2022-11-25 13:07:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FollowedTagsViewModel: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2022-11-25 13:43:20 +01:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(indexPath)")
|
2022-11-25 13:07:28 +01:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
2022-11-25 13:43:20 +01:00
|
|
|
|
2022-12-01 11:39:02 +01:00
|
|
|
let object = fetchedResultsController.records[indexPath.row]
|
2022-11-25 13:43:20 +01:00
|
|
|
|
|
|
|
let hashtagTimelineViewModel = HashtagTimelineViewModel(
|
|
|
|
context: self.context,
|
|
|
|
authContext: self.authContext,
|
|
|
|
hashtag: object.name
|
|
|
|
)
|
2022-12-01 11:39:02 +01:00
|
|
|
|
2022-11-25 13:43:20 +01:00
|
|
|
presentHashtagTimeline.send(hashtagTimelineViewModel)
|
2022-11-25 13:07:28 +01:00
|
|
|
}
|
|
|
|
}
|