Add language catalog selector
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
import MusicAssistantCore
|
||||
import SwiftUI
|
||||
|
||||
struct LanguageBrowserView: View {
|
||||
@Binding private var project: SongProject
|
||||
private let saveProject: (SongProject) async -> Bool
|
||||
private let catalog: LocalLanguageCatalog
|
||||
|
||||
@State private var searchText = ""
|
||||
@State private var selectedFamilyCategory: String?
|
||||
@State private var hasUnsavedSelectionChanges = false
|
||||
@State private var isSaving = false
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
init(
|
||||
project: Binding<SongProject>,
|
||||
saveProject: @escaping (SongProject) async -> Bool = { _ in true },
|
||||
catalog: LocalLanguageCatalog = LocalLanguageCatalog()
|
||||
) {
|
||||
_project = project
|
||||
self.saveProject = saveProject
|
||||
self.catalog = catalog
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
List(
|
||||
catalog.languages(
|
||||
matching: searchText,
|
||||
familyCategory: selectedFamilyCategory
|
||||
),
|
||||
id: \.id
|
||||
) { language in
|
||||
Toggle(isOn: selectionBinding(for: language)) {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(language.name)
|
||||
.font(.body.weight(.medium))
|
||||
|
||||
Text(language.familyCategory)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.toggleStyle(.checkbox)
|
||||
.padding(.vertical, 2)
|
||||
}
|
||||
.navigationTitle("Language Catalog")
|
||||
.searchable(text: $searchText, prompt: "Search languages")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .automatic) {
|
||||
Picker("Region", selection: $selectedFamilyCategory) {
|
||||
Text("All regions").tag(String?.none)
|
||||
|
||||
ForEach(catalog.familyCategories, id: \.self) { familyCategory in
|
||||
Text(familyCategory).tag(Optional(familyCategory))
|
||||
}
|
||||
}
|
||||
.pickerStyle(.menu)
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Done") {
|
||||
Task {
|
||||
await saveSelectionChangesIfNeeded()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
.disabled(isSaving)
|
||||
}
|
||||
|
||||
if isSaving {
|
||||
ToolbarItem(placement: .status) {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(minWidth: 480, minHeight: 560)
|
||||
.onDisappear {
|
||||
guard hasUnsavedSelectionChanges, !isSaving else { return }
|
||||
|
||||
let projectToSave = project
|
||||
hasUnsavedSelectionChanges = false
|
||||
|
||||
Task {
|
||||
_ = await saveProject(projectToSave)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func selectionBinding(for item: LanguageCatalogItem) -> Binding<Bool> {
|
||||
Binding {
|
||||
project.language(for: item.id) != nil
|
||||
} set: { isSelected in
|
||||
if isSelected {
|
||||
project.upsertLanguage(item.language)
|
||||
} else {
|
||||
project.removeLanguage(id: item.id)
|
||||
}
|
||||
hasUnsavedSelectionChanges = true
|
||||
}
|
||||
}
|
||||
|
||||
private func saveSelectionChangesIfNeeded() async {
|
||||
guard hasUnsavedSelectionChanges else { return }
|
||||
|
||||
isSaving = true
|
||||
let didSave = await saveProject(project)
|
||||
hasUnsavedSelectionChanges = !didSave
|
||||
isSaving = false
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
LanguageBrowserPreview()
|
||||
}
|
||||
|
||||
private struct LanguageBrowserPreview: View {
|
||||
@State private var project = SongProject(
|
||||
title: "Language Catalog Preview",
|
||||
idea: "Choose song languages."
|
||||
)
|
||||
|
||||
var body: some View {
|
||||
LanguageBrowserView(project: $project)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user