From f80675cd137b10867088c5af0eb919c448a5abf2 Mon Sep 17 00:00:00 2001 From: shabinder Date: Sun, 2 May 2021 01:53:41 +0530 Subject: [PATCH] Testing IOS --- .../com/shabinder/common/models/Actions.kt | 2 +- .../kotlin/com/shabinder/common/di/DI.kt | 5 +++++ .../common/di/providers/SpotifyProvider.kt | 22 ++++++------------- .../common/di/spotify/SpotifyAuth.kt | 2 ++ .../common/di/spotify/SpotifyRequests.kt | 6 +++-- .../root/integration/SpotiFlyerRootImpl.kt | 16 ++++++++++---- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/Actions.kt b/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/Actions.kt index 2d61bb47..d0e584d5 100644 --- a/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/Actions.kt +++ b/common/data-models/src/commonMain/kotlin/com/shabinder/common/models/Actions.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.Dispatchers /* * Holder to call platform actions from anywhere * */ -var methods: NativeAtomicReference = NativeAtomicReference(stubActions().freeze()) +val methods: NativeAtomicReference = NativeAtomicReference(stubActions().freeze()) /* * Interface Having All Platform Dependent Functions diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt index 0553bb3b..01ad1987 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/DI.kt @@ -23,6 +23,7 @@ import com.shabinder.common.di.providers.GaanaProvider import com.shabinder.common.di.providers.SpotifyProvider import com.shabinder.common.di.providers.YoutubeMp3 import com.shabinder.common.di.providers.YoutubeMusic +import com.shabinder.common.models.NativeAtomicReference import io.ktor.client.HttpClient import io.ktor.client.features.HttpTimeout import io.ktor.client.features.json.JsonFeature @@ -35,6 +36,7 @@ import kotlinx.serialization.json.Json import org.koin.core.context.startKoin import org.koin.dsl.KoinAppDeclaration import org.koin.dsl.module +import kotlin.native.concurrent.SharedImmutable fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) = startKoin { @@ -58,6 +60,7 @@ fun commonModule(enableNetworkLogs: Boolean) = module { single { FetchPlatformQueryResult(get(), get(), get(), get(), get(), get()) } } +@SharedImmutable val kotlinxSerializer = KotlinxSerializer( Json { isLenient = true @@ -82,5 +85,7 @@ fun createHttpClient(enableNetworkLogs: Boolean = false, serializer: KotlinxSeri } } } + /*Client Active Throughout App's Lifetime*/ +@SharedImmutable val ktorHttpClient = HttpClient {} diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt index 96a98df5..a5a053be 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/providers/SpotifyProvider.kt @@ -17,16 +17,17 @@ package com.shabinder.common.di.providers import co.touchlab.kermit.Kermit +import co.touchlab.stately.freeze import com.shabinder.common.di.Dir import com.shabinder.common.di.TokenStore import com.shabinder.common.di.finalOutputDir import com.shabinder.common.di.kotlinxSerializer +import com.shabinder.common.di.ktorHttpClient import com.shabinder.common.di.spotify.SpotifyRequests import com.shabinder.common.di.spotify.authenticateSpotify -import com.shabinder.common.models.AllPlatforms +import com.shabinder.common.models.NativeAtomicReference import com.shabinder.common.models.PlatformQueryResult import com.shabinder.common.models.TrackDetails -import com.shabinder.common.models.methods import com.shabinder.common.models.spotify.Album import com.shabinder.common.models.spotify.Image import com.shabinder.common.models.spotify.Source @@ -35,10 +36,6 @@ import io.ktor.client.HttpClient import io.ktor.client.features.defaultRequest import io.ktor.client.features.json.JsonFeature import io.ktor.client.request.header -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch -import kotlin.native.concurrent.ThreadLocal class SpotifyProvider( private val tokenStore: TokenStore, @@ -55,11 +52,10 @@ class SpotifyProvider( } }*/ - override suspend fun authenticateSpotifyClient(override: Boolean): HttpClient? { + override suspend fun authenticateSpotifyClient(override: Boolean) { val token = if (override) authenticateSpotify() else tokenStore.getToken() - return if (token == null) { + if (token == null) { logger.d { "Please Check your Network Connection" } - null } else { logger.d { "Spotify Provider Created with $token" } HttpClient { @@ -69,18 +65,14 @@ class SpotifyProvider( install(JsonFeature) { serializer = kotlinxSerializer } - }.also { httpClient = it } + }.also { httpClientRef.value = it.freeze() } } } - override lateinit var httpClient: HttpClient + override val httpClientRef = NativeAtomicReference(ktorHttpClient) suspend fun query(fullLink: String): PlatformQueryResult? { - if (!this::httpClient.isInitialized) { - authenticateSpotifyClient() - } - var spotifyLink = "https://" + fullLink.substringAfterLast("https://").substringBefore(" ").trim() diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt index 7a08f88a..ddff843e 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyAuth.kt @@ -26,6 +26,7 @@ import io.ktor.client.features.json.JsonFeature import io.ktor.client.request.forms.FormDataContent import io.ktor.client.request.post import io.ktor.http.Parameters +import kotlin.native.concurrent.SharedImmutable suspend fun authenticateSpotify(): TokenData? { return try { @@ -37,6 +38,7 @@ suspend fun authenticateSpotify(): TokenData? { } } +@SharedImmutable private val spotifyAuthClient by lazy { HttpClient { val clientId = "694d8bf4f6ec420fa66ea7fb4c68f89d" diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyRequests.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyRequests.kt index 16d947f3..9faeab37 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyRequests.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/spotify/SpotifyRequests.kt @@ -17,6 +17,7 @@ package com.shabinder.common.di.spotify import com.shabinder.common.di.gaana.corsApi +import com.shabinder.common.models.NativeAtomicReference import com.shabinder.common.models.spotify.Album import com.shabinder.common.models.spotify.PagingObjectPlaylistTrack import com.shabinder.common.models.spotify.Playlist @@ -28,9 +29,10 @@ private val BASE_URL get() = "${corsApi}https://api.spotify.com/v1" interface SpotifyRequests { - val httpClient: HttpClient + val httpClientRef: NativeAtomicReference + val httpClient get() = httpClientRef.value - suspend fun authenticateSpotifyClient(override: Boolean = false): HttpClient? + suspend fun authenticateSpotifyClient(override: Boolean = false) suspend fun getPlaylist(playlistID: String): Playlist { return httpClient.get("$BASE_URL/playlists/$playlistID") diff --git a/common/root/src/commonMain/kotlin/com/shabinder/common/root/integration/SpotiFlyerRootImpl.kt b/common/root/src/commonMain/kotlin/com/shabinder/common/root/integration/SpotiFlyerRootImpl.kt index 5d1d729d..af92eeae 100644 --- a/common/root/src/commonMain/kotlin/com/shabinder/common/root/integration/SpotiFlyerRootImpl.kt +++ b/common/root/src/commonMain/kotlin/com/shabinder/common/root/integration/SpotiFlyerRootImpl.kt @@ -26,7 +26,9 @@ import com.arkivanov.decompose.router import com.arkivanov.decompose.statekeeper.Parcelable import com.arkivanov.decompose.statekeeper.Parcelize import com.arkivanov.decompose.value.Value +import com.shabinder.common.database.getLogger import com.shabinder.common.di.Dir +import com.shabinder.common.di.createDirectories import com.shabinder.common.list.SpotiFlyerList import com.shabinder.common.main.SpotiFlyerMain import com.shabinder.common.models.Actions @@ -48,11 +50,17 @@ internal class SpotiFlyerRootImpl( init { methods.value = actions.freeze() - GlobalScope.launch(Dispatchers.Default) { + GlobalScope.launch { + /*TESTING*/ + getLogger().apply { + d("Hey...","Background Thread") + //d(directories.defaultDir(),"Background Thread") + d("Hey...","Background Thread") + } //*Authenticate Spotify Client*//* - /*if (methods.value.currentPlatform is AllPlatforms.Js) { - fetchPlatformQueryResult.spotifyProvider.authenticateSpotifyClient(override = true) - } else fetchPlatformQueryResult.spotifyProvider.authenticateSpotifyClient()*/ + /*fetchPlatformQueryResult.spotifyProvider.authenticateSpotifyClient( + override = true //methods.value.currentPlatform is AllPlatforms.Js + )*/ } }