From a3b5b647e2651c9768a4056852aff36074590e94 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 19 Jun 2017 17:08:15 -0700 Subject: [PATCH] Created notification badges --- .../kotlin/com/pitchedapps/frost/FrostApp.kt | 24 ++++---- .../com/pitchedapps/frost/MainActivity.kt | 39 ++++++++++--- .../frost/services/NotificationService.kt | 1 + .../com/pitchedapps/frost/views/BadgedIcon.kt | 55 +++++++++++++++++++ .../main/res/drawable/badge_background.xml | 9 +++ app/src/main/res/drawable/splash_screen.xml | 2 +- app/src/main/res/layout/view_badged_icon.xml | 54 +++++++++++++++++- 7 files changed, 159 insertions(+), 25 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt create mode 100644 app/src/main/res/drawable/badge_background.xml diff --git a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt index 6afc84fd6..3a559bc21 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt @@ -48,18 +48,18 @@ class FrostApp : Application() { FbCookie() super.onCreate() //Drawer profile loading logic - DrawerImageLoader.init(object : AbstractDrawerImageLoader() { - override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable, tag: String) { - Glide.with(imageView.context).load(uri).apply(RequestOptions().placeholder(placeholder)).into(imageView) - } - - override fun placeholder(ctx: Context, tag: String): Drawable { - when (tag) { - DrawerImageLoader.Tags.PROFILE.name, DrawerImageLoader.Tags.ACCOUNT_HEADER.name -> DrawerUIUtils.getPlaceHolder(ctx) - } - return super.placeholder(ctx, tag); - } - }) +// DrawerImageLoader.init(object : AbstractDrawerImageLoader() { +// override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable, tag: String) { +// Glide.with(imageView.context).load(uri).apply(RequestOptions().placeholder(placeholder)).into(imageView) +// } +// +// override fun placeholder(ctx: Context, tag: String): Drawable { +// when (tag) { +// DrawerImageLoader.Tags.PROFILE.name, DrawerImageLoader.Tags.ACCOUNT_HEADER.name -> DrawerUIUtils.getPlaceHolder(ctx) +// } +// return super.placeholder(ctx, tag); +// } +// }) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index cdfebfac9..b4320ba39 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -13,7 +13,10 @@ import android.support.v7.widget.Toolbar import android.view.Menu import android.view.MenuItem import android.widget.ImageButton -import ca.allanwang.kau.utils.* +import ca.allanwang.kau.utils.bindView +import ca.allanwang.kau.utils.showChangelog +import ca.allanwang.kau.utils.startActivitySlideIn +import ca.allanwang.kau.utils.withMinAlpha import co.zsmb.materialdrawerkt.builders.Builder import co.zsmb.materialdrawerkt.builders.accountHeader import co.zsmb.materialdrawerkt.builders.drawer @@ -30,6 +33,7 @@ import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL import com.pitchedapps.frost.fragments.WebFragment import com.pitchedapps.frost.utils.* +import com.pitchedapps.frost.views.BadgedIcon import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.PublishSubject @@ -76,11 +80,11 @@ class MainActivity : BaseActivity() { val delta: Float by lazy { positionOffset * (255 - 128).toFloat() } (0 until tabs.tabCount).asSequence().forEach { i -> - tabs.getTabAt(i)?.icon?.alpha = when (i) { - position -> (255.0 - delta).toInt() - position + 1 -> (128.0 + delta).toInt() - else -> 128 - } + (tabs.getTabAt(i)!!.customView as BadgedIcon).setAllAlpha(when (i) { + position -> 255.0f - delta + position + 1 -> 128.0f + delta + else -> 128f + }) } } }) @@ -101,6 +105,11 @@ class MainActivity : BaseActivity() { super.onTabReselected(tab) currentFragment.web.scrollOrRefresh() } + + override fun onTabSelected(tab: TabLayout.Tab) { + super.onTabSelected(tab) + (tab.customView as BadgedIcon).badgeText = null + } }) headerBadgeObservable.throttleFirst(15, TimeUnit.SECONDS).subscribeOn(Schedulers.newThread()) .map { Jsoup.parse(it) } @@ -115,10 +124,22 @@ class MainActivity : BaseActivity() { .observeOn(AndroidSchedulers.mainThread()) .subscribe { (feed, requests, messages, notifications) -> - L.d("Header subscription $feed $requests $messages $notifications") - L.d("contained nulls ${feed == null}") + (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 + } + } } - adapter.pages.forEach { tabs.addTab(tabs.newTab().setIcon(it.icon.toDrawable(this, sizeDp = 20, color = Prefs.iconColor))) } + adapter.pages.forEach { + tabs.addTab(tabs.newTab() + .setCustomView(BadgedIcon(this).apply { + iicon = it.icon + })) + } } fun setupDrawer(savedInstanceState: Bundle?) { 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 f506bd8f0..cd66b9c2f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -41,6 +41,7 @@ class NotificationService : JobService() { override fun onStartJob(params: JobParameters?): Boolean { future = doAsync { + debugNotification("Load notifs") loadFbCookiesSync().forEach { data -> L.i("Handling notifications for ${data.id}") diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt new file mode 100644 index 000000000..bc9324804 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt @@ -0,0 +1,55 @@ +package com.pitchedapps.frost.views + +import android.content.Context +import android.graphics.drawable.GradientDrawable +import android.support.constraint.ConstraintLayout +import android.util.AttributeSet +import android.widget.ImageView +import android.widget.TextView +import ca.allanwang.kau.utils.* +import com.mikepenz.iconics.typeface.IIcon +import com.pitchedapps.frost.R +import com.pitchedapps.frost.utils.Prefs + + +/** + * Created by Allan Wang on 2017-06-19. + */ +class BadgedIcon @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 +) : ConstraintLayout(context, attrs, defStyleAttr) { + + val badgeTextView: TextView by bindView(R.id.badge_text) + val badgeImage: ImageView by bindView(R.id.badge_image) + + init { + inflate(context, R.layout.view_badged_icon, this) + val badgeColor = Prefs.headerColor.withAlpha(255).colorToForeground(0.1f) + val badgeBackground = GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, intArrayOf(badgeColor, badgeColor)) + badgeBackground.cornerRadius = 13.dpToPx.toFloat() + badgeTextView.background = badgeBackground + badgeTextView.setTextColor(Prefs.iconColor) + } + + + var iicon: IIcon? = null + get() = field + set(value) { + field = value + badgeImage.setImageDrawable(value?.toDrawable(context, color = Prefs.iconColor)) + } + + fun setAllAlpha(alpha: Float) { + //badgeTextView.setTextColor(Prefs.textColor.withAlpha(alpha.toInt())) + badgeImage.drawable.alpha = alpha.toInt() + } + + var badgeText: String? + get() = badgeTextView.text.toString() + set(value) { + badgeTextView.text = value + if (value != null && value != "0") badgeTextView.visible() + else badgeTextView.gone() + } + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/badge_background.xml b/app/src/main/res/drawable/badge_background.xml new file mode 100644 index 000000000..9dfc3cf03 --- /dev/null +++ b/app/src/main/res/drawable/badge_background.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/splash_screen.xml b/app/src/main/res/drawable/splash_screen.xml index 49085412b..584696329 100644 --- a/app/src/main/res/drawable/splash_screen.xml +++ b/app/src/main/res/drawable/splash_screen.xml @@ -4,7 +4,7 @@ android:opacity="opaque"> - + - + + + + + + + + + \ No newline at end of file