Something went wrong. Try again.
tangled on ur phone
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import SwiftUI
/// One directory of a repository: folders first, then files, with the latest/// commit that touched each entry.struct TreeView: View { var repo: RepoSummary var ref: String? var path: String
@Environment(AppModel.self) private var model @State private var state: LoadState<[TreeEntry]> = .idle @State private var query = "" /// Shown in the header when browsing the default branch without a ref. @State private var defaultBranch: String?
private var title: String { path.split(separator: "/").last.map(String.init) ?? repo.name }
var body: some View { Group { if repo.repoDID == nil { RepoNotMigratedView() } else { LoadStateView(state: state, retry: { await load() }) { entries in entryList(entries) } } } .fileSearch(text: $query, isRoot: path.isEmpty) .navigationTitle(title) .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .principal) { VStack(spacing: 0) { Text(title) .font(.headline) Label { Text(ref ?? defaultBranch ?? "Default branch") } icon: { Image(systemName: "arrow.triangle.branch") .imageScale(.small) } .labelStyle(.titleAndIcon) .font(.caption) .foregroundStyle(.secondary) } } } .task { await load() } .refreshable { await load() } }
@ViewBuilder private func entryList(_ entries: [TreeEntry]) -> some View { let visible = query.isEmpty ? entries : entries.filter { $0.name.localizedCaseInsensitiveContains(query) } if entries.isEmpty { ContentUnavailableView("Empty Folder", systemImage: "folder") } else if visible.isEmpty { ContentUnavailableView.search(text: query) } else { List(visible) { entry in row(for: entry) } .listStyle(.plain) } }
@ViewBuilder private func row(for entry: TreeEntry) -> some View { if entry.isSubmodule { EntryRow(entry: entry) } else { NavigationLink(value: destination(for: entry)) { EntryRow(entry: entry) } } }
private func destination(for entry: TreeEntry) -> Route { let child = path.isEmpty ? entry.name : "\(path)/\(entry.name)" if entry.isDirectory { return .tree(repo, ref: ref, path: child) } return .file(repo, ref: ref, path: child) }
private func load() async { guard let repoDID = repo.repoDID else { return } do { let output = try await model.bobbin.tree(repoDID: repoDID, ref: ref, path: path) state = .loaded(output.entries.sorted(by: TreeView.isBefore)) } catch { state = .failed(error) } if ref == nil, defaultBranch == nil { defaultBranch = try? await model.bobbin.branches(repoDID: repoDID).defaultBranch } }
/// Folders first, then files, each alphabetically. private static func isBefore(_ lhs: TreeEntry, _ rhs: TreeEntry) -> Bool { if lhs.isDirectory != rhs.isDirectory { return lhs.isDirectory } return lhs.name.localizedStandardCompare(rhs.name) == .orderedAscending }}
private struct EntryRow: View { var entry: TreeEntry
var body: some View { HStack(alignment: .top, spacing: 12) { Image(systemName: icon) .foregroundStyle(iconColor) .frame(width: 22) .padding(.top, 2)
VStack(alignment: .leading, spacing: 3) { Text(entry.name) .lineLimit(1)
if let commit = entry.lastCommit { HStack(spacing: 8) { Text(firstLine(of: commit.message)) .lineLimit(1) Spacer(minLength: 0) RelativeTimeText(commit.committer.when) } .font(.caption) .foregroundStyle(.secondary) } } } .padding(.vertical, 2) }
private var icon: String { if entry.isDirectory { return "folder.fill" } if entry.isSubmodule { return "shippingbox" } if entry.isSymlink { return "arrow.turn.up.right" } return "doc.text" }
private var iconColor: Color { entry.isDirectory ? .blue : .secondary }
private func firstLine(of message: String) -> String { String(message.prefix(while: { $0 != "\n" })) }}
private extension View { /// A nav-bar search drawer animates in over the content when a folder is /// pushed. On iOS 26 search is minimized to a toolbar button instead; on /// older systems it's only offered at the root of the tree. @ViewBuilder func fileSearch(text: Binding<String>, isRoot: Bool) -> some View { if #available(iOS 26, *) { searchable(text: text, prompt: "Find a file") .searchToolbarBehavior(.minimize) } else if isRoot { searchable(text: text, prompt: "Find a file") } else { self } }}