[Android] 1.9.2
All checks were successful
OpenClimb Docker Deploy / build-and-push (push) Successful in 2m29s
All checks were successful
OpenClimb Docker Deploy / build-and-push (push) Successful in 2m29s
This commit is contained in:
@@ -9,6 +9,7 @@ class SyncService: ObservableObject {
|
||||
@Published var syncError: String?
|
||||
@Published var isConnected = false
|
||||
@Published var isTesting = false
|
||||
@Published var isOfflineMode = false
|
||||
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private var syncTask: Task<Void, Never>?
|
||||
@@ -19,8 +20,9 @@ class SyncService: ObservableObject {
|
||||
static let serverURL = "sync_server_url"
|
||||
static let authToken = "sync_auth_token"
|
||||
static let lastSyncTime = "last_sync_time"
|
||||
static let isConnected = "sync_is_connected"
|
||||
static let isConnected = "is_connected"
|
||||
static let autoSyncEnabled = "auto_sync_enabled"
|
||||
static let offlineMode = "offline_mode"
|
||||
}
|
||||
|
||||
var serverURL: String {
|
||||
@@ -46,12 +48,9 @@ class SyncService: ObservableObject {
|
||||
if let lastSync = userDefaults.object(forKey: Keys.lastSyncTime) as? Date {
|
||||
self.lastSyncTime = lastSync
|
||||
}
|
||||
self.isConnected = userDefaults.bool(forKey: Keys.isConnected)
|
||||
|
||||
// Perform image naming migration on initialization
|
||||
Task {
|
||||
await performImageNamingMigration()
|
||||
}
|
||||
isConnected = userDefaults.bool(forKey: Keys.isConnected)
|
||||
isAutoSyncEnabled = userDefaults.object(forKey: Keys.autoSyncEnabled) as? Bool ?? true
|
||||
isOfflineMode = userDefaults.bool(forKey: Keys.offlineMode)
|
||||
}
|
||||
|
||||
func downloadData() async throws -> ClimbDataBackup {
|
||||
@@ -211,6 +210,11 @@ class SyncService: ObservableObject {
|
||||
}
|
||||
|
||||
func syncWithServer(dataManager: ClimbingDataManager) async throws {
|
||||
if isOfflineMode {
|
||||
print("Sync skipped: Offline mode is enabled.")
|
||||
return
|
||||
}
|
||||
|
||||
guard isConfigured else {
|
||||
throw SyncError.notConfigured
|
||||
}
|
||||
@@ -1025,105 +1029,7 @@ class SyncService: ObservableObject {
|
||||
syncTask?.cancel()
|
||||
}
|
||||
|
||||
// MARK: - Image Naming Migration
|
||||
|
||||
private func performImageNamingMigration() async {
|
||||
let migrationKey = "image_naming_migration_completed_v2"
|
||||
guard !userDefaults.bool(forKey: migrationKey) else {
|
||||
print("Image naming migration already completed")
|
||||
return
|
||||
}
|
||||
|
||||
print("Starting image naming migration...")
|
||||
var updateCount = 0
|
||||
let imageManager = ImageManager.shared
|
||||
|
||||
// Get all problems from UserDefaults
|
||||
if let problemsData = userDefaults.data(forKey: "problems"),
|
||||
var problems = try? JSONDecoder().decode([Problem].self, from: problemsData)
|
||||
{
|
||||
|
||||
for problemIndex in 0..<problems.count {
|
||||
let problem = problems[problemIndex]
|
||||
guard !problem.imagePaths.isEmpty else { continue }
|
||||
|
||||
var updatedImagePaths: [String] = []
|
||||
var hasChanges = false
|
||||
|
||||
for (imageIndex, imagePath) in problem.imagePaths.enumerated() {
|
||||
let currentFilename = URL(fileURLWithPath: imagePath).lastPathComponent
|
||||
let consistentFilename = ImageNamingUtils.generateImageFilename(
|
||||
problemId: problem.id.uuidString, imageIndex: imageIndex)
|
||||
|
||||
if currentFilename != consistentFilename {
|
||||
let oldPath = imageManager.imagesDirectory.appendingPathComponent(
|
||||
currentFilename
|
||||
).path
|
||||
let newPath = imageManager.imagesDirectory.appendingPathComponent(
|
||||
consistentFilename
|
||||
).path
|
||||
|
||||
if FileManager.default.fileExists(atPath: oldPath) {
|
||||
do {
|
||||
try FileManager.default.moveItem(atPath: oldPath, toPath: newPath)
|
||||
updatedImagePaths.append(consistentFilename)
|
||||
hasChanges = true
|
||||
updateCount += 1
|
||||
print("Migrated image: \(currentFilename) -> \(consistentFilename)")
|
||||
} catch {
|
||||
print("Failed to migrate image \(currentFilename): \(error)")
|
||||
updatedImagePaths.append(imagePath)
|
||||
}
|
||||
} else {
|
||||
updatedImagePaths.append(imagePath)
|
||||
}
|
||||
} else {
|
||||
updatedImagePaths.append(imagePath)
|
||||
}
|
||||
}
|
||||
|
||||
if hasChanges {
|
||||
// Decode problem to dictionary, update imagePaths, re-encode
|
||||
if let problemData = try? JSONEncoder().encode(problem),
|
||||
var problemDict = try? JSONSerialization.jsonObject(with: problemData)
|
||||
as? [String: Any]
|
||||
{
|
||||
problemDict["imagePaths"] = updatedImagePaths
|
||||
problemDict["updatedAt"] = ISO8601DateFormatter().string(from: Date())
|
||||
if let updatedData = try? JSONSerialization.data(
|
||||
withJSONObject: problemDict),
|
||||
let updatedProblem = try? JSONDecoder().decode(
|
||||
Problem.self, from: updatedData)
|
||||
{
|
||||
problems[problemIndex] = updatedProblem
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if updateCount > 0 {
|
||||
if let updatedData = try? JSONEncoder().encode(problems) {
|
||||
userDefaults.set(updatedData, forKey: "problems")
|
||||
print("Updated \(updateCount) image paths in UserDefaults")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
userDefaults.set(true, forKey: migrationKey)
|
||||
print("Image naming migration completed, updated \(updateCount) images")
|
||||
|
||||
// Notify ClimbingDataManager to reload data if images were updated
|
||||
if updateCount > 0 {
|
||||
DispatchQueue.main.async {
|
||||
NotificationCenter.default.post(
|
||||
name: NSNotification.Name("ImageMigrationCompleted"),
|
||||
object: nil,
|
||||
userInfo: ["updateCount": updateCount]
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Merging
|
||||
// MARK: - Safe Merge Functions
|
||||
|
||||
private func mergeGyms(local: [Gym], server: [BackupGym], deletedItems: [DeletedItem]) -> [Gym]
|
||||
|
||||
Reference in New Issue
Block a user