ReadKeep/URLShare/OfflineBookmarkManager.swift
Ilyas Hallak ef13faeff7 feat: Add offline bookmark sync functionality
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.
2025-08-16 22:32:20 +02:00

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 []
}
}
}