diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-19 17:08:15 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-19 17:08:15 -0700 |
commit | a3b5b647e2651c9768a4056852aff36074590e94 (patch) | |
tree | 4398fd2f27dfda32d26c21d71ed0af746061bdc6 | |
parent | 382433780c3f4403723a78e409cb161c9fad5034 (diff) | |
download | frost-a3b5b647e2651c9768a4056852aff36074590e94.tar.gz frost-a3b5b647e2651c9768a4056852aff36074590e94.tar.bz2 frost-a3b5b647e2651c9768a4056852aff36074590e94.zip |
Created notification badges
7 files changed, 159 insertions, 25 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt b/app/src/main/kotlin/com/pitchedapps/frost/FrostApp.kt index 6afc84fd..3a559bc2 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 cdfebfac..b4320ba3 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 f506bd8f..cd66b9c2 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 00000000..bc932480 --- /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 00000000..9dfc3cf0 --- /dev/null +++ b/app/src/main/res/drawable/badge_background.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item android:id="@android:id/background"> + <shape> + <corners android:radius="10dp" /> + <solid android:color="@color/facebook_blue" /> + </shape> + </item> +</layer-list>
\ 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 49085412..58469632 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"> <!-- The background color, preferably the same as your normal theme --> <item android:drawable="@color/splashBackground"/> - <!-- Your product logo - 288px x 288px color version of your app icon --> + <!-- Your product logo - 288px x 288px color version of your app badgeImage --> <item android:bottom="@dimen/splash_logo"> <bitmap android:gravity="center" diff --git a/app/src/main/res/layout/view_badged_icon.xml b/app/src/main/res/layout/view_badged_icon.xml index d8fb3247..cc6b16cc 100644 --- a/app/src/main/res/layout/view_badged_icon.xml +++ b/app/src/main/res/layout/view_badged_icon.xml @@ -1,6 +1,54 @@ <?xml version="1.0" encoding="utf-8"?> -<android.support.constraint.ConstraintLayout - xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" - android:layout_height="match_parent"> +<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="50dp"> + + <android.support.constraint.Guideline + android:id="@+id/g_v_c" + android:layout_width="1dp" + android:layout_height="wrap_content" + android:orientation="vertical" + app:layout_constraintGuide_percent="0.5" + tools:layout_editor_absoluteX="192dp" + tools:layout_editor_absoluteY="0dp" /> + + <android.support.constraint.Guideline + android:id="@+id/g_h_c" + android:layout_width="wrap_content" + android:layout_height="1dp" + android:orientation="horizontal" + app:layout_constraintGuide_percent="0.5" + tools:layout_editor_absoluteX="0dp" + tools:layout_editor_absoluteY="256dp" /> + + <ImageView + android:id="@+id/badge_image" + android:layout_width="20dp" + android:layout_height="20dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintHorizontal_bias="0.5" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.5" /> + + <TextView + android:id="@+id/badge_text" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:gravity="center" + android:paddingEnd="5dp" + android:paddingStart="5dp" + android:textSize="12sp" + android:visibility="gone" + app:layout_constraintBottom_toTopOf="@+id/g_h_c" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0" + app:layout_constraintStart_toEndOf="@id/g_v_c" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="1" + tools:layout_editor_absoluteX="8dp" /> </android.support.constraint.ConstraintLayout>
\ No newline at end of file |