Cotton code
51 lines · 2.1 KB · SwiftRaw
// The small cap a feed row wears for the database it came from.//// Only the three feeds that read across databases draw it — Today, search and// "Asked of you" — and only when there is something to tell apart: the row// names a database and this device holds more than one. The cap is that// database's own enamel (`DatabasesModel.colours`), the one its cap on the// Databases screen wears, and its accessibility label is the database's name.import CottonKitimport SwiftUI/// A feed row's database, as a tiny cap. Draws nothing for a row with no/// database, or while this device holds one database.public struct DatabaseMark: View {    private let database: FeedRowDatabase?    @Environment(DatabasesModel.self) private var model: DatabasesModel?    public init(_ database: FeedRowDatabase?) {        self.database = database    }    public var body: some View {        if let model, let database, let colour = Self.colour(for: database, in: model) {            ZStack {                CottonMedia.image(colour.asset)?                    .resizable()                    .interpolation(.medium)                    .scaledToFit()                    // The enamel says nothing; the mark says the name.                    .accessibilityHidden(true)            }            .frame(width: 14, height: 14)            .accessibilityElement(children: .ignore)            .accessibilityLabel(Self.name(of: database, in: model))            .accessibilityIdentifier("database-mark-\(database.databaseId)")        }    }    /// The enamel a row's mark wears: its database's cap, when the row names    /// a database this device holds and it holds more than one. Nil draws    /// nothing.    static func colour(for database: FeedRowDatabase?, in model: DatabasesModel) -> CapColour? {        guard let database, model.held.count > 1 else { return nil }        return model.colours[database.databaseId]    }    /// The database's name: the held list's, else the one the row carried.    static func name(of database: FeedRowDatabase, in model: DatabasesModel) -> String {        model.database(database.databaseId)?.name ?? database.databaseName    }}