Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate signin with GoogleSignInFacade #5

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ dependencies {
testImplementation(libs.junit)
implementation(libs.compose.navigation)
implementation(libs.koin.compose)
implementation(libs.google.services)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package co.touchlab.kampkit.android

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import co.touchlab.kampkit.ui.signin.GoogleSignInData
import co.touchlab.kampkit.ui.signin.GoogleSignInFacade
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException

class AndroidGoogleSignInFacade(private val context: Context) : GoogleSignInFacade {

override fun login(onComplete: (GoogleSignInData) -> Unit) {
val startActivity = context.getActivity()
?.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val data = completeGoogleSignIn(result)
onComplete(data)
}

val googleSignInIntent = getGoogleSignInIntent(context)
startActivity?.launch(googleSignInIntent)
}

private fun completeGoogleSignIn(activityResult: ActivityResult): GoogleSignInData {
if (activityResult.resultCode != Activity.RESULT_OK) {
return GoogleSignInData(error = "Google SignIn activity error")
}
return try {
val task = GoogleSignIn
.getSignedInAccountFromIntent(activityResult.data)
val account = task.getResult(ApiException::class.java)
GoogleSignInData(email = account.email)
} catch (exception: ApiException) {
GoogleSignInData(error = exception.message)
}
}

private fun getGoogleSignInIntent(context: Context): Intent {
val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.build()
val client = GoogleSignIn.getClient(context, signInOptions)
return client.signInIntent
}
}

fun Context.getActivity(): ComponentActivity? {
return when (this) {
!is ContextWrapper -> return null
is ComponentActivity -> this
else -> baseContext.getActivity()
}
}
6 changes: 6 additions & 0 deletions app/src/main/kotlin/co/touchlab/kampkit/android/MainApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import co.touchlab.kampkit.core.AppInfo
import co.touchlab.kampkit.core.initKoin
import co.touchlab.kampkit.ui.breedDetails.BreedDetailsViewModel
import co.touchlab.kampkit.ui.breeds.BreedsViewModel
import co.touchlab.kampkit.ui.signin.GoogleSignInFacade
import co.touchlab.kampkit.ui.signin.SignInViewModel
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.parameter.parametersOf
import org.koin.dsl.module
Expand All @@ -27,6 +30,9 @@ class MainApp : Application() {
params.get(), get(), get { parametersOf("BreedDetailsViewModel") }
)
}
viewModel { SignInViewModel(get()) }
single<GoogleSignInFacade> { AndroidGoogleSignInFacade(androidContext()) }

