diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt index 901ba02d..14d15bd6 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt @@ -2,9 +2,11 @@ package com.pitchedapps.frost.dbflow import android.os.Parcel import android.os.Parcelable +import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.pitchedapps.frost.facebook.FACEBOOK_COM import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.logFrostAnswers import com.raizlabs.android.dbflow.annotation.ConflictAction import com.raizlabs.android.dbflow.annotation.Database import com.raizlabs.android.dbflow.annotation.PrimaryKey @@ -64,26 +66,31 @@ fun removeCookie(id: Long) { } fun CookieModel.fetchUsername(callback: (String) -> Unit) { - doAsync { - var result = "" - try { - result = Jsoup.connect(FbTab.PROFILE.url) - .cookie(FACEBOOK_COM, cookie) - .get().title() - L.d("Fetch username found", result) - } catch (e: Exception) { - if (e !is UnknownHostException) - L.e(e, "Fetch username failed") - } finally { - if (result.isBlank() && (name?.isNotBlank() ?: false)) { - callback(name!!) - return@doAsync + ReactiveNetwork.checkInternetConnectivity().subscribe { + yes, _ -> + if (!yes) return@subscribe callback("") + doAsync { + var result = "" + try { + result = Jsoup.connect(FbTab.PROFILE.url) + .cookie(FACEBOOK_COM, cookie) + .get().title() + L.d("Fetch username found", result) + } catch (e: Exception) { + if (e !is UnknownHostException) + e.logFrostAnswers("Fetch username failed") + } finally { + if (result.isBlank() && (name?.isNotBlank() ?: false)) { + callback(name!!) + return@doAsync + } + if (name != result) { + name = result + saveFbCookie(this@fetchUsername) + } + callback(result) } - if (name != result) { - name = result - saveFbCookie(this@fetchUsername) - } - callback(result) } + } }
\ No newline at end of file |