From 16693166822c8319c65856a240450b466369a170 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 12 Oct 2022 10:12:13 +0200 Subject: [PATCH] Remove dependency to arrow. Please use `org.matrix.android.sdk.api.util.Optional` instead. --- changelog.d/7335.misc | 1 + dependencies.gradle | 5 --- dependencies_groups.gradle | 1 - .../matrix/android/sdk/api/util/Optional.kt | 28 +++++++--------- vector/build.gradle | 3 -- .../im/vector/app/ActiveSessionDataSource.kt | 4 +-- .../java/im/vector/app/SpaceStateHandler.kt | 4 +-- .../im/vector/app/SpaceStateHandlerImpl.kt | 11 +++---- .../vector/app/core/di/ActiveSessionHolder.kt | 7 ++-- .../im/vector/app/core/files/FileSaver.kt | 19 +++++------ .../setup/KeysBackupSetupStep3Fragment.kt | 32 ++++++++----------- .../room/list/home/HomeRoomListViewModel.kt | 2 +- .../test/fakes/FakeActiveSessionDataSource.kt | 4 +-- 13 files changed, 48 insertions(+), 73 deletions(-) create mode 100644 changelog.d/7335.misc diff --git a/changelog.d/7335.misc b/changelog.d/7335.misc new file mode 100644 index 0000000000..3b14aa1339 --- /dev/null +++ b/changelog.d/7335.misc @@ -0,0 +1 @@ +Dependency to arrow has been removed. Please use `org.matrix.android.sdk.api.util.Optional` instead. diff --git a/dependencies.gradle b/dependencies.gradle index 359cf577ef..927e243298 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -14,7 +14,6 @@ def kotlinCoroutines = "1.6.4" def dagger = "2.44" def appDistribution = "16.0.0-beta04" def retrofit = "2.9.0" -def arrow = "0.8.2" def markwon = "4.6.2" def moshi = "1.14.0" def lifecycle = "2.5.1" @@ -114,10 +113,6 @@ ext.libs = [ rx : [ 'rxKotlin' : "io.reactivex.rxjava2:rxkotlin:2.4.0" ], - arrow : [ - 'core' : "io.arrow-kt:arrow-core:$arrow", - 'instances' : "io.arrow-kt:arrow-instances-core:$arrow" - ], markwon : [ 'core' : "io.noties.markwon:core:$markwon", 'extLatex' : "io.noties.markwon:ext-latex:$markwon", diff --git a/dependencies_groups.gradle b/dependencies_groups.gradle index 991d54d9af..1c1fd8e32e 100644 --- a/dependencies_groups.gradle +++ b/dependencies_groups.gradle @@ -134,7 +134,6 @@ ext.groups = [ 'commons-io', 'commons-logging', 'info.picocli', - 'io.arrow-kt', 'io.element.android', 'io.github.davidburstrom.contester', 'io.github.detekt.sarif4k', diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/Optional.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/Optional.kt index 42724746c0..2da6d4cbf8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/Optional.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/Optional.kt @@ -15,15 +15,13 @@ */ package org.matrix.android.sdk.api.util -data class Optional constructor(private val value: T?) { +data class Optional(private val value: T?) { - fun get(): T { - return value!! - } + fun get(): T = value!! - fun getOrNull(): T? { - return value - } + fun orNull(): T? = value + + fun getOrNull(): T? = value fun map(fn: (T) -> U?): Optional { return if (value == null) { @@ -33,23 +31,19 @@ data class Optional constructor(private val value: T?) { } } - fun getOrElse(fn: () -> T): T { + fun orElse(fn: () -> T): T { return value ?: fn() } - fun hasValue(): Boolean { - return value != null - } + fun hasValue(): Boolean = value != null companion object { - fun from(value: T?): Optional { - return Optional(value) - } + fun from(value: T?): Optional = Optional(value) - fun empty(): Optional { - return Optional(null) - } + fun empty(): Optional = Optional(null) } } +fun T?.toOption() = Optional(this) + fun T?.toOptional() = Optional(this) diff --git a/vector/build.gradle b/vector/build.gradle index 76f32a34db..be79828ea5 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -170,9 +170,6 @@ dependencies { // Paging implementation libs.androidx.pagingRuntimeKtx - // Functional Programming - implementation libs.arrow.core - // Pref api libs.androidx.preferenceKtx diff --git a/vector/src/main/java/im/vector/app/ActiveSessionDataSource.kt b/vector/src/main/java/im/vector/app/ActiveSessionDataSource.kt index f58c685ab9..98d6ee7a0a 100644 --- a/vector/src/main/java/im/vector/app/ActiveSessionDataSource.kt +++ b/vector/src/main/java/im/vector/app/ActiveSessionDataSource.kt @@ -17,11 +17,11 @@ package im.vector.app -import arrow.core.Option import im.vector.app.core.utils.BehaviorDataSource import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.util.Optional import javax.inject.Inject import javax.inject.Singleton @Singleton -class ActiveSessionDataSource @Inject constructor() : BehaviorDataSource>() +class ActiveSessionDataSource @Inject constructor() : BehaviorDataSource>() diff --git a/vector/src/main/java/im/vector/app/SpaceStateHandler.kt b/vector/src/main/java/im/vector/app/SpaceStateHandler.kt index 64b6fc4e8b..efa72a0161 100644 --- a/vector/src/main/java/im/vector/app/SpaceStateHandler.kt +++ b/vector/src/main/java/im/vector/app/SpaceStateHandler.kt @@ -17,10 +17,10 @@ package im.vector.app import androidx.lifecycle.DefaultLifecycleObserver -import arrow.core.Option import kotlinx.coroutines.flow.Flow import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.room.model.RoomSummary +import org.matrix.android.sdk.api.util.Optional /** * Gets info about the current space the user has navigated to, any space backstack they may have @@ -62,7 +62,7 @@ interface SpaceStateHandler : DefaultLifecycleObserver { /** * Gets a flow of the selected space for clients to react immediately to space changes. */ - fun getSelectedSpaceFlow(): Flow> + fun getSelectedSpaceFlow(): Flow> /** * Gets the id of the active space, or null if there is none. diff --git a/vector/src/main/java/im/vector/app/SpaceStateHandlerImpl.kt b/vector/src/main/java/im/vector/app/SpaceStateHandlerImpl.kt index d230e76c1e..2c00d03014 100644 --- a/vector/src/main/java/im/vector/app/SpaceStateHandlerImpl.kt +++ b/vector/src/main/java/im/vector/app/SpaceStateHandlerImpl.kt @@ -17,7 +17,6 @@ package im.vector.app import androidx.lifecycle.LifecycleOwner -import arrow.core.Option import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.utils.BehaviorDataSource import im.vector.app.features.analytics.AnalyticsTracker @@ -42,6 +41,8 @@ import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.getRoomSummary import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.sync.SyncRequestState +import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.api.util.toOption import javax.inject.Inject import javax.inject.Singleton @@ -59,7 +60,7 @@ class SpaceStateHandlerImpl @Inject constructor( ) : SpaceStateHandler { private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) - private val selectedSpaceDataSource = BehaviorDataSource>(Option.empty()) + private val selectedSpaceDataSource = BehaviorDataSource>(Optional.empty()) private val selectedSpaceFlow = selectedSpaceDataSource.stream() override fun getCurrentSpace(): RoomSummary? { @@ -98,11 +99,7 @@ class SpaceStateHandlerImpl @Inject constructor( uiStateRepository.storeSelectedSpace(spaceToSet?.roomId, activeSession.sessionId) } - if (spaceToSet == null) { - selectedSpaceDataSource.post(Option.empty()) - } else { - selectedSpaceDataSource.post(Option.just(spaceToSet)) - } + selectedSpaceDataSource.post(spaceToSet.toOption()) if (spaceId != null) { activeSession.coroutineScope.launch(Dispatchers.IO) { diff --git a/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt b/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt index 3f0507305a..64111d2fa1 100644 --- a/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt +++ b/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt @@ -17,7 +17,6 @@ package im.vector.app.core.di import android.content.Context -import arrow.core.Option import im.vector.app.ActiveSessionDataSource import im.vector.app.core.extensions.configureAndStart import im.vector.app.core.extensions.startSyncing @@ -31,6 +30,8 @@ import im.vector.app.features.session.SessionListener import kotlinx.coroutines.runBlocking import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.api.util.toOption import timber.log.Timber import java.util.concurrent.atomic.AtomicReference import javax.inject.Inject @@ -57,7 +58,7 @@ class ActiveSessionHolder @Inject constructor( fun setActiveSession(session: Session) { Timber.w("setActiveSession of ${session.myUserId}") activeSessionReference.set(session) - activeSessionDataSource.post(Option.just(session)) + activeSessionDataSource.post(session.toOption()) keyRequestHandler.start(session) incomingVerificationRequestHandler.start(session) @@ -77,7 +78,7 @@ class ActiveSessionHolder @Inject constructor( } activeSessionReference.set(null) - activeSessionDataSource.post(Option.empty()) + activeSessionDataSource.post(Optional.empty()) keyRequestHandler.stop() incomingVerificationRequestHandler.stop() diff --git a/vector/src/main/java/im/vector/app/core/files/FileSaver.kt b/vector/src/main/java/im/vector/app/core/files/FileSaver.kt index 7de5286854..c595bb2693 100644 --- a/vector/src/main/java/im/vector/app/core/files/FileSaver.kt +++ b/vector/src/main/java/im/vector/app/core/files/FileSaver.kt @@ -24,7 +24,6 @@ import android.os.Build import android.provider.MediaStore import androidx.annotation.WorkerThread import androidx.core.content.getSystemService -import arrow.core.Try import okio.buffer import okio.sink import okio.source @@ -35,11 +34,10 @@ import java.io.File * Save a string to a file with Okio. */ @WorkerThread -fun writeToFile(str: String, file: File): Try { - return Try { - file.sink().buffer().use { - it.writeString(str, Charsets.UTF_8) - } +@Throws +fun writeToFile(str: String, file: File) { + file.sink().buffer().use { + it.writeString(str, Charsets.UTF_8) } } @@ -47,11 +45,10 @@ fun writeToFile(str: String, file: File): Try { * Save a byte array to a file with Okio. */ @WorkerThread -fun writeToFile(data: ByteArray, file: File): Try { - return Try { - file.sink().buffer().use { - it.write(data) - } +@Throws +fun writeToFile(data: ByteArray, file: File) { + file.sink().buffer().use { + it.write(data) } } diff --git a/vector/src/main/java/im/vector/app/features/crypto/keysbackup/setup/KeysBackupSetupStep3Fragment.kt b/vector/src/main/java/im/vector/app/features/crypto/keysbackup/setup/KeysBackupSetupStep3Fragment.kt index 7e0fb7e417..c70c5d979e 100644 --- a/vector/src/main/java/im/vector/app/features/crypto/keysbackup/setup/KeysBackupSetupStep3Fragment.kt +++ b/vector/src/main/java/im/vector/app/features/crypto/keysbackup/setup/KeysBackupSetupStep3Fragment.kt @@ -25,7 +25,6 @@ import android.widget.TextView import android.widget.Toast import androidx.core.view.isVisible import androidx.lifecycle.lifecycleScope -import arrow.core.Try import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.dialog.MaterialAlertDialogBuilder import dagger.hilt.android.AndroidEntryPoint @@ -167,7 +166,7 @@ class KeysBackupSetupStep3Fragment : private fun exportRecoveryKeyToFile(uri: Uri, data: String) { lifecycleScope.launch(Dispatchers.Main) { - Try { + try { withContext(Dispatchers.IO) { requireContext().safeOpenOutputStream(uri) ?.use { os -> @@ -176,24 +175,19 @@ class KeysBackupSetupStep3Fragment : } } ?: throw IOException("Unable to write the file") + viewModel.copyHasBeenMade = true + activity?.let { + MaterialAlertDialogBuilder(it) + .setTitle(R.string.dialog_title_success) + .setMessage(R.string.recovery_key_export_saved) + } + } catch (throwable: Throwable) { + activity?.let { + MaterialAlertDialogBuilder(it) + .setTitle(R.string.dialog_title_error) + .setMessage(errorFormatter.toHumanReadable(throwable)) + } } - .fold( - { throwable -> - activity?.let { - MaterialAlertDialogBuilder(it) - .setTitle(R.string.dialog_title_error) - .setMessage(errorFormatter.toHumanReadable(throwable)) - } - }, - { - viewModel.copyHasBeenMade = true - activity?.let { - MaterialAlertDialogBuilder(it) - .setTitle(R.string.dialog_title_success) - .setMessage(R.string.recovery_key_export_saved) - } - } - ) ?.setCancelable(false) ?.setPositiveButton(R.string.ok, null) ?.show() diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt index 33b293497e..f358601399 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt @@ -21,7 +21,6 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer import androidx.paging.PagedList -import arrow.core.toOption import com.airbnb.mvrx.MavericksViewModelFactory import dagger.assisted.Assisted import dagger.assisted.AssistedFactory @@ -68,6 +67,7 @@ import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams import org.matrix.android.sdk.api.session.room.state.isPublic import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.toMatrixItem +import org.matrix.android.sdk.api.util.toOption import org.matrix.android.sdk.flow.flow class HomeRoomListViewModel @AssistedInject constructor( diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeActiveSessionDataSource.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeActiveSessionDataSource.kt index 4dab6daf3b..938607017f 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeActiveSessionDataSource.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeActiveSessionDataSource.kt @@ -16,15 +16,15 @@ package im.vector.app.test.fakes -import arrow.core.Option import im.vector.app.ActiveSessionDataSource import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.util.toOptional class FakeActiveSessionDataSource { val instance = ActiveSessionDataSource() fun setActiveSession(session: Session) { - instance.post(Option.just(session)) + instance.post(session.toOptional()) } }