Skip to content

Commit

Permalink
Fix playing book getting reloaded after last played sync
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Oct 31, 2023
1 parent 58f58f1 commit 8308996
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
9 changes: 2 additions & 7 deletions BookPlayer/Coordinators/LibraryListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,10 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat
override func syncList() {
Task { @MainActor in
do {
let lastPlayed: SyncableItem?

if UserDefaults.standard.bool(forKey: Constants.UserDefaults.hasScheduledLibraryContents) == true {
lastPlayed = try await syncService.syncListContents(at: nil)
try await syncService.syncListContents(at: nil)
} else {
lastPlayed = try await syncService.syncLibraryContents()
try await syncService.syncLibraryContents()

UserDefaults.standard.set(
true,
Expand All @@ -228,9 +226,6 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat
}

reloadItemsWithPadding()
if let lastPlayed {
reloadLastBook(relativePath: lastPlayed.relativePath)
}
} catch BPSyncError.reloadLastBook(let relativePath) {
reloadItemsWithPadding()
reloadLastBook(relativePath: relativePath)
Expand Down
22 changes: 6 additions & 16 deletions BookPlayer/Generated/AutoMockable.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1380,20 +1380,15 @@ class SyncServiceProtocolMock: SyncServiceProtocol {
}
var syncListContentsAtReceivedRelativePath: String?
var syncListContentsAtReceivedInvocations: [String?] = []
var syncListContentsAtReturnValue: SyncableItem?
var syncListContentsAtClosure: ((String?) async throws -> SyncableItem?)?
func syncListContents(at relativePath: String?) async throws -> SyncableItem? {
var syncListContentsAtClosure: ((String?) async throws -> Void)?
func syncListContents(at relativePath: String?) async throws {
if let error = syncListContentsAtThrowableError {
throw error
}
syncListContentsAtCallsCount += 1
syncListContentsAtReceivedRelativePath = relativePath
syncListContentsAtReceivedInvocations.append(relativePath)
if let syncListContentsAtClosure = syncListContentsAtClosure {
return try await syncListContentsAtClosure(relativePath)
} else {
return syncListContentsAtReturnValue
}
try await syncListContentsAtClosure?(relativePath)
}
//MARK: - syncLibraryContents

Expand All @@ -1402,18 +1397,13 @@ class SyncServiceProtocolMock: SyncServiceProtocol {
var syncLibraryContentsCalled: Bool {
return syncLibraryContentsCallsCount > 0
}
var syncLibraryContentsReturnValue: SyncableItem?
var syncLibraryContentsClosure: (() async throws -> SyncableItem?)?
func syncLibraryContents() async throws -> SyncableItem? {
var syncLibraryContentsClosure: (() async throws -> Void)?
func syncLibraryContents() async throws {
if let error = syncLibraryContentsThrowableError {
throw error
}
syncLibraryContentsCallsCount += 1
if let syncLibraryContentsClosure = syncLibraryContentsClosure {
return try await syncLibraryContentsClosure()
} else {
return syncLibraryContentsReturnValue
}
try await syncLibraryContentsClosure?()
}
//MARK: - syncBookmarksList

Expand Down
23 changes: 8 additions & 15 deletions Shared/Services/Sync/SyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public protocol SyncServiceProtocol {
var downloadProgressPublisher: PassthroughSubject<(String, String, String?, Double), Never> { get }

/// Fetch the contents at the relativePath and override local contents with the remote repsonse
func syncListContents(at relativePath: String?) async throws -> SyncableItem?
func syncListContents(at relativePath: String?) async throws

/// Fetch the synced identifiers and upload new local items
/// Note: Should only be called once when the user logs in
func syncLibraryContents() async throws -> SyncableItem?
func syncLibraryContents() async throws

func syncBookmarksList(relativePath: String) async throws -> [SimpleBookmark]?

Expand Down Expand Up @@ -152,7 +152,7 @@ public final class SyncService: SyncServiceProtocol, BPLogger {

public func syncListContents(
at relativePath: String?
) async throws -> SyncableItem? {
) async throws {
guard isActive else {
throw BookPlayerError.networkError("Sync is not enabled")
}
Expand Down Expand Up @@ -180,11 +180,9 @@ public final class SyncService: SyncServiceProtocol, BPLogger {
let response = try await fetchContents(at: relativePath)

try await processContentsResponse(response, parentFolder: relativePath, canDelete: true)

return response.lastItemPlayed
}

public func syncLibraryContents() async throws -> SyncableItem? {
public func syncLibraryContents() async throws {
guard
isActive,
UserDefaults.standard.bool(forKey: Constants.UserDefaults.hasQueuedJobs) == false
Expand All @@ -205,8 +203,6 @@ public final class SyncService: SyncServiceProtocol, BPLogger {
let response = try await fetchContents(at: nil)

try await processContentsResponse(response, parentFolder: nil, canDelete: false)

return response.lastItemPlayed
}

func processContentsResponse(
Expand Down Expand Up @@ -250,14 +246,11 @@ public final class SyncService: SyncServiceProtocol, BPLogger {
}

/// Only update the time if the remote last played timestamp is greater than the local timestamp
guard
let remoteLastPlayDateTimestamp = item.lastPlayDateTimestamp,
remoteLastPlayDateTimestamp > localLastPlayDateTimestamp
else {
return
if let remoteLastPlayDateTimestamp = item.lastPlayDateTimestamp,
remoteLastPlayDateTimestamp > localLastPlayDateTimestamp {
await libraryService.updateInfo(for: item)
throw BPSyncError.reloadLastBook(item.relativePath)
}

await libraryService.updateInfo(for: item)
}

public func syncBookmarksList(relativePath: String) async throws -> [SimpleBookmark]? {
Expand Down

0 comments on commit 8308996

Please sign in to comment.