diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-19 22:02:24 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-19 22:02:24 -0700 |
commit | 2d7fa0f6d0c152bb9f3fd30370ef510a0d452d00 (patch) | |
tree | 20b9d28ed89ec1d17a42127066a513db33ecb7e3 /app/src/main | |
parent | 7b90fa9819c9b4765f107ea04dd7ac687dc572ad (diff) | |
download | frost-2d7fa0f6d0c152bb9f3fd30370ef510a0d452d00.tar.gz frost-2d7fa0f6d0c152bb9f3fd30370ef510a0d452d00.tar.bz2 frost-2d7fa0f6d0c152bb9f3fd30370ef510a0d452d00.zip |
Check for username if not found on login
Diffstat (limited to 'app/src/main')
5 files changed, 77 insertions, 42 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt index ecc27946..3cee4852 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt @@ -15,6 +15,7 @@ import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.request.RequestListener import com.bumptech.glide.request.target.Target import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.dbflow.fetchUsername import com.pitchedapps.frost.dbflow.loadFbCookiesAsync import com.pitchedapps.frost.dbflow.saveFbCookie import com.pitchedapps.frost.facebook.FACEBOOK_COM @@ -120,20 +121,8 @@ class LoginActivity : BaseActivity() { } fun loadUsername(cookie: CookieModel) { - thread { - var name = "" - try { - name = Jsoup.connect(FbTab.PROFILE.url) - .cookie(FACEBOOK_COM, cookie.cookie) - .get().title() - L.d("User name found: $name") - } catch (e: Exception) { - L.e("User name fetching failed: ${e.message}") - } finally { - cookie.name = name - saveFbCookie(cookie) - usernameObservable.onSuccess(name) - } + cookie.fetchUsername { + usernameObservable.onSuccess(it) } } }
\ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index f7648243..ca50d4e2 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -82,9 +82,9 @@ class MainActivity : BaseActivity() { override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { super.onPageScrolled(position, positionOffset, positionOffsetPixels) val delta: Float by lazy { positionOffset * (255 - 128).toFloat() } - (0 until tabs.tabCount).asSequence().forEach { - i -> - (tabs.getTabAt(i)!!.customView as BadgedIcon).setAllAlpha(when (i) { + tabsForEachView { + position, view -> + view.setAllAlpha(when (position) { position -> 255.0f - delta position + 1 -> 128.0f + delta else -> 128f @@ -102,6 +102,13 @@ class MainActivity : BaseActivity() { setFrostColors(toolbar, headers = arrayOf(tabs, appBar), backgrounds = arrayOf(viewPager)) } + fun tabsForEachView(action: (position: Int, view: BadgedIcon) -> Unit) { + (0 until tabs.tabCount).asSequence().forEach { + i -> + action(i, tabs.getTabAt(i)!!.customView as BadgedIcon) + } + } + fun setupTabs() { viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs)) tabs.addOnTabSelectedListener(object : TabLayout.ViewPagerOnTabSelectedListener(viewPager) { @@ -128,13 +135,13 @@ class MainActivity : BaseActivity() { .observeOn(AndroidSchedulers.mainThread()) .subscribe { (feed, requests, messages, notifications) -> - (0 until tabs.tabCount).asSequence().forEach { - val tabBadge = tabs.getTabAt(it)!!.customView as BadgedIcon - when (tabBadge.iicon) { - FbTab.FEED.icon -> tabBadge.badgeText = feed - FbTab.FRIENDS.icon -> tabBadge.badgeText = requests - FbTab.MESSAGES.icon -> tabBadge.badgeText = messages - FbTab.NOTIFICATIONS.icon -> tabBadge.badgeText = notifications + tabsForEachView { + _, view -> + when (view.iicon) { + FbTab.FEED.icon -> view.badgeText = feed + FbTab.FRIENDS.icon -> view.badgeText = requests + FbTab.MESSAGES.icon -> view.badgeText = messages + FbTab.NOTIFICATIONS.icon -> view.badgeText = notifications } } } @@ -184,7 +191,10 @@ class MainActivity : BaseActivity() { else when (profile.identifier) { -2L -> launchNewTask(LoginActivity::class.java, clearStack = false) -3L -> launchNewTask(SelectorActivity::class.java, cookies(), false) - else -> switchUser(profile.identifier, { refreshAll() }) + else -> { + switchUser(profile.identifier, { refreshAll() }) + tabsForEachView { _, view -> view.badgeText = null } + } } false } 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 1d1aa86e..59ef6cd7 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/dbflow/CookiesDb.kt @@ -2,6 +2,8 @@ package com.pitchedapps.frost.dbflow import android.os.Parcel import android.os.Parcelable +import com.pitchedapps.frost.facebook.FACEBOOK_COM +import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.utils.L import com.raizlabs.android.dbflow.annotation.ConflictAction import com.raizlabs.android.dbflow.annotation.Database @@ -9,6 +11,8 @@ import com.raizlabs.android.dbflow.annotation.PrimaryKey import com.raizlabs.android.dbflow.annotation.Table import com.raizlabs.android.dbflow.kotlinextensions.* import com.raizlabs.android.dbflow.structure.BaseModel +import org.jetbrains.anko.doAsync +import org.jsoup.Jsoup import paperparcel.PaperParcel /** @@ -56,4 +60,28 @@ fun removeCookie(id: Long) { loadFbCookie(id)?.async?.delete({ L.d("Fb cookie $id deleted") }) +} + +fun CookieModel.fetchUsername(callback: (String) -> Unit) { + doAsync { + var result = "" + try { + result = Jsoup.connect(FbTab.PROFILE.url) + .cookie(FACEBOOK_COM, cookie) + .get().title() + L.d("User name found: $result") + } catch (e: Exception) { + L.e("User name fetching failed: ${e.message}") + } finally { + if (result.isBlank() && (name?.isNotBlank() ?: false)) { + callback(name!!) + return@doAsync + } + if (name != result) { + name = result + saveFbCookie(this@fetchUsername) + } + callback(result) + } + } }
\ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt index d357ff68..813bdce2 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -7,7 +7,6 @@ import android.app.job.JobService import android.content.Context import android.content.Intent import android.net.Uri -import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationManagerCompat import ca.allanwang.kau.utils.string import com.pitchedapps.frost.BuildConfig @@ -42,8 +41,7 @@ class NotificationService : JobService() { debugNotification("Load notifs") loadFbCookiesSync().forEach { data -> - L.i("Handling notifications for ${data.id}") - L.v("Using data $data") + L.i("Handle notifications for $data") val doc = Jsoup.connect(FbTab.NOTIFICATIONS.url).cookie(FACEBOOK_COM, data.cookie).get() val unreadNotifications = doc.getElementById("notifications_list").getElementsByClass("aclb") var notifCount = 0 @@ -64,6 +62,7 @@ class NotificationService : JobService() { } L.d("Finished notifications") jobFinished(params, false) + future = null } return true } @@ -100,22 +99,30 @@ class NotificationService : JobService() { } data class NotificationContent(val data: CookieModel, val notifId: Int, val href: String, val text: String, val timestamp: Long) { - fun createNotification(context: Context) { - val intent = Intent(context, FrostWebActivity::class.java) - intent.data = Uri.parse("$FB_URL_BASE$href") - val group = "frost_${data.id}" - val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) - val notifBuilder = context.frostNotification - .setContentTitle(context.string(R.string.app_name)) - .setContentText(text) - .setContentIntent(pendingIntent) - .setCategory(Notification.CATEGORY_SOCIAL) - .setSubText(data.name) - .setGroup(group) + fun createNotification(context: Context, verifiedUser: Boolean = false) { + //in case we haven't found the name, we will try one more time before passing the notification + if (!verifiedUser && data.name?.isBlank() ?: true) { + data.fetchUsername { + data.name = it + createNotification(context, true) + } + } else { + val intent = Intent(context, FrostWebActivity::class.java) + intent.data = Uri.parse("$FB_URL_BASE$href") + val group = "frost_${data.id}" + val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) + val notifBuilder = context.frostNotification + .setContentTitle(context.string(R.string.app_name)) + .setContentText(text) + .setContentIntent(pendingIntent) + .setCategory(Notification.CATEGORY_SOCIAL) + .setSubText(data.name) + .setGroup(group) - if (timestamp != -1L) notifBuilder.setWhen(timestamp * 1000) + if (timestamp != -1L) notifBuilder.setWhen(timestamp * 1000) - NotificationManagerCompat.from(context).notify(group, notifId, notifBuilder.build()) + NotificationManagerCompat.from(context).notify(group, notifId, notifBuilder.build()) + } } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt index cc96758b..8ae54ef3 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt @@ -47,6 +47,7 @@ class BadgedIcon @JvmOverloads constructor( var badgeText: String? get() = badgeTextView.text.toString() set(value) { + if (badgeTextView.text == value) return badgeTextView.text = value if (value != null && value != "0") badgeTextView.visible() else badgeTextView.gone() |