Skip to content

Instantly share code, notes, and snippets.

@LouisCAD
Created November 10, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouisCAD/bd6dc926e85143194bded6cfb16d51da to your computer and use it in GitHub Desktop.
Save LouisCAD/bd6dc926e85143194bded6cfb16d51da to your computer and use it in GitHub Desktop.
Can be used to load data from a Google Play Services API such as FusedLocationProvider or Wearable DataApi
import android.content.Context
import android.os.Bundle
import android.support.v4.content.Loader
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
import timber.log.Timber
/**
* Stores away the application context associated with context.
* Since Loaders can be used across multiple activities it's dangerous to
* store the context directly; always use [.getContext] to retrieve
* the Loader's Context, don't use the constructor argument directly.
* The Context returned by [.getContext] is safe to use across
* Activity instances.
* @param context used to retrieve the application context.
*/
abstract class GoogleApiClientLoader<D>(context: Context) : Loader<D>(context),
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
/**
* Do not call [GoogleApiClient.disconnect] on this object, except from
* [GoogleApiClientLoader] class.
*/
private val mGoogleApiClient: GoogleApiClient
private var mData: D? = null
/**
* @return the connection result if [.mGoogleApiClient] connection failed,
* * or null otherwise.
*/
var connectionResult: ConnectionResult? = null
private set
init {
@Suppress("LeakingThis")
mGoogleApiClient = getNewApiClient(context)
}
protected abstract fun getNewApiClient(context: Context): GoogleApiClient
protected abstract fun startLoadingData(googleApiClient: GoogleApiClient)
protected abstract fun stopLoading(googleApiClient: GoogleApiClient)
protected abstract fun onGoogleApiClientDisconnect(googleApiClient: GoogleApiClient)
protected fun onResult(data: D?) {
mData = data
deliverResult(data)
}
override fun onStartLoading() {
if (mData != null) deliverResult(mData)
if (mGoogleApiClient.isConnected) {
startLoadingData(mGoogleApiClient)
} else mGoogleApiClient.connect()
}
override fun onStopLoading() {
if (mGoogleApiClient.isConnected) stopLoading(mGoogleApiClient)
}
override fun onForceLoad() {
if (mData != null) deliverResult(mData)
if (!mGoogleApiClient.isConnected) mGoogleApiClient.connect()
}
override final fun onConnected(connectionHint: Bundle?) {
connectionResult = null
if (mData != null) deliverResult(mData)
startLoadingData(mGoogleApiClient)
}
override final fun onConnectionSuspended(cause: Int) {
Timber.wtf("onConnectionSuspended(cause=%d)", cause)
deliverResult(null)
}
override final fun onConnectionFailed(connectionResult: ConnectionResult) {
this.connectionResult = connectionResult
deliverResult(null)
}
override fun onReset() {
if (mGoogleApiClient.isConnected || mGoogleApiClient.isConnecting) {
onGoogleApiClientDisconnect(mGoogleApiClient)
mGoogleApiClient.disconnect()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment