Skip to content
This repository was archived by the owner on Nov 19, 2022. It is now read-only.

Commit 8388cd3

Browse files
committed
Messages begin at the bottom
1 parent 047921c commit 8388cd3

File tree

4 files changed

+72
-50
lines changed

4 files changed

+72
-50
lines changed

app/src/main/java/com/munchmates/android/Activities/ConversationActivity.kt

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.munchmates.android.Activities
22

33
import android.content.Intent
4-
import android.content.res.Configuration
54
import android.graphics.Color
65
import android.os.Bundle
7-
import android.support.v7.app.AppCompatActivity
6+
import android.support.v7.widget.LinearLayoutManager
7+
import android.support.v7.widget.RecyclerView
88
import android.view.*
9+
import android.widget.LinearLayout
910
import android.widget.TextView
10-
import com.google.firebase.auth.FirebaseAuth
1111
import com.google.firebase.database.*
1212
import com.munchmates.android.App
1313
import com.munchmates.android.DatabaseObjs.Message
@@ -17,13 +17,17 @@ import com.munchmates.android.DatabaseObjs.User
1717
import com.munchmates.android.R
1818
import com.munchmates.android.Utils
1919
import kotlinx.android.synthetic.main.activity_conversation.*
20+
import android.support.v7.widget.DividerItemDecoration
2021

21-
class ConversationActivity : BaseMMActivity(), View.OnClickListener {
22+
class ConversationActivity : BaseMMActivity(), View.OnClickListener, Runnable {
2223

2324
val usersRef = FirebaseDatabase.getInstance().reference
2425
var messages = arrayListOf<Message>()
2526
var uid = ""
2627
var them = User()
28+
var count = 0
29+
lateinit var cAdapter: RecyclerView.Adapter<*>
30+
lateinit var manager: LinearLayoutManager
2731

2832
override fun onCreate(savedInstanceState: Bundle?) {
2933
super.onCreate(savedInstanceState)
@@ -32,14 +36,31 @@ class ConversationActivity : BaseMMActivity(), View.OnClickListener {
3236

3337
uid = intent.getStringExtra("uid")
3438
them = App.users[uid]!!
35-
listMessages()
39+
40+
manager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true)
41+
42+
val thread = Thread(this)
43+
thread.start()
44+
}
45+
46+
override fun run() {
47+
while(true) {
48+
val total = App.user.conversations.messageList[uid]!!.messages.size
49+
if(total != count) {
50+
count = total
51+
runOnUiThread {
52+
listMessages()
53+
}
54+
}
55+
Thread.sleep(2500)
56+
}
3657
}
3758

3859
private fun listMessages() {
39-
messages = arrayListOf()
60+
messages.removeAll(messages)
4061
if (App.user.conversations.messageList.contains(uid)) {
4162
// sort and remove old messages
42-
messages = Utils.sortMessage(App.user.conversations.messageList[uid]!!.messages, uid)
63+
messages.addAll(Utils.sortMessage(App.user.conversations.messageList[uid]!!.messages, uid))
4364

4465
// remove messages from db
4566
val usersRef = FirebaseDatabase.getInstance().reference.child("USERS/${App.user.uid}/conversations/messageList/$uid/messages")
@@ -50,27 +71,13 @@ class ConversationActivity : BaseMMActivity(), View.OnClickListener {
5071
}
5172

5273
private fun buildList() {
53-
conv_list_msgs.removeAllViews()
54-
for (msg in messages) {
55-
val view = LayoutInflater.from(this).inflate(R.layout.item_message, conv_list_msgs as ViewGroup, false)
56-
57-
view.findViewById<TextView>(R.id.msg_text_msg).text = msg.text
58-
view.findViewById<TextView>(R.id.msg_text_sender).text = "UID: ${msg.sender_id}"
59-
view.findViewById<TextView>(R.id.msg_text_date).text = msg.dateTime
60-
61-
var user = App.user
62-
if (msg.sender_id == uid) {
63-
user = them
64-
}
65-
view.findViewById<TextView>(R.id.msg_text_sender).text = "${user.firstName} ${user.lastName}"
66-
67-
if (msg.sender_id == App.user.uid) {
68-
view.setBackgroundColor(Color.parseColor("#EEEEEE"))
69-
}
70-
conv_list_msgs.addView(view)
71-
conv_list_msgs.addView(LayoutInflater.from(this).inflate(R.layout.spacer, conv_list_msgs as ViewGroup, false))
74+
cAdapter = ConvAdapter(messages.toTypedArray())
75+
val recyclerView = findViewById<RecyclerView>(R.id.conv_list_msgs).apply {
76+
setHasFixedSize(true)
77+
layoutManager = manager
78+
adapter = cAdapter
7279
}
73-
conv_scroll.post { conv_scroll.fullScroll(View.FOCUS_DOWN) }
80+
recyclerView.addItemDecoration(DividerItemDecoration(recyclerView.context, manager.orientation))
7481
}
7582

7683
override fun onClick(v: View?) {
@@ -81,8 +88,6 @@ class ConversationActivity : BaseMMActivity(), View.OnClickListener {
8188
var newMsg = Message("${App.user.firstName} ${App.user.lastName}", App.user.uid, message, Utils.getDate(Utils.messageFormat), System.currentTimeMillis() / 1000.0)
8289
addMessage(newMsg, App.user, them)
8390
addMessage(newMsg, them, App.user)
84-
messages.add(newMsg)
85-
buildList()
8691
}
8792
}
8893
}
@@ -121,4 +126,30 @@ class ConversationActivity : BaseMMActivity(), View.OnClickListener {
121126
else -> return super.onOptionsItemSelected(item)
122127
}
123128
}
129+
130+
class ConvAdapter(private val data: Array<Message>): RecyclerView.Adapter<ConvAdapter.ViewHolder>() {
131+
class ViewHolder(val layout: LinearLayout): RecyclerView.ViewHolder(layout)
132+
133+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
134+
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false) as LinearLayout
135+
return ViewHolder(view)
136+
}
137+
138+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
139+
val view = holder.layout
140+
val msg = data[position]
141+
view.findViewById<TextView>(R.id.msg_text_msg).text = msg.text
142+
view.findViewById<TextView>(R.id.msg_text_sender).text = "UID: ${msg.sender_id}"
143+
view.findViewById<TextView>(R.id.msg_text_date).text = msg.dateTime
144+
145+
var user = App.users[msg.sender_id]!!
146+
view.findViewById<TextView>(R.id.msg_text_sender).text = "${user.firstName} ${user.lastName}"
147+
148+
if (msg.sender_id == App.user.uid) {
149+
view.setBackgroundColor(Color.parseColor("#EEEEEE"))
150+
}
151+
}
152+
153+
override fun getItemCount(): Int = data.size
154+
}
124155
}

