core: extract LicenseInfoRepository interface
authorDa Risk <da_risk@geekorum.com>
Wed, 12 Mar 2025 21:21:31 -0400
changeset 26 59006722b704
parent 25 a645b4ebb054
child 27 6469637350c6
core: extract LicenseInfoRepository interface
core/src/commonMain/kotlin/LicenseInfoRepository.kt
core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt
core/src/main/java/com/geekorum/aboutoss/core/gms/GmsLicenseInfoRepository.kt
sample/src/main/java/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt
ui/common/src/main/java/com/geekorum/aboutoss/ui/common/OpenSourceLicensesViewModel.kt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/commonMain/kotlin/LicenseInfoRepository.kt	Wed Mar 12 21:21:31 2025 -0400
@@ -0,0 +1,37 @@
+/*
+ * AboutOss is an utility library to retrieve and display
+ * opensource licenses in Android applications.
+ *
+ * Copyright (C) 2023 by Frederic-Charles Barthelery.
+ *
+ * This file is part of AboutOss.
+ *
+ * AboutOss is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * AboutOss is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.geekorum.aboutoss.core
+
+/**
+ * Retrieve License information 
+ */
+interface LicenseInfoRepository {
+    /**
+     * Get a map of license text per dependency
+     */
+    suspend fun getLicensesInfo(): Map<String, String>
+
+    /**
+     * Get license text for a specific dependency
+     */
+    suspend fun getLicenseFor(dependency: String): String
+}
--- a/core/src/main/java/com/geekorum/aboutoss/core/LicenseInfoRepository.kt	Wed Mar 12 20:17:28 2025 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-/*
- * AboutOss is an utility library to retrieve and display
- * opensource licenses in Android applications.
- *
- * Copyright (C) 2023 by Frederic-Charles Barthelery.
- *
- * This file is part of AboutOss.
- *
- * AboutOss is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * AboutOss is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
- */
-package com.geekorum.aboutoss.core
-
-import android.content.Context
-import com.geekorum.aboutoss.core.gms.OssLicenseParser
-import com.geekorum.aboutoss.core.gms.openRawResourcesByName
-import kotlinx.coroutines.CoroutineDispatcher
-import kotlinx.coroutines.withContext
-import okio.source
-
-/**
- * Retrieve License information stored in application resources
- */
-class LicenseInfoRepository(
-    private val appContext: Context,
-    private val mainCoroutineDispatcher: CoroutineDispatcher,
-    private val ioCoroutineDispatcher: CoroutineDispatcher,
-    private val thirdPartyLicensesResourceName: String = "third_party_licenses",
-    private val thirdPartyLicenseMetadataResourceName: String = "third_party_license_metadata"
-) {
-
-    private var licensesInfo: Map<String, String>? = null
-
-    suspend fun getLicensesInfo(): Map<String, String> = withContext(mainCoroutineDispatcher) {
-        parseLicenses()
-        checkNotNull(licensesInfo)
-    }
-
-    suspend fun getLicenseFor(dependency: String): String = withContext(mainCoroutineDispatcher) {
-        parseLicenses()
-        checkNotNull(licensesInfo).let {
-            return@withContext it[dependency] ?: error("Dependency not found")
-        }
-    }
-
-    private suspend fun parseLicenses() = withContext(mainCoroutineDispatcher) {
-        if (licensesInfo == null) {
-            val licenses = withContext(ioCoroutineDispatcher) {
-                OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicensesResourceName).use { licensesInput ->
-                    OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicenseMetadataResourceName)
-                        .use { licensesMetadataInput ->
-                            val parser = OssLicenseParser()
-                            parser.parseLicenses(
-                                thirdPartyLicensesInput = licensesInput.source(),
-                                thirdPartyLicensesMetadataInput = licensesMetadataInput.source()
-                            )
-                        }
-                }
-            }
-            licensesInfo = licenses
-        }
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/main/java/com/geekorum/aboutoss/core/gms/GmsLicenseInfoRepository.kt	Wed Mar 12 21:21:31 2025 -0400
@@ -0,0 +1,72 @@
+/*
+ * AboutOss is an utility library to retrieve and display
+ * opensource licenses in Android applications.
+ *
+ * Copyright (C) 2023 by Frederic-Charles Barthelery.
+ *
+ * This file is part of AboutOss.
+ *
+ * AboutOss is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * AboutOss is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.geekorum.aboutoss.core.gms
+
+import android.content.Context
+import com.geekorum.aboutoss.core.LicenseInfoRepository
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.withContext
+import okio.source
+
+/**
+ * Retrieve License information stored in application resources
+ */
+class GmsLicenseInfoRepository(
+    private val appContext: Context,
+    private val mainCoroutineDispatcher: CoroutineDispatcher,
+    private val ioCoroutineDispatcher: CoroutineDispatcher,
+    private val thirdPartyLicensesResourceName: String = "third_party_licenses",
+    private val thirdPartyLicenseMetadataResourceName: String = "third_party_license_metadata"
+) : LicenseInfoRepository {
+
+    private var licensesInfo: Map<String, String>? = null
+
+    override suspend fun getLicensesInfo(): Map<String, String> = withContext(mainCoroutineDispatcher) {
+        parseLicenses()
+        checkNotNull(licensesInfo)
+    }
+
+    override suspend fun getLicenseFor(dependency: String): String = withContext(mainCoroutineDispatcher) {
+        parseLicenses()
+        checkNotNull(licensesInfo).let {
+            return@withContext it[dependency] ?: error("Dependency not found")
+        }
+    }
+
+    private suspend fun parseLicenses() = withContext(mainCoroutineDispatcher) {
+        if (licensesInfo == null) {
+            val licenses = withContext(ioCoroutineDispatcher) {
+                OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicensesResourceName).use { licensesInput ->
+                    OssLicenseParser.openRawResourcesByName(appContext, thirdPartyLicenseMetadataResourceName)
+                        .use { licensesMetadataInput ->
+                            val parser = OssLicenseParser()
+                            parser.parseLicenses(
+                                thirdPartyLicensesInput = licensesInput.source(),
+                                thirdPartyLicensesMetadataInput = licensesMetadataInput.source()
+                            )
+                        }
+                }
+            }
+            licensesInfo = licenses
+        }
+    }
+}
\ No newline at end of file
--- a/sample/src/main/java/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt	Wed Mar 12 20:17:28 2025 -0400
+++ b/sample/src/main/java/com/geekorum/aboutoss/sampleapp/PrebuiltLicencesActivities.kt	Wed Mar 12 21:21:31 2025 -0400
@@ -26,7 +26,7 @@
 import androidx.lifecycle.viewmodel.CreationExtras
 import androidx.lifecycle.viewmodel.initializer
 import androidx.lifecycle.viewmodel.viewModelFactory
