Commit d6350ba8 authored by Wilko Manger's avatar Wilko Manger

Handle m.room.member events

parent 9da42919
Pipeline #245 passed with stage
in 2 minutes and 53 seconds
......@@ -21,10 +21,7 @@ package im.pattle.app.data.chat.event
import android.util.Log
import im.pattle.app.data.chat.event.ChatEventSource
import im.pattle.app.data.chat.event.model.ChatEvent
import im.pattle.app.data.chat.event.model.ChatMessage
import im.pattle.app.data.chat.event.model.ChatNotice
import im.pattle.app.data.chat.event.model.Message
import im.pattle.app.data.chat.event.model.*
import im.pattle.app.data.sync.SyncManager
import im.pattle.app.data.user.User
import im.pattle.app.sdk.Matrix
......@@ -36,6 +33,7 @@ import org.matrix.androidsdk.listeners.MXEventListener
import org.matrix.androidsdk.rest.callback.SimpleApiCallback
import org.matrix.androidsdk.rest.model.Event
import org.matrix.androidsdk.rest.model.Event.EVENT_TYPE_MESSAGE
import org.matrix.androidsdk.rest.model.Event.EVENT_TYPE_STATE_ROOM_MEMBER
import org.matrix.androidsdk.rest.model.TokensChunkEvents
import org.matrix.androidsdk.rest.model.TokensChunkResponse
import java.lang.NullPointerException
......@@ -106,6 +104,32 @@ class ChatEventRemoteSource(private val matrix: Matrix,
}
}
}
EVENT_TYPE_STATE_ROOM_MEMBER -> {
val membership = json?.get("membership")?.asString
Log.d("membership", membership)
val type = when (membership) {
"invite" -> MemberChangeType.INVITE
"join" -> MemberChangeType.JOIN
"leave" -> MemberChangeType.LEAVE
"ban" -> MemberChangeType.BAN
"knock" -> MemberChangeType.KNOCK
else -> MemberChangeType.KNOCK
}
val subject = User(event.stateKey,
json?.get("displayname")?.asString ?: "")
Log.d("subject", subject.toString())
MemberChangeEvent(
id = id,
sentAt = sentAt,
sender = sender,
subject = subject,
type = type
)
}
else -> {
// TODO: Don't handle
// everything as a message
......
/**
* Copyright (C) 2018 Wilko Manger
*
* This file is part of Pattle.
*
* Pattle is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pattle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pattle. If not, see <https://www.gnu.org/licenses/>.
*/
package im.pattle.app.data.chat.event.model
import im.pattle.app.data.user.User
import java.util.*
enum class MemberChangeType {
INVITE,
JOIN,
LEAVE,
BAN,
KNOCK
}
data class MemberChangeEvent(override val id: String,
override val sentAt: Date,
override val sender: User,
val subject: User,
val type: MemberChangeType)
: ChatEvent()
\ No newline at end of file
......@@ -29,4 +29,12 @@ data class User(val id: String,
super.equals(other)
}
}
override fun toString(): String {
return if (name != id && name != "") {
name
} else {
id
}
}
}
\ No newline at end of file
......@@ -20,7 +20,6 @@
package im.pattle.app.ui.main.chat
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View.GONE
import android.view.ViewGroup
......@@ -28,14 +27,13 @@ import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import im.pattle.app.R
import im.pattle.app.data.chat.event.model.ChatEvent
import im.pattle.app.data.chat.event.model.ChatMessage
import im.pattle.app.data.chat.event.model.ChatNotice
import im.pattle.app.data.chat.event.model.*
import im.pattle.app.data.chat.overview.ChatOverview
import im.pattle.app.data.user.User
import im.pattle.app.ui.util.*
import kotlinx.android.synthetic.main.list_item_chat_recv_message.view.*
import kotlinx.android.synthetic.main.view_date_header.view.*
import kotlinx.android.synthetic.main.view_info.view.*
import java.util.*
class ChatEventAdapter(private val isDirect: Boolean,
......@@ -50,9 +48,10 @@ class ChatEventAdapter(private val isDirect: Boolean,
private const val TEXT_MESSAGE_SENT = 1
private const val DATE_HEADER = 2
private const val NOTICE = 3
private const val MEMBERSHIP_CHANGE = 4
}
class DateHeaderHolder(val layout: LinearLayout)
class InfoHolder(val layout: LinearLayout)
: RecyclerView.ViewHolder(layout)
class EventHolder(val layout: ConstraintLayout)
......@@ -113,13 +112,17 @@ class ChatEventAdapter(private val isDirect: Boolean,
as ConstraintLayout)
}
DATE_HEADER -> {
DateHeaderHolder(inflate(R.layout.view_date_header)
InfoHolder(inflate(R.layout.view_date_header)
as LinearLayout)
}
NOTICE -> {
EventHolder(inflate(R.layout.list_item_chat_notice)
as ConstraintLayout)
}
MEMBERSHIP_CHANGE -> {
InfoHolder(inflate(R.layout.view_info)
as LinearLayout)
}
// TODO: Handle non supported items
else -> {
EventHolder(inflate(R.layout.list_item_chat_recv_message)
......@@ -134,6 +137,34 @@ class ChatEventAdapter(private val isDirect: Boolean,
val viewType = getItemViewType(position)
when (item) {
is MemberChangeEvent -> {
holder as InfoHolder
val text = when(item.type) {
MemberChangeType.INVITE -> {
context.getString(R.string.invited,
item.sender.toString(),
item.subject.toString())
}
MemberChangeType.JOIN -> {
context.getString(R.string.has_joined,
item.subject.toString())
}
MemberChangeType.LEAVE -> {
context.getString(R.string.has_left,
item.subject.toString())
}
MemberChangeType.BAN -> {
context.getString(R.string.banned,
item.subject.toString(),
item.sender.toString())
}
MemberChangeType.KNOCK -> "Knock, knock."
}
holder.layout.title.text = text
}
// TODO: Remove pasted code
is ChatNotice -> {
holder as EventHolder
......@@ -164,8 +195,8 @@ class ChatEventAdapter(private val isDirect: Boolean,
}
}
is DateHeader -> {
holder as DateHeaderHolder
holder.layout.textView.text = item.formattedDate
holder as InfoHolder
holder.layout.date.text = item.formattedDate
}
}
}
......@@ -183,6 +214,7 @@ class ChatEventAdapter(private val isDirect: Boolean,
TEXT_MESSAGE_RECEIVED
}
}
is MemberChangeEvent -> MEMBERSHIP_CHANGE
else -> UNSUPPORTED
}
}
......
......@@ -109,7 +109,7 @@ class PinnedDateHeaderManager(
private val showDateHeader = Runnable {
if (!showingDateHeader) {
if (currentDateHeader != null) {
pinnedDateHeaderView.textView.text =
pinnedDateHeaderView.date.text =
currentDateHeader!!.formattedDate
moveDateHeader(targetHeaderMargin)
......@@ -209,7 +209,7 @@ class PinnedDateHeaderManager(
currentDateHeader = firstHeaderFrom(visibleItems.first())
}
pinnedDateHeaderView.textView.text = currentDateHeader?.formattedDate
pinnedDateHeaderView.date.text = currentDateHeader?.formattedDate
}
}
......
......@@ -24,12 +24,10 @@ import android.graphics.Typeface
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import im.pattle.app.R
import im.pattle.app.data.chat.event.model.TypingEvent
......@@ -78,7 +76,7 @@ class ChatOverviewAdapter(private val onClick: (ChatOverview) -> Unit,
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val chat = items[position]
holder.layout.title.text = chat.name
holder.layout.content.text = chat.name
// Set subtitle
if (chat.lastEvent != null) {
......
......@@ -21,7 +21,7 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
android:shape="rectangle">
<!-- Background color -->
<solid android:color="@color/colorDateHeader"/>
<solid android:color="@color/colorInfo"/>
<!-- Corner radius -->
<corners android:radius="@dimen/chat_item_corner_radius"/>
......
......@@ -39,7 +39,7 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintTop_toBottomOf="@+id/content"
app:layout_constraintVertical_bias="0.0">
<ImageView
......@@ -104,7 +104,7 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/title"
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
......
......@@ -39,7 +39,7 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/screen_horizontal_margin"
......@@ -70,7 +70,7 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
app:layout_constraintBottom_toBottomOf="@+id/image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/image"
app:layout_constraintTop_toBottomOf="@+id/title" />
app:layout_constraintTop_toBottomOf="@+id/content" />
<TextView
android:id="@+id/date"
......@@ -79,8 +79,8 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
android:layout_marginEnd="@dimen/screen_horizontal_margin"
android:layout_marginRight="@dimen/screen_horizontal_margin"
android:textSize="@dimen/chat_overview_date_text_size"
app:layout_constraintBaseline_toBaselineOf="@+id/title"
app:layout_constraintBaseline_toBaselineOf="@+id/content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/title" />
app:layout_constraintStart_toEndOf="@+id/content" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -10,14 +10,14 @@
android:padding="4dp">
<TextView
android:id="@+id/textView"
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/date_header"
android:background="@drawable/info"
android:elevation="2dp"
android:gravity="center"
android:padding="8dp"
android:text="TextView"
android:textSize="13sp"
android:elevation="2dp"/>
android:textSize="13sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="@dimen/date_header_top_margin"
android:layout_marginBottom="@dimen/date_header_bottom_margin"
android:clipToPadding="false"
android:clipChildren="false"
android:padding="4dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/info"
android:gravity="center"
android:padding="8dp"
android:text="TextView"
android:textColor="@android:color/primary_text_light"
android:textSize="13sp"/>
</LinearLayout>
......@@ -26,7 +26,7 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
<color name="colorChatBackground">#f8ebea</color>
<color name="colorSentMessage">#c55951</color>
<color name="colorReceivedMessage">#fcf7f7</color>>
<color name="colorDateHeader">#ebc6c4</color>
<color name="colorInfo">#ebc6c4</color>
<color name="colorNoticeText">#808080</color>
<color name="colorPhoneInputLayout">#E8E8E8</color>
......
......@@ -125,5 +125,12 @@ along with Pattle. If not, see <https://www.gnu.org/licenses/>.
%1$s, %2$s, and others are typing…
</string>
<!-- Membbership -->
<string name="invited">%1$s invited %2$s</string>
<string name="has_joined">%1$s has joined</string>
<string name="has_left">%1$s has left</string>
<string name="banned">%1$s has been banned by %2$s</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment