2021-04-26 10:57:50 +02:00
|
|
|
//
|
|
|
|
// SettingService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-25.
|
|
|
|
//
|
|
|
|
|
2021-04-27 10:26:59 +02:00
|
|
|
import os.log
|
2021-04-26 10:57:50 +02:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonCommon
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public final class SettingService {
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
private var currentSettingUpdateSubscription: AnyCancellable?
|
|
|
|
|
|
|
|
// input
|
|
|
|
weak var apiService: APIService?
|
|
|
|
weak var authenticationService: AuthenticationService?
|
|
|
|
weak var notificationService: NotificationService?
|
|
|
|
|
|
|
|
// output
|
|
|
|
let settingFetchedResultController: SettingFetchedResultController
|
2022-10-08 07:43:06 +02:00
|
|
|
public let currentSetting = CurrentValueSubject<Setting?, Never>(nil)
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
init(
|
|
|
|
apiService: APIService,
|
|
|
|
authenticationService: AuthenticationService,
|
|
|
|
notificationService: NotificationService
|
|
|
|
) {
|
|
|
|
self.apiService = apiService
|
|
|
|
self.authenticationService = authenticationService
|
|
|
|
self.notificationService = notificationService
|
|
|
|
self.settingFetchedResultController = SettingFetchedResultController(
|
|
|
|
managedObjectContext: authenticationService.managedObjectContext,
|
|
|
|
additionalPredicate: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
// create setting (if non-exist) for authenticated users
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationService.$mastodonAuthenticationBoxes
|
2021-07-20 10:40:04 +02:00
|
|
|
.compactMap { [weak self] mastodonAuthenticationBoxes -> AnyPublisher<[MastodonAuthenticationBox], Never>? in
|
2021-04-26 10:57:50 +02:00
|
|
|
guard let self = self else { return nil }
|
|
|
|
guard let authenticationService = self.authenticationService else { return nil }
|
|
|
|
|
2021-09-29 12:42:52 +02:00
|
|
|
let managedObjectContext = authenticationService.backgroundManagedObjectContext
|
|
|
|
return managedObjectContext.performChanges {
|
|
|
|
for authenticationBox in mastodonAuthenticationBoxes {
|
|
|
|
let domain = authenticationBox.domain
|
|
|
|
let userID = authenticationBox.userID
|
|
|
|
_ = APIService.CoreData.createOrMergeSetting(
|
|
|
|
into: managedObjectContext,
|
|
|
|
property: Setting.Property(
|
|
|
|
domain: domain,
|
2021-10-08 12:47:46 +02:00
|
|
|
userID: userID
|
2021-09-29 12:42:52 +02:00
|
|
|
)
|
2021-04-26 10:57:50 +02:00
|
|
|
)
|
2021-09-29 12:42:52 +02:00
|
|
|
} // end for
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
|
|
|
.map { _ in mastodonAuthenticationBoxes }
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.sink { _ in
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
// bind current setting
|
|
|
|
Publishers.CombineLatest(
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationService.$mastodonAuthenticationBoxes,
|
2021-04-26 10:57:50 +02:00
|
|
|
settingFetchedResultController.settings
|
|
|
|
)
|
2022-10-09 14:07:57 +02:00
|
|
|
.sink { [weak self] mastodonAuthenticationBoxes, settings in
|
2021-04-26 10:57:50 +02:00
|
|
|
guard let self = self else { return }
|
2022-10-09 14:07:57 +02:00
|
|
|
guard let activeMastodonAuthenticationBox = mastodonAuthenticationBoxes.first else { return }
|
2021-04-26 10:57:50 +02:00
|
|
|
let currentSetting = settings.first(where: { setting in
|
2022-10-09 14:07:57 +02:00
|
|
|
return setting.domain == activeMastodonAuthenticationBox.domain
|
|
|
|
&& setting.userID == activeMastodonAuthenticationBox.userID
|
2021-04-26 10:57:50 +02:00
|
|
|
})
|
|
|
|
self.currentSetting.value = currentSetting
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
// observe current setting
|
|
|
|
currentSetting
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [weak self] setting in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let setting = setting else {
|
|
|
|
self.currentSettingUpdateSubscription = nil
|
|
|
|
return
|
|
|
|
}
|
2021-07-08 09:56:52 +02:00
|
|
|
|
|
|
|
SettingService.updatePreference(setting: setting)
|
2021-04-26 10:57:50 +02:00
|
|
|
self.currentSettingUpdateSubscription = ManagedObjectObserver.observe(object: setting)
|
|
|
|
.sink(receiveCompletion: { _ in
|
|
|
|
// do nothing
|
|
|
|
}, receiveValue: { change in
|
|
|
|
guard case .update(let object) = change.changeType,
|
|
|
|
let setting = object as? Setting else { return }
|
2021-07-08 09:56:52 +02:00
|
|
|
|
|
|
|
SettingService.updatePreference(setting: setting)
|
2021-04-26 10:57:50 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-09-30 13:28:09 +02:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "Notification", category: "SettingService")
|
|
|
|
|
2021-04-27 10:26:59 +02:00
|
|
|
Publishers.CombineLatest3(
|
2021-04-26 10:57:50 +02:00
|
|
|
notificationService.deviceToken,
|
2021-04-27 10:26:59 +02:00
|
|
|
currentSetting.eraseToAnyPublisher(),
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationService.$mastodonAuthenticationBoxes
|
2021-04-26 10:57:50 +02:00
|
|
|
)
|
2022-10-09 14:07:57 +02:00
|
|
|
.compactMap { [weak self] deviceToken, setting, mastodonAuthenticationBoxes -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Subscription>, Error>? in
|
2021-04-26 10:57:50 +02:00
|
|
|
guard let self = self else { return nil }
|
|
|
|
guard let deviceToken = deviceToken else { return nil }
|
|
|
|
guard let setting = setting else { return nil }
|
2022-10-09 14:07:57 +02:00
|
|
|
guard let authenticationBox = mastodonAuthenticationBoxes.first else { return nil }
|
2021-04-27 10:26:59 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
guard let subscription = setting.activeSubscription else { return nil }
|
|
|
|
|
|
|
|
guard setting.domain == authenticationBox.domain,
|
|
|
|
setting.userID == authenticationBox.userID else { return nil }
|
|
|
|
|
|
|
|
let _viewModel = self.notificationService?.dequeueNotificationViewModel(
|
|
|
|
mastodonAuthenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
guard let viewModel = _viewModel else { return nil }
|
|
|
|
let queryData = Mastodon.API.Subscriptions.QueryData(
|
|
|
|
policy: subscription.policy,
|
|
|
|
alerts: Mastodon.API.Subscriptions.QueryData.Alerts(
|
|
|
|
favourite: subscription.alert.favourite,
|
|
|
|
follow: subscription.alert.follow,
|
|
|
|
reblog: subscription.alert.reblog,
|
|
|
|
mention: subscription.alert.mention,
|
|
|
|
poll: subscription.alert.poll
|
|
|
|
)
|
|
|
|
)
|
|
|
|
let query = viewModel.createSubscribeQuery(
|
|
|
|
deviceToken: deviceToken,
|
|
|
|
queryData: queryData,
|
|
|
|
mastodonAuthenticationBox: authenticationBox
|
|
|
|
)
|
2021-04-27 10:26:59 +02:00
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
return apiService.createSubscription(
|
|
|
|
subscriptionObjectID: subscription.objectID,
|
|
|
|
query: query,
|
|
|
|
mastodonAuthenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
}
|
2021-04-27 10:26:59 +02:00
|
|
|
.debounce(for: .seconds(3), scheduler: DispatchQueue.main) // limit subscribe request emit time interval
|
|
|
|
.sink(receiveValue: { [weak self] publisher in
|
|
|
|
guard let self = self else { return }
|
|
|
|
publisher
|
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
2022-09-30 13:28:09 +02:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [Push Notification] subscribe failure: \(error.localizedDescription)")
|
2021-04-27 10:26:59 +02:00
|
|
|
case .finished:
|
2022-09-30 13:28:09 +02:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [Push Notification] subscribe success")
|
2021-04-27 10:26:59 +02:00
|
|
|
}
|
2021-06-18 12:57:02 +02:00
|
|
|
} receiveValue: { response in
|
2022-09-30 13:28:09 +02:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): subscribe response: \(response.value.endpoint)")
|
2021-04-27 10:26:59 +02:00
|
|
|
}
|
|
|
|
.store(in: &self.disposeBag)
|
|
|
|
})
|
2021-04-26 10:57:50 +02:00
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-06 09:05:24 +02:00
|
|
|
|
|
|
|
extension SettingService {
|
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
public static func openSettingsAlertController(title: String, message: String) -> UIAlertController {
|
2021-05-06 09:05:24 +02:00
|
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
let settingAction = UIAlertAction(title: L10n.Common.Controls.Actions.settings, style: .default) { _ in
|
|
|
|
guard let url = URL(string: UIApplication.openSettingsURLString) else { return }
|
|
|
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
|
|
|
}
|
|
|
|
alertController.addAction(settingAction)
|
|
|
|
let cancelAction = UIAlertAction(title: L10n.Common.Controls.Actions.cancel, style: .cancel, handler: nil)
|
|
|
|
alertController.addAction(cancelAction)
|
|
|
|
return alertController
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-07-08 09:56:52 +02:00
|
|
|
|
|
|
|
extension SettingService {
|
|
|
|
|
|
|
|
static func updatePreference(setting: Setting) {
|
|
|
|
// set theme
|
|
|
|
let themeName: ThemeName = setting.preferredTrueBlackDarkMode ? .system : .mastodon
|
|
|
|
if UserDefaults.shared.currentThemeNameRawValue != themeName.rawValue {
|
|
|
|
ThemeService.shared.set(themeName: themeName)
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: update theme style", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set avatar mode
|
|
|
|
if UserDefaults.shared.preferredStaticAvatar != setting.preferredStaticAvatar {
|
|
|
|
UserDefaults.shared.preferredStaticAvatar = setting.preferredStaticAvatar
|
|
|
|
}
|
|
|
|
|
2021-07-23 13:33:05 +02:00
|
|
|
// set emoji mode
|
|
|
|
if UserDefaults.shared.preferredStaticEmoji != setting.preferredStaticEmoji {
|
|
|
|
UserDefaults.shared.preferredStaticEmoji = setting.preferredStaticEmoji
|
|
|
|
}
|
|
|
|
|
2021-07-08 09:56:52 +02:00
|
|
|
// set browser
|
|
|
|
if UserDefaults.shared.preferredUsingDefaultBrowser != setting.preferredUsingDefaultBrowser {
|
|
|
|
UserDefaults.shared.preferredUsingDefaultBrowser = setting.preferredUsingDefaultBrowser
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 14:43:16 +02:00
|
|
|
|
2021-07-08 09:56:52 +02:00
|
|
|
}
|