@@ -3,24 +3,23 @@ package ru.stersh.youamp
3
3
import kotlinx.coroutines.flow.Flow
4
4
import kotlinx.coroutines.flow.map
5
5
import kotlinx.coroutines.flow.mapNotNull
6
- import kotlinx.coroutines.sync.Mutex
7
- import kotlinx.coroutines.sync.withLock
8
6
import ru.stersh.youamp.core.api.ApiDefaults
9
7
import ru.stersh.youamp.core.api.SubsonicApi
10
8
import ru.stersh.youamp.core.api.provider.ApiProvider
11
9
import ru.stersh.youamp.core.api.provider.NoActiveServerSettingsFound
12
10
import ru.stersh.youamp.core.room.server.SubsonicServerDao
13
11
import ru.stersh.youamp.core.room.server.SubsonicServerDb
12
+ import java.util.concurrent.ConcurrentHashMap
14
13
15
14
internal class ApiProviderImpl (
16
15
private val subsonicServerDao : SubsonicServerDao
17
16
) : ApiProvider {
18
- private val mutex = Mutex ()
19
- private var apiSonic: SubsonicApi ? = null
20
17
21
- override suspend fun getApi (): SubsonicApi = mutex.withLock {
18
+ private val apiCache = ConcurrentHashMap <Long , SubsonicApi >()
19
+
20
+ override suspend fun getApi (): SubsonicApi {
22
21
val currentServerSettings = subsonicServerDao.getActive() ? : throw NoActiveServerSettingsFound ()
23
- return @withLock requireApi(currentServerSettings)
22
+ return requireApi(currentServerSettings)
24
23
}
25
24
26
25
override suspend fun requireApiId (): Long {
@@ -43,22 +42,34 @@ internal class ApiProviderImpl(
43
42
return subsonicServerDao
44
43
.flowActive()
45
44
.map {
46
- getApiOrNull(it)
45
+ if (it == null ) {
46
+ null
47
+ } else {
48
+ getApi(it.id)
49
+ }
47
50
}
48
51
}
49
52
50
- private fun getApiOrNull (subsonicServer : SubsonicServerDb ? ): SubsonicApi ? {
51
- if (subsonicServer == null ) {
52
- return null
53
+ override fun flowApiId (): Flow <Long ?> {
54
+ return subsonicServerDao
55
+ .flowActive()
56
+ .map { it?.id }
57
+ }
58
+
59
+ override suspend fun getApi (id : Long ): SubsonicApi ? {
60
+ return apiCache.getOrPut(id) {
61
+ subsonicServerDao.getServer(id)?.let {
62
+ createNewApi(it)
63
+ }
53
64
}
54
- return requireApi(subsonicServer)
65
+ }
66
+
67
+ override suspend fun requireApi (id : Long ): SubsonicApi {
68
+ return requireNotNull(getApi(id))
55
69
}
56
70
57
71
private fun requireApi (subsonicServer : SubsonicServerDb ): SubsonicApi {
58
- if (! isSameSettings(subsonicServer)) {
59
- apiSonic = createNewApi(subsonicServer)
60
- }
61
- return requireNotNull(apiSonic)
72
+ return apiCache.getOrPut(subsonicServer.id) { createNewApi(subsonicServer) }
62
73
}
63
74
64
75
private fun createNewApi (subsonicServer : SubsonicServerDb ): SubsonicApi {
@@ -71,12 +82,4 @@ internal class ApiProviderImpl(
71
82
useLegacyAuth = subsonicServer.useLegacyAuth
72
83
)
73
84
}
74
-
75
- private fun isSameSettings (subsonicServer : SubsonicServerDb ): Boolean {
76
- val currentApiSonic = apiSonic ? : return false
77
- return currentApiSonic.username == subsonicServer.username &&
78
- currentApiSonic.password == subsonicServer.password &&
79
- currentApiSonic.url == subsonicServer.url &&
80
- currentApiSonic.useLegacyAuth == subsonicServer.useLegacyAuth
81
- }
82
85
}
0 commit comments