Skip to content

Commit

Permalink
Improve MessagesActivity by using RecyclerView
Browse files Browse the repository at this point in the history
Add MessagesRecyclerViewAdapter, Remove MessageFragment
Rename fragment_message to view_message

Change some modifiers in GraphView, GraphData
  • Loading branch information
JtheDroid committed Apr 14, 2019
1 parent 7378c75 commit e78bbb5
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 108 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "de.jthedroid.whatsappchatanalyzer"
minSdkVersion 23
targetSdkVersion 28
versionCode 2
versionName "0.02"
versionCode 3
versionName "0.03"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.jthedroid.whatsappchatanalyzer;

class GraphData implements ChatData {
private float[] rawXData, rawYData;
final private float[] rawXData, rawYData;
private float[] xData, yData;
private String[] xDesc, yDesc;
final private String[] xDesc, yDesc;

GraphData(float[] rawXData, float[] rawYData, String[] xDesc, String[] yDesc) {
this.rawXData = rawXData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class GraphView extends View { //TODO: add touch interaction: (scrolling
private GraphData graphData;
private float[] valuesX, valuesY;
private Bitmap bitmap = null;
private Thread thread;
private GraphViewRunnable runnable;
private final Thread thread;
private final GraphViewRunnable runnable;
private View loadingView;
private float lastW, lastH;
private float x, y;
private boolean showTap = false;
private int highlightIndex;
private float padding = 50, textPadding = 5;
private boolean darkTheme;
private Rect textXPos, textXBox, textYPos, textYBox;
final private float padding = 50, textPadding = 5;
private final boolean darkTheme;
final private Rect textXPos, textXBox, textYPos, textYBox;

public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package de.jthedroid.whatsappchatanalyzer;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.Objects;

public class MessagesActivity extends AppCompatActivity {
public class MessagesActivity extends ThemeMenuActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messages);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
int msgCount = Objects.requireNonNull(DataStorage.getInstance().chat.getValue()).getMsgCount();
String tag;
for (int i = 0; i < msgCount; i++) {
tag = "msg" + i;
if (manager.findFragmentByTag(tag) == null)
transaction.add(R.id.linearLayoutMessages, MessageFragment.newInstance(i), tag);
}
transaction.commit();
RecyclerView recyclerView = findViewById(R.id.recyclerViewMessages);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter adapter = new MessagesRecyclerViewAdapter(Objects.requireNonNull(DataStorage.getInstance().chat.getValue()).getMessages());
recyclerView.setAdapter(adapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.jthedroid.whatsappchatanalyzer;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.ArrayList;

public class MessagesRecyclerViewAdapter extends RecyclerView.Adapter {
private final DateFormat dateFormat;
private final ArrayList<Message> messages;


MessagesRecyclerViewAdapter(ArrayList<Message> messages) {
this.messages = messages;
dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
}

@NonNull
@Override
public MessagesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new MessagesViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_message, viewGroup, false));
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
onBindViewHolder((MessagesViewHolder) viewHolder, i);
}

private void onBindViewHolder(MessagesViewHolder holder, int i) {
Message m = messages.get(i);
holder.tvMessage.setText(m.getMsg());
holder.tvDate.setText(dateFormat.format(m.getDate()));
Sender s = m.getSender();
holder.tvSender.setVisibility(s == null ? View.GONE : View.VISIBLE);
if (s != null) holder.tvSender.setText(m.getSender().getName());
}

@Override
public int getItemCount() {
return messages.size();
}

public static class MessagesViewHolder extends RecyclerView.ViewHolder {

final TextView tvMessage, tvSender, tvDate;

MessagesViewHolder(@NonNull View itemView) {
super(itemView);
tvMessage = itemView.findViewById(R.id.textViewMessage);
tvSender = itemView.findViewById(R.id.textViewSender);
tvDate = itemView.findViewById(R.id.textViewDate);
}
}
}
24 changes: 5 additions & 19 deletions app/src/main/res/layout/activity_messages.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.jthedroid.whatsappchatanalyzer.MessagesActivity">
android:layout_height="match_parent">

<ScrollView
android:id="@+id/scrollViewMessages"
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewMessages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:id="@+id/linearLayoutMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>

android:scrollbars="vertical"
tools:listitem="@layout/view_message" />
</android.support.constraint.ConstraintLayout>
20 changes: 0 additions & 20 deletions app/src/main/res/layout/fragment_message.xml

This file was deleted.

54 changes: 54 additions & 0 deletions app/src/main/res/layout/view_message.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="4dp">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textViewSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Sender" />

<TextView
android:id="@+id/textViewMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewSender"
tools:text="Message" />

<TextView
android:id="@+id/textViewDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@android:color/darker_gray"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Date" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

</FrameLayout>

0 comments on commit e78bbb5

Please sign in to comment.