Kotlin: use orEmpty()

This commit is contained in:
Benoit Marty 2020-05-25 14:04:13 +02:00
parent e793a46576
commit 691e7fe616
19 changed files with 26 additions and 26 deletions

View file

@ -76,7 +76,7 @@ fun RegistrationFlowResponse.toFlowResult(): FlowResult {
this.flows?.forEach { it.stages?.mapTo(allFlowTypes) { type -> type } } this.flows?.forEach { it.stages?.mapTo(allFlowTypes) { type -> type } }
allFlowTypes.forEach { type -> allFlowTypes.forEach { type ->
val isMandatory = flows?.all { type in it.stages ?: emptyList() } == true val isMandatory = flows?.all { type in it.stages.orEmpty() } == true
val stage = when (type) { val stage = when (type) {
LoginFlowTypes.RECAPTCHA -> Stage.ReCaptcha(isMandatory, ((params?.get(type) as? Map<*, *>)?.get("public_key") as? String) LoginFlowTypes.RECAPTCHA -> Stage.ReCaptcha(isMandatory, ((params?.get(type) as? Map<*, *>)?.get("public_key") as? String)
@ -88,7 +88,7 @@ fun RegistrationFlowResponse.toFlowResult(): FlowResult {
else -> Stage.Other(isMandatory, type, (params?.get(type) as? Map<*, *>)) else -> Stage.Other(isMandatory, type, (params?.get(type) as? Map<*, *>))
} }
if (type in completedStages ?: emptyList()) { if (type in completedStages.orEmpty()) {
completedStage.add(stage) completedStage.add(stage)
} else { } else {
missingStage.add(stage) missingStage.add(stage)

View file

@ -262,7 +262,7 @@ internal class DefaultCryptoService @Inject constructor(
override fun onSuccess(data: DevicesListResponse) { override fun onSuccess(data: DevicesListResponse) {
// Save in local DB // Save in local DB
cryptoStore.saveMyDevicesInfo(data.devices ?: emptyList()) cryptoStore.saveMyDevicesInfo(data.devices.orEmpty())
callback.onSuccess(data) callback.onSuccess(data)
} }
} }
@ -446,7 +446,7 @@ internal class DefaultCryptoService @Inject constructor(
} }
override fun getCryptoDeviceInfo(userId: String): List<CryptoDeviceInfo> { override fun getCryptoDeviceInfo(userId: String): List<CryptoDeviceInfo> {
return cryptoStore.getUserDeviceList(userId) ?: emptyList() return cryptoStore.getUserDeviceList(userId).orEmpty()
} }
override fun getLiveCryptoDeviceInfo(): LiveData<List<CryptoDeviceInfo>> { override fun getLiveCryptoDeviceInfo(): LiveData<List<CryptoDeviceInfo>> {

View file

@ -34,7 +34,7 @@ internal class EnsureOlmSessionsForUsersAction @Inject constructor(private val o
suspend fun handle(users: List<String>): MXUsersDevicesMap<MXOlmSessionResult> { suspend fun handle(users: List<String>): MXUsersDevicesMap<MXOlmSessionResult> {
Timber.v("## ensureOlmSessionsForUsers() : ensureOlmSessionsForUsers $users") Timber.v("## ensureOlmSessionsForUsers() : ensureOlmSessionsForUsers $users")
val devicesByUser = users.associateWith { userId -> val devicesByUser = users.associateWith { userId ->
val devices = cryptoStore.getUserDevices(userId)?.values ?: emptyList() val devices = cryptoStore.getUserDevices(userId)?.values.orEmpty()
devices.filter { devices.filter {
// Don't bother setting up session to ourself // Don't bother setting up session to ourself

View file

@ -103,7 +103,7 @@ internal class MXMegolmDecryption(private val userId: String,
senderCurve25519Key = olmDecryptionResult.senderKey, senderCurve25519Key = olmDecryptionResult.senderKey,
claimedEd25519Key = olmDecryptionResult.keysClaimed?.get("ed25519"), claimedEd25519Key = olmDecryptionResult.keysClaimed?.get("ed25519"),
forwardingCurve25519KeyChain = olmDecryptionResult.forwardingCurve25519KeyChain forwardingCurve25519KeyChain = olmDecryptionResult.forwardingCurve25519KeyChain
?: emptyList() .orEmpty()
) )
} else { } else {
throw MXCryptoError.Base(MXCryptoError.ErrorType.MISSING_FIELDS, MXCryptoError.MISSING_FIELDS_REASON) throw MXCryptoError.Base(MXCryptoError.ErrorType.MISSING_FIELDS, MXCryptoError.MISSING_FIELDS_REASON)

View file

@ -44,7 +44,7 @@ internal class MXOlmEncryption(
ensureSession(userIds) ensureSession(userIds)
val deviceInfos = ArrayList<CryptoDeviceInfo>() val deviceInfos = ArrayList<CryptoDeviceInfo>()
for (userId in userIds) { for (userId in userIds) {
val devices = cryptoStore.getUserDevices(userId)?.values ?: emptyList() val devices = cryptoStore.getUserDevices(userId)?.values.orEmpty()
for (device in devices) { for (device in devices) {
val key = device.identityKey() val key = device.identityKey()
if (key == olmDevice.deviceCurve25519Key) { if (key == olmDevice.deviceCurve25519Key) {

View file

@ -450,7 +450,7 @@ internal class RealmCryptoStore @Inject constructor(
} }
) )
return Transformations.map(liveData) { return Transformations.map(liveData) {
it.firstOrNull() ?: emptyList() it.firstOrNull().orEmpty()
} }
} }
@ -480,7 +480,7 @@ internal class RealmCryptoStore @Inject constructor(
} }
) )
return Transformations.map(liveData) { return Transformations.map(liveData) {
it.firstOrNull() ?: emptyList() it.firstOrNull().orEmpty()
} }
} }

View file

@ -45,7 +45,7 @@ internal object PushRulesMapper {
private fun fromActionStr(actionsStr: String?): List<Any> { private fun fromActionStr(actionsStr: String?): List<Any> {
try { try {
return actionsStr?.let { moshiActionsAdapter.fromJson(it) } ?: emptyList() return actionsStr?.let { moshiActionsAdapter.fromJson(it) }.orEmpty()
} catch (e: Throwable) { } catch (e: Throwable) {
Timber.e(e, "## failed to map push rule actions <$actionsStr>") Timber.e(e, "## failed to map push rule actions <$actionsStr>")
return emptyList() return emptyList()

View file

@ -49,7 +49,7 @@ internal class RoomSummaryMapper @Inject constructor(private val timelineEventMa
membership = roomSummaryEntity.membership, membership = roomSummaryEntity.membership,
versioningState = roomSummaryEntity.versioningState, versioningState = roomSummaryEntity.versioningState,
readMarkerId = roomSummaryEntity.readMarkerId, readMarkerId = roomSummaryEntity.readMarkerId,
userDrafts = roomSummaryEntity.userDrafts?.userDrafts?.map { DraftMapper.map(it) } ?: emptyList(), userDrafts = roomSummaryEntity.userDrafts?.userDrafts?.map { DraftMapper.map(it) }.orEmpty(),
canonicalAlias = roomSummaryEntity.canonicalAlias, canonicalAlias = roomSummaryEntity.canonicalAlias,
aliases = roomSummaryEntity.aliases.toList(), aliases = roomSummaryEntity.aliases.toList(),
isEncrypted = roomSummaryEntity.isEncrypted, isEncrypted = roomSummaryEntity.isEncrypted,

View file

@ -49,7 +49,7 @@ internal class TimelineEventMapper @Inject constructor(private val readReceiptsS
it.user it.user
}?.sortedByDescending { }?.sortedByDescending {
it.originServerTs it.originServerTs
} ?: emptyList() }.orEmpty()
) )
} }
} }

View file

@ -131,7 +131,7 @@ internal class RoomSummaryUpdater @Inject constructor(
?.canonicalAlias ?.canonicalAlias
val roomAliases = ContentMapper.map(lastAliasesEvent?.content).toModel<RoomAliasesContent>()?.aliases val roomAliases = ContentMapper.map(lastAliasesEvent?.content).toModel<RoomAliasesContent>()?.aliases
?: emptyList() .orEmpty()
roomSummaryEntity.aliases.clear() roomSummaryEntity.aliases.clear()
roomSummaryEntity.aliases.addAll(roomAliases) roomSummaryEntity.aliases.addAll(roomAliases)
roomSummaryEntity.flatAliases = roomAliases.joinToString(separator = "|", prefix = "|") roomSummaryEntity.flatAliases = roomAliases.joinToString(separator = "|", prefix = "|")

View file

@ -143,7 +143,7 @@ class DraftRepository @Inject constructor(private val monarchy: Monarchy) {
} }
) )
return Transformations.map(liveData) { return Transformations.map(liveData) {
it.firstOrNull() ?: emptyList() it.firstOrNull().orEmpty()
} }
} }

View file

@ -111,7 +111,7 @@ internal class DefaultReadService @AssistedInject constructor(
{ readReceiptsSummaryMapper.map(it) } { readReceiptsSummaryMapper.map(it) }
) )
return Transformations.map(liveRealmData) { return Transformations.map(liveRealmData) {
it.firstOrNull() ?: emptyList() it.firstOrNull().orEmpty()
} }
} }

View file

@ -48,7 +48,7 @@ internal class DefaultUpdateQuickReactionTask @Inject constructor(private val mo
monarchy.doWithRealm { realm -> monarchy.doWithRealm { realm ->
res = updateQuickReaction(realm, params.reaction, params.oppositeReaction, params.eventId) res = updateQuickReaction(realm, params.reaction, params.oppositeReaction, params.eventId)
} }
return UpdateQuickReactionTask.Result(res?.first, res?.second ?: emptyList()) return UpdateQuickReactionTask.Result(res?.first, res?.second.orEmpty())
} }
private fun updateQuickReaction(realm: Realm, reaction: String, oppositeReaction: String, eventId: String): Pair<String?, List<String>?> { private fun updateQuickReaction(realm: Realm, reaction: String, oppositeReaction: String, eventId: String): Pair<String?, List<String>?> {

View file

@ -44,7 +44,7 @@ data class TermsResponse(
version = tos[VERSION] as? String version = tos[VERSION] as? String
) )
} }
}?.filterNotNull() ?: emptyList() }?.filterNotNull().orEmpty()
} }
private companion object { private companion object {

View file

@ -43,7 +43,7 @@ class AttachmentsPreviewActivity : VectorBaseActivity(), ToolbarConfigurable {
} }
fun getOutput(intent: Intent): List<ContentAttachmentData> { fun getOutput(intent: Intent): List<ContentAttachmentData> {
return intent.getParcelableArrayListExtra(ATTACHMENTS_PREVIEW_RESULT) ?: emptyList() return intent.getParcelableArrayListExtra<ContentAttachmentData>(ATTACHMENTS_PREVIEW_RESULT).orEmpty()
} }
fun getKeepOriginalSize(intent: Intent): Boolean { fun getKeepOriginalSize(intent: Intent): Boolean {

View file

@ -151,8 +151,8 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
private fun changeThreePidState(threePid: ThreePid, state: Async<SharedState>) { private fun changeThreePidState(threePid: ThreePid, state: Async<SharedState>) {
setState { setState {
val currentMails = emailList() ?: emptyList() val currentMails = emailList().orEmpty()
val phones = phoneNumbersList() ?: emptyList() val phones = phoneNumbersList().orEmpty()
copy( copy(
emailList = Success( emailList = Success(
currentMails.map { currentMails.map {
@ -178,8 +178,8 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
private fun changeThreePidSubmitState(threePid: ThreePid, submitState: Async<Unit>) { private fun changeThreePidSubmitState(threePid: ThreePid, submitState: Async<Unit>) {
setState { setState {
val currentMails = emailList() ?: emptyList() val currentMails = emailList().orEmpty()
val phones = phoneNumbersList() ?: emptyList() val phones = phoneNumbersList().orEmpty()
copy( copy(
emailList = Success( emailList = Success(
currentMails.map { currentMails.map {

View file

@ -123,12 +123,12 @@ class HomeDetailFragment @Inject constructor(
?.navigator ?.navigator
?.requestSessionVerification(requireContext(), newest.deviceId ?: "") ?.requestSessionVerification(requireContext(), newest.deviceId ?: "")
unknownDeviceDetectorSharedViewModel.handle( unknownDeviceDetectorSharedViewModel.handle(
UnknownDeviceDetectorSharedViewModel.Action.IgnoreDevice(newest.deviceId?.let { listOf(it) } ?: emptyList()) UnknownDeviceDetectorSharedViewModel.Action.IgnoreDevice(newest.deviceId?.let { listOf(it) }.orEmpty())
) )
} }
dismissedAction = Runnable { dismissedAction = Runnable {
unknownDeviceDetectorSharedViewModel.handle( unknownDeviceDetectorSharedViewModel.handle(
UnknownDeviceDetectorSharedViewModel.Action.IgnoreDevice(newest.deviceId?.let { listOf(it) } ?: emptyList()) UnknownDeviceDetectorSharedViewModel.Action.IgnoreDevice(newest.deviceId?.let { listOf(it) }.orEmpty())
) )
} }
} }

View file

@ -382,7 +382,7 @@ class VectorPreferences @Inject constructor(private val context: Context) {
fun getUnknownDeviceDismissedList(): List<String> { fun getUnknownDeviceDismissedList(): List<String> {
return tryThis { return tryThis {
defaultPrefs.getStringSet(SETTINGS_UNKNOWN_DEVICE_DISMISSED_LIST, null)?.toList() defaultPrefs.getStringSet(SETTINGS_UNKNOWN_DEVICE_DISMISSED_LIST, null)?.toList()
} ?: emptyList() }.orEmpty()
} }
/** /**

View file

@ -415,7 +415,7 @@ class VectorSettingsSecurityPrivacyFragment @Inject constructor(
session.cryptoService().fetchDevicesList(object : MatrixCallback<DevicesListResponse> { session.cryptoService().fetchDevicesList(object : MatrixCallback<DevicesListResponse> {
override fun onSuccess(data: DevicesListResponse) { override fun onSuccess(data: DevicesListResponse) {
if (isAdded) { if (isAdded) {
refreshCryptographyPreference(data.devices ?: emptyList()) refreshCryptographyPreference(data.devices.orEmpty())
} }
} }