Skip to content

Commit

Permalink
minor clean-up in api-client module
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr committed Oct 2, 2023
1 parent 83caec2 commit c188d5f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
16 changes: 6 additions & 10 deletions api-client/src/main/java/com/trynoice/api/client/NoiceApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.trynoice.api.client.interceptors.AcceptLanguageHeaderInjector
import com.trynoice.api.client.interceptors.AccessTokenInjector
import com.trynoice.api.client.interceptors.RefreshTokenInjector
import com.trynoice.api.client.models.AuthCredentials
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand All @@ -24,7 +24,7 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.io.File
import java.util.*
import java.util.Date


/**
Expand Down Expand Up @@ -71,7 +71,7 @@ class NoiceApiClient(
.addInterceptor(AcceptLanguageHeaderInjector())
.addInterceptor(
AccessTokenInjector { refresh ->
if (refresh) refreshCredentialsSync()
if (refresh) runBlocking { refreshCredentials() }
credentialRepository.getAccessToken()
}
)
Expand Down Expand Up @@ -108,12 +108,12 @@ class NoiceApiClient(
}

/**
* Subscription management related APIs.
* Account and user management related APIs.
*/
fun accounts() = accountApi

/**
* Account and user management related APIs.
* Subscription management related APIs.
*/
fun subscriptions() = subscriptionApi

Expand All @@ -135,7 +135,7 @@ class NoiceApiClient(

fun isSignedIn(): Boolean = signedInState.value

fun getSignedInState(): StateFlow<Boolean> = signedInState
fun isSignedInFlow(): Flow<Boolean> = signedInState

/**
* Signs out the currently logged in user.
Expand Down Expand Up @@ -188,8 +188,4 @@ class NoiceApiClient(
}
}
}

private fun refreshCredentialsSync() {
runBlocking { refreshCredentials() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.trynoice.api.client.models.AuthCredentials
import com.trynoice.api.client.models.Profile
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
Expand Down Expand Up @@ -63,7 +64,7 @@ class NoiceApiClientTest {
runCatching { mockServer.takeRequest() }
.onSuccess { assertEquals(testSignInToken, it.getHeader("X-Refresh-Token")) }

assertEquals(true, apiClient.getSignedInState().value)
assertEquals(true, apiClient.isSignedInFlow().first())
assertEquals(testCredentials.refreshToken, credentialRepository.getRefreshToken())
assertEquals(testCredentials.accessToken, credentialRepository.getAccessToken())
}
Expand Down Expand Up @@ -142,7 +143,7 @@ class NoiceApiClientTest {
assertEquals(testCredentials.refreshToken, it.getHeader("X-Refresh-Token"))
}

assertEquals(false, apiClient.getSignedInState().value)
assertEquals(false, apiClient.isSignedInFlow().first())
assertNull(credentialRepository.getRefreshToken())
assertNull(credentialRepository.getAccessToken())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class AccountViewModel @Inject constructor(
) : ViewModel() {

val isSignedIn = accountRepository.isSignedIn()
.stateIn(viewModelScope, SharingStarted.Eagerly, true)

val isSubscribed = subscriptionRepository.isSubscribed()
.stateIn(viewModelScope, SharingStarted.Eagerly, true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ class ViewSubscriptionPlansViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
) : ViewModel() {

val isSignedIn = accountRepository.isSignedIn()
val activeSubscription: Subscription?

private val plansResource = MutableSharedFlow<Resource<List<SubscriptionPlan>>>()
private val premiumCountResource = MutableSharedFlow<Resource<Int>>()

val isSignedIn = accountRepository.isSignedIn()
.stateIn(viewModelScope, SharingStarted.Eagerly, true)

val isLoading: StateFlow<Boolean> = combine(plansResource, premiumCountResource) { p, c ->
p is Resource.Loading || c is Resource.Loading
}.stateIn(viewModelScope, SharingStarted.Eagerly, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.trynoice.api.client.models.SignInParams
import com.trynoice.api.client.models.SignUpParams
import com.trynoice.api.client.models.UpdateProfileParams
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import retrofit2.HttpException
import retrofit2.Response
import java.io.IOException
Expand All @@ -32,9 +31,9 @@ class AccountRepository @Inject constructor(
) {

/**
* @return a [StateFlow] that notifies changes to the current signed-in state of the api client.
* @return a [Flow] that notifies changes to the current signed-in state of the api client.
*/
fun isSignedIn(): StateFlow<Boolean> = apiClient.getSignedInState()
fun isSignedIn(): Flow<Boolean> = apiClient.isSignedInFlow()

/**
* Returns a [Flow] that emits the profile [Resource] of the authenticated user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import java.util.*
import java.util.Date
import javax.inject.Inject
import kotlin.math.min
import kotlin.time.Duration
Expand Down Expand Up @@ -56,7 +56,7 @@ class SubscriptionStatusPollService : LifecycleService() {
Log.d(LOG_TAG, "scheduling poll after $pollDelay or sooner if sign-in state changes")
// wait until timeout or signed-in state change
withTimeoutOrNull(pollDelay) {
apiClient.getSignedInState()
apiClient.isSignedInFlow()
.takeWhile { it == isSignedIn }
.collect()
}
Expand Down

0 comments on commit c188d5f

Please sign in to comment.