Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert project to androidx and group messages from the same author. #1

Merged
merged 6 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath 'com.novoda:bintray-release:0.8.0'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
7 changes: 4 additions & 3 deletions chatkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'

android {
compileSdkVersion 27
compileSdkVersion 28
buildToolsVersion "27.0.3"

defaultConfig {
Expand All @@ -19,6 +19,7 @@ android {
}

publish {
userOrg = 'egeniq'
groupId = 'com.github.stfalcon'
artifactId = 'chatkit'
publishVersion = '0.3.3'
Expand All @@ -36,7 +37,7 @@ ext {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "com.android.support:appcompat-v7:$supportVersion"
implementation "com.android.support:design:$supportVersion"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.0.0'
implementation "com.google.android:flexbox:$flexboxVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.stfalcon.chatkit.commons;

import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
import android.widget.ImageView;

/**
Expand Down
10 changes: 5 additions & 5 deletions chatkit/src/main/java/com/stfalcon/chatkit/commons/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.stfalcon.chatkit.commons;

import android.support.v7.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public interface IMessage {
* @return the message creation date
*/
Date getCreatedAt();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.stfalcon.chatkit.commons.models;

import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
import com.stfalcon.chatkit.messages.MessageHolders;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package com.stfalcon.chatkit.dialogs;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.util.AttributeSet;

import com.stfalcon.chatkit.commons.models.IDialog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.stfalcon.chatkit.messages;

import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewCompat;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;

import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.util.SparseArray;
Expand Down Expand Up @@ -564,6 +565,7 @@ protected ViewHolder getHolder(ViewGroup parent, int viewType, MessagesListStyle

@SuppressWarnings("unchecked")
protected void bind(final ViewHolder holder, final Object item, boolean isSelected,
boolean isContinuous, boolean isLastItemInGroup,
final ImageLoader imageLoader,
final View.OnClickListener onMessageClickListener,
final View.OnLongClickListener onMessageLongClickListener,
Expand All @@ -573,6 +575,8 @@ protected void bind(final ViewHolder holder, final Object item, boolean isSelect
if (item instanceof IMessage) {
((MessageHolders.BaseMessageViewHolder) holder).isSelected = isSelected;
((MessageHolders.BaseMessageViewHolder) holder).imageLoader = imageLoader;
((BaseMessageViewHolder) holder).isFirstItemInGroup = isContinuous;
((BaseMessageViewHolder) holder).isLastItemInGroup = isLastItemInGroup;
holder.itemView.setOnLongClickListener(onMessageLongClickListener);
holder.itemView.setOnClickListener(onMessageClickListener);

Expand Down Expand Up @@ -676,6 +680,9 @@ public static abstract class BaseMessageViewHolder<MESSAGE extends IMessage> ext

boolean isSelected;

boolean isFirstItemInGroup;

boolean isLastItemInGroup;
/**
* For setting custom data to ViewHolder
*/
Expand Down Expand Up @@ -705,6 +712,31 @@ public boolean isSelected() {
return isSelected;
}

/**
* Returns whether item belongs to the same user
*
* @return weather item belongs to the same user.
*/
public boolean isFirstItemInGroup() {
return isFirstItemInGroup;
}

/**
* Returns whether the current item and the next belongs to the same user
*/
public boolean isLastItemInGroup() {
return isLastItemInGroup;
}

public List<View> getVisibleViewsForFirstItemInGroup() {
return new ArrayList<>();
}

public List<View> getVisibleViewsForLastItemInGroup() {
return new ArrayList<>();
}


/**
* Returns weather is selection mode enabled
*
Expand Down Expand Up @@ -769,6 +801,26 @@ public void onBind(MESSAGE message) {
if (text != null) {
text.setText(message.getText());
}

if (isLastItemInGroup()) {
for (View continousView : getVisibleViewsForLastItemInGroup()) {
continousView.setVisibility(View.VISIBLE);
}
} else {
for (View continousView : getVisibleViewsForLastItemInGroup()) {
continousView.setVisibility(View.GONE);
}
}

if (isFirstItemInGroup()) {
for (View continousView : getVisibleViewsForFirstItemInGroup()) {
continousView.setVisibility(View.VISIBLE);
}
} else {
for (View continousView : getVisibleViewsForFirstItemInGroup()) {
continousView.setVisibility(View.INVISIBLE);
}
}
}

@Override
Expand Down Expand Up @@ -828,6 +880,26 @@ public void onBind(MESSAGE message) {
if (text != null) {
text.setText(message.getText());
}

if (isLastItemInGroup()) {
for (View continousView : getVisibleViewsForLastItemInGroup()) {
continousView.setVisibility(View.VISIBLE);
}
} else {
for (View continousView : getVisibleViewsForLastItemInGroup()) {
continousView.setVisibility(View.GONE);
}
}

if (isFirstItemInGroup()) {
for (View continousView : getVisibleViewsForFirstItemInGroup()) {
continousView.setVisibility(View.VISIBLE);
}
} else {
for (View continousView : getVisibleViewsForFirstItemInGroup()) {
continousView.setVisibility(View.INVISIBLE);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.Space;
import androidx.core.view.ViewCompat;
import androidx.legacy.widget.Space;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.v4.graphics.drawable.DrawableCompat;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.core.graphics.drawable.DrawableCompat;
import android.util.AttributeSet;

import com.stfalcon.chatkit.R;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package com.stfalcon.chatkit.messages;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.util.AttributeSet;

import com.stfalcon.chatkit.commons.models.IMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;

import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.util.SparseArray;
Expand All @@ -34,6 +36,7 @@
import com.stfalcon.chatkit.commons.ImageLoader;
import com.stfalcon.chatkit.commons.ViewHolder;
import com.stfalcon.chatkit.commons.models.IMessage;
import com.stfalcon.chatkit.commons.models.IUser;
import com.stfalcon.chatkit.utils.DateFormatter;

import java.util.ArrayList;
Expand Down Expand Up @@ -103,13 +106,70 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Wrapper wrapper = items.get(position);
holders.bind(holder, wrapper.item, wrapper.isSelected, imageLoader,
boolean isFirstItemInGroup = true;
boolean isLastItemInGroup = true;
if (position < items.size() - 1) {
isFirstItemInGroup = isFirstItemInGroup(items.get(position), items.get(position + 1));
}

if (position > 0) {
isLastItemInGroup = isLastItemInGroup(items.get(position), items.get(position - 1));
}
holders.bind(holder, wrapper.item, wrapper.isSelected, isFirstItemInGroup, isLastItemInGroup, imageLoader,
getMessageClickListener(wrapper),
getMessageLongClickListener(wrapper),
dateHeadersFormatter,
viewClickListenersArray);
}

private boolean isFirstItemInGroup(Wrapper currentMsg, Wrapper precedingMsg) {
// null check
if (currentMsg == null || precedingMsg == null) {
return true;
}

IUser currentUser, precedingUser;
if (currentMsg.item instanceof IMessage) {
currentUser = ((IMessage) currentMsg.item).getUser();
} else {
return true;
}
if (precedingMsg.item instanceof IMessage) {
precedingUser = ((IMessage) precedingMsg.item).getUser();
} else {
return true;
}

// If admin message or
if (currentUser == null || precedingUser == null) return true;
return !(currentUser.getId().equals(precedingUser.getId()));
}

private boolean isLastItemInGroup(Wrapper currentMsg, Wrapper nextMessage) {
// null check
if (currentMsg == null || nextMessage == null) {
return true;
}

IUser currentUser, nextUser;
if (currentMsg.item instanceof IMessage) {
currentUser = ((IMessage) currentMsg.item).getUser();
} else {
return true;
}
if (nextMessage.item instanceof IMessage) {
nextUser = ((IMessage) nextMessage.item).getUser();
} else {
return true;
}

// If admin message or
if (currentUser == null || nextUser == null) {
return true;
}
return !currentUser.getId().equals(nextUser.getId());
}

@Override
public int getItemCount() {
return items.size();
Expand Down Expand Up @@ -155,7 +215,11 @@ public void addToStart(MESSAGE message, boolean scroll) {
}
Wrapper<MESSAGE> element = new Wrapper<>(message);
items.add(0, element);
boolean isContinuous = isPreviousSameAuthor(message.getUser().getId(), 0);
notifyItemRangeInserted(0, isNewMessageToday ? 2 : 1);
if (isContinuous) {
notifyItemChanged(1);
}
if (layoutManager != null && scroll) {
layoutManager.scrollToPosition(0);
}
Expand Down
Loading