-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Setting up Kodein, implementing Google sign in
There's still a lot of work to do here. Maybe would be good to break things here in smaller commits? (the first feature is always a bit problematic on its commits size)
- Loading branch information
1 parent
5e62200
commit de94fff
Showing
25 changed files
with
540 additions
and
10 deletions.
There are no files selected for viewing
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.rafaeltoledo.social | ||
|
||
import android.app.Application | ||
import net.rafaeltoledo.social.di.authModule | ||
import net.rafaeltoledo.social.di.firebaseModule | ||
import net.rafaeltoledo.social.di.viewModelModule | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.KodeinAware | ||
import org.kodein.di.android.androidModule | ||
|
||
open class SocialApp : Application(), KodeinAware { | ||
|
||
override val kodein = initKodein() | ||
|
||
private fun initKodein() = Kodein.lazy { | ||
import(androidModule(this@SocialApp)) | ||
import(firebaseModule) | ||
import(authModule) | ||
import(viewModelModule) | ||
} | ||
} |
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,3 @@ | ||
package net.rafaeltoledo.social.data | ||
|
||
data class User(val id: String) |
11 changes: 11 additions & 0 deletions
11
app/src/main/kotlin/net/rafaeltoledo/social/data/auth/AuthManager.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,11 @@ | ||
package net.rafaeltoledo.social.data.auth | ||
|
||
import kotlinx.coroutines.experimental.Deferred | ||
import net.rafaeltoledo.social.data.User | ||
|
||
interface AuthManager { | ||
|
||
fun googleSignIn(token: String): Deferred<User> | ||
|
||
fun isUserLoggedIn(): Boolean | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/kotlin/net/rafaeltoledo/social/data/auth/DelegatedAuth.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,19 @@ | ||
package net.rafaeltoledo.social.data.auth | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
|
||
interface DelegatedAuth { | ||
|
||
fun <T : DelegatedAuth> build(activity: Activity): T | ||
|
||
fun signIn(activity: Activity) | ||
|
||
fun onResult(requestCode: Int, resultCode: Int, data: Intent?): AuthResult | ||
|
||
fun signOut(callback: (Status) -> Unit) | ||
} | ||
|
||
enum class Status { CANCELED, SUCCESS, FAILURE } | ||
|
||
data class AuthResult(val status: Status, val token: String? = null) |
51 changes: 51 additions & 0 deletions
51
app/src/main/kotlin/net/rafaeltoledo/social/data/auth/GoogleAuth.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,51 @@ | ||
package net.rafaeltoledo.social.data.auth | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import com.google.android.gms.auth.api.signin.GoogleSignIn | ||
import com.google.android.gms.auth.api.signin.GoogleSignInClient | ||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions | ||
import com.google.android.gms.common.api.ApiException | ||
import net.rafaeltoledo.social.BuildConfig | ||
|
||
class GoogleAuth : DelegatedAuth { | ||
|
||
private var client: GoogleSignInClient? = null | ||
|
||
override fun <T : DelegatedAuth> build(activity: Activity): T { | ||
if (client != null) { | ||
@Suppress("UNCHECKED_CAST") return this as T | ||
} | ||
|
||
val options = GoogleSignInOptions.Builder() | ||
.requestIdToken(BuildConfig.GOOGLE_REQUEST_ID_TOKEN) | ||
.requestEmail() | ||
.build() | ||
|
||
client = GoogleSignIn.getClient(activity, options) | ||
|
||
@Suppress("UNCHECKED_CAST") return this as T | ||
} | ||
|
||
override fun signIn(activity: Activity) { | ||
activity.startActivityForResult(client?.signInIntent, RESULT_SIGN_IN) | ||
} | ||
|
||
override fun onResult(requestCode: Int, resultCode: Int, data: Intent?): AuthResult { | ||
val task = GoogleSignIn.getSignedInAccountFromIntent(data) | ||
try { | ||
val account = task.getResult(ApiException::class.java) | ||
return AuthResult(Status.SUCCESS, account.idToken) | ||
} catch (e: ApiException) { | ||
return AuthResult(Status.FAILURE) | ||
} | ||
} | ||
|
||
override fun signOut(callback: (Status) -> Unit) { | ||
client?.signOut()?.addOnCompleteListener { callback.invoke(if (it.isSuccessful) Status.SUCCESS else Status.FAILURE) } | ||
} | ||
|
||
companion object { | ||
const val RESULT_SIGN_IN = 1441 | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/kotlin/net/rafaeltoledo/social/data/firebase/FirebaseAuthManager.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,30 @@ | ||
package net.rafaeltoledo.social.data.firebase | ||
|
||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.auth.GoogleAuthProvider | ||
import kotlinx.coroutines.experimental.CompletableDeferred | ||
import kotlinx.coroutines.experimental.Deferred | ||
import net.rafaeltoledo.social.data.User | ||
import net.rafaeltoledo.social.data.auth.AuthManager | ||
|
||
class FirebaseAuthManager : AuthManager { | ||
|
||
private val auth = FirebaseAuth.getInstance() | ||
|
||
override fun googleSignIn(token: String): Deferred<User> { | ||
val deferred = CompletableDeferred<User>() | ||
|
||
auth.signInWithCredential(GoogleAuthProvider.getCredential(token, null)) | ||
.addOnCompleteListener { | ||
if (it.isSuccessful) { | ||
deferred.complete(User(it.result.user.uid)) | ||
} else { | ||
deferred.completeExceptionally(it.exception!!) | ||
} | ||
} | ||
|
||
return deferred | ||
} | ||
|
||
override fun isUserLoggedIn() = auth.currentUser != null | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/kotlin/net/rafaeltoledo/social/di/AuthModule.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,12 @@ | ||
package net.rafaeltoledo.social.di | ||
|
||
import net.rafaeltoledo.social.data.auth.DelegatedAuth | ||
import net.rafaeltoledo.social.data.auth.GoogleAuth | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.generic.bind | ||
import org.kodein.di.generic.instance | ||
|
||
val authModule = Kodein.Module { | ||
|
||
bind<DelegatedAuth>() with instance(GoogleAuth()) | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/kotlin/net/rafaeltoledo/social/di/FirebaseModule.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,12 @@ | ||
package net.rafaeltoledo.social.di | ||
|
||
import net.rafaeltoledo.social.data.auth.AuthManager | ||
import net.rafaeltoledo.social.data.firebase.FirebaseAuthManager | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.generic.bind | ||
import org.kodein.di.generic.singleton | ||
|
||
val firebaseModule = Kodein.Module { | ||
|
||
bind<AuthManager>() with singleton { FirebaseAuthManager() } | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/kotlin/net/rafaeltoledo/social/di/ViewModelModule.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,26 @@ | ||
package net.rafaeltoledo.social.di | ||
|
||
import android.arch.lifecycle.ViewModel | ||
import android.arch.lifecycle.ViewModelProvider | ||
import net.rafaeltoledo.social.ui.BaseViewModel | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.generic.bind | ||
import org.kodein.di.generic.singleton | ||
|
||
val viewModelModule = Kodein.Module { | ||
|
||
bind<ViewModelProvider.Factory>() with singleton { | ||
object : ViewModelProvider.Factory { | ||
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | ||
|
||
val newInstance = modelClass.newInstance() | ||
|
||
if (BaseViewModel::class.java.isInstance(newInstance)) { | ||
(newInstance as BaseViewModel).whenReady(kodein) | ||
} | ||
|
||
return newInstance | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/kotlin/net/rafaeltoledo/social/ui/BaseActivity.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,21 @@ | ||
package net.rafaeltoledo.social.ui | ||
|
||
import android.arch.lifecycle.ViewModel | ||
import android.arch.lifecycle.ViewModelProvider | ||
import android.arch.lifecycle.ViewModelProviders | ||
import android.support.v7.app.AppCompatActivity | ||
import org.kodein.di.KodeinAware | ||
import org.kodein.di.android.closestKodein | ||
import org.kodein.di.generic.instance | ||
import kotlin.reflect.KClass | ||
|
||
abstract class BaseActivity : AppCompatActivity(), KodeinAware { | ||
|
||
override val kodein by closestKodein() | ||
|
||
private val viewModelFactory: ViewModelProvider.Factory by instance() | ||
|
||
protected fun <T : ViewModel> getViewModelInstance(kClass: KClass<T>) = | ||
ViewModelProviders.of(this, viewModelFactory) | ||
.get(kClass.java) | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/kotlin/net/rafaeltoledo/social/ui/BaseViewModel.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,21 @@ | ||
package net.rafaeltoledo.social.ui | ||
|
||
import android.arch.lifecycle.MutableLiveData | ||
import android.arch.lifecycle.ViewModel | ||
import org.kodein.di.Kodein | ||
import org.kodein.di.KodeinAware | ||
|
||
abstract class BaseViewModel : ViewModel(), KodeinAware { | ||
|
||
override lateinit var kodein: Kodein | ||
|
||
val loading = MutableLiveData<Boolean>() | ||
|
||
init { | ||
loading.postValue(false) | ||
} | ||
|
||
fun whenReady(kodein: Kodein) { | ||
this.kodein = kodein | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/kotlin/net/rafaeltoledo/social/ui/feature/main/MainActivity.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,31 @@ | ||
package net.rafaeltoledo.social.ui.feature.main | ||
|
||
import android.content.Intent | ||
import android.databinding.DataBindingUtil | ||
import android.os.Bundle | ||
import net.rafaeltoledo.social.R | ||
import net.rafaeltoledo.social.data.auth.AuthManager | ||
import net.rafaeltoledo.social.databinding.ActivityMainBinding | ||
import net.rafaeltoledo.social.ui.BaseActivity | ||
import net.rafaeltoledo.social.ui.feature.signin.SignInActivity | ||
import org.kodein.di.generic.instance | ||
|
||
class MainActivity : BaseActivity() { | ||
|
||
private val auth: AuthManager by instance() | ||
|
||
private lateinit var binding: ActivityMainBinding | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
if (!auth.isUserLoggedIn()) { | ||
startActivity(Intent(this, SignInActivity::class.java)) | ||
finish() | ||
} | ||
} | ||
} |
Oops, something went wrong.