Cotton code
139 lines · 6.4 KB · SwiftRaw
// A GROUP OR A PLACE (mail.html "A machine group"; the side page's Drafts, Sent, Archived and Bin; the whole Inbox; and// Mail's own list under its chips, owner 26 Nov 2026):// Back to Inbox, its title and its counted line; why its mail is here, linked to the rules; its rows, newest first,// with the same swipes and long press as the home; and at the foot the two buttons that carry their counts, "Mark 17// read" and "Archive 214", Archive in one tap with the exact count (owner, 25 Nov 2026). The after-state stands on the// page until he leaves it. `mail.list {place, account?, after?}`.#if os(iOS)import CottonKitimport SwiftUIstruct PostListScreen: View {    let model: PostModel    let place: String    var body: some View {        VStack(spacing: 0) {            PostBar(back: PostWords.line("home.title"), onBack: { model.pop() }).padding(.horizontal, PostInk.side)            PostListBody(model: model, place: place, titled: true) { EmptyView() }        }        .background(PostInk.ground)        .accessibilityIdentifier("post-list-\(place)")    }}/// A place's list, newest first, paged as the last row comes into view, with the bulk buttons at its foot: the whole/// of a group's or a place's screen, and Mail's own list under its chips (`header` stands above it in the scroll).struct PostListBody<Header: View>: View {    let model: PostModel    let place: String    /// The place's own title above its line (a pushed place); Mail draws its own.    var titled: Bool    /// When Mail has nothing to list at all (no account yet), its own empty line instead of the place's.    var empty: String? = nil    var first: PostFirstSync? = nil    @ViewBuilder var header: () -> Header    @State private var list: PostRead<PostList>?    @State private var more: [PostRow] = []    @State private var next: String?    @State private var after: PostAfter?    @Environment(\.dynamicTypeSize) private var type    var body: some View {        VStack(spacing: 0) {            ScrollView {                LazyVStack(alignment: .leading, spacing: 0) {                    header()                    if titled { PostTitle(text: list?.answer?.title ?? "") }                    if let line = list?.answer?.line { PostLead(text: line) }                    if let bin = list?.answer?.bin { PostNote(text: bin.line) }                    PostAfterView(model: model, after: $after).padding(.top, 8)                    switch list {                    case .read(let list)?:                        let rows = list.rows + more                        if rows.isEmpty, let line = empty ?? list.empty {                            PostEmpty(line: line, franked: place == "inbox", first: first ?? list.firstSync)                        }                        ForEach(Array(rows.enumerated()), id: \.element.id) { index, row in                            PostMailRow(model: model, row: row, last: index == rows.count - 1, after: $after)                                .onAppear { if index == rows.count - 1 { Task { await page() } } }                        }                        if let why = list.why {                            HStack(alignment: .firstTextBaseline, spacing: 4) {                                Text(why.line).font(PostType.small).foregroundStyle(PostInk.quiet).fixedSize(horizontal: false, vertical: true)                            }                            .padding(.top, 14)                            PostWordButton(label: why.rules, id: "post-list-rules") { model.tab = .you; model.paths[.you] = [.rules] }                        }                    case .refused(let line)?: PostRefused(line: line)                    case nil: EmptyView()                    }                }                .padding(.horizontal, PostInk.side)                .padding(.bottom, 24)            }            .refreshable { model.changed() }            if let bulk = list?.answer?.bulk, !bulk.isEmpty {                // Side by side, or one over the other when the words would not fit.                AnyLayout(type.isAccessibilitySize ? AnyLayout(VStackLayout(spacing: 10)) : AnyLayout(HStackLayout(spacing: 10))) {                    ForEach(bulk) { act in                        PostButton(label: act.label, weight: act.act == "archive" ? .go : .quiet, id: "post-bulk-\(act.act)") {                            Task { await run(act) }                        }                        .disabled(act.count == 0)                        .opacity(act.count == 0 ? 0.45 : 1)                    }                }                .padding(.horizontal, PostInk.side)                .padding(.vertical, 12)            }        }        .task(id: "\(model.generation)/\(model.account ?? "")/\(place)") {            var request: [String: JSONValue] = ["place": .string(place)]            request.merge(model.accountRequest) { a, _ in a }            let read = await model.read("mail.list", request, as: PostList.self)            list = read            more = []            next = read.answer?.after        }        .onChange(of: place) { _, _ in after = nil }    }    /// The next page, as the last row comes into view.    private func page() async {        guard let cursor = next else { return }        next = nil        var request: [String: JSONValue] = ["place": .string(place), "after": .string(cursor)]        request.merge(model.accountRequest) { a, _ in a }        if case .read(let page) = await model.read("mail.list", request, as: PostList.self) {            more += page.rows            next = page.after        }    }    /// A group's bulk act: Archive every mail of the group in the Inbox now, in one tap; Mark read the unread shown.    private func run(_ act: PostList.Bulk) async {        let done: PostRead<PostAfter>        switch act.act {        case "archive":            done = await model.archive(scope: "group", key: place.hasPrefix("group:") ? String(place.dropFirst(6)) : place)        default:            let unread = ((list?.answer?.rows ?? []) + more).filter(\.unread).compactMap(\.email)            done = await model.act(act.act, emails: unread)        }        switch done {        case .read(let answer): after = answer        case .refused(let line): after = PostAfter(after: "refused", line: line)        }    }}extension PostAfter {    /// A refusal said where the after-state stands, with nothing to undo.    init(after: String, line: String) {        self.after = after        self.line = line    }}#endif