1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-22 12:22:08 +01:00

Accent color orange; Notification/Message count badge; Reworked progressbar #40

This commit is contained in:
Gregor Santner 2016-09-11 17:15:26 +02:00
parent 1dcd05af10
commit ac19efd847
13 changed files with 203 additions and 46 deletions

View file

@ -33,6 +33,7 @@ import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -46,6 +47,7 @@ import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
@ -77,6 +79,7 @@ import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.data.AppSettings;
import com.github.dfa.diaspora_android.data.PodUserProfile; import com.github.dfa.diaspora_android.data.PodUserProfile;
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener; import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
import com.github.dfa.diaspora_android.ui.BadgeDrawable;
import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.ContextMenuWebView;
import com.github.dfa.diaspora_android.ui.CustomWebViewClient; import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
@ -699,25 +702,21 @@ public class MainActivity extends AppCompatActivity
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main__menu_top, menu); getMenuInflater().inflate(R.menu.main__menu_top, menu);
return true; return super.onCreateOptionsMenu(menu);
} }
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem itemNotification = menu.findItem(R.id.action_notifications); MenuItem item;
if (itemNotification != null) {
if (podUserProfile.getNotificationCount() > 0) {
itemNotification.setIcon(R.drawable.ic_notifications_colored_48px);
} else {
itemNotification.setIcon(R.drawable.ic_notifications_white_48px);
}
MenuItem itemConversation = menu.findItem(R.id.action_conversations); if ((item = menu.findItem(R.id.action_notifications)) != null) {
if (podUserProfile.getUnreadMessagesCount() > 0) { LayerDrawable icon = (LayerDrawable) item.getIcon();
itemConversation.setIcon(R.drawable.ic_email_colored_48px); BadgeDrawable.setBadgeCount(this, icon, podUserProfile.getNotificationCount());
} else { }
itemConversation.setIcon(R.drawable.ic_mail_white_48px);
} if ((item = menu.findItem(R.id.action_conversations)) != null) {
LayerDrawable icon = (LayerDrawable) item.getIcon();
BadgeDrawable.setBadgeCount(this, icon, podUserProfile.getUnreadMessagesCount());
} }
return super.onPrepareOptionsMenu(menu); return super.onPrepareOptionsMenu(menu);
} }

View file

