2018-10-29 17:48:31 +01:00
|
|
|
package im.vector.riotredesign.features.home.room.detail
|
2018-10-19 15:30:40 +02:00
|
|
|
|
|
|
|
import android.os.Bundle
|
2018-12-29 17:54:03 +01:00
|
|
|
import android.os.Parcelable
|
2018-10-19 20:34:54 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager
|
2018-10-19 15:30:40 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2018-12-29 17:54:03 +01:00
|
|
|
import com.airbnb.mvrx.args
|
|
|
|
import com.airbnb.mvrx.fragmentViewModel
|
2018-10-29 14:57:36 +01:00
|
|
|
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
2018-10-19 15:30:40 +02:00
|
|
|
import im.vector.riotredesign.R
|
|
|
|
import im.vector.riotredesign.core.platform.RiotFragment
|
2018-10-29 14:57:36 +01:00
|
|
|
import im.vector.riotredesign.core.platform.ToolbarConfigurable
|
2018-11-02 09:12:26 +01:00
|
|
|
import im.vector.riotredesign.features.home.AvatarRenderer
|
2019-01-03 21:26:39 +01:00
|
|
|
import im.vector.riotredesign.features.home.HomePermalinkHandler
|
2018-11-02 10:30:50 +01:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController
|
2018-12-29 17:54:03 +01:00
|
|
|
import kotlinx.android.parcel.Parcelize
|
2018-10-21 20:27:50 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_room_detail.*
|
2018-10-19 15:30:40 +02:00
|
|
|
import org.koin.android.ext.android.inject
|
2018-11-27 12:23:15 +01:00
|
|
|
import org.koin.core.parameter.parametersOf
|
2019-01-03 21:26:39 +01:00
|
|
|
import timber.log.Timber
|
2018-10-19 15:30:40 +02:00
|
|
|
|
2018-12-29 17:54:03 +01:00
|
|
|
@Parcelize
|
|
|
|
data class RoomDetailArgs(
|
|
|
|
val roomId: String,
|
|
|
|
val eventId: String? = null
|
|
|
|
) : Parcelable
|
|
|
|
|
2018-12-19 11:50:44 +01:00
|
|
|
class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
|
2018-10-19 15:30:40 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
2018-12-29 17:54:03 +01:00
|
|
|
fun newInstance(args: RoomDetailArgs): RoomDetailFragment {
|
2018-10-19 15:30:40 +02:00
|
|
|
return RoomDetailFragment().apply {
|
2018-12-29 17:54:03 +01:00
|
|
|
setArguments(args)
|
2018-10-19 15:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 17:54:03 +01: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 21:26:39 +01:00
|
|
|
private val homePermalinkHandler by inject<HomePermalinkHandler>()
|
2018-11-25 16:17:13 +01:00
|
|
|
private lateinit var scrollOnNewMessageCallback: ScrollOnNewMessageCallback
|
2018-10-19 15:30:40 +02: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 14:57:36 +01:00
|
|
|
setupRecyclerView()
|
|
|
|
setupToolbar()
|
2018-11-08 19:08:14 +01:00
|
|
|
sendButton.setOnClickListener {
|
|
|
|
val textMessage = composerEditText.text.toString()
|
|
|
|
if (textMessage.isNotBlank()) {
|
|
|
|
composerEditText.text = null
|
2018-12-29 17:54:03 +01:00
|
|
|
roomDetailViewModel.accept(RoomDetailActions.SendMessage(textMessage))
|
2018-11-08 19:08:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 17:54:03 +01:00
|
|
|
roomDetailViewModel.subscribe { renderState(it) }
|
2018-10-19 15:30:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:57:36 +01:00
|
|
|
private fun setupToolbar() {
|
|
|
|
val parentActivity = riotActivity
|
|
|
|
if (parentActivity is ToolbarConfigurable) {
|
|
|
|
parentActivity.configure(toolbar)
|
|
|
|
}
|
2018-10-19 15:30:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 20:34:54 +02:00
|
|
|
private fun setupRecyclerView() {
|
2018-10-22 15:17:50 +02:00
|
|
|
val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
|
2019-01-03 21:26:39 +01:00
|
|
|
//scrollOnNewMessageCallback = ScrollOnNewMessageCallback(layoutManager)
|
2018-10-21 20:27:50 +02:00
|
|
|
recyclerView.layoutManager = layoutManager
|
2019-01-03 21:26:39 +01:00
|
|
|
//timelineEventController.addModelBuildListener { it.dispatchTo(scrollOnNewMessageCallback) }
|
2019-01-07 11:39:26 +01:00
|
|
|
recyclerView.setHasFixedSize(true)
|
|
|
|
recyclerView.setItemViewCacheSize(20)
|
2018-10-31 11:22:42 +01:00
|
|
|
recyclerView.setController(timelineEventController)
|
2018-12-19 11:50:44 +01:00
|
|
|
timelineEventController.callback = this
|
2018-10-21 20:27:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 17:54:03 +01:00
|
|
|
private fun renderState(state: RoomDetailViewState) {
|
2019-01-03 21:26:39 +01:00
|
|
|
Timber.v("Render state")
|
|
|
|
val timeline = state.asyncTimeline()
|
|
|
|
if (timeline != null) {
|
|
|
|
renderTimeline(timeline)
|
2018-12-30 13:02:20 +01:00
|
|
|
}
|
2018-12-29 19:57:38 +01:00
|
|
|
renderRoomSummary(state.asyncRoomSummary())
|
2018-12-29 17:54:03 +01:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:57:36 +01:00
|
|
|
private fun renderRoomSummary(roomSummary: RoomSummary?) {
|
|
|
|
roomSummary?.let {
|
2018-10-29 17:20:08 +01:00
|
|
|
toolbarTitleView.text = it.displayName
|
2018-11-02 09:12:26 +01:00
|
|
|
AvatarRenderer.render(it, toolbarAvatarImageView)
|
2018-10-29 17:20:08 +01:00
|
|
|
if (it.topic.isNotEmpty()) {
|
|
|
|
toolbarSubtitleView.visibility = View.VISIBLE
|
|
|
|
toolbarSubtitleView.text = it.topic
|
|
|
|
} else {
|
|
|
|
toolbarSubtitleView.visibility = View.GONE
|
|
|
|
}
|
2018-10-29 14:57:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 17:54:03 +01:00
|
|
|
private fun renderTimeline(timeline: Timeline?) {
|
2019-01-03 21:26:39 +01:00
|
|
|
//scrollOnNewMessageCallback.hasBeenUpdated.set(true)
|
2018-12-29 17:54:03 +01:00
|
|
|
timelineEventController.timeline = timeline
|
2018-10-29 14:57:36 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 11:50:44 +01:00
|
|
|
// TimelineEventController.Callback ************************************************************
|
|
|
|
|
|
|
|
override fun onUrlClicked(url: String) {
|
2019-01-03 21:26:39 +01:00
|
|
|
homePermalinkHandler.launch(url)
|
2018-12-19 11:50:44 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 10:17:37 +01:00
|
|
|
}
|