Skip to content

Commit

Permalink
Removes viewBinding.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed May 10, 2022
1 parent e16f3be commit bcfd21c
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 174 deletions.
7 changes: 0 additions & 7 deletions financial-connections/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ dependencies {
ktlint "com.pinterest:ktlint:$ktlintVersion"
}

android {
buildFeatures {
viewBinding true
}
}


ext {
artifactId = "financial-connections"
artifactName = "financial-connections"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".FinancialConnectionsSheetActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>
tools:context=".FinancialConnectionsSheetActivity" />
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand All @@ -8,6 +8,7 @@
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
app:indicatorColor="@color/stripe_toolbar_color_default"
app:indicatorSize="@dimen/stripe_connectionssheet_loading_indicator_size"
Expand All @@ -17,4 +18,4 @@
app:layout_constraintTop_toTopOf="parent"
app:trackThickness="@dimen/stripe_connectionssheet_loading_indicator_stroke_width" />

</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,24 @@ package com.stripe.android.financialconnections

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.activity.viewModels
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.InternalMavericksApi
import com.airbnb.mvrx.Mavericks
import com.airbnb.mvrx.MavericksView
import com.airbnb.mvrx.MavericksViewModelProvider
import com.airbnb.mvrx.asMavericksArgs
import com.airbnb.mvrx.viewModel
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenAuthFlowWithUrl
import com.stripe.android.financialconnections.databinding.ActivityFinancialconnectionsSheetBinding
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityArgs
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityResult
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityResult.Canceled
import com.stripe.android.financialconnections.presentation.CreateBrowserIntentForUrl
import java.security.InvalidParameterException

internal class FinancialConnectionsSheetActivity : AppCompatActivity() {
internal class FinancialConnectionsSheetActivity :
AppCompatActivity(R.layout.activity_financialconnections_sheet) {

@VisibleForTesting
internal val viewBinding by lazy {
ActivityFinancialconnectionsSheetBinding.inflate(layoutInflater)
}

val viewModel: FinancialConnectionsSheetViewModel by viewModel()
val viewModel: FinancialConnectionsSheetViewModel by viewModel()

private val starterArgs: FinancialConnectionsSheetActivityArgs? by lazy {
FinancialConnectionsSheetActivityArgs.fromIntent(intent)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)
if (savedInstanceState == null) addFragment()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@ import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.fragment.app.Fragment
import androidx.activity.result.contract.ActivityResultContracts
import androidx.viewbinding.ViewBinding
import androidx.fragment.app.Fragment
import com.airbnb.mvrx.MavericksView
import com.airbnb.mvrx.UniqueOnly
import com.airbnb.mvrx.activityViewModel
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.FinishWithResult
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenAuthFlowWithUrl
import com.stripe.android.financialconnections.databinding.FragmentFinancialConnectionsSheetBinding
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityResult
import com.stripe.android.financialconnections.presentation.CreateBrowserIntentForUrl

class FinancialConnectionsSheetFragment : Fragment(), MavericksView {
class FinancialConnectionsSheetFragment :
Fragment(R.layout.fragment_financial_connections_sheet), MavericksView {

private val startForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
viewModel.onActivityResult()
}

private val viewBinding: FragmentFinancialConnectionsSheetBinding by viewBinding()
private val viewModel: FinancialConnectionsSheetViewModel by activityViewModel()

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -37,7 +33,7 @@ class FinancialConnectionsSheetFragment : Fragment(), MavericksView {

private fun observeAsyncs() {
viewModel.onAsync(
asyncProp = FinancialConnectionsSheetState::sideEffect,
asyncProp = FinancialConnectionsSheetState::viewEffect,
deliveryMode = UniqueOnly(subscriptionId = "financial-connections-view-effect"),
onSuccess = { viewEffect ->
when (viewEffect) {
Expand All @@ -48,6 +44,9 @@ class FinancialConnectionsSheetFragment : Fragment(), MavericksView {
)
}

/**
* handle state changes here.
*/
override fun invalidate() = Unit

private fun OpenAuthFlowWithUrl.launch() {
Expand All @@ -74,13 +73,3 @@ class FinancialConnectionsSheetFragment : Fragment(), MavericksView {
}
}


/**
* Create bindings for a view similar to bindView.
*
* To use, just call
* private val binding: FHomeWorkoutDetailsBinding by viewBinding()
* with your binding class and access it as you normally would.
*/
inline fun <reified T : ViewBinding> Fragment.viewBinding() =
FragmentViewBindingDelegate(T::class.java, this)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.stripe.android.financialconnections

import androidx.lifecycle.SavedStateHandle
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.PersistState
import com.airbnb.mvrx.Uninitialized
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityArgs
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityResult
Expand All @@ -15,42 +15,17 @@ import com.stripe.android.financialconnections.model.FinancialConnectionsSession
internal data class FinancialConnectionsSheetState(
val initialArgs: FinancialConnectionsSheetActivityArgs,
val activityRecreated: Boolean = false,
val manifest: FinancialConnectionsSessionManifest? = null,
val authFlowActive: Boolean = false,
val sideEffect: Async<FinancialConnectionsSheetViewEffect> = Uninitialized
@PersistState val manifest: FinancialConnectionsSessionManifest? = null,
@PersistState val authFlowActive: Boolean = false,
val viewEffect: Async<FinancialConnectionsSheetViewEffect> = Uninitialized
) : MavericksState {

/**
* Constructor used by Mavericks to build the initial state.
*/
constructor(args: FinancialConnectionsSheetActivityArgs) : this(
initialArgs = args
)

/**
* Restores existing persisted fields into the current [FinancialConnectionsSheetState]
*/
internal fun from(savedStateHandle: SavedStateHandle): FinancialConnectionsSheetState {
return copy(
manifest = savedStateHandle.get(KEY_MANIFEST) ?: manifest,
authFlowActive = savedStateHandle.get(KEY_AUTHFLOW_ACTIVE) ?: authFlowActive,
)
}

/**
* Saves the persistable fields of this state that changed to the given [SavedStateHandle]
*/
internal fun to(
savedStateHandle: SavedStateHandle,
previousValue: FinancialConnectionsSheetState
) {
if (previousValue.manifest != manifest)
savedStateHandle.set(KEY_MANIFEST, manifest)
if (previousValue.authFlowActive != authFlowActive)
savedStateHandle.set(KEY_AUTHFLOW_ACTIVE, authFlowActive)
}

companion object {
private const val KEY_MANIFEST = "key_manifest"
private const val KEY_AUTHFLOW_ACTIVE = "key_authflow_active"
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
private val fetchFinancialConnectionsSession: FetchFinancialConnectionsSession,
private val fetchFinancialConnectionsSessionForToken: FetchFinancialConnectionsSessionForToken,
private val eventReporter: FinancialConnectionsEventReporter,
private val initialState: FinancialConnectionsSheetState
initialState: FinancialConnectionsSheetState
) : MavericksViewModel<FinancialConnectionsSheetState>(initialState) {

init {
Expand Down Expand Up @@ -68,13 +68,13 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
* @param manifest the manifest containing the hosted auth flow URL to launch
*
*/
private suspend fun openAuthFlow(manifest: FinancialConnectionsSessionManifest) {
private fun openAuthFlow(manifest: FinancialConnectionsSessionManifest) {
// stores manifest in state for future references.
setState {
copy(
manifest = manifest,
authFlowActive = true,
sideEffect = Success(OpenAuthFlowWithUrl(manifest.hostedAuthUrl))
viewEffect = Success(OpenAuthFlowWithUrl(manifest.hostedAuthUrl))
)
}
}
Expand Down Expand Up @@ -111,9 +111,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
internal fun onResume() {
setState {
if (authFlowActive && activityRecreated.not()) {
copy(
sideEffect = Success(FinishWithResult(Canceled))
)
copy(viewEffect = Success(FinishWithResult(Canceled)))
} else this
}
}
Expand All @@ -126,9 +124,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
internal fun onActivityResult() {
setState {
if (authFlowActive && activityRecreated) {
copy(
sideEffect = Success(FinishWithResult(Canceled))
)
copy(viewEffect = Success(FinishWithResult(Canceled)))
} else this
}
}
Expand All @@ -147,7 +143,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
}.onSuccess {
val result = FinancialConnectionsSheetActivityResult.Completed(it)
eventReporter.onResult(starterArgs.configuration, result)
setState { copy(sideEffect = Success(FinishWithResult(result))) }
setState { copy(viewEffect = Success(FinishWithResult(result))) }
}.onFailure {
onFatal(it)
}
Expand All @@ -170,7 +166,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
}.onSuccess { (las, token) ->
val result = FinancialConnectionsSheetActivityResult.Completed(las, token)
eventReporter.onResult(starterArgs.configuration, result)
setState { copy(sideEffect = Success(FinishWithResult(result))) }
setState { copy(viewEffect = Success(FinishWithResult(result))) }
}.onFailure {
onFatal(it)
}
Expand All @@ -186,7 +182,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
private suspend fun onFatal(throwable: Throwable) {
val result = FinancialConnectionsSheetActivityResult.Failed(throwable)
eventReporter.onResult(starterArgs.configuration, result)
setState { copy(sideEffect = Success(FinishWithResult(result))) }
setState { copy(viewEffect = Success(FinishWithResult(result))) }
}

/**
Expand All @@ -197,7 +193,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
private suspend fun onUserCancel() {
val result = Canceled
eventReporter.onResult(starterArgs.configuration, result)
setState { copy(sideEffect = Success(FinishWithResult(result))) }
setState { copy(viewEffect = Success(FinishWithResult(result))) }
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.financialconnections.launcher

import android.content.Intent
import android.os.Parcelable
import com.airbnb.mvrx.Mavericks
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import kotlinx.parcelize.Parcelize
import java.security.InvalidParameterException
Expand Down Expand Up @@ -39,11 +40,8 @@ internal sealed class FinancialConnectionsSheetActivityArgs constructor(
}

companion object {
const val EXTRA_ARGS =
"com.stripe.android.financialconnections.ConnectionsSheetContract.extra_args"

internal fun fromIntent(intent: Intent): FinancialConnectionsSheetActivityArgs? {
return intent.getParcelableExtra(EXTRA_ARGS)
return intent.getParcelableExtra(Mavericks.KEY_ARG)
}
}
}
Loading

0 comments on commit bcfd21c

Please sign in to comment.