2021-05-27 07:56:55 +02:00
|
|
|
//
|
|
|
|
// ProfileFieldSection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-5-25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-07-23 13:10:27 +02:00
|
|
|
import MastodonMeta
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-05-27 07:56:55 +02:00
|
|
|
|
|
|
|
enum ProfileFieldSection: Equatable, Hashable {
|
|
|
|
case main
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileFieldSection {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
struct Configuration {
|
|
|
|
weak var profileFieldCollectionViewCellDelegate: ProfileFieldCollectionViewCellDelegate?
|
|
|
|
weak var profileFieldEditCollectionViewCellDelegate: ProfileFieldEditCollectionViewCellDelegate?
|
|
|
|
}
|
|
|
|
|
|
|
|
static func diffableDataSource(
|
|
|
|
collectionView: UICollectionView,
|
|
|
|
context: AppContext,
|
|
|
|
configuration: Configuration
|
2021-05-27 07:56:55 +02:00
|
|
|
) -> UICollectionViewDiffableDataSource<ProfileFieldSection, ProfileFieldItem> {
|
2022-01-27 14:23:39 +01:00
|
|
|
collectionView.register(ProfileFieldCollectionViewHeaderFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: ProfileFieldCollectionViewHeaderFooterView.headerReuseIdentifer)
|
|
|
|
collectionView.register(ProfileFieldCollectionViewHeaderFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: ProfileFieldCollectionViewHeaderFooterView.footerReuseIdentifer)
|
|
|
|
|
|
|
|
let fieldCellRegistration = UICollectionView.CellRegistration<ProfileFieldCollectionViewCell, ProfileFieldItem> { cell, indexPath, item in
|
|
|
|
guard case let .field(field) = item else { return }
|
|
|
|
|
|
|
|
// set key
|
|
|
|
do {
|
|
|
|
let mastodonContent = MastodonContent(content: field.name.value, emojis: field.emojiMeta)
|
|
|
|
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
|
|
|
|
cell.keyMetaLabel.configure(content: metaContent)
|
|
|
|
} catch {
|
|
|
|
let content = PlaintextMetaContent(string: field.name.value)
|
|
|
|
cell.keyMetaLabel.configure(content: content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set value
|
|
|
|
do {
|
|
|
|
let mastodonContent = MastodonContent(content: field.value.value, emojis: field.emojiMeta)
|
|
|
|
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
|
|
|
|
cell.valueMetaLabel.configure(content: metaContent)
|
|
|
|
} catch {
|
|
|
|
let content = PlaintextMetaContent(string: field.value.value)
|
|
|
|
cell.valueMetaLabel.configure(content: content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set background
|
|
|
|
var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell()
|
|
|
|
backgroundConfiguration.backgroundColor = UIColor.secondarySystemBackground
|
|
|
|
cell.backgroundConfiguration = backgroundConfiguration
|
|
|
|
|
|
|
|
cell.delegate = configuration.profileFieldCollectionViewCellDelegate
|
|
|
|
}
|
|
|
|
|
|
|
|
let editFieldCellRegistration = UICollectionView.CellRegistration<ProfileFieldEditCollectionViewCell, ProfileFieldItem> { cell, indexPath, item in
|
|
|
|
guard case let .editField(field) = item else { return }
|
|
|
|
|
|
|
|
cell.keyTextField.text = field.name.value
|
|
|
|
cell.valueTextField.text = field.value.value
|
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: cell.keyTextField)
|
|
|
|
.compactMap { $0.object as? UITextField }
|
|
|
|
.map { $0.text ?? "" }
|
|
|
|
.removeDuplicates()
|
|
|
|
.assign(to: \.value, on: field.name)
|
2021-05-27 08:57:20 +02:00
|
|
|
.store(in: &cell.disposeBag)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: cell.valueTextField)
|
|
|
|
.compactMap { $0.object as? UITextField }
|
|
|
|
.map { $0.text ?? "" }
|
|
|
|
.removeDuplicates()
|
|
|
|
.assign(to: \.value, on: field.value)
|
2021-05-27 07:56:55 +02:00
|
|
|
.store(in: &cell.disposeBag)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// set background
|
|
|
|
var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell()
|
|
|
|
backgroundConfiguration.backgroundColor = UIColor.secondarySystemBackground
|
|
|
|
cell.backgroundConfiguration = backgroundConfiguration
|
2021-05-27 07:56:55 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
cell.delegate = configuration.profileFieldEditCollectionViewCellDelegate
|
|
|
|
}
|
|
|
|
|
|
|
|
let addEntryCellRegistration = UICollectionView.CellRegistration<ProfileFieldAddEntryCollectionViewCell, ProfileFieldItem> { cell, indexPath, item in
|
|
|
|
guard case .addEntry = item else { return }
|
|
|
|
|
|
|
|
var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell()
|
|
|
|
backgroundConfiguration.backgroundColorTransformer = .init { [weak cell] _ in
|
|
|
|
guard let cell = cell else {
|
|
|
|
return .secondarySystemBackground
|
|
|
|
}
|
|
|
|
let state = cell.configurationState
|
|
|
|
if state.isHighlighted || state.isSelected {
|
|
|
|
return .secondarySystemBackground.withAlphaComponent(0.5)
|
|
|
|
} else {
|
|
|
|
return .secondarySystemBackground
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cell.backgroundConfiguration = backgroundConfiguration
|
|
|
|
}
|
|
|
|
|
2022-02-14 09:22:03 +01:00
|
|
|
let noResultCellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, ProfileFieldItem> { cell, indexPath, item in
|
|
|
|
guard case .noResult = item else { return }
|
|
|
|
|
|
|
|
var contentConfiguration = cell.defaultContentConfiguration()
|
|
|
|
contentConfiguration.text = L10n.Scene.Search.Searching.EmptyState.noResults // FIXME:
|
|
|
|
contentConfiguration.textProperties.alignment = .center
|
|
|
|
cell.contentConfiguration = contentConfiguration
|
|
|
|
|
|
|
|
|
|
|
|
var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell()
|
|
|
|
backgroundConfiguration.backgroundColorTransformer = .init { _ in
|
|
|
|
return .secondarySystemBackground
|
|
|
|
}
|
|
|
|
cell.backgroundConfiguration = backgroundConfiguration
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let dataSource = UICollectionViewDiffableDataSource<ProfileFieldSection, ProfileFieldItem>(collectionView: collectionView) { collectionView, indexPath, item in
|
|
|
|
switch item {
|
|
|
|
case .field:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(
|
|
|
|
using: fieldCellRegistration,
|
|
|
|
for: indexPath,
|
|
|
|
item: item
|
|
|
|
)
|
|
|
|
case .editField:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(
|
|
|
|
using: editFieldCellRegistration,
|
|
|
|
for: indexPath,
|
|
|
|
item: item
|
|
|
|
)
|
|
|
|
case .addEntry:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(
|
|
|
|
using: addEntryCellRegistration,
|
|
|
|
for: indexPath,
|
|
|
|
item: item
|
2022-02-14 09:22:03 +01:00
|
|
|
)
|
|
|
|
case .noResult:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(
|
|
|
|
using: noResultCellRegistration,
|
|
|
|
for: indexPath,
|
|
|
|
item: item
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
2021-05-27 07:56:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataSource.supplementaryViewProvider = { collectionView, kind, indexPath in
|
|
|
|
switch kind {
|
|
|
|
case UICollectionView.elementKindSectionHeader:
|
|
|
|
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: ProfileFieldCollectionViewHeaderFooterView.headerReuseIdentifer, for: indexPath) as! ProfileFieldCollectionViewHeaderFooterView
|
2022-01-27 14:23:39 +01:00
|
|
|
reusableView.frame.size.height = 20
|
2021-05-27 07:56:55 +02:00
|
|
|
return reusableView
|
|
|
|
case UICollectionView.elementKindSectionFooter:
|
|
|
|
let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: ProfileFieldCollectionViewHeaderFooterView.footerReuseIdentifer, for: indexPath) as! ProfileFieldCollectionViewHeaderFooterView
|
|
|
|
return reusableView
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataSource
|
|
|
|
}
|
|
|
|
}
|