From 1037c35dfcc37cd7dcfbd37f4c4e6464d15241e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=9C=D0=B8=D0=BD=D0=B3=D0=B5=D1=80?= Date: Sun, 23 Mar 2025 19:40:13 +0600 Subject: [PATCH] [~]Fix data saving on profile screen --- .../data/repository/ProfileRepositoryImpl.kt | 51 ++++++++++++------- .../elevator/expects/PlatformExpects.ios.kt | 3 +- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/band/effective/office/elevator/data/repository/ProfileRepositoryImpl.kt b/composeApp/src/commonMain/kotlin/band/effective/office/elevator/data/repository/ProfileRepositoryImpl.kt index 864b0391..2f9f0860 100644 --- a/composeApp/src/commonMain/kotlin/band/effective/office/elevator/data/repository/ProfileRepositoryImpl.kt +++ b/composeApp/src/commonMain/kotlin/band/effective/office/elevator/data/repository/ProfileRepositoryImpl.kt @@ -13,8 +13,11 @@ import band.effective.office.network.api.Api import band.effective.office.network.dto.UserDTO import band.effective.office.network.model.Either import band.effective.office.network.model.ErrorResponse +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.update import org.koin.core.component.KoinComponent @@ -28,6 +31,13 @@ class ProfileRepositoryImpl( private val idPhoneNumber = OfficeElevatorConfig.integrationPhoneId private val idTelegram = OfficeElevatorConfig.integrationTelegramId + private val updateUserFlow: MutableSharedFlow = + MutableSharedFlow(replay = 1, extraBufferCapacity = 1) + + init { + updateUserFlow.tryEmit(Unit) + } + private val lastResponse: MutableStateFlow, User>> = MutableStateFlow( Either.Error( @@ -53,32 +63,35 @@ class ProfileRepositoryImpl( lastResponse.update { requestResult } } val dateForEmit = bdSource.getCurrentUserInfo().packageEither(requestResult) + updateUserFlow.tryEmit(Unit) emit(dateForEmit) } //TODO(Artem Gruzdev) maybe can easier this method - override suspend fun getUser(): Flow, User>> = flow { - - val cashedUser = bdSource.getCurrentUserInfo() - if (cashedUser == null) { - emit( - Either.Error( - ErrorWithData( - error = ErrorResponse(code = 404, description = "you don`t login"), - saveData = null + @OptIn(ExperimentalCoroutinesApi::class) + override suspend fun getUser(): Flow, User>> = updateUserFlow.flatMapLatest { + flow { + val cashedUser = bdSource.getCurrentUserInfo() + if (cashedUser == null) { + emit( + Either.Error( + ErrorWithData( + error = ErrorResponse(code = 404, description = "you don`t login"), + saveData = null + ) ) ) - ) - } else { - val requestResult = api.getUser(cashedUser.id).convert(lastResponse.value) - val userFromApi = requestResult.getData() - if (userFromApi != null && userFromApi != cashedUser) { - bdSource.update(userFromApi) - lastResponse.update { requestResult } - } + } else { + val requestResult = api.getUser(cashedUser.id).convert(lastResponse.value) + val userFromApi = requestResult.getData() + if (userFromApi != null && userFromApi != cashedUser) { + bdSource.update(userFromApi) + lastResponse.update { requestResult } + } - val dateForEmit = bdSource.getCurrentUserInfo().packageEither(requestResult) - emit(dateForEmit) + val dateForEmit = bdSource.getCurrentUserInfo().packageEither(requestResult) + emit(dateForEmit) + } } } diff --git a/composeApp/src/iosMain/kotlin/band/effective/office/elevator/expects/PlatformExpects.ios.kt b/composeApp/src/iosMain/kotlin/band/effective/office/elevator/expects/PlatformExpects.ios.kt index 3bea6204..9f32b874 100644 --- a/composeApp/src/iosMain/kotlin/band/effective/office/elevator/expects/PlatformExpects.ios.kt +++ b/composeApp/src/iosMain/kotlin/band/effective/office/elevator/expects/PlatformExpects.ios.kt @@ -4,8 +4,7 @@ import platform.UIKit.UIPasteboard import dev.icerock.moko.resources.StringResource import dev.icerock.moko.resources.desc.desc -actual fun setClipboardText(text: String, label: String, toastMessage: StringResource) { +actual fun setClipboardText(text: String, label: String) { UIPasteboard.generalPasteboard.string = text - showToast(toastMessage.desc().localized()) } -- GitLab