single<SharedPreferences> {
get<Context>().getSharedPreferences("KAMPSTARTER_SETTINGS", MODE_PRIVATE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ private const val BREEDS = "breeds"
private const val BREED_DETAILS = "breedDetails"
private const val BREED_ID_ARG = "breedId"

private const val SIGN_IN = "signIn"

@Composable
fun MainNavCoordinator() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = "breeds") {
NavHost(navController = navController, startDestination = SIGN_IN) {
composable(SIGN_IN) { SignInScreen(koinViewModel()) }
composable(BREEDS) {
BreedsScreen(
viewModel = koinViewModel(),
Expand Down
76 changes: 76 additions & 0 deletions app/src/main/kotlin/co/touchlab/kampkit/android/ui/SignInScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package co.touchlab.kampkit.android.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import co.touchlab.kampkit.ui.signin.GoogleSignInData
import co.touchlab.kampkit.ui.signin.SignInViewModel
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException

@Composable
fun SignInScreen(viewModel: SignInViewModel) {
// val signInLauncher =
// rememberLauncherForActivityResult(
// ActivityResultContracts.StartActivityForResult()
// ) { signInActivityResult ->
// val signInData = signInActivityResult.extractGoogleSignInData()
// viewModel.onSignInClick(signInData)
// }

val state by viewModel.signInState.collectAsStateWithLifecycle()
Column {
state.error?.let { error ->
Text(error, color = Color.Red)
}

if (state.isUserLoggedIn) {
Text("Logged in as ${state.currentUserName}")
Button(onClick = { viewModel.onSignOutClick() }) {
Text("Log out")
}
} else {
// val context = LocalContext.current
Button(
onClick = viewModel::onGoogleSignIn//{ signInLauncher.launch(getGoogleSignInIntent(context)) }
) {
Text("Google Sign In")
}
}
}
}

// private fun ActivityResult.extractGoogleSignInData(): GoogleSignInData {
// if (resultCode != Activity.RESULT_OK) {
// return GoogleSignInData(error = "Google SignIn activity error")
// }
// return try {
// val task = GoogleSignIn.getSignedInAccountFromIntent(data)
// val account = task.getResult(ApiException::class.java)
// GoogleSignInData(email = account.email)
// } catch (exception: ApiException) {
// GoogleSignInData(error = exception.message)
// }
// }
//
// private fun getGoogleSignInIntent(context: Context): Intent {
// val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
// .requestEmail()
// .requestProfile()
// .build()
// val client = GoogleSignIn.getClient(context, signInOptions)
// return client.signInIntent
// }

2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ koin = "3.2.0"
multiplatformSettings = "1.0.0-alpha01"
turbine = "0.12.1"
sqlDelight = "1.5.5"
googleServices = "20.5.0"

[libraries]
android-desugaring = { module = "com.android.tools:desugar_jdk_libs", version.ref = "android-desugaring" }
Expand Down Expand Up @@ -92,6 +93,7 @@ touchlab-stately = { module = "co.touchlab:stately-common", version.ref = "state

turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
google-services = { module = "com.google.android.gms:play-services-auth", version.ref = "googleServices"}

[plugins]
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint-gradle" }
Expand Down
41 changes: 41 additions & 0 deletions ios/KaMPKitiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

/* Begin PBXBuildFile section */
3C61D1352A55909300D4DF1D /* BreedDetailsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C61D1342A55909300D4DF1D /* BreedDetailsViewModel.swift */; };
3C652F282A5D336600B402BA /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 3C652F272A5D336600B402BA /* GoogleSignIn */; };
3C652F2A2A5D336600B402BA /* GoogleSignInSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 3C652F292A5D336600B402BA /* GoogleSignInSwift */; };
3C652F2D2A5D34B300B402BA /* SignInScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C652F2C2A5D34B300B402BA /* SignInScreen.swift */; };
3C652F2F2A5D34BD00B402BA /* SignInViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C652F2E2A5D34BD00B402BA /* SignInViewModel.swift */; };
3C6AEC072A30881B0003F34A /* BreedDetailsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */; };
3C6AEC092A30C17A0003F34A /* MainNavCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C6AEC082A30C17A0003F34A /* MainNavCoordinator.swift */; };
3C73D04A2A335103003E6929 /* BreedsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C73D0492A335103003E6929 /* BreedsViewModel.swift */; };
Expand Down Expand Up @@ -45,6 +49,8 @@
1DFCC00C8DAA719770A18D1A /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; };
2A1ED6A4A2A53F5F75C58E5F /* Pods-KaMPKitiOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-KaMPKitiOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-KaMPKitiOS/Pods-KaMPKitiOS.release.xcconfig"; sourceTree = "<group>"; };
3C61D1342A55909300D4DF1D /* BreedDetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedDetailsViewModel.swift; sourceTree = "<group>"; };
3C652F2C2A5D34B300B402BA /* SignInScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInScreen.swift; sourceTree = "<group>"; };
3C652F2E2A5D34BD00B402BA /* SignInViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInViewModel.swift; sourceTree = "<group>"; };
3C6AEC062A30881B0003F34A /* BreedDetailsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedDetailsScreen.swift; sourceTree = "<group>"; };
3C6AEC082A30C17A0003F34A /* MainNavCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainNavCoordinator.swift; sourceTree = "<group>"; };
3C73D0492A335103003E6929 /* BreedsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BreedsViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -72,7 +78,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
3C652F2A2A5D336600B402BA /* GoogleSignInSwift in Frameworks */,
3DFF917C64A18A83DA010EE1 /* Pods_KaMPKitiOS.framework in Frameworks */,
3C652F282A5D336600B402BA /* GoogleSignIn in Frameworks */,
3CD290EC2A251417004C7AD1 /* KMPNativeCoroutinesCombine in Frameworks */,
3CD290EE2A251417004C7AD1 /* KMPNativeCoroutinesCore in Frameworks */,
);
Expand Down Expand Up @@ -104,6 +112,15 @@
path = BreedDetails;
sourceTree = "<group>";
};
3C652F2B2A5D34A500B402BA /* SignIn */ = {
isa = PBXGroup;
children = (
3C652F2C2A5D34B300B402BA /* SignInScreen.swift */,
3C652F2E2A5D34BD00B402BA /* SignInViewModel.swift */,
);
path = SignIn;
sourceTree = "<group>";
};
3C73D0482A335061003E6929 /* Breeds */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -182,6 +199,7 @@
F1465EFF23AA94BF0055F7C3 /* KaMPKitiOS */ = {
isa = PBXGroup;
children = (
3C652F2B2A5D34A500B402BA /* SignIn */,
3C61D1332A55908200D4DF1D /* BreedDetails */,
3C73D0482A335061003E6929 /* Breeds */,
F1465F0023AA94BF0055F7C3 /* AppDelegate.swift */,
Expand Down Expand Up @@ -235,6 +253,8 @@
packageProductDependencies = (
3CD290EB2A251417004C7AD1 /* KMPNativeCoroutinesCombine */,
3CD290ED2A251417004C7AD1 /* KMPNativeCoroutinesCore */,
3C652F272A5D336600B402BA /* GoogleSignIn */,
3C652F292A5D336600B402BA /* GoogleSignInSwift */,
);
productName = KaMPKitiOS;
productReference = F1465EFD23AA94BF0055F7C3 /* KaMPKitiOS.app */;
Expand Down Expand Up @@ -310,6 +330,7 @@
mainGroup = F1465EF423AA94BF0055F7C3;
packageReferences = (
3CD290EA2A251417004C7AD1 /* XCRemoteSwiftPackageReference "KMP-NativeCoroutines" */,
3C652F262A5D336600B402BA /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */,
);
productRefGroup = F1465EFE23AA94BF0055F7C3 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -416,7 +437,9 @@
files = (
46B5284D249C5CF400A7725D /* Koin.swift in Sources */,
3C73D04A2A335103003E6929 /* BreedsViewModel.swift in Sources */,
3C652F2D2A5D34B300B402BA /* SignInScreen.swift in Sources */,
3C61D1352A55909300D4DF1D /* BreedDetailsViewModel.swift in Sources */,
3C652F2F2A5D34BD00B402BA /* SignInViewModel.swift in Sources */,
46A5B5EF26AF54F7002EFEAA /* BreedsScreen.swift in Sources */,
46A5B5EF26AF54F7002EFEAA /* BreedsScreen.swift in Sources */,
F1465F0123AA94BF0055F7C3 /* AppDelegate.swift in Sources */,
Expand Down Expand Up @@ -778,6 +801,14 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
3C652F262A5D336600B402BA /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/google/GoogleSignIn-iOS";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 7.0.0;
};
};
3CD290EA2A251417004C7AD1 /* XCRemoteSwiftPackageReference "KMP-NativeCoroutines" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/rickclephas/KMP-NativeCoroutines.git";
Expand All @@ -789,6 +820,16 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
3C652F272A5D336600B402BA /* GoogleSignIn */ = {
isa = XCSwiftPackageProductDependency;
package = 3C652F262A5D336600B402BA /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;
productName = GoogleSignIn;
};
3C652F292A5D336600B402BA /* GoogleSignInSwift */ = {
isa = XCSwiftPackageProductDependency;
package = 3C652F262A5D336600B402BA /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */;
productName = GoogleSignInSwift;
};
3CD290EB2A251417004C7AD1 /* KMPNativeCoroutinesCombine */ = {
isa = XCSwiftPackageProductDependency;
package = 3CD290EA2A251417004C7AD1 /* XCRemoteSwiftPackageReference "KMP-NativeCoroutines" */;
Expand Down
11 changes: 11 additions & 0 deletions ios/KaMPKitiOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,16 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>GIDClientID</key>
<string>40914781142-l0i1eau2hbmgnciqj438kah5i5t61mus.apps.googleusercontent.com</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.40914781142-l0i1eau2hbmgnciqj438kah5i5t61mus</string>
</array>
</dict>
</array>
</dict>
</plist>
7 changes: 6 additions & 1 deletion ios/KaMPKitiOS/MainNavCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MainNavCoordinator: BreedsNavCoordinator {
}

func start() {
let controller = buildBreedsController(navCoordinator: self)
let controller = buildSignInController(viewController: navController)
navController.pushViewController(controller, animated: false)
}

Expand All @@ -37,6 +37,11 @@ private func buildBreedDetailsController(breedId: Int64) -> UIHostingController<
return UIHostingController(rootView: BreedDetailsScreen(viewModel: viewModel))
}

private func buildSignInController(viewController: UIViewController) -> UIHostingController<SignInScreen> {
let viewModel = SignInViewModel(viewController: viewController)
return UIHostingController(rootView: SignInScreen(viewModel: viewModel))
}

protocol BreedsNavCoordinator {
func onBreedDetailsRequest(breedId: Int64)
}
Loading
Loading