app/src/main/java/com/munchmates/android/Utils.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.munchmates.android
22

3-
import com.google.firebase.database.FirebaseDatabase
43
import com.munchmates.android.DatabaseObjs.Message
54
import com.munchmates.android.DatabaseObjs.Sender
65
import java.text.SimpleDateFormat
@@ -34,27 +33,23 @@ class Utils {
3433
val unsorted = HashMap(messages)
3534
val filtered = hashMapOf<String, Message>()
3635
val sorted = arrayListOf<Message>()
37-
val delta = messages.size - 20
3836

3937
var i = 0
4038
while(unsorted.size > 0) {
41-
var lowest = ""
39+
var highest = ""
4240
var timestamp = 0.0
4341
for(message in unsorted) {
44-
if(message.value.timeStamp < timestamp || lowest == "") {
42+
if(message.value.timeStamp > timestamp || highest == "") {
4543
timestamp = message.value.timeStamp
46-
lowest = message.key
44+
highest = message.key
4745
}
4846
}
4947

50-
if(i >= delta) {
51-
filtered[lowest] = unsorted[lowest]!!
52-
sorted.add(unsorted[lowest]!!)
53-
}
54-
else if(uid.isNullOrEmpty()) {
55-
sorted.add(unsorted[lowest]!!)
48+
if(!(i >= 20 && !uid.isNullOrEmpty())) {
49+
filtered[highest] = unsorted[highest]!!
50+
sorted.add(unsorted[highest]!!)
5651
}
57-
unsorted.remove(lowest)
52+
unsorted.remove(highest)
5853
i++
5954
}
6055

app/src/main/res/layout/activity_conversation.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
android:orientation="vertical" android:layout_width="match_parent"
44
android:layout_height="match_parent">
55

6-
<ScrollView
7-
android:id="@+id/conv_scroll"
6+
<android.support.v7.widget.RecyclerView
7+
android:id="@+id/conv_list_msgs"
88
android:layout_width="match_parent"
99
android:layout_height="0dp"
10+
android:scrollbars="vertical"
1011
android:layout_weight="1">
11-
<LinearLayout
12-
android:id="@+id/conv_list_msgs"
13-
android:layout_width="match_parent"
14-
android:layout_height="wrap_content"
15-
android:orientation="vertical"/>
16-
</ScrollView>
12+
</android.support.v7.widget.RecyclerView>
1713

1814
<View android:layout_width="match_parent" android:layout_height="1dp" android:background="#80808080" />
1915

app/src/main/res/layout/item_message.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:orientation="vertical" android:layout_width="match_parent"
4-
android:layout_height="match_parent" android:padding="8dp">
4+
android:layout_height="wrap_content" android:padding="8dp">
55

66
<LinearLayout
77
android:layout_width="match_parent"

0 commit comments

Comments
 (0)