Skip to content

Commit

Permalink
Add Google signin
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Apr 11, 2024
1 parent 8917af5 commit 721e88e
Show file tree
Hide file tree
Showing 23 changed files with 510 additions and 496 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fr.paug.androidmakers
import android.app.Application
import com.androidmakers.di.viewModelModule
import fr.androidmakers.di.DependenciesBuilder
import fr.paug.androidmakers.di.androidViewModelModule

class AndroidMakersApplication : Application() {

Expand All @@ -12,9 +11,9 @@ class AndroidMakersApplication : Application() {

DependenciesBuilder(this).inject(
listOf(
androidViewModelModule,
viewModelModule
viewModelModule,
)
)
}
}

30 changes: 15 additions & 15 deletions androidApp/src/main/java/fr/paug/androidmakers/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import com.androidmakers.ui.LocalPlatformContext
import com.androidmakers.ui.MainLayout
import com.androidmakers.ui.common.SigninCallbacks
import com.androidmakers.ui.common.navigation.UserData
import com.androidmakers.ui.theme.AndroidMakersTheme
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
Expand All @@ -29,16 +30,12 @@ import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.auth.GoogleAuthProvider
import dev.gitlive.firebase.auth.auth
import fr.androidmakers.store.firebase.toUser
import fr.paug.androidmakers.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.compose.KoinContext

