Skip to content

Commit

Permalink
refactor: combineする処理をクラスに切り出した
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Jul 16, 2023
1 parent f31c2d7 commit 434646e
Showing 1 changed file with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -86,25 +87,11 @@ class UserDetailViewModel @Inject constructor(
private val _errors = MutableSharedFlow<Throwable>(extraBufferCapacity = 100)
val errors = _errors.asSharedFlow()

private val userProfileArgType = combine(
private val userProfileArgType = UserProfileArgTypeCombiner(viewModelScope).create(
userId,
fqdnUserName,
currentAccount,
) { userId, fqdnUserName, currentAccount ->
when {
userId != null && currentAccount != null -> {
UserProfileArgType.UserId(User.Id(currentAccount.accountId, userId))
}

fqdnUserName != null && currentAccount != null -> {
UserProfileArgType.FqdnUserName(fqdnUserName, currentAccount)
}

else -> {
UserProfileArgType.None
}
}
}.stateIn(viewModelScope, SharingStarted.Lazily, UserProfileArgType.None)
).stateIn(viewModelScope, SharingStarted.Lazily, UserProfileArgType.None)


@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -410,4 +397,38 @@ sealed interface UserProfileArgType {

object None : UserProfileArgType

}

class UserProfileArgTypeCombiner(
private val scope: CoroutineScope,
) {
fun create(
userIdFlow: StateFlow<String?>,
fqdnUserNameFlow: StateFlow<String?>,
currentAccountFlow: StateFlow<Account?>
): StateFlow<UserProfileArgType> {
return combine(
userIdFlow,
fqdnUserNameFlow,
currentAccountFlow,
) { userId, fqdnUserName, currentAccount ->
when {
userId != null && currentAccount != null -> {
UserProfileArgType.UserId(User.Id(currentAccount.accountId, userId))
}

fqdnUserName != null && currentAccount != null -> {
UserProfileArgType.FqdnUserName(fqdnUserName, currentAccount)
}

else -> {
UserProfileArgType.None
}
}
}.stateIn(
scope,
SharingStarted.WhileSubscribed(5_000),
UserProfileArgType.None
)
}
}

0 comments on commit 434646e

Please sign in to comment.