19 * You should have received a copy of the GNU General Public License |
19 * You should have received a copy of the GNU General Public License |
20 * along with AboutOss. If not, see <http://www.gnu.org/licenses/>. |
20 * along with AboutOss. If not, see <http://www.gnu.org/licenses/>. |
21 */ |
21 */ |
22 package com.geekorum.aboutoss.core.licensee |
22 package com.geekorum.aboutoss.core.licensee |
23 |
23 |
|
24 import com.geekorum.aboutoss.core.LicenseInfoRepository |
24 import kotlinx.coroutines.CoroutineDispatcher |
25 import kotlinx.coroutines.CoroutineDispatcher |
25 import kotlinx.coroutines.withContext |
26 import kotlinx.coroutines.withContext |
26 import kotlinx.serialization.ExperimentalSerializationApi |
27 import kotlinx.serialization.ExperimentalSerializationApi |
27 import okio.Source |
28 import okio.Source |
28 |
29 |
29 class LicenseeLicenseInfoRepository( |
30 class LicenseeLicenseInfoRepository( |
30 private val produceInput: suspend () -> Source, |
31 private val produceInput: suspend () -> Source, |
31 private val mainCoroutineDispatcher: CoroutineDispatcher, |
32 private val mainCoroutineDispatcher: CoroutineDispatcher, |
32 private val ioCoroutineDispatcher: CoroutineDispatcher, |
33 private val ioCoroutineDispatcher: CoroutineDispatcher, |
33 ) { |
34 ) : LicenseInfoRepository { |
34 |
35 |
35 private var licensesInfo: Map<String, String>? = null |
36 private var licensesInfo: Map<String, String>? = null |
36 |
37 |
37 suspend fun getLicensesInfo(): Map<String, String> = withContext(mainCoroutineDispatcher) { |
38 override suspend fun getLicensesInfo(): Map<String, String> = withContext(mainCoroutineDispatcher) { |
38 parseLicenses() |
39 parseLicenses() |
39 checkNotNull(licensesInfo) |
40 checkNotNull(licensesInfo) |
40 } |
41 } |
41 |
42 |
42 suspend fun getLicenseFor(dependency: String): String = withContext(mainCoroutineDispatcher) { |
43 override suspend fun getLicenseFor(dependency: String): String = withContext(mainCoroutineDispatcher) { |
43 parseLicenses() |
44 parseLicenses() |
44 checkNotNull(licensesInfo).let { |
45 checkNotNull(licensesInfo).let { |
45 return@withContext it[dependency] ?: error("Dependency not found") |
46 return@withContext it[dependency] ?: error("Dependency not found") |
46 } |
47 } |
47 } |
48 } |