mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 18:35:40 +03:00
add viewmodel, business logic, events, state, fragment and layout
This commit is contained in:
parent
a34d445215
commit
9811d6fefc
6 changed files with 253 additions and 0 deletions
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright (c) 2021 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.app.features.roomprofile.notifications
|
||||
|
||||
import im.vector.app.core.platform.VectorViewModelAction
|
||||
import org.matrix.android.sdk.api.session.room.notification.RoomNotificationState
|
||||
|
||||
sealed class RoomNotificationSettingsAction : VectorViewModelAction {
|
||||
data class SelectNotificationState(val notificationState: RoomNotificationState): RoomNotificationSettingsAction()
|
||||
object Save: RoomNotificationSettingsAction()
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2021 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.app.features.roomprofile.notifications
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.airbnb.mvrx.fragmentViewModel
|
||||
import im.vector.app.core.platform.VectorBaseFragment
|
||||
import im.vector.app.databinding.ViewEditRoomNotificationSettingsBinding
|
||||
import im.vector.app.features.home.AvatarRenderer
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* In this screen:
|
||||
* - the account has been created and we propose the user to set an avatar and a display name
|
||||
*/
|
||||
class RoomNotificationSettingsFragment @Inject constructor(
|
||||
val roomNotificationSettingsViewModel: RoomNotificationSettingsViewModel.Factory,
|
||||
private val avatarRenderer: AvatarRenderer,
|
||||
) : VectorBaseFragment<ViewEditRoomNotificationSettingsBinding>() {
|
||||
|
||||
private val viewModel: RoomNotificationSettingsViewModel by fragmentViewModel()
|
||||
|
||||
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): ViewEditRoomNotificationSettingsBinding {
|
||||
return ViewEditRoomNotificationSettingsBinding.inflate(inflater, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
observeViewEvents()
|
||||
}
|
||||
|
||||
private fun observeViewEvents() {
|
||||
viewModel.observeViewEvents {
|
||||
when (it) {
|
||||
is RoomNotificationSettingsViewEvents.Failure -> displayErrorDialog(it.throwable)
|
||||
RoomNotificationSettingsViewEvents.SaveComplete -> TODO()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (c) 2021 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.app.features.roomprofile.notifications
|
||||
|
||||
import im.vector.app.core.platform.VectorViewEvents
|
||||
|
||||
sealed class RoomNotificationSettingsViewEvents : VectorViewEvents {
|
||||
data class Failure(val throwable: Throwable) : RoomNotificationSettingsViewEvents()
|
||||
object SaveComplete : RoomNotificationSettingsViewEvents()
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (c) 2021 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.app.features.roomprofile.notifications
|
||||
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.airbnb.mvrx.FragmentViewModelContext
|
||||
import com.airbnb.mvrx.MvRxViewModelFactory
|
||||
import com.airbnb.mvrx.ViewModelContext
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import im.vector.app.core.platform.VectorViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import org.matrix.android.sdk.api.session.room.Room
|
||||
import org.matrix.android.sdk.rx.rx
|
||||
|
||||
class RoomNotificationSettingsViewModel @AssistedInject constructor(
|
||||
@Assisted initialState: RoomNotificationSettingsViewState,
|
||||
private val room: Room
|
||||
) : VectorViewModel<RoomNotificationSettingsViewState, RoomNotificationSettingsAction, RoomNotificationSettingsViewEvents>(initialState) {
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory {
|
||||
fun create(initialState: RoomNotificationSettingsViewState): RoomNotificationSettingsViewModel
|
||||
}
|
||||
|
||||
companion object : MvRxViewModelFactory<RoomNotificationSettingsViewModel, RoomNotificationSettingsViewState> {
|
||||
|
||||
@JvmStatic
|
||||
override fun create(viewModelContext: ViewModelContext, state: RoomNotificationSettingsViewState): RoomNotificationSettingsViewModel? {
|
||||
val fragment: RoomNotificationSettingsFragment = (viewModelContext as FragmentViewModelContext).fragment()
|
||||
return fragment.roomNotificationSettingsViewModel.create(state)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
observeNotificationState()
|
||||
}
|
||||
|
||||
private fun observeNotificationState() {
|
||||
room.rx()
|
||||
.liveNotificationState()
|
||||
.subscribe{
|
||||
setState {
|
||||
copy(notificationState = it )
|
||||
}
|
||||
}
|
||||
.disposeOnClear()
|
||||
}
|
||||
|
||||
override fun handle(action: RoomNotificationSettingsAction) {
|
||||
when (action) {
|
||||
is RoomNotificationSettingsAction.SelectNotificationState -> handleSelectNotificationState(action)
|
||||
is RoomNotificationSettingsAction.Save -> handleSaveNotificationSelection(action)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleSelectNotificationState(action: RoomNotificationSettingsAction.SelectNotificationState) {
|
||||
setState {
|
||||
copy(notificationState = action.notificationState)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleSaveNotificationSelection(action: RoomNotificationSettingsAction.Save) {
|
||||
setState { copy(isLoading = true) }
|
||||
withState { state ->
|
||||
viewModelScope.launch {
|
||||
runCatching { room.setRoomNotificationState(state.notificationState) }
|
||||
.onFailure { _viewEvents.post(RoomNotificationSettingsViewEvents.Failure(it)) }
|
||||
setState {
|
||||
copy(isLoading = false)
|
||||
}
|
||||
_viewEvents.post(RoomNotificationSettingsViewEvents.SaveComplete)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2021 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.app.features.roomprofile.notifications
|
||||
|
||||
import com.airbnb.mvrx.MvRxState
|
||||
import org.matrix.android.sdk.api.session.room.notification.RoomNotificationState
|
||||
|
||||
data class RoomNotificationSettingsViewState(
|
||||
val isLoading: Boolean,
|
||||
val roomEncrypted: Boolean,
|
||||
val notificationState: RoomNotificationState,
|
||||
val avatarData: AvatarData?
|
||||
) : MvRxState
|
||||
|
||||
data class AvatarData (
|
||||
val displayName: String,
|
||||
val avatarUrl: String
|
||||
)
|
||||
|
||||
val RoomNotificationSettingsViewState.notificationOptions: List<RoomNotificationState>
|
||||
get() {
|
||||
return if (roomEncrypted) {
|
||||
listOf(RoomNotificationState.ALL_MESSAGES, RoomNotificationState.MUTE)
|
||||
} else
|
||||
RoomNotificationState.values().asList()
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RadioGroup
|
||||
android:id="@+id/roomNotificationRadioGroup"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
</RadioGroup>
|
||||
</LinearLayout>
|
Loading…
Reference in a new issue