Skip to content

Instantly share code, notes, and snippets.

@eduardb
Last active April 1, 2024 18:31
Show Gist options
  • Save eduardb/e5ee5045427787263b69eb2b4cd5f907 to your computer and use it in GitHub Desktop.
Save eduardb/e5ee5045427787263b69eb2b4cd5f907 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
@Suppress("UNCHECKED_CAST")
public fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
flow: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
flow5: Flow<T5>,
flow6: Flow<T6>,
flow7: Flow<T7>,
transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R
): Flow<R> =
combine(flow, flow2, flow3, flow4, flow5, flow6, flow7) { args: Array<*> ->
transform(
args[0] as T1,
args[1] as T2,
args[2] as T3,
args[3] as T4,
args[4] as T5,
args[5] as T6,
args[6] as T7,
)
}
@Suppress("UNCHECKED_CAST")
public fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combine(
flow: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
flow5: Flow<T5>,
flow6: Flow<T6>,
flow7: Flow<T7>,
flow8: Flow<T8>,
transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8) -> R
): Flow<R> =
combine(flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8) { args: Array<*> ->
transform(
args[0] as T1,
args[1] as T2,
args[2] as T3,
args[3] as T4,
args[4] as T5,
args[5] as T6,
args[6] as T7,
args[7] as T8,
)
}
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.stateIn
class MyViewModel : ViewModel() {
private val flow1: MutableStateFlow<Any> = MutableStateFlow(Any())
private val flow2: Flow<Any> = flowOf(Any())
private val flow3: MutableStateFlow<Any> = MutableStateFlow(Any())
// ... 14 more flows
private val flow17: MutableStateFlow<Any> = MutableStateFlow(Any())
val uiState =
combine(
flow1,
flow2,
flow3,
// ... 14 more flows
flow17,
) { f1, f2, f3, f17 ->
UiState(f1, f2, f3, f17)
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), UiState())
}
class MyActivity : AppCompatActivity() {
private val viewModel: MyViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
MyScreen(uiState)
}
}
fun restartActivity() {
val intent = Intent(this, MyActivity::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
}
}
data class UiState(
val f1: Any = Any(),
val f2: Any = Any(),
val f3: Any = Any(),
val f17: Any = Any(),
)
@Composable
fun MyScreen(uiState: UiState) {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment