Sources/OrgstarMobile/RenameScreen.swift

3ae217cf9d3d7884e3e5238934298e929bcc5cc0
orgstar/Sources/OrgstarMobile/RenameScreen.swift

128 lines · 6194 bytes

  1#if os(iOS)
  2import OrgApp
  3import OrgCore
  4import SwiftUI
  5
  6/// Rename Tag… and Rename TODO Keyword… from Commands: the old and new name and where, then
  7/// every change by file and line to untick, then Rename.
  8struct RenameScreen: View {
  9    let kind: ReplaceKind
 10    let workspace: WorkspaceModel
 11    let session: DocumentSession
 12    let done: (String) -> Void
 13    @State private var model: ReplaceInFiles?
 14    @State private var old: String
 15    @State private var new = ""
 16    @State private var includeConfig = true
 17    /// A root's path, or empty for every folder.
 18    @State private var scope = ""
 19    @Environment(MobileModel.self) private var app
 20    @Environment(\.dismiss) private var dismiss
 21
 22    init(kind: ReplaceKind, workspace: WorkspaceModel, session: DocumentSession, initial: String, done: @escaping (String) -> Void) {
 23        self.kind = kind
 24        self.workspace = workspace
 25        self.session = session
 26        self.done = done
 27        _old = State(initialValue: initial)
 28    }
 29
 30    var body: some View {
 31        NavigationStack {
 32            VStack(spacing: 0) {
 33                Form {
 34                    Section {
 35                        HStack {
 36                            TextField(kind == .renameTag ? LocalizedStringKey("Tag") : LocalizedStringKey("Keyword"), text: $old)
 37                                .textInputAutocapitalization(.never).autocorrectionDisabled()
 38                            Menu {
 39                                ForEach(suggestions, id: \.self) { name in Button(name) { old = name } }
 40                            } label: {
 41                                Image(systemName: "chevron.down.circle")
 42                            }
 43                            .disabled(suggestions.isEmpty)
 44                            .accessibilityLabel(Text("Choose", comment: "Rename: menu of the existing names"))
 45                        }
 46                        TextField("New name", text: $new)
 47                            .textInputAutocapitalization(.never).autocorrectionDisabled()
 48                        Picker("In", selection: $scope) {
 49                            Text("All Folders").tag("")
 50                            ForEach(workspace.rootPaths, id: \.self) { root in
 51                                Text(verbatim: (root as NSString).lastPathComponent).tag(root)
 52                            }
 53                        }
 54                        Toggle(kind == .renameTag ? LocalizedStringKey("Also rename it in org-tag-alist in config.toml")
 55                                                  : LocalizedStringKey("Also rename it in org-todo-keywords in config.toml"), isOn: $includeConfig)
 56                    }
 57                    Section {
 58                        if let model, model.isSearching {
 59                            HStack {
 60                                ProgressView(value: Double(model.progress.done), total: Double(max(1, model.progress.total)))
 61                                Button("Stop") { model.cancel() }
 62                            }
 63                        } else {
 64                            Button("Find") { search() }
 65                                .disabled(old.isEmpty || new.isEmpty)
 66                        }
 67                        if let model, let text = summary(model) {
 68                            Text(text).foregroundStyle(model.problem == nil ? .secondary : .primary)
 69                        }
 70                    }
 71                }
 72                .frame(maxHeight: model?.files.isEmpty == false ? 330 : .infinity)
 73                if let model, !model.files.isEmpty || (model.configChange != nil && model.changesConfig) {
 74                    ReplacePreview(model: model)
 75                }
 76            }
 77            .navigationTitle(kind.title)
 78            .navigationBarTitleDisplayMode(.inline)
 79            .toolbar {
 80                ToolbarItem(placement: .cancellationAction) {
 81                    Button("Cancel") {
 82                        model?.cancel()
 83                        dismiss()
 84                    }
 85                }
 86                ToolbarItem(placement: .confirmationAction) {
 87                    Button(kind.applyTitle) {
 88                        if let model { done(model.apply()) }
 89                        dismiss()
 90                    }
 91                    .disabled(model.map { $0.isSearching || ($0.tickedCount == 0 && !($0.configChange != nil && $0.changesConfig)) } ?? true)
 92                }
 93            }
 94            .onChange(of: includeConfig) { if model?.mode != nil { search() } }
 95        }
 96    }
 97
 98    private var suggestions: [String] {
 99        switch kind {
100        case .renameTag: ((try? workspace.index.tags()) ?? []).sorted { $0.localizedStandardCompare($1) == .orderedAscending }
101        default:
102            session.defaults.todoSequences.flatMap { ($0.active + $0.done).map(\.name) }.reduce(into: [String]()) { if !$0.contains($1) { $0.append($1) } }
103        }
104    }
105
106    private func summary(_ model: ReplaceInFiles) -> String? {
107        if let problem = model.problem { return problem }
108        guard model.mode != nil, !model.isSearching else { return nil }
109        if model.files.isEmpty, model.configChange == nil { return String(localized: "No Matches", comment: "Replace in Files: the search found nothing") }
110        return String(localized: "\(model.tickedCount) of \(model.changeCount) changes; files: \(model.files.count)", comment: "Replace in Files: what applying does")
111    }
112
113    private func search() {
114        let model = self.model ?? ReplaceInFiles(session: session, workspace: workspace, config: config)
115        self.model = model
116        let from = old.trimmingCharacters(in: .whitespaces), to = new.trimmingCharacters(in: .whitespaces)
117        model.start(kind == .renameTag ? .renameTag(from: from, to: to) : .renameKeyword(from: from, to: to), scope: scope.isEmpty ? nil : scope, includeConfig: includeConfig)
118    }
119
120    /// config.toml through the app's settings, which write the synced folder's file.
121    private var config: ReplaceInFiles.Config {
122        var config = ReplaceInFiles.Config.standard
123        let app = app
124        config.set = { key, value in app.set(key, to: .string(value)) }
125        return config
126    }
127}
128#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form