-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from theshiftstudio/tudor/account-flow
Having fun with FirebaseAuthUI, Workflows & Compose. YAY!
- Loading branch information
Showing
31 changed files
with
1,032 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
app/src/main/java/com/shiftstudio/workflowshenanigans/MainActivity.kt
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/shiftstudio/workflowshenanigans/ShenanigansModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.shiftstudio.workflowshenanigans | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class ShenanigansModule { | ||
|
||
@Binds | ||
abstract fun bindShenanigansWorkflow(impl: ShenanigansWorkflowImpl): ShenanigansWorkflow | ||
} |
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/shiftstudio/workflowshenanigans/ShenanigansWorkflow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
@file:OptIn(WorkflowUiExperimentalApi::class) | ||
|
||
package com.shiftstudio.workflowshenanigans | ||
|
||
import androidx.activity.ComponentActivity | ||
import com.shiftstudio.workflowshenanigans.ShenanigansWorkflow.ActivityAndProps | ||
import com.shiftstudio.workflowshenanigans.account.AccountViewRegistry | ||
import com.shiftstudio.workflowshenanigans.account.AccountWorkflow | ||
import com.shiftstudio.workflowshenanigans.login.LoginViewRegistry | ||
import com.shiftstudio.workflowshenanigans.welcome.WelcomeWorkflow | ||
import com.squareup.workflow1.Snapshot | ||
import com.squareup.workflow1.StatefulWorkflow | ||
import com.squareup.workflow1.Workflow | ||
import com.squareup.workflow1.action | ||
import com.squareup.workflow1.parse | ||
import com.squareup.workflow1.ui.NamedViewFactory | ||
import com.squareup.workflow1.ui.ViewRegistry | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.backstack.BackStackContainer | ||
import com.squareup.workflow1.ui.backstack.BackStackScreen | ||
import com.squareup.workflow1.ui.backstack.toBackStackScreen | ||
import com.squareup.workflow1.ui.plus | ||
import javax.inject.Inject | ||
|
||
interface ShenanigansWorkflow : Workflow<ActivityAndProps<Unit>, Nothing, BackStackScreen<Any>> { | ||
|
||
data class ActivityAndProps<R>( | ||
val activity: ComponentActivity, | ||
val props: R | ||
) | ||
} | ||
|
||
val ShenanigansViewRegistry: ViewRegistry = ViewRegistry(BackStackContainer, NamedViewFactory) + | ||
AccountViewRegistry + | ||
LoginViewRegistry | ||
|
||
class ShenanigansWorkflowImpl @Inject constructor( | ||
private val welcomeWorkflow: WelcomeWorkflow, | ||
private val accountWorkflow: AccountWorkflow, | ||
) : ShenanigansWorkflow, | ||
StatefulWorkflow<ActivityAndProps<Unit>, ShenanigansWorkflowImpl.State, Nothing, BackStackScreen<Any>>() { | ||
|
||
sealed class State { | ||
object Welcome : State() | ||
|
||
object Account : State() | ||
} | ||
|
||
override fun initialState(props: ActivityAndProps<Unit>, snapshot: Snapshot?): State { | ||
return snapshot?.bytes | ||
?.parse { source -> if (source.readInt() == 0) State.Welcome else State.Account } | ||
?: State.Welcome | ||
} | ||
|
||
override fun render( | ||
renderProps: ActivityAndProps<Unit>, | ||
renderState: State, | ||
context: RenderContext | ||
): BackStackScreen<Any> { | ||
val backStack = mutableListOf<Any>() | ||
|
||
val welcomeScreen = context.renderChild(welcomeWorkflow, Unit) { output -> | ||
handleWelcomeOutput(output) | ||
} | ||
backStack += welcomeScreen | ||
|
||
when (renderState) { | ||
is State.Welcome -> { | ||
// We always add the welcome screen to the backstack, so this is a no op. | ||
} | ||
is State.Account -> { | ||
val accountScreen = context.renderChild(accountWorkflow, renderProps) { | ||
goToWelcome() | ||
} | ||
backStack += accountScreen | ||
} | ||
} | ||
|
||
return backStack.toBackStackScreen() | ||
} | ||
|
||
private fun handleWelcomeOutput(output: WelcomeWorkflow.Output) = action { | ||
when (output) { | ||
is WelcomeWorkflow.Output.GoToAccount -> { | ||
state = State.Account | ||
} | ||
} | ||
} | ||
|
||
private fun goToWelcome() = action { | ||
state = State.Welcome | ||
} | ||
|
||
override fun snapshotState(state: State): Snapshot { | ||
return Snapshot.of(if (state == State.Welcome) 0 else 1) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/shiftstudio/workflowshenanigans/account/AccountModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.shiftstudio.workflowshenanigans.account | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class AccountModule { | ||
|
||
@Binds | ||
abstract fun bindAccountWorkflow(impl: AccountWorkflowImpl): AccountWorkflow | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/shiftstudio/workflowshenanigans/account/AccountScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@file:OptIn(WorkflowUiExperimentalApi::class) | ||
|
||
package com.shiftstudio.workflowshenanigans.account | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Surface | ||
import androidx.compose.material.Text | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import com.squareup.workflow1.ui.ViewRegistry | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.compose.composeViewFactory | ||
|
||
private val LoadingUserBinding = composeViewFactory<LoadingUserRendering> { _, _ -> | ||
Surface(color = MaterialTheme.colors.background) { | ||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
} | ||
} | ||
|
||
private val UserRenderingBinding = composeViewFactory<UserRendering> { rendering, _ -> | ||
Column(horizontalAlignment = Alignment.CenterHorizontally) { | ||
Text(text = rendering.user.name) | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
Button(onClick = { rendering.onSignOut() }) { | ||
Text(text = "Sign out") | ||
} | ||
} | ||
} | ||
|
||
val AccountViewRegistry = ViewRegistry(LoadingUserBinding, UserRenderingBinding) |
Oops, something went wrong.