Skip to content

Commit

Permalink
made selection work
Browse files Browse the repository at this point in the history
  • Loading branch information
zankia committed Feb 4, 2018
1 parent d16664b commit 455b450
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 31 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ Android application for chatting
## Features
* realtime chat
* timestamp handling
* mulitple selection (useless for now)
* awesome look

## Installation
### APK
The apk is available in [the latest release](https://github.com/zankia/AndroidChat/releases)
The apk is available in [the latest release](https://github.com/zankia/AndroidChat/releases/latest)

### IDE
* import the project
* connect to firebase realtime database
* use gradle wrapper to build

## Screenshot
![screen1](resources/screen1.png)
![screenshot1](resources/screen1.png)
![screenshot2](resources/screen2.png)
![screenshot3](resources/screen3.png)
![screenshot4](resources/screen4.png)
11 changes: 11 additions & 0 deletions app/src/main/java/fr/zankia/android/chat/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ private void logout() {
finish();
}

@Override
public void onBackPressed() {
if(mMessageAdapter.isSelection()) {
mMessageAdapter.setSelection(false);
mMessageAdapter.resetSelection();
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
Expand Down Expand Up @@ -145,6 +155,7 @@ public void onItemLongClick(int position, final Message message) {
public void onClick(DialogInterface dialogInterface, int i) {
switch (i) {
case 0:
mMessageAdapter.setSelection(true);
break;

case 1:
Expand Down
44 changes: 38 additions & 6 deletions app/src/main/java/fr/zankia/android/chat/MessageAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,59 @@

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.curioustechizen.ago.RelativeTimeTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
private static final int TYPE_SENT = 1;

private Listener mListener;
private List<Message> mMessages;
private int selected = -1;
private boolean selection;
private List<Boolean> selected;
private User user;

public MessageAdapter(Listener listener, List<Message> messages, User user) {
this.mListener = listener;
this.mMessages = messages;
this.user = user;
this.selection = false;
this.selected = new ArrayList<>();
resetSelection();
}

public void resetSelection() {
selected.clear();
for (int i = 0; i < mMessages.size(); ++i) {
selected.add(false);
}
}


public void setSelection(boolean selection) {
this.selection = selection;
notifyDataSetChanged();
}

public boolean isSelection() {
return selection;
}


public void setData(List<Message> messages) {
this.mMessages = messages;
resetSelection();
this.notifyDataSetChanged();
}

Expand All @@ -48,7 +70,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setData(mMessages.get(position), position == selected);
holder.setData(mMessages.get(position), selection, selected.get(position));
}

@Override
Expand All @@ -62,6 +84,7 @@ public int getItemViewType(int position) {
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
private CheckBox checkBox;
private TextView nickname, message;
private ImageView image;
private RelativeTimeTextView timestamp;
Expand All @@ -75,9 +98,18 @@ public ViewHolder(View itemView) {
this.message = itemView.findViewById(R.id.message);
this.timestamp = itemView.findViewById(R.id.timestamp);
this.image = itemView.findViewById(R.id.userImage);
this.checkBox = itemView.findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
selected.set(getAdapterPosition(), b);
}
});
}

public void setData(Message m, boolean selected) {
public void setData(Message m, boolean selection, boolean selected) {
checkBox.setVisibility(selection ? View.VISIBLE : View.GONE);
checkBox.setChecked(selected);
Context context = image.getContext();
Glide
.with(context)
Expand All @@ -99,7 +131,7 @@ public boolean onLongClick(View view) {

@Override
public void onClick(View view) {
selected = getAdapterPosition();
selected.set(getAdapterPosition(), !checkBox.isChecked());
notifyDataSetChanged();
}
}
Expand Down
30 changes: 18 additions & 12 deletions app/src/main/res/layout/row_message_received.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="4dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:clipToPadding="false">
android:paddingTop="4dp">

<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="@android:color/holo_blue_light"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="12dp">
app:cardBackgroundColor="@android:color/holo_blue_light"
app:cardCornerRadius="4dp"
app:contentPadding="12dp">

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -31,7 +29,7 @@
android:layout_height="32dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:background="@color/cardview_light_background"/>
android:background="@color/cardview_light_background" />

<LinearLayout
android:layout_width="wrap_content"
Expand All @@ -49,7 +47,7 @@
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="content"/>
tools:text="content" />

<com.github.curioustechizen.ago.RelativeTimeTextView
android:id="@+id/timestamp"
Expand All @@ -64,4 +62,12 @@
</LinearLayout>

</android.support.v7.widget.CardView>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_gravity="right"
android:visibility="gone" />

</FrameLayout>
27 changes: 16 additions & 11 deletions app/src/main/res/layout/row_message_sent.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:gravity="right"
android:paddingBottom="4dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:clipToPadding="false">
android:paddingTop="4dp">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="center"
card_view:cardBackgroundColor="@android:color/holo_green_light"
card_view:cardCornerRadius="4dp"
Expand All @@ -30,8 +29,7 @@
android:id="@+id/userImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp" />
android:layout_marginEnd="12dp" />

<LinearLayout
android:layout_width="wrap_content"
Expand Down Expand Up @@ -64,4 +62,11 @@
</LinearLayout>

</android.support.v7.widget.CardView>
</RelativeLayout>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:visibility="gone" />

</LinearLayout>

0 comments on commit 455b450

Please sign in to comment.