class MainActivity : AppCompatActivity() {
private val viewModel: MainActivityViewModel by viewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -49,14 +46,12 @@ class MainActivity : AppCompatActivity() {
setContent {
val rememberedActivity = remember { this }

val userState = viewModel.user.collectAsState(null)
val darkTheme = isSystemInDarkTheme()

CompositionLocalProvider(
LocalPlatformContext provides rememberedActivity,
) {
KoinContext {
AndroidMakersTheme {
val darkTheme = isSystemInDarkTheme()
DisposableEffect(darkTheme) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
Expand All @@ -72,9 +67,12 @@ class MainActivity : AppCompatActivity() {
}

MainLayout(
user = userState.value,
versionName = BuildConfig.VERSION_NAME,
versionCode = BuildConfig.VERSION_CODE.toString(),
signinCallbacks = SigninCallbacks(
signin = { signin() },
signout = { signout() },
)
)
}
}
Expand Down Expand Up @@ -114,7 +112,9 @@ class MainActivity : AppCompatActivity() {
val result = auth.signInWithCredential(firebaseCredential)
// Sign in success, update UI with the signed-in user's information
lifecycleScope.launch {
viewModel.setUser(result.user?.toUser())
UserData().userRepository.setUser(result.user?.toUser())
println("user id=${result.user?.uid}")
println("idToken=${result.user?.getIdToken(true)}")
}
}
}
Expand All @@ -124,7 +124,7 @@ class MainActivity : AppCompatActivity() {
} catch (e: ApiException) {
e.printStackTrace()
lifecycleScope.launch {
viewModel.setUser(null)
UserData().userRepository.setUser(null)
}
}
}
Expand All @@ -134,22 +134,22 @@ class MainActivity : AppCompatActivity() {
fun signout() {
val activity = this
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(activity.getString(R.string.default_web_client_id))
.requestIdToken("985196411897-r7edbi9jgo3hfupekcmdrg66inonj0o5.apps.googleusercontent.com")
.build()
val googleSignInClient = GoogleSignIn.getClient(activity, gso)

lifecycleScope.launch {
Firebase.auth.signOut()
googleSignInClient.signOut()
googleSignInClient.revokeAccess()
viewModel.setUser(null)
UserData().userRepository.setUser(null)
}
}

fun signin() {
val activity = this
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(activity.getString(R.string.default_web_client_id))
.requestIdToken("985196411897-r7edbi9jgo3hfupekcmdrg66inonj0o5.apps.googleusercontent.com")
.build()
val googleSignInClient = GoogleSignIn.getClient(activity, gso)

Expand All @@ -159,5 +159,5 @@ class MainActivity : AppCompatActivity() {
companion object {
const val REQ_SIGNIN = 33
}

}

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ datastore = "1.1.0-beta02"
skie = "0.6.2"
firebase = "1.11.1"
koin = "3.5.3"
precompose = "1.6.0-rc02"
precompose = "1.6.0"
compose-plugin = "1.6.1"
image-loader = "1.7.8"
pullrefresh = "1.3.0"
Expand Down Expand Up @@ -88,7 +88,7 @@ android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", ver
firebase-crashlytics-gradlePlugin = { group = "com.google.firebase", name = "firebase-crashlytics-gradle", version.ref = "crashlytics-plugin" }
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
materii-pullrefresh = { group = "dev.materii.pullrefresh", name = "pullrefresh", version.ref = "pullrefresh" }

okio = "com.squareup.okio:okio:3.9.0"
# Wear dependencies
play-services-wearable = { group = "com.google.android.gms", name = "play-services-wearable", version.ref = "playServicesWearable" }
androidx-splashscreen = "androidx.core:core-splashscreen:1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion shared/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ apollo {
service("service") {
packageName.set("fr.androidmakers.store.graphql")
generateDataBuilders.set(true)
mapScalar("LocalDateTime", "kotlinx.datetime.LocalDateTime", "com.apollographql.apollo3.adapter.KotlinxLocalDateTimeAdapter")
mapScalar("GraphQLLocalDateTime", "kotlinx.datetime.LocalDateTime", "com.apollographql.apollo3.adapter.KotlinxLocalDateTimeAdapter")

introspection {
schemaFile.set(file("src/commonMain/graphql/schema.graphqls"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.androidmakers.store.graphql

import android.content.Context
import android.service.autofill.UserData
import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.api.http.HttpRequest
import com.apollographql.apollo3.api.http.HttpResponse
Expand All @@ -11,11 +12,15 @@ import com.apollographql.apollo3.network.http.HttpInterceptor
import com.apollographql.apollo3.network.http.HttpInterceptorChain
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import fr.androidmakers.domain.repo.UserRepository
import fr.androidmakers.store.firebase.FirebaseUserRepository
import java.util.PrimitiveIterator

actual class ApolloClientBuilder(
context: Context,
private val url: String,
private val conference: String
context: Context,
private val url: String,
private val conference: String,
private val userRepository: UserRepository,
) {
private val memoryCacheFactory = MemoryCacheFactory(20_000_000).chain(SqlNormalizedCacheFactory(context))
actual fun build(): ApolloClient {
Expand All @@ -30,10 +35,10 @@ actual class ApolloClientBuilder(
/**
*
*/
// val token = Firebase.auth.currentUser?.getIdToken(false)?.result?.token
// if (token != null) {
// addHeader("Authorization", "Bearer $token")
// }
val token = userRepository.getUser()?.idToken
if (token != null) {
addHeader("Authorization", "Bearer $token")
}
}
.build()
)
Expand Down
15 changes: 10 additions & 5 deletions shared/data/src/commonMain/graphql/bookmarks.graphql
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
query Bookmarks {
bookmarks {
id
sessionIds
bookmarkConnection {
nodes {
id
}
}
}

mutation AddBookmark($sessionId: String!) {
addBookmark(sessionId: $sessionId) {
sessionIds
nodes {
id
}
}
}

mutation RemoveBookmark($sessionId: String!) {
removeBookmark(sessionId: $sessionId) {
sessionIds
nodes {
id
}
}
}
2 changes: 1 addition & 1 deletion shared/data/src/commonMain/graphql/extra.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extend type Room @typePolicy(keyFields: "id")

extend type Session @typePolicy(keyFields: "id")
extend type Query @fieldPolicy(forField: "session", keyArgs: "id")
extend type RootQuery @fieldPolicy(forField: "session", keyArgs: "id")

extend type Speaker @typePolicy(keyFields: "id")

Expand Down
Loading

0 comments on commit 721e88e

Please sign in to comment.