Commit fe855947 authored by Grigory Fedorov's avatar Grigory Fedorov

Refactoring: new ContactTitleExpandableToolbarInflater:

 for creating activities with fake expandable  toolbar with contact title.
parent 3d1431d2
......@@ -2,200 +2,37 @@ package com.xabber.android.ui;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.github.ksoichiro.android.observablescrollview.ScrollUtils;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.intent.AccountIntentBuilder;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.helper.ContactTitleInflater;
import com.xabber.android.ui.helper.ContactTitleExpandableToolbarInflater;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
import static java.lang.Math.pow;
import static java.lang.Math.round;
import static java.lang.Math.sqrt;
public class ContactViewerNew extends ManagedActivity {
public class ContactViewerNew extends ManagedActivity implements ObservableScrollViewCallbacks {
private int toolbarHeight;
private View avatarView;
private View titleView;
private int paddingLeftMin;
private int paddingRight;
private int actionBarSize;
private int toolbarHeightDelta;
private int avatarLargeSize;
private int avatarNormalSize;
private int avatarRadius;
private View contactNamePanel;
private int contactTitlePaddingBottomBig;
private int contactTitlePaddingBottomSmall;
private ContactTitleExpandableToolbarInflater contactTitleExpandableToolbarInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_viewer);
setSupportActionBar((Toolbar) findViewById(R.id.contact_viewer_toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
int[] accountActionBarColors;
int[] accountStatusBarColors;
accountActionBarColors = getResources().getIntArray(R.array.account_action_bar);
accountStatusBarColors = getResources().getIntArray(R.array.account_status_bar);
titleView = findViewById(R.id.title);
avatarView = findViewById(R.id.avatar);
contactNamePanel = findViewById(R.id.contact_name_panel);
paddingLeftMin = getResources().getDimensionPixelSize(R.dimen.contact_title_padding_left);
paddingRight = getResources().getDimensionPixelSize(R.dimen.contact_title_padding_right);
avatarLargeSize = getResources().getDimensionPixelSize(R.dimen.avatar_large_size);
avatarNormalSize = getResources().getDimensionPixelSize(R.dimen.avatar_normal_size);
avatarRadius = getResources().getDimensionPixelSize(R.dimen.avatar_radius);
contactTitlePaddingBottomBig = getResources().getDimensionPixelSize(R.dimen.contact_title_padding_bottom_big);
contactTitlePaddingBottomSmall = getResources().getDimensionPixelSize(R.dimen.contact_title_padding_bottom_small);
TypedArray a = getTheme().obtainStyledAttributes(R.style.Theme, new int[]{R.attr.colorPrimary});
contactTitleExpandableToolbarInflater = new ContactTitleExpandableToolbarInflater(this);
AbstractContact bestContact = RosterManager.getInstance().getBestContact(getAccount(getIntent()), getUser(getIntent()));
contactTitleExpandableToolbarInflater.onCreate(bestContact);
ContactTitleInflater.updateTitle(titleView, this, bestContact);
int colorLevel = AccountManager.getInstance().getColorLevel(bestContact.getAccount());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(accountStatusBarColors[colorLevel]);
}
titleView.setBackgroundDrawable(new ColorDrawable(accountActionBarColors[colorLevel]));
toolbarHeight = getResources().getDimensionPixelSize(R.dimen.toolbar_height);
final ObservableScrollView scrollView = (ObservableScrollView) findViewById(R.id.scroll);
scrollView.setScrollViewCallbacks(this);
ScrollUtils.addOnGlobalLayoutListener(findViewById(R.id.contact_viewer_toolbar), new Runnable() {
@Override
public void run() {
updateFlexibleSpaceText(scrollView.getCurrentScrollY());
}
});
}
@Override
protected void onResume() {
super.onResume();
actionBarSize = getActionBarSize();
toolbarHeightDelta = toolbarHeight - actionBarSize;
}
protected int getActionBarSize() {
TypedValue typedValue = new TypedValue();
int[] textSizeAttr = new int[]{R.attr.actionBarSize};
int indexOfAttrTextSize = 0;
TypedArray a = obtainStyledAttributes(typedValue.data, textSizeAttr);
int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
a.recycle();
return actionBarSize;
}
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
updateFlexibleSpaceText(scrollY);
}
@Override
public void onDownMotionEvent() {
}
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
}
private void updateFlexibleSpaceText(final int scrollY) {
setLeftPadding(scrollY);
setTopPadding(scrollY);
setAvatarSize(scrollY);
setHeight(scrollY);
}
private void setTopPadding(int scrollY) {
int paddingDelta = contactTitlePaddingBottomBig - contactTitlePaddingBottomSmall;
int paddingBottom = contactTitlePaddingBottomBig - scrollY * paddingDelta / toolbarHeightDelta;
if (scrollY <= 0) {
paddingBottom = contactTitlePaddingBottomBig;
}
if (scrollY >= toolbarHeightDelta) {
paddingBottom = contactTitlePaddingBottomSmall;
}
contactNamePanel.setPadding(0, 0, 0, paddingBottom);
}
private void setAvatarSize(int scrollY) {
int newAvatarSize = avatarLargeSize - (scrollY / 2);
if (newAvatarSize < avatarNormalSize) {
newAvatarSize = avatarNormalSize;
}
if (avatarView.getWidth() != newAvatarSize) {
avatarView.getLayoutParams().width = newAvatarSize;
avatarView.getLayoutParams().height = newAvatarSize;
}
}
private void setHeight(int scrollY) {
int newHeight = toolbarHeight - scrollY;
if (newHeight < actionBarSize) {
newHeight = actionBarSize;
}
titleView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, newHeight));
}
private void setLeftPadding(int scrollY) {
int paddingLeft = (int) round(sqrt(pow(avatarRadius, 2) - pow(scrollY - avatarRadius, 2)));
if (scrollY < 0) {
paddingLeft = paddingLeftMin;
}
if (scrollY > avatarRadius) {
paddingLeft = avatarRadius;
}
if (paddingLeft < paddingLeftMin) {
paddingLeft = paddingLeftMin;
}
contactTitleExpandableToolbarInflater.onResume();
titleView.setPadding(paddingLeft, 0, paddingRight, 0);
}
public static Intent createIntent(Context context, String account, String user) {
......
......@@ -92,7 +92,7 @@ public class StatusEditor extends ManagedListActivity implements
setListAdapter(adapter);
statusTextView = (EditText) header.findViewById(R.id.status_text);
statusModeView = (Spinner) header.findViewById(R.id.status_mode);
statusModeView = (Spinner) header.findViewById(R.id.status_icon);
statusModeView.setAdapter(new StatusModeAdapter(this));
findViewById(R.id.ok).setOnClickListener(this);
......
......@@ -129,7 +129,7 @@ public class AccountToggleAdapter implements UpdatableAdapter {
* @return The data for the specified view.
*/
public String getItemForView(View view) {
if (view.getId() == R.id.status_mode) {
if (view.getId() == R.id.status_icon) {
view = (View) view.getParent();
}
for (int index = 0; index < linearLayout.getChildCount(); index++) {
......@@ -146,7 +146,7 @@ public class AccountToggleAdapter implements UpdatableAdapter {
final ImageView disabled;
public AccountViewHolder(View view) {
statusMode = (ImageView) view.findViewById(R.id.status_mode);
statusMode = (ImageView) view.findViewById(R.id.status_icon);
avatar = (ImageView) view.findViewById(R.id.avatar);
disabled = (ImageView) view.findViewById(R.id.disabled);
}
......
......@@ -92,7 +92,7 @@ public class OccupantListAdapter extends BaseAdapter implements
final TextView statusTextView = (TextView) view
.findViewById(R.id.status);
final ImageView statusModeView = (ImageView) view
.findViewById(R.id.status_mode);
.findViewById(R.id.status_icon);
if (MUCManager.getInstance().getNickname(account, room)
.equalsIgnoreCase(occupant.getNickname()))
avatarView.setImageDrawable(AvatarManager.getInstance()
......
......@@ -58,7 +58,7 @@ public class StatusContactInflater extends BaseContactInflater {
public ViewHolder(View view) {
super(view);
statusMode = (ImageView) view.findViewById(R.id.status_mode);
statusMode = (ImageView) view.findViewById(R.id.status_icon);
}
}
......
package com.xabber.android.ui.helper;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.github.ksoichiro.android.observablescrollview.ScrollUtils;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.androiddev.R;
import static java.lang.Math.pow;
import static java.lang.Math.round;
import static java.lang.Math.sqrt;
public class ContactTitleExpandableToolbarInflater implements ObservableScrollViewCallbacks {
private final ActionBarActivity activity;
private View avatarView;
private View titleView;
private View contactNamePanel;
private int toolbarHeight;
private int paddingLeftMin;
private int paddingRight;
private int actionBarSize;
private int toolbarHeightDelta;
private int avatarLargeSize;
private int avatarNormalSize;
private int avatarRadius;
private int contactTitlePaddingBottomBig;
private int contactTitlePaddingBottomSmall;
public ContactTitleExpandableToolbarInflater(ActionBarActivity activity) {
this.activity = activity;
}
public void onCreate(AbstractContact abstractContact) {
activity.setContentView(R.layout.expandable_contact_title_activity);
activity.setSupportActionBar((Toolbar) activity.findViewById(R.id.toolbar_overlay));
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
avatarView = activity.findViewById(R.id.avatar);
contactNamePanel = activity.findViewById(R.id.contact_name_panel);
titleView = activity.findViewById(R.id.expandable_contact_title);
ContactTitleInflater.updateTitle(titleView, activity, abstractContact);
int[] accountStatusBarColors = activity.getResources().getIntArray(R.array.account_status_bar);
int colorLevel = AccountManager.getInstance().getColorLevel(abstractContact.getAccount());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(accountStatusBarColors[colorLevel]);
}
final ObservableScrollView scrollView = (ObservableScrollView) activity.findViewById(R.id.scroll);
scrollView.setScrollViewCallbacks(this);
ScrollUtils.addOnGlobalLayoutListener(activity.findViewById(R.id.toolbar_overlay), new Runnable() {
@Override
public void run() {
updateFlexibleSpaceText(scrollView.getCurrentScrollY());
}
});
}
public void onResume() {
Resources resources = activity.getResources();
paddingLeftMin = resources.getDimensionPixelSize(R.dimen.contact_title_padding_left);
paddingRight = resources.getDimensionPixelSize(R.dimen.contact_title_padding_right);
avatarLargeSize = resources.getDimensionPixelSize(R.dimen.avatar_large_size);
avatarNormalSize = resources.getDimensionPixelSize(R.dimen.avatar_normal_size);
avatarRadius = resources.getDimensionPixelSize(R.dimen.avatar_radius);
contactTitlePaddingBottomBig = resources.getDimensionPixelSize(R.dimen.contact_title_padding_bottom_big);
contactTitlePaddingBottomSmall = resources.getDimensionPixelSize(R.dimen.contact_title_padding_bottom_small);
toolbarHeight = resources.getDimensionPixelSize(R.dimen.toolbar_height);
actionBarSize = getActionBarSize();
toolbarHeightDelta = toolbarHeight - actionBarSize;
}
protected int getActionBarSize() {
TypedValue typedValue = new TypedValue();
int[] textSizeAttr = new int[]{R.attr.actionBarSize};
int indexOfAttrTextSize = 0;
TypedArray a = activity.obtainStyledAttributes(typedValue.data, textSizeAttr);
int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
a.recycle();
return actionBarSize;
}
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
updateFlexibleSpaceText(scrollY);
}
@Override
public void onDownMotionEvent() {
}
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
}
private void updateFlexibleSpaceText(final int scrollY) {
setLeftPadding(scrollY);
setTopPadding(scrollY);
setAvatarSize(scrollY);
setHeight(scrollY);
}
private void setTopPadding(int scrollY) {
int paddingDelta = contactTitlePaddingBottomBig - contactTitlePaddingBottomSmall;
int paddingBottom = contactTitlePaddingBottomBig - scrollY * paddingDelta / toolbarHeightDelta;
if (scrollY <= 0) {
paddingBottom = contactTitlePaddingBottomBig;
}
if (scrollY >= toolbarHeightDelta) {
paddingBottom = contactTitlePaddingBottomSmall;
}
contactNamePanel.setPadding(0, 0, 0, paddingBottom);
}
private void setAvatarSize(int scrollY) {
int newAvatarSize = avatarLargeSize - (scrollY / 2);
if (newAvatarSize < avatarNormalSize) {
newAvatarSize = avatarNormalSize;
}
if (avatarView.getWidth() != newAvatarSize) {
avatarView.getLayoutParams().width = newAvatarSize;
avatarView.getLayoutParams().height = newAvatarSize;
}
}
private void setHeight(int scrollY) {
int newHeight = toolbarHeight - scrollY;
if (newHeight < actionBarSize) {
newHeight = actionBarSize;
}
titleView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, newHeight));
}
private void setLeftPadding(int scrollY) {
int paddingLeft = (int) round(sqrt(pow(avatarRadius, 2) - pow(scrollY - avatarRadius, 2)));
if (scrollY < 0) {
paddingLeft = paddingLeftMin;
}
if (scrollY > avatarRadius) {
paddingLeft = avatarRadius;
}
if (paddingLeft < paddingLeftMin) {
paddingLeft = paddingLeftMin;
}
titleView.setPadding(paddingLeft, 0, paddingRight, 0);
}
}
......@@ -62,7 +62,7 @@ public class ContactTitleInflater {
}
private static void setStatus(Activity activity, View titleView, AbstractContact abstractContact) {
final ImageView statusModeView = (ImageView) titleView.findViewById(R.id.status_mode);
final ImageView statusModeView = (ImageView) titleView.findViewById(R.id.status_icon);
int statusLevel = abstractContact.getStatusMode().getStatusLevel();
if (isContactOffline(statusLevel)) {
......
......@@ -58,7 +58,7 @@ public class StatusPreference extends Preference {
@Override
protected void onBindView(View view) {
super.onBindView(view);
((ImageView) view.findViewById(R.id.status_mode))
((ImageView) view.findViewById(R.id.status_icon))
.setImageLevel(statusMode.getStatusLevel());
}
......
......@@ -27,7 +27,7 @@
android:src="@drawable/ic_avatar_1"
/>
<ImageView
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_status_account"
......
......@@ -88,7 +88,7 @@
android:textColor="?android:attr/textColorSecondary"
/>
<ImageView
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:layout_width="@dimen/status_icon_width"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title"
android:id="@+id/expandable_contact_title"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/green_500"
......@@ -76,7 +76,7 @@
</LinearLayout>
<ImageView
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:src="@drawable/ic_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
......@@ -13,7 +13,7 @@
android:scrollbars="vertical">
<FrameLayout
android:id="@+id/body"
android:id="@+id/scrollable_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/toolbar_height">
......@@ -32,7 +32,7 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title"
android:id="@+id/expandable_contact_title"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@color/green_500"
......@@ -94,7 +94,7 @@
</LinearLayout>
<ImageView
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:src="@drawable/ic_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......@@ -105,7 +105,7 @@
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/contact_viewer_toolbar"
android:id="@+id/toolbar_overlay"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
......
......@@ -72,7 +72,7 @@
android:textColor="?android:attr/textColorSecondary"
/>
<ImageView
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:layout_width="@dimen/status_icon_width"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
......
......@@ -14,7 +14,7 @@
-->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
......
......@@ -18,7 +18,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Spinner
android:id="@+id/status_mode"
android:id="@+id/status_icon"
android:prompt="@string/status_editor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
......
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