summaryrefslogtreecommitdiffhomepage
path: root/android/app/src
diff options
context:
space:
mode:
authorJonatan Rhodin <jonatan.rhodin@mullvad.net>2024-06-13 16:31:40 +0200
committerJonatan Rhodin <jonatan.rhodin@mullvad.net>2024-06-14 13:27:05 +0200
commite80e9ee68559fcd32747c28a829e70d2121e9344 (patch)
tree0caefca40bdc425b44a5c1dc719b1f409933fb6f /android/app/src
parent6fb4623503fda3df77a9c7c3cc152a45506ba1a4 (diff)
downloadmullvadvpn-e80e9ee68559fcd32747c28a829e70d2121e9344.tar.xz
mullvadvpn-e80e9ee68559fcd32747c28a829e70d2121e9344.zip
Add api acccess method functionality
Diffstat (limited to 'android/app/src')
-rw-r--r--android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ApiAccessRepository.kt85
1 files changed, 85 insertions, 0 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ApiAccessRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ApiAccessRepository.kt
new file mode 100644
index 0000000000..bba17d84a0
--- /dev/null
+++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ApiAccessRepository.kt
@@ -0,0 +1,85 @@
+package net.mullvad.mullvadvpn.repository
+
+import arrow.core.raise.either
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.mapNotNull
+import kotlinx.coroutines.flow.stateIn
+import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService
+import net.mullvad.mullvadvpn.lib.model.ApiAccessMethod
+import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodId
+import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodName
+import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodSetting
+import net.mullvad.mullvadvpn.lib.model.GetApiAccessMethodError
+import net.mullvad.mullvadvpn.lib.model.NewAccessMethodSetting
+
+class ApiAccessRepository(
+ private val managementService: ManagementService,
+ dispatcher: CoroutineDispatcher = Dispatchers.IO
+) {
+ val accessMethods =
+ managementService.settings
+ .mapNotNull { it.apiAccessMethodSettings }
+ .stateIn(CoroutineScope(dispatcher), SharingStarted.Eagerly, null)
+
+ val currentAccessMethod =
+ managementService.currentAccessMethod.stateIn(
+ CoroutineScope(dispatcher),
+ SharingStarted.Eagerly,
+ null
+ )
+
+ suspend fun addApiAccessMethod(newAccessMethodSetting: NewAccessMethodSetting) =
+ managementService.addApiAccessMethod(newAccessMethodSetting)
+
+ suspend fun removeApiAccessMethod(apiAccessMethodId: ApiAccessMethodId) =
+ managementService.removeApiAccessMethod(apiAccessMethodId)
+
+ suspend fun setCurrentApiAccessMethod(apiAccessMethodId: ApiAccessMethodId) =
+ managementService.setApiAccessMethod(apiAccessMethodId)
+
+ private suspend fun updateApiAccessMethod(apiAccessMethodSetting: ApiAccessMethodSetting) =
+ managementService.updateApiAccessMethod(apiAccessMethodSetting)
+
+ suspend fun updateApiAccessMethod(
+ apiAccessMethodId: ApiAccessMethodId,
+ apiAccessMethodName: ApiAccessMethodName,
+ apiAccessMethod: ApiAccessMethod
+ ) = either {
+ val apiAccessMethodSetting = getApiAccessMethodSettingById(apiAccessMethodId).bind()
+ updateApiAccessMethod(
+ apiAccessMethodSetting.copy(
+ id = apiAccessMethodId,
+ name = apiAccessMethodName,
+ apiAccessMethod = apiAccessMethod
+ )
+ )
+ .bind()
+ }
+
+ suspend fun testCustomApiAccessMethod(customProxy: ApiAccessMethod.CustomProxy) =
+ managementService.testCustomApiAccessMethod(customProxy)
+
+ suspend fun testApiAccessMethodById(apiAccessMethodId: ApiAccessMethodId) =
+ managementService.testApiAccessMethodById(apiAccessMethodId)
+
+ fun getApiAccessMethodSettingById(id: ApiAccessMethodId) =
+ either<GetApiAccessMethodError, ApiAccessMethodSetting> {
+ accessMethods.value?.firstOrNull { it.id == id }
+ ?: raise(GetApiAccessMethodError.NotFound)
+ }
+
+ fun apiAccessMethodSettingById(id: ApiAccessMethodId): Flow<ApiAccessMethodSetting> =
+ accessMethods.mapNotNull { it?.firstOrNull { accessMethod -> accessMethod.id == id } }
+
+ fun enabledApiAccessMethods(): Flow<List<ApiAccessMethodSetting>> =
+ accessMethods.mapNotNull { it?.filter { accessMethod -> accessMethod.enabled } }
+
+ suspend fun setEnabledApiAccessMethod(id: ApiAccessMethodId, enabled: Boolean) = either {
+ val accessMethod = getApiAccessMethodSettingById(id).bind()
+ updateApiAccessMethod(accessMethod.copy(enabled = enabled)).bind()
+ }
+}