Add comprehensive offline bookmark support with sync capabilities: - Implement offline bookmark storage using Core Data with App Group sharing - Add Share Extension support for saving bookmarks when server unavailable - Create LocalBookmarksSyncView for managing offline bookmark queue - Add OfflineSyncManager for automatic and manual sync operations - Implement ServerConnectivity monitoring and status handling - Add badge notifications on More tab for pending offline bookmarks - Fix tag pagination in Share Extension with unique IDs for proper rendering - Update PhoneTabView with event-based badge count updates - Add App Group entitlements for data sharing between main app and extension The offline system provides seamless bookmark saving when disconnected, with automatic sync when connection is restored and manual sync options.
61 lines
1.9 KiB
Swift
61 lines
1.9 KiB
Swift
import Foundation
|
|
import CoreData
|
|
|
|
class OfflineBookmarkManager {
|
|
static let shared = OfflineBookmarkManager()
|
|
|
|
private init() {}
|
|
|
|
// MARK: - Core Data Stack for Share Extension
|
|
|
|
var context: NSManagedObjectContext {
|
|
return CoreDataManager.shared.context
|
|
}
|
|
|
|
// MARK: - Offline Storage Methods
|
|
|
|
func saveOfflineBookmark(url: String, title: String = "", tags: [String] = []) -> Bool {
|
|
let tagsString = tags.joined(separator: ",")
|
|
|
|
// Check if URL already exists offline
|
|
let fetchRequest: NSFetchRequest<ArticleURLEntity> = ArticleURLEntity.fetchRequest()
|
|
fetchRequest.predicate = NSPredicate(format: "url == %@", url)
|
|
|
|
do {
|
|
let existingEntities = try context.fetch(fetchRequest)
|
|
if let existingEntity = existingEntities.first {
|
|
// Update existing entry
|
|
existingEntity.tags = tagsString
|
|
existingEntity.title = title
|
|
} else {
|
|
// Create new entry
|
|
let entity = ArticleURLEntity(context: context)
|
|
entity.id = UUID()
|
|
entity.url = url
|
|
entity.title = title
|
|
entity.tags = tagsString
|
|
}
|
|
|
|
try context.save()
|
|
print("Bookmark saved offline: \(url)")
|
|
return true
|
|
} catch {
|
|
print("Failed to save offline bookmark: \(error)")
|
|
return false
|
|
}
|
|
}
|
|
|
|
func getTags() -> [String] {
|
|
let fetchRequest: NSFetchRequest<TagEntity> = TagEntity.fetchRequest()
|
|
|
|
do {
|
|
let tagEntities = try context.fetch(fetchRequest)
|
|
return tagEntities.compactMap { $0.name }.sorted()
|
|
} catch {
|
|
print("Failed to fetch tags: \(error)")
|
|
return []
|
|
}
|
|
}
|
|
|
|
}
|