This commit introduces a comprehensive refactoring of the tag management system, replacing the previous API-based approach with a Core Data-first strategy for improved performance and offline support. Major Changes: Tag Management Architecture: - Add CoreDataTagManagementView using @FetchRequest for reactive updates - Implement cache-first sync strategy in LabelsRepository - Create SyncTagsUseCase following Clean Architecture principles - Add TagSortOrder enum for configurable tag sorting (by count/alphabetically) - Mark LegacyTagManagementView as deprecated Share Extension Improvements: - Replace API-based tag loading with Core Data queries - Display top 150 tags sorted by usage count - Remove unnecessary label fetching logic - Add "Most used tags" localized title - Improve offline bookmark tag management Main App Enhancements: - Add tag sync triggers in AddBookmarkView and BookmarkLabelsView - Implement user-configurable tag sorting in settings - Add sort order indicator labels with localization - Automatic UI updates via SwiftUI @FetchRequest reactivity Settings & Configuration: - Add TagSortOrder setting with persistence - Refactor Settings model structure - Add FontFamily and FontSize domain models - Improve settings repository with tag sort order support Use Case Layer: - Add SyncTagsUseCase for background tag synchronization - Update UseCaseFactory with tag sync support - Add mock implementations for testing Localization: - Add German and English translations for: - "Most used tags" - "Sorted by usage count" - "Sorted alphabetically" Technical Improvements: - Batch tag updates with conflict detection - Background sync with silent failure handling - Reduced server load through local caching - Better separation of concerns following Clean Architecture
169 lines
8.0 KiB
Swift
169 lines
8.0 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
import UniformTypeIdentifiers
|
|
import CoreData
|
|
|
|
class ShareBookmarkViewModel: ObservableObject {
|
|
@Published var url: String?
|
|
@Published var title: String = ""
|
|
@Published var selectedLabels: Set<String> = []
|
|
@Published var statusMessage: (text: String, isError: Bool, emoji: String)? = nil
|
|
@Published var isSaving: Bool = false
|
|
@Published var searchText: String = ""
|
|
@Published var isServerReachable: Bool = true
|
|
let tagSortOrder: TagSortOrder = .byCount // Share Extension always uses byCount
|
|
let extensionContext: NSExtensionContext?
|
|
|
|
private let logger = Logger.viewModel
|
|
private let serverCheck = ShareExtensionServerCheck.shared
|
|
|
|
init(extensionContext: NSExtensionContext?) {
|
|
self.extensionContext = extensionContext
|
|
logger.info("ShareBookmarkViewModel initialized with extension context: \(extensionContext != nil)")
|
|
extractSharedContent()
|
|
}
|
|
|
|
private func extractSharedContent() {
|
|
logger.debug("Starting to extract shared content")
|
|
guard let extensionContext = extensionContext else {
|
|
logger.warning("No extension context available for content extraction")
|
|
return
|
|
}
|
|
|
|
var extractedUrl: String?
|
|
var extractedTitle: String?
|
|
|
|
for item in extensionContext.inputItems {
|
|
guard let inputItem = item as? NSExtensionItem else { continue }
|
|
|
|
// Use the inputItem's attributedTitle or attributedContentText as potential title
|
|
if let attributedTitle = inputItem.attributedTitle?.string, !attributedTitle.isEmpty {
|
|
extractedTitle = attributedTitle
|
|
logger.info("Extracted title from input item: \(attributedTitle)")
|
|
} else if let attributedContent = inputItem.attributedContentText?.string, !attributedContent.isEmpty {
|
|
extractedTitle = attributedContent
|
|
logger.info("Extracted title from content text: \(attributedContent)")
|
|
}
|
|
|
|
for attachment in inputItem.attachments ?? [] {
|
|
if attachment.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
|
|
attachment.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { [weak self] (url, error) in
|
|
DispatchQueue.main.async {
|
|
if let url = url as? URL {
|
|
self?.url = url.absoluteString
|
|
self?.logger.info("Extracted URL from shared content: \(url.absoluteString)")
|
|
|
|
// Set title if we extracted one and current title is empty
|
|
if let title = extractedTitle, self?.title.isEmpty == true {
|
|
self?.title = title
|
|
self?.logger.info("Set title from shared content: \(title)")
|
|
}
|
|
} else if let error = error {
|
|
self?.logger.error("Failed to extract URL: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if attachment.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {
|
|
attachment.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil) { [weak self] (text, error) in
|
|
DispatchQueue.main.async {
|
|
if let text = text as? String {
|
|
// Only treat as URL if it's a valid URL and we don't have one yet
|
|
if self?.url == nil, let url = URL(string: text), url.scheme != nil {
|
|
self?.url = url.absoluteString
|
|
self?.logger.info("Extracted URL from shared text: \(url.absoluteString)")
|
|
} else {
|
|
// If not a valid URL or we already have a URL, treat as potential title
|
|
if self?.title.isEmpty == true {
|
|
self?.title = text
|
|
self?.logger.info("Set title from shared text: \(text)")
|
|
}
|
|
}
|
|
} else if let error = error {
|
|
self?.logger.error("Failed to extract text: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func save() {
|
|
logger.info("Starting to save bookmark with title: '\(title)', URL: '\(url ?? "nil")', labels: \(selectedLabels.count)")
|
|
guard let url = url, !url.isEmpty else {
|
|
logger.warning("Save attempted without valid URL")
|
|
statusMessage = ("No URL found.", true, "❌")
|
|
return
|
|
}
|
|
isSaving = true
|
|
logger.debug("Set saving state to true")
|
|
|
|
// Check server connectivity
|
|
Task {
|
|
let serverReachable = await serverCheck.checkServerReachability()
|
|
logger.debug("Server connectivity for save: \(serverReachable)")
|
|
if serverReachable {
|
|
// Online - try to save via API
|
|
logger.info("Attempting to save bookmark via API")
|
|
await SimpleAPI.addBookmark(title: title, url: url, labels: Array(selectedLabels)) { [weak self] message, error in
|
|
self?.logger.info("API save completed - Success: \(!error), Message: \(message)")
|
|
self?.statusMessage = (message, error, error ? "❌" : "✅")
|
|
self?.isSaving = false
|
|
if !error {
|
|
self?.logger.debug("Bookmark saved successfully, completing extension request")
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
self?.completeExtensionRequest()
|
|
}
|
|
} else {
|
|
self?.logger.error("Failed to save bookmark via API: \(message)")
|
|
}
|
|
}
|
|
} else {
|
|
// Server not reachable - save locally
|
|
logger.info("Server not reachable, attempting local save")
|
|
let success = OfflineBookmarkManager.shared.saveOfflineBookmark(
|
|
url: url,
|
|
title: title,
|
|
tags: Array(selectedLabels)
|
|
)
|
|
logger.info("Local save result: \(success)")
|
|
|
|
await MainActor.run {
|
|
self.isSaving = false
|
|
if success {
|
|
self.logger.info("Bookmark saved locally successfully")
|
|
self.statusMessage = ("Server not reachable. Saved locally and will sync later.", false, "🏠")
|
|
} else {
|
|
self.logger.error("Failed to save bookmark locally")
|
|
self.statusMessage = ("Failed to save locally.", true, "❌")
|
|
}
|
|
}
|
|
|
|
if success {
|
|
try? await Task.sleep(nanoseconds: 2_000_000_000)
|
|
await MainActor.run {
|
|
self.completeExtensionRequest()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func completeExtensionRequest() {
|
|
logger.debug("Completing extension request")
|
|
guard let context = extensionContext else {
|
|
logger.warning("Extension context not available for completion")
|
|
return
|
|
}
|
|
|
|
context.completeRequest(returningItems: []) { [weak self] error in
|
|
if error {
|
|
self?.logger.error("Extension completion failed: \(error)")
|
|
} else {
|
|
self?.logger.info("Extension request completed successfully")
|
|
}
|
|
}
|
|
}
|
|
}
|