2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-29 16:48:31 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.detail
|
2018-10-19 13:30:40 +00:00
|
|
|
|
|
|
|
import android.os.Bundle
|
2018-12-29 16:54:03 +00:00
|
|
|
import android.os.Parcelable
|
2018-10-19 13:30:40 +00:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-01-16 18:25:43 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2019-01-30 17:39:54 +00:00
|
|
|
import com.airbnb.epoxy.EpoxyVisibilityTracker
|
2019-01-07 18:38:36 +00:00
|
|
|
import com.airbnb.mvrx.Success
|
2018-12-29 16:54:03 +00:00
|
|
|
import com.airbnb.mvrx.args
|
|
|
|
import com.airbnb.mvrx.fragmentViewModel
|
2019-01-30 17:39:54 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
|
2018-10-19 13:30:40 +00:00
|
|
|
import im.vector.riotredesign.R
|
|
|
|
import im.vector.riotredesign.core.platform.RiotFragment
|
2018-10-29 13:57:36 +00:00
|
|
|
import im.vector.riotredesign.core.platform.ToolbarConfigurable
|
2018-11-02 08:12:26 +00:00
|
|
|
import im.vector.riotredesign.features.home.AvatarRenderer
|
2019-01-03 20:26:39 +00:00
|
|
|
import im.vector.riotredesign.features.home.HomePermalinkHandler
|
2018-11-02 09:30:50 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController
|
2018-12-29 16:54:03 +00:00
|
|
|
import kotlinx.android.parcel.Parcelize
|
2018-10-21 18:27:50 +00:00
|
|
|
import kotlinx.android.synthetic.main.fragment_room_detail.*
|
2018-10-19 13:30:40 +00:00
|
|
|
import org.koin.android.ext.android.inject
|
2018-11-27 11:23:15 +00:00
|
|
|
import org.koin.core.parameter.parametersOf
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2018-12-29 16:54:03 +00:00
|
|
|
@Parcelize
|
|
|
|
data class RoomDetailArgs(
|
|
|
|
val roomId: String,
|
|
|
|
val eventId: String? = null
|
|
|
|
) : Parcelable
|
|
|
|
|
2018-12-19 10:50:44 +00:00
|
|
|
class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
|
2018-10-19 13:30:40 +00:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
2018-12-29 16:54:03 +00:00
|
|
|
fun newInstance(args: RoomDetailArgs): RoomDetailFragment {
|
2018-10-19 13:30:40 +00:00
|
|
|
return RoomDetailFragment().apply {
|
2018-12-29 16:54:03 +00:00
|
|
|
setArguments(args)
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 16:54:03 +00:00
|
|
|
private val roomDetailViewModel: RoomDetailViewModel by fragmentViewModel()
|
|
|
|
private val roomDetailArgs: RoomDetailArgs by args()
|
|
|
|
|
|
|
|
private val timelineEventController by inject<TimelineEventController> { parametersOf(roomDetailArgs.roomId) }
|
2019-01-03 20:26:39 +00:00
|
|
|
private val homePermalinkHandler by inject<HomePermalinkHandler>()
|
2018-11-25 15:17:13 +00:00
|
|
|
private lateinit var scrollOnNewMessageCallback: ScrollOnNewMessageCallback
|
2018-10-19 13:30:40 +00:00
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
return inflater.inflate(R.layout.fragment_room_detail, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
2018-10-29 13:57:36 +00:00
|
|
|
setupRecyclerView()
|
|
|
|
setupToolbar()
|
2019-01-11 15:35:49 +00:00
|
|
|
setupSendButton()
|
2018-12-29 16:54:03 +00:00
|
|
|
roomDetailViewModel.subscribe { renderState(it) }
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 11:07:38 +00:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2019-01-30 17:39:54 +00:00
|
|
|
roomDetailViewModel.process(RoomDetailActions.IsDisplayed)
|
2019-01-11 11:07:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:57:36 +00:00
|
|
|
private fun setupToolbar() {
|
|
|
|
val parentActivity = riotActivity
|
|
|
|
if (parentActivity is ToolbarConfigurable) {
|
|
|
|
parentActivity.configure(toolbar)
|
|
|
|
}
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:34:54 +00:00
|
|
|
private fun setupRecyclerView() {
|
2019-01-30 17:39:54 +00:00
|
|
|
val epoxyVisibilityTracker = EpoxyVisibilityTracker()
|
|
|
|
epoxyVisibilityTracker.attach(recyclerView)
|
2019-01-16 18:25:43 +00:00
|
|
|
val layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, true)
|
2019-01-07 18:38:36 +00:00
|
|
|
scrollOnNewMessageCallback = ScrollOnNewMessageCallback(layoutManager)
|
2018-10-21 18:27:50 +00:00
|
|
|
recyclerView.layoutManager = layoutManager
|
2019-01-07 10:39:26 +00:00
|
|
|
recyclerView.setHasFixedSize(true)
|
2019-01-14 15:18:39 +00:00
|
|
|
timelineEventController.addModelBuildListener { it.dispatchTo(scrollOnNewMessageCallback) }
|
2018-10-31 10:22:42 +00:00
|
|
|
recyclerView.setController(timelineEventController)
|
2018-12-19 10:50:44 +00:00
|
|
|
timelineEventController.callback = this
|
2018-10-21 18:27:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:35:49 +00:00
|
|
|
private fun setupSendButton() {
|
|
|
|
sendButton.setOnClickListener {
|
|
|
|
val textMessage = composerEditText.text.toString()
|
|
|
|
if (textMessage.isNotBlank()) {
|
|
|
|
composerEditText.text = null
|
2019-01-30 17:39:54 +00:00
|
|
|
roomDetailViewModel.process(RoomDetailActions.SendMessage(textMessage))
|
2019-01-11 15:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 16:54:03 +00:00
|
|
|
private fun renderState(state: RoomDetailViewState) {
|
2019-01-07 18:38:36 +00:00
|
|
|
renderRoomSummary(state)
|
|
|
|
renderTimeline(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun renderTimeline(state: RoomDetailViewState) {
|
|
|
|
when (state.asyncTimelineData) {
|
2019-01-14 15:18:39 +00:00
|
|
|
is Success -> {
|
|
|
|
val timelineData = state.asyncTimelineData()
|
|
|
|
val lockAutoScroll = timelineData?.let {
|
|
|
|
it.events == timelineEventController.currentList && it.isLoadingForward
|
|
|
|
} ?: true
|
|
|
|
|
|
|
|
scrollOnNewMessageCallback.isLocked.set(lockAutoScroll)
|
|
|
|
timelineEventController.update(timelineData)
|
|
|
|
}
|
2018-12-30 12:02:20 +00:00
|
|
|
}
|
2018-12-29 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:38:36 +00:00
|
|
|
private fun renderRoomSummary(state: RoomDetailViewState) {
|
|
|
|
state.asyncRoomSummary()?.let {
|
2018-10-29 16:20:08 +00:00
|
|
|
toolbarTitleView.text = it.displayName
|
2018-11-02 08:12:26 +00:00
|
|
|
AvatarRenderer.render(it, toolbarAvatarImageView)
|
2018-10-29 16:20:08 +00:00
|
|
|
if (it.topic.isNotEmpty()) {
|
|
|
|
toolbarSubtitleView.visibility = View.VISIBLE
|
|
|
|
toolbarSubtitleView.text = it.topic
|
|
|
|
} else {
|
|
|
|
toolbarSubtitleView.visibility = View.GONE
|
|
|
|
}
|
2018-10-29 13:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:50:44 +00:00
|
|
|
// TimelineEventController.Callback ************************************************************
|
|
|
|
|
|
|
|
override fun onUrlClicked(url: String) {
|
2019-01-03 20:26:39 +00:00
|
|
|
homePermalinkHandler.launch(url)
|
2018-12-19 10:50:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 17:39:54 +00:00
|
|
|
override fun onEventVisible(event: TimelineEvent, index: Int) {
|
|
|
|
roomDetailViewModel.process(RoomDetailActions.EventDisplayed(event, index))
|
|
|
|
}
|
|
|
|
|
2018-11-02 09:17:37 +00:00
|
|
|
}
|