Skip to content

Commit

Permalink
implement addLifecycleEvent, removeLifecycleEvent in non-androidx pro…
Browse files Browse the repository at this point in the history
…ject (issue #44)
  • Loading branch information
WindSekirun committed Dec 16, 2018
1 parent 28cc3d8 commit 373539f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions library/build.gradle
Expand Up @@ -82,6 +82,9 @@ dependencies {
implementation 'com.chibatching.kotpref:initializer:2.6.0'
// guava
implementation 'com.google.guava:guava:22.0-android'

implementation "android.arch.lifecycle:extensions:1.1.1"
implementation 'android.arch.lifecycle:common-java8:1.1.1'
}

task sourcesJar(type: Jar) {
Expand Down
@@ -1,5 +1,6 @@
package com.github.windsekirun.rxsociallogin

import android.arch.lifecycle.Lifecycle
import android.content.Intent
import android.support.v4.app.FragmentActivity
import com.github.windsekirun.rxsociallogin.intenal.impl.OnResponseListener
Expand Down Expand Up @@ -39,6 +40,18 @@ abstract class BaseSocialLogin constructor(childActivity: FragmentActivity) {

}

open fun addLifecycleEvent(lifecycle: Lifecycle) {

}

open fun removeLifecycleEvent(lifecycle: Lifecycle) {

}

open fun onPauseEvent() {

}

protected fun callbackAsFail(exception: Exception) {
responseListener?.onResult(null, exception)
}
Expand Down
Expand Up @@ -4,6 +4,7 @@
package com.github.windsekirun.rxsociallogin

import android.app.Application
import android.arch.lifecycle.Lifecycle
import android.content.Intent
import android.support.annotation.CheckResult
import android.support.v4.app.FragmentActivity
Expand Down Expand Up @@ -180,6 +181,30 @@ object RxSocialLogin {
return configMap[type]!!
}

@JvmStatic
fun addLifecycleEvent(lifecycle: Lifecycle) {
moduleMap.values.forEach {
it?.addLifecycleEvent(lifecycle)
}
}

@JvmStatic
fun removeLifecycleEvent(lifecycle: Lifecycle) {
moduleMap.values.forEach {
it?.removeLifecycleEvent(lifecycle)
}
}

@JvmStatic
fun getLoginModule(platformType: PlatformType): BaseSocialLogin? {
if (!moduleMap.containsKey(platformType)) {
throw LoginFailedException(EXCEPTION_CONFIG_MISSING)
}

return moduleMap[platformType]
}


/**
* set [SocialConfig] object with given [PlatformType]
* Additional settings for platforms not created through the Application class are not allowed.
Expand Down
@@ -1,5 +1,8 @@
package com.github.windsekirun.rxsociallogin.google

import android.arch.lifecycle.DefaultLifecycleObserver
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleOwner
import android.content.Intent
import android.support.v4.app.FragmentActivity
import com.github.windsekirun.rxsociallogin.BaseSocialLogin
Expand All @@ -18,7 +21,7 @@ import com.google.android.gms.common.api.GoogleApiClient
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider

class GoogleLogin constructor(activity: FragmentActivity) : BaseSocialLogin(activity) {
class GoogleLogin constructor(activity: FragmentActivity) : BaseSocialLogin(activity), DefaultLifecycleObserver {
private val googleApiClient: GoogleApiClient by lazy {
val builder = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(config.clientTokenId)
Expand Down Expand Up @@ -60,6 +63,28 @@ class GoogleLogin constructor(activity: FragmentActivity) : BaseSocialLogin(acti
if (googleApiClient.isConnected) googleApiClient.clearDefaultAccountAndReconnect()
}

override fun addLifecycleEvent(lifecycle: Lifecycle) {
super.addLifecycleEvent(lifecycle)
lifecycle.addObserver(this)
}

override fun removeLifecycleEvent(lifecycle: Lifecycle) {
super.removeLifecycleEvent(lifecycle)
lifecycle.removeObserver(this)
}

override fun onPause(owner: LifecycleOwner) {
onPauseEvent()
}

override fun onPauseEvent() {
super.onPauseEvent()
if (googleApiClient.isConnected) {
googleApiClient.stopAutoManage(activity!!)
googleApiClient.disconnect()
}
}

private fun authWithFirebase(acct: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
val disposable = auth.signInWithCredential(credential, activity, PlatformType.GOOGLE)
Expand Down

0 comments on commit 373539f

Please sign in to comment.