Commit 97d8fa39 authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge branch 'develop' into fix/validate-server-with-insecure-connection

parents c747aabc 3185bcc9
...@@ -121,7 +121,7 @@ dependencies { ...@@ -121,7 +121,7 @@ dependencies {
compile 'com.trello:rxlifecycle-android:1.0' compile 'com.trello:rxlifecycle-android:1.0'
compile 'com.trello:rxlifecycle-components:1.0' compile 'com.trello:rxlifecycle-components:1.0'
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' compile rootProject.ext.textDrawable
compile 'frankiesardo:icepick:3.2.0' compile 'frankiesardo:icepick:3.2.0'
provided 'frankiesardo:icepick-processor:3.2.0' provided 'frankiesardo:icepick-processor:3.2.0'
......
...@@ -8,6 +8,7 @@ import android.support.v7.graphics.drawable.DrawerArrowDrawable; ...@@ -8,6 +8,7 @@ import android.support.v7.graphics.drawable.DrawerArrowDrawable;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import java.util.List;
import chat.rocket.android.LaunchUtil; import chat.rocket.android.LaunchUtil;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
...@@ -17,11 +18,14 @@ import chat.rocket.android.fragment.sidebar.SidebarMainFragment; ...@@ -17,11 +18,14 @@ import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig; import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.ddp.User; import chat.rocket.android.model.ddp.User;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmObjectObserver; import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.widget.RoomToolbar;
import hugo.weaving.DebugLog; import hugo.weaving.DebugLog;
/** /**
...@@ -30,6 +34,7 @@ import hugo.weaving.DebugLog; ...@@ -30,6 +34,7 @@ import hugo.weaving.DebugLog;
public class MainActivity extends AbstractAuthedActivity { public class MainActivity extends AbstractAuthedActivity {
private RealmObjectObserver<Session> sessionObserver; private RealmObjectObserver<Session> sessionObserver;
private RealmListObserver<RoomSubscription> unreadRoomSubscriptionObserver;
private boolean isForeground; private boolean isForeground;
private StatusTicker statusTicker; private StatusTicker statusTicker;
...@@ -150,6 +155,7 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -150,6 +155,7 @@ public class MainActivity extends AbstractAuthedActivity {
protected void onServerConfigIdUpdated() { protected void onServerConfigIdUpdated() {
super.onServerConfigIdUpdated(); super.onServerConfigIdUpdated();
updateSessionObserver(); updateSessionObserver();
updateUnreadRoomSubscriptionObserver();
updateSidebarMainFragment(); updateSidebarMainFragment();
} }
...@@ -207,6 +213,44 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -207,6 +213,44 @@ public class MainActivity extends AbstractAuthedActivity {
} }
} }
private void updateUnreadRoomSubscriptionObserver() {
if (unreadRoomSubscriptionObserver != null) {
unreadRoomSubscriptionObserver.unsub();
unreadRoomSubscriptionObserver = null;
}
if (serverConfigId == null) {
return;
}
RealmHelper realmHelper = RealmStore.get(serverConfigId);
if (realmHelper == null) {
return;
}
unreadRoomSubscriptionObserver = realmHelper
.createListObserver(realm ->
realm.where(RoomSubscription.class)
.equalTo(RoomSubscription.ALERT, true)
.equalTo(RoomSubscription.OPEN, true)
.findAll())
.setOnUpdateListener(this::updateRoomToolbarUnreadCount);
unreadRoomSubscriptionObserver.sub();
}
private void updateRoomToolbarUnreadCount(List<RoomSubscription> unreadRooms) {
RoomToolbar toolbar = (RoomToolbar) findViewById(R.id.activity_main_toolbar);
if (toolbar != null) {
//ref: Rocket.Chat:client/startup/unread.js
final int numUnreadChannels = unreadRooms.size();
int numMentionsSum = 0;
for (RoomSubscription room : unreadRooms) {
numMentionsSum += room.getUnread();
}
toolbar.setUnreadBudge(numUnreadChannels, numMentionsSum);
}
}
private void updateSidebarMainFragment() { private void updateSidebarMainFragment() {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId)) .replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId))
...@@ -231,6 +275,10 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -231,6 +275,10 @@ public class MainActivity extends AbstractAuthedActivity {
sessionObserver.unsub(); sessionObserver.unsub();
sessionObserver = null; sessionObserver = null;
} }
if (unreadRoomSubscriptionObserver != null) {
unreadRoomSubscriptionObserver.unsub();
unreadRoomSubscriptionObserver = null;
}
super.onDestroy(); super.onDestroy();
} }
......
...@@ -18,6 +18,7 @@ ext { ...@@ -18,6 +18,7 @@ ext {
okhttp3 = 'com.squareup.okhttp3:okhttp:3.4.1' okhttp3 = 'com.squareup.okhttp3:okhttp:3.4.1'
picasso = 'com.squareup.picasso:picasso:2.5.2' picasso = 'com.squareup.picasso:picasso:2.5.2'
picasso2Okhttp3Downloader = 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0' picasso2Okhttp3Downloader = 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
textDrawable = 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
preDexLibs = !"true".equals(System.getenv("CI")) preDexLibs = !"true".equals(System.getenv("CI"))
} }
......
...@@ -35,6 +35,7 @@ dependencies { ...@@ -35,6 +35,7 @@ dependencies {
compile rootProject.ext.supportAppCompat compile rootProject.ext.supportAppCompat
compile rootProject.ext.supportDesign compile rootProject.ext.supportDesign
compile 'org.nibor.autolink:autolink:0.5.0' compile 'org.nibor.autolink:autolink:0.5.0'
compile rootProject.ext.textDrawable
compile rootProject.ext.okhttp3 compile rootProject.ext.okhttp3
compile rootProject.ext.picasso compile rootProject.ext.picasso
compile rootProject.ext.picasso2Okhttp3Downloader compile rootProject.ext.picasso2Okhttp3Downloader
......
...@@ -2,20 +2,29 @@ package chat.rocket.android.widget; ...@@ -2,20 +2,29 @@ package chat.rocket.android.widget;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.graphics.drawable.VectorDrawableCompat; import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat; import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable;
import java.lang.reflect.Field;
public class RoomToolbar extends Toolbar { public class RoomToolbar extends Toolbar {
private TextView titleTextView; private TextView titleTextView;
private ImageView badgeImageView;
public RoomToolbar(Context context) { public RoomToolbar(Context context) {
super(context); super(context);
...@@ -95,4 +104,70 @@ public class RoomToolbar extends Toolbar { ...@@ -95,4 +104,70 @@ public class RoomToolbar extends Toolbar {
titleTextView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null); titleTextView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} }
public void setUnreadBudge(int numUnreadChannels, int numMentionsSum) {
if (getNavigationIcon() == null) {
return;
}
if (badgeImageView == null) {
badgeImageView = new AppCompatImageView(getContext());
badgeImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
if (badgeImageView.getParent() != this) { //ref: Toolbar#isChildOrHidden
addView(badgeImageView, generateDefaultLayoutParams());
}
if (numUnreadChannels > 0) {
if (numMentionsSum > 0) {
badgeImageView.setImageDrawable(getBadgeDrawable(numMentionsSum));
} else {
badgeImageView.setScaleType(ImageView.ScaleType.CENTER);
badgeImageView.setImageResource(R.drawable.badge_without_number);
}
badgeImageView.setVisibility(View.VISIBLE);
} else {
badgeImageView.setVisibility(View.GONE);
}
}
private Drawable getBadgeDrawable(int number) {
String icon = number > 99 ? "99+" : Integer.toString(number);
return TextDrawable.builder()
.beginConfig()
.useFont(Typeface.SANS_SERIF)
.endConfig()
.buildRound(icon, ContextCompat.getColor(getContext(), R.color.badge_color));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (badgeImageView != null && badgeImageView.getVisibility() != View.GONE) {
try {
Field field = Toolbar.class.getDeclaredField("mNavButtonView");
field.setAccessible(true);
ImageButton navButtonView = (ImageButton) field.get(this);
int iconLeft = navButtonView.getLeft();
int iconTop = navButtonView.getTop();
int iconRight = navButtonView.getRight();
int iconBottom = navButtonView.getBottom();
// put badge image at right-top side on the NavButtonView,
// with 1/8 margin and 1/4 scale.
int badgeLeft = iconLeft + (iconRight - iconLeft) * 5 / 8;
int badgeRight = iconLeft + (iconRight - iconLeft) * 7 / 8;
int badgeTop = iconTop + (iconBottom - iconTop) / 8;
int badgeBottom = iconTop + (iconBottom - iconTop) * 3 / 8;
badgeImageView.layout(badgeLeft, badgeTop, badgeRight, badgeBottom);
} catch (Exception exception) {
return;
}
}
}
} }
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/badge_color" />
<size android:width="@dimen/badge_size" android:height="@dimen/badge_size"/>
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="badge_color">#FFFF0000</color>
<dimen name="badge_size">8dp</dimen>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment