From 42cf45c8f34435ac2c4c8f71a14bfbc9771f020c Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 20 Feb 2019 12:06:33 +0100 Subject: [PATCH] Timeline: handle room history visibility --- .../riotredesign/features/home/HomeModule.kt | 6 +- .../RoomHistoryVisibilityItemFactory.kt | 56 +++++++++++++++++++ .../detail/timeline/TimelineItemFactory.kt | 14 +++-- .../model/RoomHistoryVisibilityContent.kt | 27 +++++++++ 4 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/RoomHistoryVisibilityItemFactory.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/RoomHistoryVisibilityContent.kt diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt index 0500181862..4514fe1532 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt @@ -48,12 +48,16 @@ class HomeModule { RoomMemberItemFactory(get()) } + single { + RoomHistoryVisibilityItemFactory(get()) + } + single { DefaultItemFactory() } single { - TimelineItemFactory(get(), get(), get(), get(), get()) + TimelineItemFactory(get(), get(), get(), get(), get(), get()) } single { diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/RoomHistoryVisibilityItemFactory.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/RoomHistoryVisibilityItemFactory.kt new file mode 100644 index 0000000000..e692b81de2 --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/RoomHistoryVisibilityItemFactory.kt @@ -0,0 +1,56 @@ +/* + * + * * Copyright 2019 New Vector Ltd + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package im.vector.riotredesign.features.home.room.detail.timeline + +import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.events.model.toModel +import im.vector.matrix.android.api.session.room.model.RoomHistoryVisibility +import im.vector.matrix.android.api.session.room.model.RoomHistoryVisibilityContent +import im.vector.matrix.android.api.session.room.model.RoomMember +import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import im.vector.riotredesign.R +import im.vector.riotredesign.core.resources.StringProvider + + +class RoomHistoryVisibilityItemFactory(private val stringProvider: StringProvider) { + + fun create(event: TimelineEvent): NoticeItem? { + val roomMember = event.roomMember ?: return null + val noticeText = buildRoomMemberNotice(event.root, roomMember) ?: return null + return NoticeItem_() + .noticeText(noticeText) + .avatarUrl(roomMember.avatarUrl) + .memberName(roomMember.displayName) + } + + private fun buildRoomMemberNotice(event: Event, roomMember: RoomMember): CharSequence? { + val content = event.content.toModel() ?: return null + val formattedVisibility = when (content.historyVisibility) { + RoomHistoryVisibility.SHARED -> stringProvider.getString(R.string.notice_room_visibility_shared) + RoomHistoryVisibility.INVITED -> stringProvider.getString(R.string.notice_room_visibility_invited) + RoomHistoryVisibility.JOINED -> stringProvider.getString(R.string.notice_room_visibility_joined) + RoomHistoryVisibility.WORLD_READABLE -> stringProvider.getString(R.string.notice_room_visibility_world_readable) + } + return stringProvider.getString(R.string.notice_made_future_room_visibility, roomMember.displayName, formattedVisibility) + } + + +} + + diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineItemFactory.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineItemFactory.kt index 366ea2d92e..3f6a7b4bcc 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineItemFactory.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineItemFactory.kt @@ -25,6 +25,7 @@ class TimelineItemFactory(private val messageItemFactory: MessageItemFactory, private val roomNameItemFactory: RoomNameItemFactory, private val roomTopicItemFactory: RoomTopicItemFactory, private val roomMemberItemFactory: RoomMemberItemFactory, + private val roomHistoryVisibilityItemFactory: RoomHistoryVisibilityItemFactory, private val defaultItemFactory: DefaultItemFactory) { fun create(event: TimelineEvent, @@ -33,14 +34,15 @@ class TimelineItemFactory(private val messageItemFactory: MessageItemFactory, return try { when (event.root.type) { - EventType.MESSAGE -> messageItemFactory.create(event, nextEvent, callback) - EventType.STATE_ROOM_NAME -> roomNameItemFactory.create(event) - EventType.STATE_ROOM_TOPIC -> roomTopicItemFactory.create(event) - EventType.STATE_ROOM_MEMBER -> roomMemberItemFactory.create(event) + EventType.MESSAGE -> messageItemFactory.create(event, nextEvent, callback) + EventType.STATE_ROOM_NAME -> roomNameItemFactory.create(event) + EventType.STATE_ROOM_TOPIC -> roomTopicItemFactory.create(event) + EventType.STATE_ROOM_MEMBER -> roomMemberItemFactory.create(event) + EventType.STATE_HISTORY_VISIBILITY -> roomHistoryVisibilityItemFactory.create(event) EventType.STATE_ROOM_CREATE, EventType.STATE_ROOM_POWER_LEVELS, - EventType.REDACTION -> EmptyItem_() - else -> defaultItemFactory.create(event) + EventType.REDACTION -> EmptyItem_() + else -> defaultItemFactory.create(event) } } catch (e: Exception) { defaultItemFactory.create(event, e) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/RoomHistoryVisibilityContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/RoomHistoryVisibilityContent.kt new file mode 100644 index 0000000000..0c9a1f9e42 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/RoomHistoryVisibilityContent.kt @@ -0,0 +1,27 @@ +/* + * + * * Copyright 2019 New Vector Ltd + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package im.vector.matrix.android.api.session.room.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class RoomHistoryVisibilityContent( + @Json(name = "history_visibility") val historyVisibility: RoomHistoryVisibility +) \ No newline at end of file