Skip to content

Commit

Permalink
Add CompositionLocals for Firebase auth user data
Browse files Browse the repository at this point in the history
See #368 and #585

* Scaffold `:core:auth:compose` module
* Include module in settings script
* Add composable to provide CompositionLocals
  • Loading branch information
EdricChan03 committed Apr 16, 2024
1 parent 5d8de33 commit ae690e0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/auth/compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
43 changes: 43 additions & 0 deletions core/auth/compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
plugins {
com.edricchan.studybuddy.library.`android-compose`
}

android {
namespace = "com.edricchan.studybuddy.core.auth"

defaultConfig {
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}

dependencies {
implementation(projects.ui.theming.compose)

// Firebase dependencies
api(platform(libs.firebase.bom))
api(libs.firebase.auth.ktx)
implementation(libs.playServices.auth)

// Compose dependencies
implementation(libs.bundles.androidx.compose)
implementation(libs.androidx.lifecycle.runtime.compose)

debugImplementation(libs.bundles.androidx.compose.tooling)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext)
androidTestImplementation(libs.androidx.test.espresso.core)

// Compose rule support
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
}
Empty file.
21 changes: 21 additions & 0 deletions core/auth/compose/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions core/auth/compose/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.edricchan.studybuddy.core.auth

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocal
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.firebase.Firebase
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.auth
import kotlinx.coroutines.flow.Flow

/** [CompositionLocal] of the currently signed-in [FirebaseUser], or `null`. */
val LocalCurrentUser: ProvidableCompositionLocal<FirebaseUser?> =
compositionLocalOf { Firebase.auth.currentUser }

/** [CompositionLocal] of whether there's a currently signed-in [FirebaseUser]. */
val LocalIsSignedIn: ProvidableCompositionLocal<Boolean> =
compositionLocalOf { Firebase.auth.currentUser != null }

/**
* Provides the [LocalCurrentUser] and [LocalIsSignedIn] composition locals with the
* given [userFlow].
*
* The [userFlow] will be collected as a state and used for [LocalCurrentUser].
*/
@Composable
fun ProvideCurrentUser(
initialUser: FirebaseUser? = Firebase.auth.currentUser,
userFlow: Flow<FirebaseUser?>,
content: @Composable () -> Unit
) {
val user by userFlow.collectAsStateWithLifecycle(initialValue = initialUser)

CompositionLocalProvider(
LocalCurrentUser provides user,
LocalIsSignedIn provides (user != null),
content = content
)
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ include(
":ui:widgets:compose:navigation",
":ui:widgets:compose:option-bottom-sheet",
":ui:widgets:modal-bottom-sheet",
":core:auth:compose",
":core:auth:gms",
":core:deeplink",
":core:di",
Expand Down

0 comments on commit ae690e0

Please sign in to comment.