-import com.geekorum.aboutoss.core.LicenseInfoRepository
+import com.geekorum.aboutoss.core.gms.GmsLicenseInfoRepository
 import com.geekorum.aboutoss.ui.common.OpenSourceLicensesViewModel
 import com.geekorum.aboutoss.ui.material3.OpenSourceLicensesActivity
 import com.geekorum.geekdroid.network.BrowserLauncher
@@ -73,7 +73,7 @@
 fun CreationExtras.createPrebuildOpenSourceLicensesViewModel(): OpenSourceLicensesViewModel {
     val application =
         this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]!!
-    val licenseInfoRepository = LicenseInfoRepository(
+    val licenseInfoRepository = GmsLicenseInfoRepository(
         appContext = application,
         mainCoroutineDispatcher = Dispatchers.Main,
         ioCoroutineDispatcher = Dispatchers.IO,
--- a/ui/common/src/main/java/com/geekorum/aboutoss/ui/common/OpenSourceLicensesViewModel.kt	Wed Mar 12 20:17:28 2025 -0400
+++ b/ui/common/src/main/java/com/geekorum/aboutoss/ui/common/OpenSourceLicensesViewModel.kt	Wed Mar 12 21:21:31 2025 -0400
@@ -30,6 +30,7 @@
 import androidx.lifecycle.viewmodel.initializer
 import androidx.lifecycle.viewmodel.viewModelFactory
 import com.geekorum.aboutoss.core.LicenseInfoRepository
+import com.geekorum.aboutoss.core.gms.GmsLicenseInfoRepository
 import com.geekorum.geekdroid.network.BrowserLauncher
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.SharingStarted
@@ -74,7 +75,7 @@
         val Factory = viewModelFactory {
             initializer {
                 val application = this[APPLICATION_KEY]!!
-                val licenseInfoRepository = LicenseInfoRepository(
+                val licenseInfoRepository = GmsLicenseInfoRepository(
                     appContext = application,
                     mainCoroutineDispatcher = Dispatchers.Main,
                     ioCoroutineDispatcher = Dispatchers.IO