Skip to content

Commit

Permalink
Change episode sync call to be Completable
Browse files Browse the repository at this point in the history
The Single<Void> return was throwing NoSuchElementExceptions in my
testing
  • Loading branch information
mchowning committed Jan 17, 2023
1 parent 0b84d15 commit 727f27e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import au.com.shiftyjelly.pocketcasts.servers.sync.login.LoginTokenRequest
import au.com.shiftyjelly.pocketcasts.servers.sync.login.LoginTokenResponse
import au.com.shiftyjelly.pocketcasts.servers.sync.register.RegisterRequest
import au.com.shiftyjelly.pocketcasts.servers.sync.register.RegisterResponse
import io.reactivex.Completable
import io.reactivex.Single
import okhttp3.RequestBody
import retrofit2.Call
Expand Down Expand Up @@ -76,7 +77,7 @@ interface SyncServer {
suspend fun historyYear(@Header("Authorization") authorization: String, @Body request: HistoryYearSyncRequest): HistoryYearResponse

@POST("/sync/update_episode")
fun episodeProgressSync(@Header("Authorization") authorization: String, @Body request: EpisodeSyncRequest): Single<Void>
fun episodeProgressSync(@Header("Authorization") authorization: String, @Body request: EpisodeSyncRequest): Completable

@GET("/subscription/status")
fun subscriptionStatus(@Header("Authorization") authorization: String): Single<SubscriptionStatusResponse>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ open class SyncServerManager @Inject constructor(
}

fun episodeSync(request: EpisodeSyncRequest): Completable {
return getCacheTokenOrLogin { token ->
return getCacheTokenOrLoginCompletable { token ->
server.episodeProgressSync(addBearer(token), request)
}.ignoreElement()
}
}

fun subscriptionStatus(): Single<SubscriptionStatusResponse> {
Expand Down Expand Up @@ -351,6 +351,12 @@ open class SyncServerManager @Inject constructor(
}
}

private fun getCacheTokenOrLoginCompletable(serverCall: (token: String) -> Completable): Completable {
return getCacheTokenOrLogin { token ->
serverCall(token).toSingleDefault(Unit)
}.ignoreElement()
}

private fun <T : Any> getCacheTokenOrLogin(serverCall: (token: String) -> Single<T>): Single<T> {
if (settings.isLoggedIn()) {
return Single.fromCallable { settings.getSyncToken() ?: throw RuntimeException("Failed to get token") }
Expand Down

0 comments on commit 727f27e

Please sign in to comment.