Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Diffiable/Settings/SettingsSection.swift

176 lines
8.0 KiB
Swift
Raw Normal View History

//
// SettingsSection.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-4-25.
//
2021-07-22 07:47:56 +02:00
import UIKit
import CoreData
import CoreDataStack
import MastodonAsset
import MastodonLocalization
enum SettingsSection: Hashable {
2021-06-30 14:11:38 +02:00
case appearance
2022-02-14 12:57:15 +01:00
case appearancePreference
case preference
2022-02-08 11:17:17 +01:00
case notifications
case boringZone
case spicyZone
var title: String {
switch self {
2022-03-10 09:20:25 +01:00
case .appearance: return L10n.Scene.Settings.Section.LookAndFeel.title
2022-02-14 12:57:15 +01:00
case .appearancePreference: return ""
2022-03-10 09:20:25 +01:00
case .preference: return ""
2021-07-05 10:07:17 +02:00
case .notifications: return L10n.Scene.Settings.Section.Notifications.title
2021-07-15 15:04:31 +02:00
case .boringZone: return L10n.Scene.Settings.Section.BoringZone.title
case .spicyZone: return L10n.Scene.Settings.Section.SpicyZone.title
}
}
}
2021-07-22 07:47:56 +02:00
extension SettingsSection {
static func tableViewDiffableDataSource(
for tableView: UITableView,
managedObjectContext: NSManagedObjectContext,
settingsAppearanceTableViewCellDelegate: SettingsAppearanceTableViewCellDelegate,
settingsToggleCellDelegate: SettingsToggleCellDelegate
) -> UITableViewDiffableDataSource<SettingsSection, SettingsItem> {
UITableViewDiffableDataSource(tableView: tableView) { [
weak settingsAppearanceTableViewCellDelegate,
weak settingsToggleCellDelegate
] tableView, indexPath, item -> UITableViewCell? in
switch item {
2022-02-08 11:17:17 +01:00
case .appearance(let record):
2021-07-22 07:47:56 +02:00
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SettingsAppearanceTableViewCell.self), for: indexPath) as! SettingsAppearanceTableViewCell
2022-02-08 11:17:17 +01:00
managedObjectContext.performAndWait {
guard let setting = record.object(in: managedObjectContext) else { return }
cell.configure(setting: setting)
2021-07-22 07:47:56 +02:00
}
cell.delegate = settingsAppearanceTableViewCellDelegate
return cell
2022-02-14 12:57:15 +01:00
case .appearancePreference(let record, let appearanceType):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SettingsToggleTableViewCell.self), for: indexPath) as! SettingsToggleTableViewCell
cell.delegate = settingsToggleCellDelegate
managedObjectContext.performAndWait {
guard let setting = record.object(in: managedObjectContext) else { return }
SettingsSection.configureSettingToggle(cell: cell, item: item, setting: setting)
ManagedObjectObserver.observe(object: setting)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
// do nothing
}, receiveValue: { [weak cell] change in
guard let cell = cell else { return }
guard case .update(let object) = change.changeType,
let setting = object as? Setting else { return }
SettingsSection.configureSettingToggle(cell: cell, item: item, setting: setting)
})
.store(in: &cell.disposeBag)
}
return cell
2022-02-08 11:17:17 +01:00
case .preference(let record, _):
2021-07-22 07:47:56 +02:00
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SettingsToggleTableViewCell.self), for: indexPath) as! SettingsToggleTableViewCell
2022-02-08 11:17:17 +01:00
cell.delegate = settingsToggleCellDelegate
2021-07-22 07:47:56 +02:00
managedObjectContext.performAndWait {
2022-02-08 11:17:17 +01:00
guard let setting = record.object(in: managedObjectContext) else { return }
SettingsSection.configureSettingToggle(cell: cell, item: item, setting: setting)
2021-07-22 07:47:56 +02:00
ManagedObjectObserver.observe(object: setting)
2022-02-08 11:17:17 +01:00
.receive(on: DispatchQueue.main)
2021-07-22 07:47:56 +02:00
.sink(receiveCompletion: { _ in
// do nothing
}, receiveValue: { [weak cell] change in
guard let cell = cell else { return }
guard case .update(let object) = change.changeType,
let setting = object as? Setting else { return }
2022-02-08 11:17:17 +01:00
SettingsSection.configureSettingToggle(cell: cell, item: item, setting: setting)
2021-07-22 07:47:56 +02:00
})
.store(in: &cell.disposeBag)
}
return cell
2022-02-08 11:17:17 +01:00
case .notification(let record, let switchMode):
2021-07-22 07:47:56 +02:00
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SettingsToggleTableViewCell.self), for: indexPath) as! SettingsToggleTableViewCell
managedObjectContext.performAndWait {
2022-02-08 11:17:17 +01:00
guard let setting = record.object(in: managedObjectContext) else { return }
if let subscription = setting.activeSubscription {
SettingsSection.configureSettingToggle(cell: cell, switchMode: switchMode, subscription: subscription)
}
2021-07-22 07:47:56 +02:00
ManagedObjectObserver.observe(object: setting)
.sink(receiveCompletion: { _ in
// do nothing
}, receiveValue: { [weak cell] change in
guard let cell = cell else { return }
guard case .update(let object) = change.changeType,
let setting = object as? Setting else { return }
2022-02-08 11:17:17 +01:00
guard let subscription = setting.activeSubscription else { return }
SettingsSection.configureSettingToggle(cell: cell, switchMode: switchMode, subscription: subscription)
2021-07-22 07:47:56 +02:00
})
.store(in: &cell.disposeBag)
}
2022-02-08 11:17:17 +01:00
cell.delegate = settingsToggleCellDelegate
2021-07-22 07:47:56 +02:00
return cell
case .boringZone(let item),
.spicyZone(let item):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SettingsLinkTableViewCell.self), for: indexPath) as! SettingsLinkTableViewCell
cell.update(with: item)
return cell
2022-02-08 11:17:17 +01:00
} // end switch
2021-07-22 07:47:56 +02:00
}
}
}
extension SettingsSection {
static func configureSettingToggle(
cell: SettingsToggleTableViewCell,
item: SettingsItem,
setting: Setting
) {
2022-02-14 12:57:15 +01:00
switch item {
case .appearancePreference(_, let appearanceType):
cell.textLabel?.text = appearanceType.title
switch appearanceType {
case .preferredTrueDarkMode:
cell.switchButton.isOn = setting.preferredTrueBlackDarkMode
}
case .preference(_, let preferenceType):
cell.textLabel?.text = preferenceType.title
switch preferenceType {
case .disableAvatarAnimation:
cell.switchButton.isOn = setting.preferredStaticAvatar
case .disableEmojiAnimation:
cell.switchButton.isOn = setting.preferredStaticEmoji
case .useDefaultBrowser:
cell.switchButton.isOn = setting.preferredUsingDefaultBrowser
}
default:
assertionFailure()
2021-07-22 07:47:56 +02:00
}
}
static func configureSettingToggle(
cell: SettingsToggleTableViewCell,
switchMode: SettingsItem.NotificationSwitchMode,
subscription: NotificationSubscription
) {
cell.textLabel?.text = switchMode.title
let enabled: Bool?
switch switchMode {
case .favorite: enabled = subscription.alert.favourite
case .follow: enabled = subscription.alert.follow
case .reblog: enabled = subscription.alert.reblog
case .mention: enabled = subscription.alert.mention
}
cell.update(enabled: enabled)
}
}