Cotton code
108 lines · 4.6 KB · SwiftRaw
// THE LIST ROW, as Android's `ListRow.kt`: full width on the theme's ground,// generous side margins; the title bold, the preview in a lighter weight (as// many lines as the core says, two at most), the date small and dim beneath// with a pin by it when pinned, a thin line of the checklist's progress under// that, the note's first picture as a small rounded thumbnail at the right,// a hairline rule under the row. Every word and size is the core's.#if canImport(UIKit)import SwiftUIimport UIKitpublic struct WritingListRow: View {    public var title: String?    public var preview: String    public var date: String    /// How many lines the preview may take (the core's `lines`).    public var lines: Int    public var look: WritingLook    public var pinned: Bool    /// The checklist's ticked and all boxes; no line is drawn for a note with none.    public var done: Int    public var total: Int    public var thumbnail: UIImage?    @Environment(\.dynamicTypeSize) private var typeSize    public init(title: String?, preview: String, date: String, lines: Int, look: WritingLook, pinned: Bool = false, done: Int = 0,                total: Int = 0, thumbnail: UIImage? = nil) {        self.title = title        self.preview = preview        self.date = date        self.lines = lines        self.look = look        self.pinned = pinned        self.done = done        self.total = total        self.thumbnail = thumbnail    }    public static let side: CGFloat = 20    public var body: some View {        let theme = look.theme        HStack(alignment: .top, spacing: 14) {            VStack(alignment: .leading, spacing: 2) {                if let title, !title.isEmpty {                    Text(verbatim: title)                        .font(Font(WritingFonts.font(look.type.face, size: CGFloat(look.type.list.title), weight: .bold)))                        .foregroundStyle(Color(uiColor: theme.heading))                        .lineLimit(typeSize.isAccessibilitySize ? 3 : 1)                }                if !preview.isEmpty {                    Text(verbatim: preview.replacingOccurrences(of: "\n", with: " "))                        .font(Font(WritingFonts.font(look.type.face, size: CGFloat(look.type.list.preview), weight: .regular)))                        .foregroundStyle(Color(uiColor: theme.dim))                        .lineLimit(min(2, max(1, lines)))                        .lineSpacing(3)                }                HStack(spacing: 4) {                    if pinned {                        WritingMark("pinned", colour: Color(uiColor: theme.accent), size: 12)                            .accessibilityIdentifier("row-pinned")                    }                    Text(verbatim: date)                        .font(Font(WritingFonts.font(look.type.face, size: CGFloat(look.type.list.date), weight: .medium)))                        .foregroundStyle(Color(uiColor: theme.faint))                }                .padding(.top, 3)                if total > 0 {                    GeometryReader { box in                        ZStack(alignment: .leading) {                            Capsule().fill(Color(uiColor: theme.rule))                            Capsule().fill(Color(uiColor: theme.accent))                                .frame(width: box.size.width * CGFloat(done) / CGFloat(max(total, 1)))                        }                    }                    .frame(height: 2.5)                    .containerRelativeFrame(.horizontal) { width, _ in width * 0.42 }                    .frame(maxWidth: .infinity, alignment: .leading)                    .padding(.top, 6)                    .accessibilityIdentifier("row-progress")                }            }            .frame(maxWidth: .infinity, alignment: .leading)            if let thumbnail {                Image(uiImage: thumbnail)                    .resizable()                    .scaledToFill()                    .frame(width: 60, height: 60)                    .background(Color(uiColor: theme.sheet))                    .clipShape(RoundedRectangle(cornerRadius: 8))                    .accessibilityIdentifier("row-thumbnail")            }        }        .padding(.horizontal, Self.side)        .padding(.top, 14)        .padding(.bottom, 13)        .background(Color(uiColor: theme.ground))        .overlay(alignment: .bottom) {            Rectangle()                .fill(Color(uiColor: theme.rule))                .frame(height: 1 / 2)                .padding(.leading, Self.side)        }        .contentShape(Rectangle())        .accessibilityElement(children: .combine)    }}#endif