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
|
2018-12-29 16:54:03 +00:00
|
|
|
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
|
2019-02-28 17:50:30 +00:00
|
|
|
import im.vector.riotredesign.core.epoxy.LayoutManagerStateRestorer
|
2018-10-19 13:30:40 +00:00
|
|
|
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-02-28 17:50:30 +00:00
|
|
|
import im.vector.riotredesign.features.home.HomeModule
|
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
|
2019-03-15 18:27:56 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.helper.EndlessRecyclerViewScrollListener
|
2019-03-12 07:29:49 +00:00
|
|
|
import im.vector.riotredesign.features.media.MediaContentRenderer
|
|
|
|
import im.vector.riotredesign.features.media.MediaViewerActivity
|
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
|
2019-02-28 17:50:30 +00:00
|
|
|
import org.koin.android.scope.ext.android.bindScope
|
|
|
|
import org.koin.android.scope.ext.android.getOrCreateScope
|
2019-03-01 20:44:26 +00:00
|
|
|
import org.koin.core.parameter.parametersOf
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2019-03-12 15:54:33 +00:00
|
|
|
|
2018-12-29 16:54:03 +00:00
|
|
|
@Parcelize
|
|
|
|
data class RoomDetailArgs(
|
|
|
|
val roomId: String,
|
|
|
|
val eventId: String? = null
|
|
|
|
) : Parcelable
|
|
|
|
|
2019-02-28 17:50:30 +00:00
|
|
|
|
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()
|
2019-03-01 20:44:26 +00:00
|
|
|
private val timelineEventController: TimelineEventController by inject { parametersOf(this) }
|
|
|
|
private val homePermalinkHandler: HomePermalinkHandler by inject()
|
2019-02-28 17:50:30 +00:00
|
|
|
|
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)
|
2019-02-28 17:50:30 +00:00
|
|
|
bindScope(getOrCreateScope(HomeModule.ROOM_DETAIL_SCOPE))
|
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
|
|
|
}
|
|
|
|
|
2019-02-28 17:50:30 +00:00
|
|
|
// PRIVATE METHODS *****************************************************************************
|
|
|
|
|
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-02-28 17:50:30 +00:00
|
|
|
val stateRestorer = LayoutManagerStateRestorer(layoutManager).register()
|
2019-01-07 18:38:36 +00:00
|
|
|
scrollOnNewMessageCallback = ScrollOnNewMessageCallback(layoutManager)
|
2018-10-21 18:27:50 +00:00
|
|
|
recyclerView.layoutManager = layoutManager
|
2019-03-28 15:07:05 +00:00
|
|
|
recyclerView.itemAnimator = null
|
2019-01-07 10:39:26 +00:00
|
|
|
recyclerView.setHasFixedSize(true)
|
2019-02-28 17:50:30 +00:00
|
|
|
timelineEventController.addModelBuildListener {
|
|
|
|
it.dispatchTo(stateRestorer)
|
|
|
|
it.dispatchTo(scrollOnNewMessageCallback)
|
|
|
|
}
|
2019-03-21 19:21:45 +00:00
|
|
|
|
|
|
|
recyclerView.addOnScrollListener(
|
|
|
|
EndlessRecyclerViewScrollListener(layoutManager, RoomDetailViewModel.PAGINATION_COUNT) { direction ->
|
|
|
|
roomDetailViewModel.process(RoomDetailActions.LoadMore(direction))
|
|
|
|
})
|
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()) {
|
2019-01-30 17:39:54 +00:00
|
|
|
roomDetailViewModel.process(RoomDetailActions.SendMessage(textMessage))
|
2019-03-15 18:27:56 +00:00
|
|
|
composerEditText.text = null
|
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)
|
2019-03-15 18:27:56 +00:00
|
|
|
timelineEventController.setTimeline(state.timeline)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 19:21:45 +00:00
|
|
|
// TimelineEventController.Callback ************************************************************
|
2018-12-19 10:50:44 +00:00
|
|
|
|
|
|
|
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-03-15 18:27:56 +00:00
|
|
|
override fun onEventVisible(event: TimelineEvent) {
|
|
|
|
roomDetailViewModel.process(RoomDetailActions.EventDisplayed(event))
|
2019-01-30 17:39:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 15:54:33 +00:00
|
|
|
override fun onMediaClicked(mediaData: MediaContentRenderer.Data, view: View) {
|
2019-03-12 07:29:49 +00:00
|
|
|
val intent = MediaViewerActivity.newIntent(riotActivity, mediaData)
|
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
|
2018-11-02 09:17:37 +00:00
|
|
|
}
|