forked from GitHub-Mirror/riotX-android
Get Public rooms and join public room
This commit is contained in:
@ -0,0 +1,46 @@
|
||||
/*
|
||||
*
|
||||
* * 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.core.epoxy
|
||||
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
import com.airbnb.epoxy.EpoxyAttribute
|
||||
import com.airbnb.epoxy.EpoxyModelClass
|
||||
import im.vector.riotredesign.R
|
||||
|
||||
@EpoxyModelClass(layout = R.layout.item_error_retry)
|
||||
abstract class ErrorWithRetryItem : VectorEpoxyModel<ErrorWithRetryItem.Holder>() {
|
||||
|
||||
@EpoxyAttribute
|
||||
var text: String? = null
|
||||
|
||||
@EpoxyAttribute
|
||||
var listener: (() -> Unit)? = null
|
||||
|
||||
override fun bind(holder: Holder) {
|
||||
holder.textView.text = text
|
||||
holder.buttonView.setOnClickListener { listener?.invoke() }
|
||||
}
|
||||
|
||||
|
||||
class Holder : VectorEpoxyHolder() {
|
||||
val textView by bind<TextView>(R.id.itemErrorRetryText)
|
||||
val buttonView by bind<Button>(R.id.itemErrorRetryButton)
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
*
|
||||
* * 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.core.epoxy
|
||||
|
||||
import android.widget.TextView
|
||||
import com.airbnb.epoxy.EpoxyAttribute
|
||||
import com.airbnb.epoxy.EpoxyModelClass
|
||||
import im.vector.riotredesign.R
|
||||
|
||||
@EpoxyModelClass(layout = R.layout.item_no_result)
|
||||
abstract class NoResultItem : VectorEpoxyModel<NoResultItem.Holder>() {
|
||||
|
||||
@EpoxyAttribute
|
||||
var text: String? = null
|
||||
|
||||
override fun bind(holder: Holder) {
|
||||
holder.textView.text = text
|
||||
}
|
||||
|
||||
|
||||
class Holder : VectorEpoxyHolder() {
|
||||
val textView by bind<TextView>(R.id.itemNoResultText)
|
||||
}
|
||||
}
|
@ -40,6 +40,7 @@ import im.vector.riotredesign.core.platform.VectorBaseActivity
|
||||
import im.vector.riotredesign.features.home.room.detail.LoadingRoomDetailFragment
|
||||
import im.vector.riotredesign.features.rageshake.BugReporter
|
||||
import im.vector.riotredesign.features.rageshake.VectorUncaughtExceptionHandler
|
||||
import im.vector.riotredesign.features.roomdirectory.RoomDirectoryActivity
|
||||
import im.vector.riotredesign.features.settings.VectorSettingsActivity
|
||||
import im.vector.riotredesign.features.workers.signout.SignOutUiWorker
|
||||
import kotlinx.android.synthetic.main.activity_home.*
|
||||
@ -124,23 +125,29 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable {
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
android.R.id.home -> {
|
||||
drawerLayout.openDrawer(GravityCompat.START)
|
||||
return true
|
||||
}
|
||||
R.id.sliding_menu_settings -> {
|
||||
R.id.sliding_menu_settings -> {
|
||||
startActivity(VectorSettingsActivity.getIntent(this, "TODO"))
|
||||
return true
|
||||
}
|
||||
R.id.sliding_menu_sign_out -> {
|
||||
R.id.sliding_menu_sign_out -> {
|
||||
SignOutUiWorker(this).perform(Matrix.getInstance().currentSession!!)
|
||||
return true
|
||||
}
|
||||
// TODO Temporary code here to create a room
|
||||
R.id.tmp_menu_create_room -> {
|
||||
R.id.tmp_menu_create_room -> {
|
||||
homeActivityViewModel.createRoom()
|
||||
return true
|
||||
}
|
||||
// TODO Temporary code here to open room directory
|
||||
R.id.tmp_menu_open_room_directory -> {
|
||||
// Start Activity for now
|
||||
startActivity(Intent(this, RoomDirectoryActivity::class.java))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.roomdirectory
|
||||
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.extensions.addFragment
|
||||
import im.vector.riotredesign.core.platform.VectorBaseActivity
|
||||
|
||||
class RoomDirectoryActivity : VectorBaseActivity() {
|
||||
|
||||
|
||||
override fun getLayoutRes() = R.layout.activity_simple
|
||||
|
||||
override fun initUiAndData() {
|
||||
if (isFirstCreation()) {
|
||||
addFragment(RoomDirectoryFragment(), R.id.simpleFragmentContainer)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.roomdirectory
|
||||
|
||||
import com.airbnb.epoxy.TypedEpoxyController
|
||||
import com.airbnb.epoxy.VisibilityState
|
||||
import com.airbnb.mvrx.Fail
|
||||
import com.airbnb.mvrx.Incomplete
|
||||
import com.airbnb.mvrx.Success
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.epoxy.errorWithRetryItem
|
||||
import im.vector.riotredesign.core.epoxy.loadingItem
|
||||
import im.vector.riotredesign.core.epoxy.noResultItem
|
||||
import im.vector.riotredesign.core.resources.StringProvider
|
||||
|
||||
class RoomDirectoryController(private val stringProvider: StringProvider) : TypedEpoxyController<RoomDirectoryViewState>() {
|
||||
|
||||
var callback: Callback? = null
|
||||
|
||||
override fun buildModels(viewState: RoomDirectoryViewState) {
|
||||
val publicRooms = viewState.publicRooms
|
||||
|
||||
if (publicRooms.isEmpty()
|
||||
&& viewState.asyncPublicRoomsRequest is Success) {
|
||||
// No result
|
||||
noResultItem {
|
||||
id("noResult")
|
||||
text(stringProvider.getString(R.string.no_result_placeholder))
|
||||
}
|
||||
} else {
|
||||
publicRooms.forEach {
|
||||
buildPublicRoom(it, viewState)
|
||||
}
|
||||
|
||||
if ((viewState.hasMore && viewState.asyncPublicRoomsRequest is Success)
|
||||
|| viewState.asyncPublicRoomsRequest is Incomplete) {
|
||||
loadingItem {
|
||||
// Change id to avoid list to scroll automatically when first results are displayed
|
||||
if (publicRooms.isEmpty()) {
|
||||
id("loading")
|
||||
} else {
|
||||
id("loadMore")
|
||||
}
|
||||
onVisibilityStateChanged { _, _, visibilityState ->
|
||||
if (visibilityState == VisibilityState.VISIBLE) {
|
||||
callback?.loadMore()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (viewState.asyncPublicRoomsRequest is Fail) {
|
||||
errorWithRetryItem {
|
||||
id("error")
|
||||
text(viewState.asyncPublicRoomsRequest.error.localizedMessage)
|
||||
listener { callback?.loadMore() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildPublicRoom(publicRoom: PublicRoom, viewState: RoomDirectoryViewState) {
|
||||
roomDirectoryItem {
|
||||
id(publicRoom.roomId)
|
||||
roomId(publicRoom.roomId)
|
||||
avatarUrl(publicRoom.avatarUrl)
|
||||
roomName(publicRoom.name)
|
||||
nbOfMembers(publicRoom.numJoinedMembers)
|
||||
when {
|
||||
viewState.joinedRoomsIds.contains(publicRoom.roomId) -> joinState(RoomDirectoryItem.JoinState.JOINED)
|
||||
viewState.joiningRoomsIds.contains(publicRoom.roomId) -> joinState(RoomDirectoryItem.JoinState.JOINING)
|
||||
else -> joinState(RoomDirectoryItem.JoinState.NOT_JOINED)
|
||||
}
|
||||
joinListener {
|
||||
callback?.onPublicRoomJoin(publicRoom)
|
||||
}
|
||||
globalListener {
|
||||
callback?.onPublicRoomClicked(publicRoom)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Callback {
|
||||
fun onPublicRoomClicked(publicRoom: PublicRoom)
|
||||
fun onPublicRoomJoin(publicRoom: PublicRoom)
|
||||
fun loadMore()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.roomdirectory
|
||||
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.airbnb.epoxy.EpoxyVisibilityTracker
|
||||
import com.airbnb.mvrx.fragmentViewModel
|
||||
import com.airbnb.mvrx.withState
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.platform.SimpleTextWatcher
|
||||
import im.vector.riotredesign.core.platform.VectorBaseFragment
|
||||
import kotlinx.android.synthetic.main.fragment_room_directory.*
|
||||
import org.koin.android.ext.android.get
|
||||
import timber.log.Timber
|
||||
|
||||
|
||||
/**
|
||||
* What can be improved:
|
||||
* - When filtering more (when entering new chars), we could filter on result we already have, during the new server request, to avoid empty screen effect
|
||||
*
|
||||
* FIXME Rotate screen launch again the request
|
||||
*
|
||||
* For Nad:
|
||||
* Display number of rooms?
|
||||
* Picto size are not correct
|
||||
* Where I put the room directory picker?
|
||||
*
|
||||
*/
|
||||
class RoomDirectoryFragment : VectorBaseFragment(), RoomDirectoryController.Callback {
|
||||
|
||||
private val viewModel: RoomDirectoryViewModel by fragmentViewModel()
|
||||
|
||||
private val roomDirectoryController = RoomDirectoryController(get())
|
||||
|
||||
override fun getLayoutResId() = R.layout.fragment_room_directory
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
vectorBaseActivity.setSupportActionBar(toolbar)
|
||||
|
||||
vectorBaseActivity.supportActionBar?.let {
|
||||
it.setDisplayShowHomeEnabled(true)
|
||||
it.setDisplayHomeAsUpEnabled(true)
|
||||
}
|
||||
|
||||
roomDirectoryFilter.addTextChangedListener(object : SimpleTextWatcher() {
|
||||
override fun afterTextChanged(s: Editable) {
|
||||
// TODO Debounce
|
||||
viewModel.filterWith(roomDirectoryFilter.text.toString())
|
||||
}
|
||||
})
|
||||
|
||||
createNewRoom.setOnClickListener {
|
||||
vectorBaseActivity.notImplemented()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||
super.onActivityCreated(savedInstanceState)
|
||||
|
||||
setupRecyclerView()
|
||||
}
|
||||
|
||||
private fun setupRecyclerView() {
|
||||
val epoxyVisibilityTracker = EpoxyVisibilityTracker()
|
||||
epoxyVisibilityTracker.attach(roomDirectoryList)
|
||||
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
|
||||
roomDirectoryList.layoutManager = layoutManager
|
||||
roomDirectoryController.callback = this
|
||||
|
||||
roomDirectoryList.setController(roomDirectoryController)
|
||||
}
|
||||
|
||||
override fun onPublicRoomClicked(publicRoom: PublicRoom) {
|
||||
Timber.v("PublicRoomClicked: $publicRoom")
|
||||
vectorBaseActivity.notImplemented()
|
||||
}
|
||||
|
||||
override fun onPublicRoomJoin(publicRoom: PublicRoom) {
|
||||
Timber.v("PublicRoomJoinClicked: $publicRoom")
|
||||
viewModel.joinRoom(publicRoom)
|
||||
}
|
||||
|
||||
override fun loadMore() {
|
||||
viewModel.loadMore()
|
||||
}
|
||||
|
||||
override fun invalidate() = withState(viewModel) { state ->
|
||||
// Populate list with Epoxy
|
||||
roomDirectoryController.setData(state)
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.roomdirectory
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.core.view.isInvisible
|
||||
import androidx.core.view.isVisible
|
||||
import com.airbnb.epoxy.EpoxyAttribute
|
||||
import com.airbnb.epoxy.EpoxyModelClass
|
||||
import im.vector.riotredesign.R
|
||||
import im.vector.riotredesign.core.epoxy.VectorEpoxyHolder
|
||||
import im.vector.riotredesign.core.epoxy.VectorEpoxyModel
|
||||
import im.vector.riotredesign.features.home.AvatarRenderer
|
||||
|
||||
@EpoxyModelClass(layout = R.layout.item_room_directory)
|
||||
abstract class RoomDirectoryItem : VectorEpoxyModel<RoomDirectoryItem.Holder>() {
|
||||
|
||||
// TODO Manage join state waiting from the sync
|
||||
enum class JoinState {
|
||||
NOT_JOINED,
|
||||
JOINING,
|
||||
JOINED
|
||||
}
|
||||
|
||||
@EpoxyAttribute
|
||||
var avatarUrl: String? = null
|
||||
|
||||
@EpoxyAttribute
|
||||
var roomId: String? = null
|
||||
|
||||
@EpoxyAttribute
|
||||
var roomName: String? = null
|
||||
|
||||
@EpoxyAttribute
|
||||
var nbOfMembers: Int = 0
|
||||
|
||||
@EpoxyAttribute
|
||||
var joinState: JoinState = JoinState.NOT_JOINED
|
||||
|
||||
@EpoxyAttribute
|
||||
var globalListener: (() -> Unit)? = null
|
||||
|
||||
@EpoxyAttribute
|
||||
var joinListener: (() -> Unit)? = null
|
||||
|
||||
override fun bind(holder: Holder) {
|
||||
holder.rootView.setOnClickListener { globalListener?.invoke() }
|
||||
|
||||
AvatarRenderer.render(avatarUrl, roomId!!, roomName, holder.avatarView)
|
||||
holder.nameView.text = roomName
|
||||
// TODO Use formatter for big numbers?
|
||||
holder.counterView.text = nbOfMembers.toString()
|
||||
|
||||
if (joinState == JoinState.NOT_JOINED) {
|
||||
holder.joinButton.isVisible = true
|
||||
} else {
|
||||
// We use isInvisible because we want to keep button space in the layout
|
||||
holder.joinButton.isInvisible = true
|
||||
}
|
||||
holder.joiningView.isVisible = joinState == JoinState.JOINING
|
||||
holder.joinedView.isVisible = joinState == JoinState.JOINED
|
||||
|
||||
holder.joinButton.setOnClickListener { joinListener?.invoke() }
|
||||
}
|
||||
|
||||
|
||||
class Holder : VectorEpoxyHolder() {
|
||||
val rootView by bind<ViewGroup>(R.id.itemRoomDirectoryLayout)
|
||||
|
||||
val avatarView by bind<ImageView>(R.id.itemRoomDirectoryAvatar)
|
||||
val nameView by bind<TextView>(R.id.itemRoomDirectoryName)
|
||||
val counterView by bind<TextView>(R.id.itemRoomDirectoryMembersCount)
|
||||
|
||||
val joinedView by bind<View>(R.id.itemRoomDirectoryJoined)
|
||||
val joinButton by bind<View>(R.id.itemRoomDirectoryJoin)
|
||||
val joiningView by bind<View>(R.id.itemRoomDirectoryJoining)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,184 @@
|
||||
/*
|
||||
* 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.roomdirectory
|
||||
|
||||
import com.airbnb.mvrx.*
|
||||
import im.vector.matrix.android.api.MatrixCallback
|
||||
import im.vector.matrix.android.api.session.Session
|
||||
import im.vector.matrix.android.api.session.room.model.Membership
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsFilter
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsParams
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsResponse
|
||||
import im.vector.matrix.android.api.util.Cancelable
|
||||
import im.vector.matrix.rx.rx
|
||||
import im.vector.riotredesign.core.platform.VectorViewModel
|
||||
import org.koin.android.ext.android.get
|
||||
import timber.log.Timber
|
||||
|
||||
private const val PUBLIC_ROOMS_LIMIT = 20
|
||||
|
||||
class RoomDirectoryViewModel(initialState: RoomDirectoryViewState,
|
||||
private val session: Session) : VectorViewModel<RoomDirectoryViewState>(initialState) {
|
||||
|
||||
companion object : MvRxViewModelFactory<RoomDirectoryViewModel, RoomDirectoryViewState> {
|
||||
|
||||
@JvmStatic
|
||||
override fun create(viewModelContext: ViewModelContext, state: RoomDirectoryViewState): RoomDirectoryViewModel? {
|
||||
val currentSession = viewModelContext.activity.get<Session>()
|
||||
|
||||
return RoomDirectoryViewModel(state, currentSession)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Store in ViewState?
|
||||
private var currentFilter: String = ""
|
||||
|
||||
private var since: String? = null
|
||||
|
||||
private var currentTask: Cancelable? = null
|
||||
|
||||
init {
|
||||
// Load with empty filter
|
||||
load()
|
||||
|
||||
// Observe joined room (from the sync)
|
||||
observeJoinedRooms()
|
||||
}
|
||||
|
||||
private fun observeJoinedRooms() {
|
||||
session
|
||||
.rx()
|
||||
.liveRoomSummaries()
|
||||
.execute { async ->
|
||||
val joinedRoomIds = async.invoke()
|
||||
?.filter {
|
||||
// Keep only joined room
|
||||
it.membership == Membership.JOIN
|
||||
}
|
||||
?.map {
|
||||
it.roomId
|
||||
}
|
||||
?.toList()
|
||||
?: emptyList()
|
||||
|
||||
|
||||
copy(joinedRoomsIds = joinedRoomIds,
|
||||
// Remove (newly) joined room id from the joining room list
|
||||
joiningRoomsIds = joiningRoomsIds.toMutableList().apply { removeAll(joinedRoomIds) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun filterWith(filter: String) {
|
||||
currentTask?.cancel()
|
||||
|
||||
currentFilter = filter
|
||||
|
||||
// Reset since token
|
||||
since = null
|
||||
|
||||
setState {
|
||||
copy(
|
||||
publicRooms = emptyList(),
|
||||
asyncPublicRoomsRequest = Loading(),
|
||||
hasMore = false)
|
||||
}
|
||||
|
||||
load()
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
if (currentTask == null) {
|
||||
setState {
|
||||
copy(
|
||||
asyncPublicRoomsRequest = Loading()
|
||||
)
|
||||
}
|
||||
|
||||
load()
|
||||
}
|
||||
}
|
||||
|
||||
private fun load() {
|
||||
currentTask = session.getPublicRooms(null, // TODO session.sessionParams.homeServerConnectionConfig.homeServerUri.toString(),
|
||||
PublicRoomsParams(
|
||||
limit = PUBLIC_ROOMS_LIMIT,
|
||||
filter = PublicRoomsFilter(searchTerm = currentFilter),
|
||||
includeAllNetworks = false, // TODO
|
||||
since = since,
|
||||
thirdPartyInstanceId = null // TODO
|
||||
),
|
||||
object : MatrixCallback<PublicRoomsResponse> {
|
||||
override fun onSuccess(data: PublicRoomsResponse) {
|
||||
currentTask = null
|
||||
|
||||
since = data.nextBatch
|
||||
|
||||
setState {
|
||||
copy(
|
||||
asyncPublicRoomsRequest = Success(data.chunk!!),
|
||||
// It's ok to append at the end of the list, so I use publicRooms.size()
|
||||
publicRooms = publicRooms.appendAt(data.chunk!!, publicRooms.size),
|
||||
hasMore = since != null
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(failure: Throwable) {
|
||||
currentTask = null
|
||||
|
||||
setState {
|
||||
copy(
|
||||
asyncPublicRoomsRequest = Fail(failure)
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun joinRoom(publicRoom: PublicRoom) = withState { state ->
|
||||
if (state.joiningRoomsIds.contains(publicRoom.roomId)) {
|
||||
// Request already sent, should not happen
|
||||
Timber.w("Try to join an already joining room. Should not happen")
|
||||
return@withState
|
||||
}
|
||||
|
||||
setState {
|
||||
copy(
|
||||
joiningRoomsIds = joiningRoomsIds.toMutableList().apply { add(publicRoom.roomId) }
|
||||
)
|
||||
}
|
||||
|
||||
session.joinRoom(publicRoom.roomId, object : MatrixCallback<Unit> {
|
||||
override fun onSuccess(data: Unit) {
|
||||
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
|
||||
// Instead, we wait for the room to be joined
|
||||
}
|
||||
|
||||
override fun onFailure(failure: Throwable) {
|
||||
// TODO Notify the user
|
||||
setState {
|
||||
copy(
|
||||
joiningRoomsIds = joiningRoomsIds.toMutableList().apply { remove(publicRoom.roomId) }
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.roomdirectory
|
||||
|
||||
import com.airbnb.mvrx.Async
|
||||
import com.airbnb.mvrx.MvRxState
|
||||
import com.airbnb.mvrx.Uninitialized
|
||||
import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoom
|
||||
|
||||
data class RoomDirectoryViewState(
|
||||
// Store cumul of pagination result
|
||||
val publicRooms: List<PublicRoom> = emptyList(),
|
||||
// Current pagination request
|
||||
val asyncPublicRoomsRequest: Async<List<PublicRoom>> = Uninitialized,
|
||||
// True if more result are available server side
|
||||
val hasMore: Boolean = false,
|
||||
// List of roomIds that the user wants to join
|
||||
val joiningRoomsIds: List<String> = emptyList(),
|
||||
// List of joined roomId,
|
||||
val joinedRoomsIds: List<String> = emptyList()
|
||||
) : MvRxState
|
Reference in New Issue
Block a user