From 4c44dbc9933bd726c1da0bf326102835c4974d6b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 30 May 2017 01:03:01 -0700 Subject: create retrofacebook and token retrieval --- .../com/pitchedapps/frost/facebook/FbToken.kt | 13 ++++++ .../com/pitchedapps/frost/facebook/UrlData.kt | 3 +- .../pitchedapps/frost/facebook/retro/FrostApi.kt | 52 ++++++++++++++++++++++ .../pitchedapps/frost/facebook/retro/FrostData.kt | 8 ++++ .../frost/facebook/retro/FrostInterceptor.kt | 29 ++++++++++++ .../com/pitchedapps/frost/facebook/retro/IFrost.kt | 16 +++++++ 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/FbToken.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostApi.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostData.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/IFrost.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbToken.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbToken.kt new file mode 100644 index 00000000..22dc25f7 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbToken.kt @@ -0,0 +1,13 @@ +package com.pitchedapps.frost.facebook + +import com.facebook.AccessToken + +/** + * Created by Allan Wang on 2017-05-30. + */ +val token: String? + get() = AccessToken.getCurrentAccessToken()?.token + +fun setToken() { + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt index 80972050..d5f9db6e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/UrlData.kt @@ -18,6 +18,7 @@ import io.realm.annotations.PrimaryKey * Created by Allan Wang on 2017-05-29. */ enum class FbUrl(@StringRes val titleId: Int, val icon: IIcon, val url: String) { + LOGIN(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, "https://www.facebook.com/v2.9/dialog/oauth?client_id=$FB_KEY&redirect_uri=https://touch.facebook.com/&response_type=token,granted_scopes"), FEED(R.string.feed, CommunityMaterial.Icon.cmd_newspaper, "https://touch.facebook.com/"), PROFILE(R.string.profile, CommunityMaterial.Icon.cmd_account, "https://touch.facebook.com/me/"), EVENTS(R.string.events, GoogleMaterial.Icon.gmd_event, "https://touch.facebook.com/events/upcoming"), @@ -60,6 +61,6 @@ fun loadFbTab(c: Context): List { val realmList = mutableListOf() realm(RealmFiles.TABS, Realm.Transaction { it.copyFromRealm(realmList) }) if (realmList.isNotEmpty()) return realmList.map { FbTab(it) } - return FbUrl.values().map { it.tabInfo(c) } + return listOf(FbUrl.FEED, FbUrl.MESSAGES, FbUrl.NOTIFICATIONS).map { it.tabInfo(c) } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostApi.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostApi.kt new file mode 100644 index 00000000..746bf0df --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostApi.kt @@ -0,0 +1,52 @@ +package com.pitchedapps.frost.facebook.retro + +import android.content.Context +import com.facebook.stetho.okhttp3.StethoInterceptor +import com.google.gson.GsonBuilder +import com.pitchedapps.frost.BuildConfig +import io.reactivex.schedulers.Schedulers +import okhttp3.Cache +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import retrofit2.Retrofit +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory +import retrofit2.converter.gson.GsonConverterFactory +import java.io.File + +/** + * Created by Allan Wang on 2017-05-30. + * + * API for data retrieval + */ +object FrostApi { + + internal lateinit var frostApi: IFrost + + operator fun invoke(context: Context) { + val cacheDir = File(context.cacheDir, "responses") + val cacheSize = 5L * 1024 * 1024 //10MiB + val cache = Cache(cacheDir, cacheSize) + + val client = OkHttpClient.Builder() + .addInterceptor(FrostInterceptor(context)) + .cache(cache) + + + //add logger and stetho last + + if (BuildConfig.DEBUG || BuildConfig.BUILD_TYPE == "releaseTest") { //log if not full release + client.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)) + client.addNetworkInterceptor(StethoInterceptor()) + } + + val gson = GsonBuilder().setLenient() + + val retrofit = Retrofit.Builder() + .baseUrl("https://graph.facebook.com/") + .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())) + .addConverterFactory(GsonConverterFactory.create(gson.create())) + .client(client.build()) + .build(); + frostApi = retrofit.create(IFrost::class.java) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostData.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostData.kt new file mode 100644 index 00000000..07d686a7 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostData.kt @@ -0,0 +1,8 @@ +package com.pitchedapps.frost.facebook.retro + +/** + * Created by Allan Wang on 2017-05-30. + * + * Collection of Graph API outputs + */ +data class Me(val name: String, val id: String) \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt new file mode 100644 index 00000000..f745aedf --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/FrostInterceptor.kt @@ -0,0 +1,29 @@ +package com.pitchedapps.frost.facebook.retro + +import android.content.Context +import com.pitchedapps.frost.facebook.token +import com.pitchedapps.frost.utils.Utils +import okhttp3.Interceptor +import okhttp3.Response + +/** + * Created by Allan Wang on 2017-05-30. + */ +private val maxStale = 60 * 60 * 24 * 28 //maxAge to get from cache if online (4 weeks) +const val ACCESS_TOKEN = "access_token" + +class FrostInterceptor(val context: Context) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response? { + val request = chain.request() + val requestBuilder = request.newBuilder() + val urlBase = request.url() + val urlWithToken = urlBase.newBuilder() + if (urlBase.queryParameter(ACCESS_TOKEN) == null && token != null) + urlWithToken.addQueryParameter(ACCESS_TOKEN, token) + requestBuilder.url(urlWithToken.build()) + if (!Utils.isNetworkAvailable(context)) requestBuilder.addHeader("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) + return chain.proceed(requestBuilder.build()) + } + +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/IFrost.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/IFrost.kt new file mode 100644 index 00000000..6c50fa74 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/retro/IFrost.kt @@ -0,0 +1,16 @@ +package com.pitchedapps.frost.facebook.retro + +import com.pitchedapps.frost.facebook.token +import retrofit2.Call +import retrofit2.http.GET +import retrofit2.http.Query + +/** + * Created by Allan Wang on 2017-05-30. + */ +interface IFrost { + + @GET("me") + fun me(@Query(ACCESS_TOKEN) accessToken: String? = token): Call + +} \ No newline at end of file -- cgit v1.2.3