@ -0,0 +1,127 @@
package com.github.dfa.diaspora_android.ui;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.content.ContextCompat;
import com.github.dfa.diaspora_android.R;
public class BadgeDrawable extends Drawable {
// Source: http://mobikul.com/adding-badge-count-on-menu-items-like-cart-notification-etc/
private static final String BADGE_VALUE_OVERFLOW = "*";
private Paint badgeBackground;
private Paint badgeStroke;
private Paint badgeText;
private Rect textRect = new Rect();
private String badgeValue = "";
private boolean shouldDraw;
public BadgeDrawable(Context context) {
float textSize = context.getResources().getDimension(R.dimen.textsize_badge_count);
badgeBackground = new Paint();
badgeBackground.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.accent));
badgeBackground.setAntiAlias(true);
badgeBackground.setStyle(Paint.Style.FILL);
badgeStroke = new Paint();
badgeStroke.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.colorPrimaryDark));
badgeStroke.setAntiAlias(true);
badgeStroke.setStyle(Paint.Style.FILL);
badgeText = new Paint();
badgeText.setColor(Color.WHITE);
badgeText.setTypeface(Typeface.DEFAULT);
badgeText.setTextSize(textSize);
badgeText.setAntiAlias(true);
badgeText.setTextAlign(Paint.Align.CENTER);
}
@Override
public void draw(Canvas canvas) {
if (!shouldDraw) {
return;
}
Rect bounds = getBounds();
float width = bounds.right - bounds.left;
float height = bounds.bottom - bounds.top;
// Position the badge in the top-right quadrant of the icon.
float radius = ((Math.max(width, height) / 2)) / 2;
float centerX = (width - radius - 1) + 5;
float centerY = radius - 5;
if (badgeValue.length() <= 2) {
// Draw badge circle.
canvas.drawCircle(centerX, centerY, (int) (radius + 7.5), badgeStroke);
canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), badgeBackground);
} else {
canvas.drawCircle(centerX, centerY, (int) (radius + 8.5), badgeStroke);
canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), badgeBackground);
//canvas.drawRoundRect(radius, radius, radius, radius, 10, 10, badgeBackground);
}
// Draw badge count text inside the circle.
badgeText.getTextBounds(badgeValue, 0, badgeValue.length(), textRect);
float textHeight = textRect.bottom - textRect.top;
float textY = centerY + (textHeight / 2f);
if (badgeValue.length() > 2)
canvas.drawText(BADGE_VALUE_OVERFLOW, centerX, textY, badgeText);
else
canvas.drawText(badgeValue, centerX, textY, badgeText);
}
/*
Sets the count (i.e notifications) to display.
*/
public void setCount(String count) {
badgeValue = count;
// Only draw a badge if there are notifications.
shouldDraw = !count.equalsIgnoreCase("0");
invalidateSelf();
}
@Override
public void setAlpha(int alpha) {
// do nothing
}
@Override
public void setColorFilter(ColorFilter cf) {
// do nothing
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
public static void setBadgeCount(Context context, LayerDrawable icon, Integer count) {
setBadgeCount(context, icon, count.toString());
}
public static void setBadgeCount(Context context, LayerDrawable icon, String count) {
BadgeDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
}

View file

@ -1,4 +1,4 @@
<vector android:height="24dp" android:viewportHeight="24.0" <vector android:height="24dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#4CAF50" android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/> <path android:fillColor="#FF5300" android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
</vector> </vector>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_mail_white_48px"
android:gravity="center" />
<!-- set a place holder Drawable so android:drawable isn't null -->
<item
android:id="@+id/ic_badge"
android:drawable="@drawable/ic_mail_white_48px" />
</layer-list>

View file

@ -1,4 +0,0 @@
<vector android:height="24dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#4CAF50" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/>
</vector>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_notifications_white_48px"
android:gravity="center" />
<!-- set a place holder Drawable so android:drawable isn't null -->
<item
android:id="@+id/ic_badge"
android:drawable="@drawable/ic_notifications_white_48px" />
</layer-list>

View file

@ -0,0 +1,15 @@
<?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>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/colorAccent"/>
</shape>
</clip>
</item>
</layer-list>

View file

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_layout" android:id="@+id/content_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.MainActivity" tools:context=".activity.MainActivity"
tools:showIn="@layout/main__app_bar"> tools:showIn="@layout/main__app_bar">
<FrameLayout <FrameLayout
android:id="@+id/placeholder_webview" android:id="@+id/placeholder_webview"
@ -19,11 +19,11 @@
<ProgressBar <ProgressBar
android:id="@+id/progressBar" android:id="@+id/progressBar"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
style="@style/Widget.AppCompat.ProgressBar.Horizontal" style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="7dp"
android:indeterminate="false" android:indeterminate="false"
android:layout_height="wrap_content" android:progressDrawable="@drawable/progressbar"
android:layout_marginTop="-7dp" /> app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</RelativeLayout> </RelativeLayout>

View file

@ -26,11 +26,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp" android:layout_marginBottom="30dp"
android:shadowColor="@color/black"
android:shadowDx="-4"
android:shadowDy="4"
android:shadowRadius="6"
android:text="@string/app_name" android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge" android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" android:textColor="@color/white"

View file

@ -1,13 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:orientation="vertical">
<com.github.dfa.diaspora_android.ui.ContextMenuWebView <com.github.dfa.diaspora_android.ui.ContextMenuWebView
android:id="@+id/webView" android:id="@+id/webView"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent" />
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true" />
</LinearLayout> </LinearLayout>

View file

@ -5,13 +5,13 @@
<item <item
android:id="@+id/action_notifications" android:id="@+id/action_notifications"
android:icon="@drawable/ic_notifications_white_48px" android:icon="@drawable/ic_notifications_white_48px__layer"
android:title="@string/notifications" android:title="@string/notifications"
app:showAsAction="always" /> app:showAsAction="always" />
<item <item
android:id="@+id/action_conversations" android:id="@+id/action_conversations"
android:icon="@drawable/ic_mail_white_48px" android:icon="@drawable/ic_mail_white_48px__layer"
android:title="@string/conversations" android:title="@string/conversations"
app:showAsAction="always" /> app:showAsAction="always" />

View file

@ -8,11 +8,11 @@
<color name="primary">#3F51B5</color> <color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color> <color name="primary_dark">#303F9F</color>
<color name="primary_light">#C5CAE9</color> <color name="primary_light">#C5CAE9</color>
<color name="accent">#4CAF50</color> <color name="accent">#FF5300</color>
<color name="primary_text">#212121</color> <color name="primary_text">#212121</color>
<color name="secondary_text">#757575</color> <color name="secondary_text">#727272</color>
<color name="icons">#FFFFFF</color> <color name="icons">#FFFFFF</color>
<color name="divider">#BDBDBD</color> <color name="divider">#B6B6B6</color>
<!-- End colors from Palette --> <!-- End colors from Palette -->
<color name="white">#ffffff</color> <color name="white">#ffffff</color>

View file

@ -11,4 +11,7 @@
<!-- Per the design guidelines, navigation drawers should be between 240dp and 320dp: <!-- Per the design guidelines, navigation drawers should be between 240dp and 320dp:
https://developer.android.com/design/patterns/navigation-drawer.html --> https://developer.android.com/design/patterns/navigation-drawer.html -->
<dimen name="navigation_drawer_width">250dp</dimen> <dimen name="navigation_drawer_width">250dp</dimen>
<dimen name="textsize_badge_count">11sp</dimen>
</resources> </resources>