Skip to content

Commit

Permalink
Separate title and author sections
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Jun 12, 2023
1 parent d0757e4 commit 45d897c
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 76 deletions.
4 changes: 2 additions & 2 deletions BookPlayer/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Search";
"books_title" = "Books";
"folders_title" = "Folders";
"item_title_placeholder" = "Title";
"item_author_placeholder" = "Author";
"section_item_title" = "Title";
"section_item_author" = "Author";
"artwork_options_title" = "Artwork options";
"artwork_photolibrary_title" = "Choose from Photo Library";
"artwork_clipboard_title" = "Paste from clipboard";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class ItemDetailsFormViewModel: ObservableObject {
@Published var author: String
/// Artwork image
@Published var selectedImage: UIImage?
/// Original item title
var titlePlaceholder: String
/// Original item author
var authorPlaceholder: String
/// Determines if there's an update for the artwork
var artworkIsUpdated: Bool = false
/// Flag to show the author field
Expand All @@ -28,7 +32,9 @@ class ItemDetailsFormViewModel: ObservableObject {
/// Initializer
init(item: SimpleLibraryItem) {
self.title = item.title
self.titlePlaceholder = item.title
self.author = item.details
self.authorPlaceholder = item.details
self.showAuthor = item.type == .book
self.originalImageDataProvider = ArtworkService.getArtworkProvider(for: item.relativePath)
let cachedImageURL = ArtworkService.getCachedImageURL(for: item.relativePath)
Expand Down
84 changes: 55 additions & 29 deletions BookPlayer/Library/ItemDetails Screen/ItemDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,18 @@ class ItemDetailsViewModel: BaseViewModel<Coordinator> {
onTransition?(.cancel)
}

// swiftlint:disable:next function_body_length
func handleSaveAction() {
var cacheKey = item.relativePath
let cacheKey: String

let storedTitle = libraryService.getItemProperty(
#keyPath(LibraryItem.title),
relativePath: item.relativePath
) as? String

if storedTitle != formViewModel.title {
switch item.type {
case .book:
libraryService.renameBook(at: item.relativePath, with: formViewModel.title)
case .bound, .folder:
do {
let newRelativePath = try libraryService.renameFolder(at: item.relativePath, with: formViewModel.title)
cacheKey = newRelativePath
syncService.scheduleRenameFolder(at: item.relativePath, name: formViewModel.title)
} catch {
sendEvent(.showAlert(content: BPAlertContent.errorAlert(message: error.localizedDescription)))
return
}
}
do {
cacheKey = try updateTitle(formViewModel.title, relativePath: item.relativePath)
} catch {
sendEvent(.showAlert(content: BPAlertContent.errorAlert(message: error.localizedDescription)))
return
}

let storedDetails = libraryService.getItemProperty(
#keyPath(LibraryItem.details),
relativePath: item.relativePath
) as? String

if formViewModel.showAuthor,
storedDetails != formViewModel.author {
libraryService.updateDetails(at: item.relativePath, details: formViewModel.author)
if formViewModel.showAuthor {
updateAuthor(formViewModel.author, relativePath: item.relativePath)
}

guard formViewModel.artworkIsUpdated else {
Expand Down Expand Up @@ -120,6 +99,53 @@ class ItemDetailsViewModel: BaseViewModel<Coordinator> {
}
}

/// Update the item title if necessary
/// - Returns: The new relative path to be used as the cache key
func updateTitle(_ newTitle: String, relativePath: String) throws -> String {
var cacheKey = relativePath
let cleanedTitle = newTitle.trimmingCharacters(in: .whitespacesAndNewlines)

guard !cleanedTitle.isEmpty else {
return cacheKey
}

let storedTitle = libraryService.getItemProperty(
#keyPath(LibraryItem.title),
relativePath: relativePath
) as? String

guard storedTitle != cleanedTitle else {
return cacheKey
}

switch item.type {
case .book:
libraryService.renameBook(at: relativePath, with: cleanedTitle)
case .bound, .folder:
let newRelativePath = try libraryService.renameFolder(at: relativePath, with: cleanedTitle)
cacheKey = newRelativePath
syncService.scheduleRenameFolder(at: relativePath, name: cleanedTitle)
}

return cacheKey
}

/// Update the item's author if necessary
func updateAuthor(_ newAuthor: String, relativePath: String) {
let cleanedAuthor = newAuthor.trimmingCharacters(in: .whitespacesAndNewlines)

guard !cleanedAuthor.isEmpty else { return }

let storedDetails = libraryService.getItemProperty(
#keyPath(LibraryItem.details),
relativePath: relativePath
) as? String

guard storedDetails != cleanedAuthor else { return }

libraryService.updateDetails(at: relativePath, details: cleanedAuthor)
}

private func sendEvent(_ event: ItemDetailsViewModel.Events) {
eventsPublisher.send(event)
}
Expand Down
16 changes: 11 additions & 5 deletions BookPlayer/Library/ItemDetails Screen/Views/ItemDetailsForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ struct ItemDetailsForm: View {

var body: some View {
Form {
Section(header: Text("details_title".localized)
Section(header: Text("section_item_title".localized)
.foregroundColor(themeViewModel.secondaryColor)
) {
ClearableTextField("item_title_placeholder".localized, text: $viewModel.title)
if viewModel.showAuthor {
ClearableTextField("item_author_placeholder".localized, text: $viewModel.author)
}
ClearableTextField(viewModel.titlePlaceholder, text: $viewModel.title)
}
.listRowBackground(themeViewModel.secondarySystemBackgroundColor)

if viewModel.showAuthor {
Section(header: Text("section_item_author".localized)
.foregroundColor(themeViewModel.secondaryColor)
) {
ClearableTextField(viewModel.authorPlaceholder, text: $viewModel.author)
}
.listRowBackground(themeViewModel.secondarySystemBackgroundColor)
}

ItemDetailsArtworkSectionView(image: $viewModel.selectedImage) {
showingArtworkOptions = true
}
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/ar.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "يبحث";
"books_title" = "كتب";
"folders_title" = "المجلدات";
"item_title_placeholder" = "عنوان";
"item_author_placeholder" = "مؤلف";
"section_item_title" = "عنوان";
"section_item_author" = "مؤلف";
"artwork_options_title" = "خيارات العمل الفني";
"artwork_photolibrary_title" = "اختر من مكتبة الصور";
"artwork_clipboard_title" = "لصق من الحافظة";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/cs.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Vyhledávání";
"books_title" = "knihy";
"folders_title" = "Složky";
"item_title_placeholder" = "Titul";
"item_author_placeholder" = "Autor";
"section_item_title" = "Titul";
"section_item_author" = "Autor";
"artwork_options_title" = "Možnosti uměleckého díla";
"artwork_photolibrary_title" = "Vyberte si z knihovny fotografií";
"artwork_clipboard_title" = "Vložit ze schránky";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/da.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Søg";
"books_title" = "Bøger";
"folders_title" = "Mapper";
"item_title_placeholder" = "Titel";
"item_author_placeholder" = "Forfatter";
"section_item_title" = "Titel";
"section_item_author" = "Forfatter";
"artwork_options_title" = "Muligheder for kunstværker";
"artwork_photolibrary_title" = "Vælg fra Fotobibliotek";
"artwork_clipboard_title" = "Indsæt fra udklipsholder";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Suchen";
"books_title" = "Bücher";
"folders_title" = "Ordner";
"item_title_placeholder" = "Titel";
"item_author_placeholder" = "Autor";
"section_item_title" = "Titel";
"section_item_author" = "Autor";
"artwork_options_title" = "Artwork-Optionen";
"artwork_photolibrary_title" = "Wählen Sie aus der Fotobibliothek";
"artwork_clipboard_title" = "Aus Zwischenablage einfügen";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Search";
"books_title" = "Books";
"folders_title" = "Folders";
"item_title_placeholder" = "Title";
"item_author_placeholder" = "Author";
"section_item_title" = "Title";
"section_item_author" = "Author";
"artwork_options_title" = "Artwork options";
"artwork_photolibrary_title" = "Choose from Photo Library";
"artwork_clipboard_title" = "Paste from clipboard";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/es.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Buscar";
"books_title" = "Libros";
"folders_title" = "Carpetas";
"item_title_placeholder" = "Título";
"item_author_placeholder" = "Autor";
"section_item_title" = "Título";
"section_item_author" = "Autor";
"artwork_options_title" = "Opciones de ilustraciones";
"artwork_photolibrary_title" = "Elija de la biblioteca de fotos";
"artwork_clipboard_title" = "Pegar desde el portapapeles";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/fi.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Hae";
"books_title" = "Kirjat";
"folders_title" = "Kansiot";
"item_title_placeholder" = "Otsikko";
"item_author_placeholder" = "Tekijä";
"section_item_title" = "Otsikko";
"section_item_author" = "Tekijä";
"artwork_options_title" = "Taideteosvaihtoehdot";
"artwork_photolibrary_title" = "Valitse valokuvakirjastosta";
"artwork_clipboard_title" = "Liitä leikepöydältä";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/fr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Recherche";
"books_title" = "Livres";
"folders_title" = "Dossiers";
"item_title_placeholder" = "Titre";
"item_author_placeholder" = "Auteur";
"section_item_title" = "Titre";
"section_item_author" = "Auteur";
"artwork_options_title" = "Options d'illustration";
"artwork_photolibrary_title" = "Choisissez parmi la photothèque";
"artwork_clipboard_title" = "Coller depuis le presse-papiers";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/hu.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Keresés";
"books_title" = "Könyvek";
"folders_title" = "Mappák";
"item_title_placeholder" = "Cím";
"item_author_placeholder" = "Szerző";
"section_item_title" = "Cím";
"section_item_author" = "Szerző";
"artwork_options_title" = "Műalkotási lehetőségek";
"artwork_photolibrary_title" = "Válasszon a Photo Library közül";
"artwork_clipboard_title" = "Beillesztés a vágólapról";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/it.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Ricerca";
"books_title" = "Libri";
"folders_title" = "Cartelle";
"item_title_placeholder" = "Titolo";
"item_author_placeholder" = "Autore";
"section_item_title" = "Titolo";
"section_item_author" = "Autore";
"artwork_options_title" = "Opzioni di opere d'arte";
"artwork_photolibrary_title" = "Scegli dalla Libreria foto";
"artwork_clipboard_title" = "Incolla dagli appunti";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/nl.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Zoekopdracht";
"books_title" = "Boeken";
"folders_title" = "mappen";
"item_title_placeholder" = "Titel";
"item_author_placeholder" = "Auteur";
"section_item_title" = "Titel";
"section_item_author" = "Auteur";
"artwork_options_title" = "Kunstwerk opties";
"artwork_photolibrary_title" = "Kies uit de fotobibliotheek";
"artwork_clipboard_title" = "Plakken vanaf het klembord";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/pl.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Szukaj";
"books_title" = "Książki";
"folders_title" = "Lornetka składana";
"item_title_placeholder" = "Tytuł";
"item_author_placeholder" = "Autor";
"section_item_title" = "Tytuł";
"section_item_author" = "Autor";
"artwork_options_title" = "Opcje grafiki";
"artwork_photolibrary_title" = "Wybierz z biblioteki zdjęć";
"artwork_clipboard_title" = "Wklej ze schowka";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/pt-BR.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Procurar";
"books_title" = "livros";
"folders_title" = "Pastas";
"item_title_placeholder" = "Título";
"item_author_placeholder" = "Autor";
"section_item_title" = "Título";
"section_item_author" = "Autor";
"artwork_options_title" = "Opções de arte";
"artwork_photolibrary_title" = "Escolha na biblioteca de fotos";
"artwork_clipboard_title" = "Colar da área de transferência";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/ro.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Căutare";
"books_title" = "Cărți";
"folders_title" = "Foldere";
"item_title_placeholder" = "Titlu";
"item_author_placeholder" = "Autor";
"section_item_title" = "Titlu";
"section_item_author" = "Autor";
"artwork_options_title" = "Opțiuni de artă";
"artwork_photolibrary_title" = "Alegeți din Biblioteca foto";
"artwork_clipboard_title" = "Lipiți din clipboard";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/ru.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Поиск";
"books_title" = "Книги";
"folders_title" = "Папки";
"item_title_placeholder" = "Заголовок";
"item_author_placeholder" = "Автор";
"section_item_title" = "Заголовок";
"section_item_author" = "Автор";
"artwork_options_title" = "Варианты оформления";
"artwork_photolibrary_title" = "Выберите из библиотеки фотографий";
"artwork_clipboard_title" = "Вставить из буфера обмена";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/sk-SK.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Vyhľadávanie";
"books_title" = "knihy";
"folders_title" = "Priečinky";
"item_title_placeholder" = "Názov";
"item_author_placeholder" = "Autor";
"section_item_title" = "Názov";
"section_item_author" = "Autor";
"artwork_options_title" = "Možnosti umeleckých diel";
"artwork_photolibrary_title" = "Vyberte si z knižnice fotografií";
"artwork_clipboard_title" = "Prilepiť zo schránky";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/sv.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Sök";
"books_title" = "Böcker";
"folders_title" = "Mappar";
"item_title_placeholder" = "Titel";
"item_author_placeholder" = "Författare";
"section_item_title" = "Titel";
"section_item_author" = "Författare";
"artwork_options_title" = "Alternativ för konstverk";
"artwork_photolibrary_title" = "Välj från Fotobibliotek";
"artwork_clipboard_title" = "Klistra in från urklipp";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/tr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Aramak";
"books_title" = "Kitabın";
"folders_title" = "Klasörler";
"item_title_placeholder" = "Başlık";
"item_author_placeholder" = "Yazar";
"section_item_title" = "Başlık";
"section_item_author" = "Yazar";
"artwork_options_title" = "Sanat eseri seçenekleri";
"artwork_photolibrary_title" = "Fotoğraf Kitaplığından Seçin";
"artwork_clipboard_title" = "Panodan yapıştır";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/uk.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "Пошук";
"books_title" = "Книги";
"folders_title" = "Папки";
"item_title_placeholder" = "Назва";
"item_author_placeholder" = "Автор";
"section_item_title" = "Назва";
"section_item_author" = "Автор";
"artwork_options_title" = "Варіанти художніх робіт";
"artwork_photolibrary_title" = "Виберіть із бібліотеки фотографій";
"artwork_clipboard_title" = "Вставити з буфера обміну";
Expand Down
4 changes: 2 additions & 2 deletions BookPlayer/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"search_title" = "搜索";
"books_title" = "图书";
"folders_title" = "文件夹";
"item_title_placeholder" = "标题";
"item_author_placeholder" = "作者";
"section_item_title" = "标题";
"section_item_author" = "作者";
"artwork_options_title" = "艺术品选项";
"artwork_photolibrary_title" = "从照片库中选择";
"artwork_clipboard_title" = "从剪贴板粘贴";
Expand Down

0 comments on commit 45d897c

Please sign in to comment.