Skip to content

Commit

Permalink
Add rename-folder network call
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Jun 2, 2023
1 parent 9d7cf1d commit 9270ee9
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 6 deletions.
14 changes: 11 additions & 3 deletions BookPlayer/Library/ItemDetails Screen/ItemDetailsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ class ItemDetailsViewModel: BaseViewModel<ItemDetailsCoordinator> {
var cacheKey = item.relativePath

if item.title != formViewModel.title {
if item.type == .book {
switch item.type {
case .book:
libraryService.renameBook(at: item.relativePath, with: formViewModel.title)
} else if let updatedCacheKey = try? libraryService.renameFolder(at: item.relativePath, with: formViewModel.title) {
cacheKey = updatedCacheKey
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
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions BookPlayer/Profile/Profile Screen/QueuedSyncTasksView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct QueuedSyncTasksView<Model: QueuedSyncTasksViewModelProtocol>: View {
return "arrow.2.circlepath"
case .move:
return "arrow.forward"
case .renameFolder:
return "square.and.pencil"
case .delete:
return "xmark.bin.fill"
case .shallowDelete:
Expand Down
2 changes: 1 addition & 1 deletion BookPlayerTests/Services/LibraryServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ class LibraryServiceTests: XCTestCase {
duration: 100
)

_ = try self.sut.renameBook(at: book.relativePath, with: "rename-test")
sut.renameBook(at: book.relativePath, with: "rename-test")
XCTAssert(book.title == "rename-test")
}

Expand Down
2 changes: 0 additions & 2 deletions Shared/Services/LibraryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,6 @@ extension LibraryService {
items.forEach({ rebuildRelativePaths(for: $0, parentFolder: folder.relativePath) })
}

// TODO: Sync folder rename

self.dataManager.saveContext()

return newRelativePath
Expand Down
10 changes: 10 additions & 0 deletions Shared/Services/Sync/LibraryAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum LibraryAPI {
case upload(params: [String: Any])
case update(params: [String: Any])
case move(origin: String, destination: String)
case renameFolder(path: String, name: String)
case remoteFileURL(path: String)
case remoteContentsURL(path: String)
case delete(path: String)
Expand All @@ -33,6 +34,8 @@ extension LibraryAPI: Endpoint {
return "/v1/library"
case .move:
return "/v1/library/move"
case .renameFolder:
return "/v1/library/rename"
case .remoteFileURL:
return "/v1/library"
case .remoteContentsURL:
Expand Down Expand Up @@ -60,6 +63,8 @@ extension LibraryAPI: Endpoint {
return .post
case .move:
return .post
case .renameFolder:
return .post
case .remoteFileURL:
return .get
case .remoteContentsURL:
Expand Down Expand Up @@ -93,6 +98,11 @@ extension LibraryAPI: Endpoint {
"origin": origin,
"destination": destination
]
case .renameFolder(let path, let name):
return [
"relativePath": path,
"name": name
]
case .remoteFileURL(let path):
return [
"relativePath": path,
Expand Down
9 changes: 9 additions & 0 deletions Shared/Services/Sync/LibraryItemSyncJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum JobType: String {
case upload
case update
case move
case renameFolder
case delete
case shallowDelete
case setBookmark
Expand Down Expand Up @@ -49,6 +50,7 @@ class LibraryItemSyncJob: Job, BPLogger {
self.parameters = parameters
}

// swiftlint:disable:next function_body_length
func onRun(callback: SwiftQueue.JobResult) {
Task { [weak self, callback] in
guard let self = self else {
Expand Down Expand Up @@ -79,6 +81,13 @@ class LibraryItemSyncJob: Job, BPLogger {
}
let _: Empty = try await self.provider.request(.move(origin: origin, destination: destination))
callback.done(.success)
case .renameFolder:
guard let name = parameters["name"] as? String else {
throw BookPlayerError.runtimeError("Missing parameters for renaming")
}

let _: Empty = try await provider.request(.renameFolder(path: self.relativePath, name: name))
callback.done(.success)
case .delete:
let _: Empty = try await provider.request(.delete(path: self.relativePath))
callback.done(.success)
Expand Down
18 changes: 18 additions & 0 deletions Shared/Services/Sync/SyncJobScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public protocol JobSchedulerProtocol {
)
/// Delete a bookmark
func scheduleDeleteBookmarkJob(with relativePath: String, time: Double)
/// Rename a folder
func scheduleRenameFolderJob(with relativePath: String, name: String)
/// Get all queued jobs
func getAllQueuedJobs() -> [QueuedJobInfo]
/// Cancel all stored and ongoing jobs
Expand Down Expand Up @@ -216,6 +218,22 @@ public class SyncJobScheduler: JobSchedulerProtocol, BPLogger {
.schedule(manager: libraryQueueManager)
}

public func scheduleRenameFolderJob(with relativePath: String, name: String) {
let params: [String: Any] = [
"relativePath": relativePath,
"name": name,
"jobType": JobType.renameFolder.rawValue
]

JobBuilder(type: LibraryItemSyncJob.type)
.singleInstance(forId: "\(JobType.renameFolder.identifier)/\(relativePath)")
.persist()
.retry(limit: .unlimited)
.internet(atLeast: .cellular)
.with(params: params)
.schedule(manager: libraryQueueManager)
}

public func getAllQueuedJobs() -> [QueuedJobInfo] {
return pendingOperations.compactMap({ job -> QueuedJobInfo? in
guard
Expand Down
8 changes: 8 additions & 0 deletions Shared/Services/Sync/SyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public protocol SyncServiceProtocol {

func scheduleMove(items: [String], to parentFolder: String?)

func scheduleRenameFolder(at relativePath: String, name: String)

func scheduleSetBookmark(
relativePath: String,
time: Double,
Expand Down Expand Up @@ -440,4 +442,10 @@ extension SyncService {
time: bookmark.time
)
}

public func scheduleRenameFolder(at relativePath: String, name: String) {
guard isActive else { return }

jobManager.scheduleRenameFolderJob(with: relativePath, name: name)
}
}

0 comments on commit 9270ee9

Please sign in to comment.