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 18:34:54 +00:00
|
|
|
import android.support.v7.widget.LinearLayoutManager
|
2018-10-19 13:30:40 +00:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2018-12-29 16:54:03 +00:00
|
|
|
import com.airbnb.mvrx.args
|
|
|
|
import com.airbnb.mvrx.fragmentViewModel
|
2018-10-29 13:57:36 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
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
|
2019-01-03 20:26:39 +00:00
|
|
|
import timber.log.Timber
|
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()
|
2018-11-08 18:08:14 +00:00
|
|
|
sendButton.setOnClickListener {
|
|
|
|
val textMessage = composerEditText.text.toString()
|
|
|
|
if (textMessage.isNotBlank()) {
|
|
|
|
composerEditText.text = null
|
2018-12-29 16:54:03 +00:00
|
|
|
roomDetailViewModel.accept(RoomDetailActions.SendMessage(textMessage))
|
2018-11-08 18:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 16:54:03 +00:00
|
|
|
roomDetailViewModel.subscribe { renderState(it) }
|
2018-10-19 13:30:40 +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() {
|
2018-10-22 13:17:50 +00:00
|
|
|
val layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
|
2019-01-03 20:26:39 +00:00
|
|
|
//scrollOnNewMessageCallback = ScrollOnNewMessageCallback(layoutManager)
|
2018-10-21 18:27:50 +00:00
|
|
|
recyclerView.layoutManager = layoutManager
|
2019-01-03 20:26:39 +00:00
|
|
|
//timelineEventController.addModelBuildListener { it.dispatchTo(scrollOnNewMessageCallback) }
|
2019-01-07 10:39:26 +00:00
|
|
|
recyclerView.setHasFixedSize(true)
|
|
|
|
recyclerView.setItemViewCacheSize(20)
|
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
|
|
|
}
|
|
|
|
|
2018-12-29 16:54:03 +00:00
|
|
|
private fun renderState(state: RoomDetailViewState) {
|
2019-01-03 20:26:39 +00:00
|
|
|
Timber.v("Render state")
|
|
|
|
val timeline = state.asyncTimeline()
|
|
|
|
if (timeline != null) {
|
|
|
|
renderTimeline(timeline)
|
2018-12-30 12:02:20 +00:00
|
|
|
}
|
2018-12-29 18:57:38 +00:00
|
|
|
renderRoomSummary(state.asyncRoomSummary())
|
2018-12-29 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:57:36 +00:00
|
|
|
private fun renderRoomSummary(roomSummary: RoomSummary?) {
|
|
|
|
roomSummary?.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-29 16:54:03 +00:00
|
|
|
private fun renderTimeline(timeline: Timeline?) {
|
2019-01-03 20:26:39 +00:00
|
|
|
//scrollOnNewMessageCallback.hasBeenUpdated.set(true)
|
2018-12-29 16:54:03 +00:00
|
|
|
timelineEventController.timeline = timeline
|
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
|
|
|
}
|
|
|
|
|
2018-11-02 09:17:37 +00:00
|
|
|
}
|