mastodon-ios/Mastodon/Diffiable/Section/PollSection.swift

45 lines
1.4 KiB
Swift
Raw Normal View History

//
// PollSection.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-2.
//
import UIKit
import CoreData
import CoreDataStack
2021-03-02 12:10:45 +01:00
enum PollSection: Equatable, Hashable {
case main
}
extension PollSection {
static func tableViewDiffableDataSource(
for tableView: UITableView,
managedObjectContext: NSManagedObjectContext
) -> UITableViewDiffableDataSource<PollSection, PollItem> {
return UITableViewDiffableDataSource<PollSection, PollItem>(tableView: tableView) { tableView, indexPath, item -> UITableViewCell? in
2021-03-02 12:10:45 +01:00
switch item {
case .opion(let objectID, let attribute):
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PollOptionTableViewCell.self), for: indexPath) as! PollOptionTableViewCell
managedObjectContext.performAndWait {
let option = managedObjectContext.object(with: objectID) as! PollOption
PollSection.configure(cell: cell, pollOption: option, itemAttribute: attribute)
}
return cell
}
}
}
}
2021-03-02 12:10:45 +01:00
extension PollSection {
static func configure(
cell: PollOptionTableViewCell,
pollOption: PollOption,
itemAttribute: PollItem.Attribute
) {
cell.optionLabel.text = pollOption.title
cell.configure(state: itemAttribute.isOptionVoted ? .on : .off)
2021-03-02 12:10:45 +01:00
}
}