2021-04-08 13:47:31 +02:00
|
|
|
//
|
|
|
|
// SettingsViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by ihugo on 2021/4/7.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import Foundation
|
|
|
|
import MastodonSDK
|
|
|
|
import UIKit
|
|
|
|
import os.log
|
2021-07-13 11:39:38 +02:00
|
|
|
import AuthenticationServices
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
class SettingsViewModel {
|
2021-04-08 13:47:31 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
let context: AppContext
|
2021-07-13 11:39:38 +02:00
|
|
|
var mastodonAuthenticationController: MastodonAuthenticationController?
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
// input
|
|
|
|
let setting: CurrentValueSubject<Setting, Never>
|
2021-04-12 15:42:43 +02:00
|
|
|
var updateDisposeBag = Set<AnyCancellable>()
|
|
|
|
var createDisposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
let viewDidLoad = PassthroughSubject<Void, Never>()
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
// output
|
|
|
|
var dataSource: UITableViewDiffableDataSource<SettingsSection, SettingsItem>!
|
2021-04-12 15:42:43 +02:00
|
|
|
/// create a subscription when:
|
|
|
|
/// - does not has one
|
|
|
|
/// - does not find subscription for selected trigger when change trigger
|
|
|
|
let createSubscriptionSubject = PassthroughSubject<(triggerBy: String, values: [Bool?]), Never>()
|
2021-07-06 09:20:32 +02:00
|
|
|
let currentInstance = CurrentValueSubject<Mastodon.Entity.Instance?, Never>(nil)
|
2021-04-12 15:42:43 +02:00
|
|
|
|
|
|
|
/// update a subscription when:
|
|
|
|
/// - change switch for specified alerts
|
|
|
|
let updateSubscriptionSubject = PassthroughSubject<(triggerBy: String, values: [Bool?]), Never>()
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-17 08:01:57 +02:00
|
|
|
lazy var privacyURL: URL? = {
|
|
|
|
guard let box = AppContext.shared.authenticationService.activeMastodonAuthenticationBox.value else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return Mastodon.API.privacyURL(domain: box.domain)
|
|
|
|
}()
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
init(context: AppContext, setting: Setting) {
|
2021-04-08 13:47:31 +02:00
|
|
|
self.context = context
|
2021-04-26 10:57:50 +02:00
|
|
|
self.setting = CurrentValueSubject(setting)
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
self.setting
|
|
|
|
.sink(receiveValue: { [weak self] setting in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.processDataSource(setting)
|
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
2021-07-06 09:20:32 +02:00
|
|
|
|
|
|
|
context.authenticationService.activeMastodonAuthenticationBox
|
|
|
|
.compactMap { $0?.domain }
|
|
|
|
.map { context.apiService.instance(domain: $0) }
|
|
|
|
.switchToLatest()
|
|
|
|
.sink { [weak self] completion in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: fetch instance fail: %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
|
|
|
self.currentInstance.value = nil
|
|
|
|
case .finished:
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: fetch instance success", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
|
|
|
|
}
|
|
|
|
} receiveValue: { [weak self] response in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.currentInstance.value = response.value
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s:", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SettingsViewModel {
|
2021-07-13 11:39:38 +02:00
|
|
|
|
|
|
|
func openAuthenticationPage(
|
|
|
|
authenticateURL: URL,
|
|
|
|
presentationContextProvider: ASWebAuthenticationPresentationContextProviding
|
|
|
|
) {
|
|
|
|
let authenticationController = MastodonAuthenticationController(
|
|
|
|
context: self.context,
|
|
|
|
authenticateURL: authenticateURL
|
|
|
|
)
|
|
|
|
|
|
|
|
self.mastodonAuthenticationController = authenticationController
|
|
|
|
authenticationController.authenticationSession?.presentationContextProvider = presentationContextProvider
|
|
|
|
authenticationController.authenticationSession?.start()
|
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
// MARK: - Private methods
|
2021-04-26 10:57:50 +02:00
|
|
|
private func processDataSource(_ setting: Setting) {
|
|
|
|
guard let dataSource = self.dataSource else { return }
|
2021-04-08 13:47:31 +02:00
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<SettingsSection, SettingsItem>()
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
// appearance
|
2021-06-22 14:52:30 +02:00
|
|
|
let appearanceItems = [SettingsItem.appearance(settingObjectID: setting.objectID)]
|
2021-06-30 14:11:38 +02:00
|
|
|
snapshot.appendSections([.appearance])
|
|
|
|
snapshot.appendItems(appearanceItems, toSection: .appearance)
|
2021-07-05 10:07:17 +02:00
|
|
|
|
2021-07-08 09:56:52 +02:00
|
|
|
// notification
|
2021-04-26 10:57:50 +02:00
|
|
|
let notificationItems = SettingsItem.NotificationSwitchMode.allCases.map { mode in
|
|
|
|
SettingsItem.notification(settingObjectID: setting.objectID, switchMode: mode)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
snapshot.appendSections([.notifications])
|
|
|
|
snapshot.appendItems(notificationItems, toSection: .notifications)
|
|
|
|
|
2021-07-08 09:56:52 +02:00
|
|
|
// preference
|
|
|
|
snapshot.appendSections([.preference])
|
2021-07-15 14:48:26 +02:00
|
|
|
let preferenceItems: [SettingsItem] = [
|
2021-07-22 07:47:56 +02:00
|
|
|
.preference(settingObjectID: setting.objectID, preferenceType: .darkMode),
|
|
|
|
.preference(settingObjectID: setting.objectID, preferenceType: .disableAvatarAnimation),
|
|
|
|
.preference(settingObjectID: setting.objectID, preferenceType: .useDefaultBrowser),
|
2021-07-15 14:48:26 +02:00
|
|
|
]
|
|
|
|
snapshot.appendItems(preferenceItems,toSection: .preference)
|
2021-07-08 09:56:52 +02:00
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
// boring zone
|
2021-04-26 10:57:50 +02:00
|
|
|
let boringZoneSettingsItems: [SettingsItem] = {
|
|
|
|
let links: [SettingsItem.Link] = [
|
2021-07-13 11:39:38 +02:00
|
|
|
.accountSettings,
|
2021-04-26 10:57:50 +02:00
|
|
|
.termsOfService,
|
|
|
|
.privacyPolicy
|
|
|
|
]
|
|
|
|
let items = links.map { SettingsItem.boringZone(item: $0) }
|
|
|
|
return items
|
|
|
|
}()
|
|
|
|
snapshot.appendSections([.boringZone])
|
|
|
|
snapshot.appendItems(boringZoneSettingsItems, toSection: .boringZone)
|
|
|
|
|
|
|
|
let spicyZoneSettingsItems: [SettingsItem] = {
|
|
|
|
let links: [SettingsItem.Link] = [
|
|
|
|
.clearMediaCache,
|
|
|
|
.signOut
|
|
|
|
]
|
|
|
|
let items = links.map { SettingsItem.spicyZone(item: $0) }
|
|
|
|
return items
|
|
|
|
}()
|
|
|
|
snapshot.appendSections([.spicyZone])
|
|
|
|
snapshot.appendItems(spicyZoneSettingsItems, toSection: .spicyZone)
|
|
|
|
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: false)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
2021-04-12 15:42:43 +02:00
|
|
|
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
extension SettingsViewModel {
|
|
|
|
func setupDiffableDataSource(
|
|
|
|
for tableView: UITableView,
|
|
|
|
settingsAppearanceTableViewCellDelegate: SettingsAppearanceTableViewCellDelegate,
|
|
|
|
settingsToggleCellDelegate: SettingsToggleCellDelegate
|
|
|
|
) {
|
2021-07-22 07:47:56 +02:00
|
|
|
dataSource = SettingsSection.tableViewDiffableDataSource(
|
|
|
|
for: tableView,
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
|
|
|
settingsAppearanceTableViewCellDelegate: settingsAppearanceTableViewCellDelegate,
|
|
|
|
settingsToggleCellDelegate: settingsToggleCellDelegate
|
|
|
|
)
|
2021-04-26 10:57:50 +02:00
|
|
|
processDataSource(self.setting.value)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
}
|