srctree

Robin Lindén parent e7667ccf 39b43a7b
Implement a chat view

inlinesplit
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,23 @@
package ltd.evilcorp.atox
 
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ListView
 
class ChatActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
 
val username = intent.getStringExtra("username")
Log.e("ChatActivity", username)
 
val listView = findViewById<ListView>(R.id.messages)
val messages = ArrayList<MessageModel>()
messages.add(MessageModel("hello", Sender.Received))
messages.add(MessageModel("how are you", Sender.Received))
messages.add(MessageModel("I'm good, thanks.", Sender.Sent))
listView.adapter = MessagesAdapter(this, messages)
}
}
 
app/src/main/java/ltd/evilcorp/atox/ContactListActivity.kt added: 193, removed: 13, total 180
@@ -1,8 +1,11 @@
package ltd.evilcorp.atox
 
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ListView
import kotlinx.android.synthetic.main.contact_list_view_item.view.*
 
class ContactListActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@@ -25,4 +28,10 @@ class ContactListActivity : AppCompatActivity() {
)
listView.adapter = ContactAdapter(this, contacts)
}
 
fun openChat(view: View) {
val intent = Intent(this, ChatActivity::class.java)
intent.putExtra("username", view.name.text)
startActivity(intent)
}
}
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,41 @@
package ltd.evilcorp.atox
 
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
 
class MessagesAdapter(private val context: Context, private val messages: MutableList<MessageModel>) : BaseAdapter() {
override fun getCount(): Int = messages.size
override fun getItem(position: Int): Any = messages[position]
override fun getItemId(position: Int): Long = position.toLong()
 
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view: View?
val vh: ViewHolder
 
if (convertView == null) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = inflater.inflate(
if (messages[position].sender == Sender.Sent) R.layout.message_sent else R.layout.message_received,
null,
true
)
vh = ViewHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ViewHolder
}
 
vh.message.text = messages[position].message
 
return view!!
}
 
private class ViewHolder(row: View) {
val message: TextView = row.findViewById(R.id.message) as TextView
}
}
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,8 @@
package ltd.evilcorp.atox
 
enum class Sender {
Sent,
Received,
}
 
class MessageModel(val message: String, val sender: Sender)
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android">
<shape/>
</inset>
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorGray"/>
<corners android:topLeftRadius="5dp" android:radius="30dp"/>
</shape>
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary"/>
<corners android:topRightRadius="5dp" android:radius="30dp"/>
</shape>
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="match_parent"
android:layout_height="match_parent"
tools:context=".ChatActivity">
<ListView
android:id="@+id/messages"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:divider="@drawable/message_divider"
android:dividerHeight="2dp"
app:layout_constraintBottom_toTopOf="@+id/message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="@+id/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:hint="Message, obviously"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/send"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Send"
android:onClick="sendMessage"
app:layout_constraintBottom_toBottomOf="@+id/message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/message"
app:layout_constraintTop_toTopOf="@+id/message"/>
</android.support.constraint.ConstraintLayout>
 
app/src/main/res/layout/contact_list_view_item.xml added: 193, removed: 13, total 180
@@ -9,7 +9,8 @@
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp">
android:layout_marginStart="10dp"
android:onClick="openChat">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="8dp">
<TextView
android:id="@+id/message"
android:background="@drawable/received_message_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:padding="8dp"
android:textColor="@android:color/white"/>
</android.support.constraint.ConstraintLayout>
 
filename was Deleted added: 193, removed: 13, total 180
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:background="@drawable/sent_message_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:gravity="center"
android:padding="8dp"
android:textColor="@android:color/white"/>
</LinearLayout>
 
app/src/main/res/values/colors.xml added: 193, removed: 13, total 180
@@ -3,4 +3,5 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorGray">#555</color>
</resources>