Skip to content

Commit

Permalink
Nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed Mar 21, 2024
1 parent 35bcd35 commit 0f39b41
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.stripe.android.financialconnections.core

import android.annotation.SuppressLint
import android.content.Context
import android.content.ContextWrapper
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import com.stripe.android.core.utils.ContextUtils.extractActivityFromContext
import com.stripe.android.financialconnections.di.FinancialConnectionsSheetNativeComponent
import com.stripe.android.financialconnections.ui.FinancialConnectionsSheetNativeActivity
import kotlinx.coroutines.flow.distinctUntilChanged
Expand All @@ -34,21 +32,6 @@ internal fun parentActivity(): FinancialConnectionsSheetNativeActivity {
return extractActivityFromContext(LocalContext.current) as FinancialConnectionsSheetNativeActivity
}

private fun extractActivityFromContext(context: Context): ComponentActivity? {
var currentContext = context
if (currentContext is ComponentActivity) {
return currentContext
} else {
while (currentContext is ContextWrapper) {
if (currentContext is ComponentActivity) {
return currentContext
}
currentContext = currentContext.baseContext
}
}
return null
}

/**
* Creates a Compose State variable that will only update when the value of this property changes.
* Prefer this to subscribing to entire state classes which will trigger a recomposition whenever
Expand All @@ -57,9 +40,9 @@ private fun extractActivityFromContext(context: Context): ComponentActivity? {
* If you find yourself subscribing to many state properties in a single composable,
* consider breaking it up into smaller ones.
*/
@SuppressLint("StateFlowValueCalledInComposition")
@Composable
internal fun <VM : FinancialConnectionsViewModel<S>, S, A> VM.collectAsState(prop1: KProperty1<S, A>): State<A> {
val mappedFlow = remember(prop1) { stateFlow.map { prop1.get(it) }.distinctUntilChanged() }
return mappedFlow.collectAsState(initial = prop1.get(stateFlow.value))
val initialValue = remember { stateFlow.value }
return mappedFlow.collectAsState(initial = prop1.get(initialValue))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import com.stripe.android.financialconnections.core.Result.Loading
import com.stripe.android.financialconnections.core.Result.Success
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

internal abstract class FinancialConnectionsViewModel<S>(
initialState: S
) : ViewModel() {

val stateFlow: MutableStateFlow<S> = MutableStateFlow(initialState)
private val _stateFlow: MutableStateFlow<S> = MutableStateFlow(initialState)
val stateFlow: StateFlow<S> = _stateFlow


protected open fun <T : Any?> (suspend () -> T).execute(
reducer: S.(Result<T>) -> S,
Expand All @@ -38,9 +41,7 @@ internal abstract class FinancialConnectionsViewModel<S>(
}
}

protected fun setState(reducer: S.() -> S) {
stateFlow.update { state -> state.reducer() }
}
protected fun setState(reducer: S.() -> S) = _stateFlow.update(reducer)
}

internal sealed class Result<out T>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.stripe.android.core.utils

import android.content.Context
import android.content.ContextWrapper
import android.content.pm.PackageInfo
import androidx.annotation.RestrictTo
import androidx.core.app.ComponentActivity

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
object ContextUtils {
Expand All @@ -11,4 +13,19 @@ object ContextUtils {
get() = runCatching {
packageManager.getPackageInfo(packageName, 0)
}.getOrNull()

fun extractActivityFromContext(context: Context): ComponentActivity? {
var currentContext = context
if (currentContext is ComponentActivity) {
return currentContext
} else {
while (currentContext is ContextWrapper) {
if (currentContext is ComponentActivity) {
return currentContext
}
currentContext = currentContext.baseContext
}
}
return null
}
}

0 comments on commit 0f39b41

Please sign in to comment.