From c188d5f9b9cafa4c4fd11eac1cd07920dfdbf5ce Mon Sep 17 00:00:00 2001 From: Ashutosh Gangwar Date: Mon, 2 Oct 2023 20:15:04 +0530 Subject: [PATCH] minor clean-up in api-client module --- .../com/trynoice/api/client/NoiceApiClient.kt | 16 ++++++---------- .../trynoice/api/client/NoiceApiClientTest.kt | 5 +++-- .../noice/fragment/AccountFragment.kt | 2 ++ .../fragment/ViewSubscriptionPlansFragment.kt | 4 +++- .../noice/repository/AccountRepository.kt | 5 ++--- .../service/SubscriptionStatusPollService.kt | 4 ++-- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/api-client/src/main/java/com/trynoice/api/client/NoiceApiClient.kt b/api-client/src/main/java/com/trynoice/api/client/NoiceApiClient.kt index ed4c0e4f7..a6f70cc71 100644 --- a/api-client/src/main/java/com/trynoice/api/client/NoiceApiClient.kt +++ b/api-client/src/main/java/com/trynoice/api/client/NoiceApiClient.kt @@ -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 @@ -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 /** @@ -71,7 +71,7 @@ class NoiceApiClient( .addInterceptor(AcceptLanguageHeaderInjector()) .addInterceptor( AccessTokenInjector { refresh -> - if (refresh) refreshCredentialsSync() + if (refresh) runBlocking { refreshCredentials() } credentialRepository.getAccessToken() } ) @@ -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 @@ -135,7 +135,7 @@ class NoiceApiClient( fun isSignedIn(): Boolean = signedInState.value - fun getSignedInState(): StateFlow = signedInState + fun isSignedInFlow(): Flow = signedInState /** * Signs out the currently logged in user. @@ -188,8 +188,4 @@ class NoiceApiClient( } } } - - private fun refreshCredentialsSync() { - runBlocking { refreshCredentials() } - } } diff --git a/api-client/src/test/java/com/trynoice/api/client/NoiceApiClientTest.kt b/api-client/src/test/java/com/trynoice/api/client/NoiceApiClientTest.kt index 14bb23969..562efc6d6 100644 --- a/api-client/src/test/java/com/trynoice/api/client/NoiceApiClientTest.kt +++ b/api-client/src/test/java/com/trynoice/api/client/NoiceApiClientTest.kt @@ -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 @@ -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()) } @@ -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()) } diff --git a/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/AccountFragment.kt b/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/AccountFragment.kt index 28c839b28..2e2666c13 100644 --- a/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/AccountFragment.kt +++ b/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/AccountFragment.kt @@ -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) diff --git a/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/ViewSubscriptionPlansFragment.kt b/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/ViewSubscriptionPlansFragment.kt index ce15e0716..4e85e0395 100644 --- a/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/ViewSubscriptionPlansFragment.kt +++ b/app/src/main/java/com/github/ashutoshgngwr/noice/fragment/ViewSubscriptionPlansFragment.kt @@ -150,12 +150,14 @@ class ViewSubscriptionPlansViewModel @Inject constructor( savedStateHandle: SavedStateHandle, ) : ViewModel() { - val isSignedIn = accountRepository.isSignedIn() val activeSubscription: Subscription? private val plansResource = MutableSharedFlow>>() private val premiumCountResource = MutableSharedFlow>() + val isSignedIn = accountRepository.isSignedIn() + .stateIn(viewModelScope, SharingStarted.Eagerly, true) + val isLoading: StateFlow = combine(plansResource, premiumCountResource) { p, c -> p is Resource.Loading || c is Resource.Loading }.stateIn(viewModelScope, SharingStarted.Eagerly, true) diff --git a/app/src/main/java/com/github/ashutoshgngwr/noice/repository/AccountRepository.kt b/app/src/main/java/com/github/ashutoshgngwr/noice/repository/AccountRepository.kt index 9241b6219..0ce48bec2 100644 --- a/app/src/main/java/com/github/ashutoshgngwr/noice/repository/AccountRepository.kt +++ b/app/src/main/java/com/github/ashutoshgngwr/noice/repository/AccountRepository.kt @@ -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 @@ -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 = apiClient.getSignedInState() + fun isSignedIn(): Flow = apiClient.isSignedInFlow() /** * Returns a [Flow] that emits the profile [Resource] of the authenticated user. diff --git a/app/src/main/java/com/github/ashutoshgngwr/noice/service/SubscriptionStatusPollService.kt b/app/src/main/java/com/github/ashutoshgngwr/noice/service/SubscriptionStatusPollService.kt index 105ecd9ba..5df19104e 100644 --- a/app/src/main/java/com/github/ashutoshgngwr/noice/service/SubscriptionStatusPollService.kt +++ b/app/src/main/java/com/github/ashutoshgngwr/noice/service/SubscriptionStatusPollService.kt @@ -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 @@ -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() }