Create poll event content.

This commit is contained in:
Onuray Sahin 2021-10-28 10:50:29 +03:00
parent 6cee266a95
commit dd58dd800c
11 changed files with 153 additions and 20 deletions

View File

@ -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 org.matrix.android.sdk.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class MessagePollContent(
@Json(name = "m.poll.start") val pollCreationInfo: PollCreationInfo? = null
)

View File

@ -0,0 +1,26 @@
/*
* 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 org.matrix.android.sdk.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class PollAnswer(
@Json(name = "id") val id: String? = null,
@Json(name = "m.text") val answer: String? = null
)

View File

@ -0,0 +1,28 @@
/*
* 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 org.matrix.android.sdk.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class PollCreationInfo(
@Json(name = "question") val question: PollQuestion? = null,
@Json(name = "kind") val kind: String? = "m.poll.disclosed",
@Json(name = "max_selections") val maxSelections: Int = 1,
@Json(name = "answers") val answers: List<PollAnswer>? = null
)

View File

@ -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 org.matrix.android.sdk.api.session.room.model.message
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class PollQuestion(
@Json(name = "m.text") val question: String? = null
)

View File

@ -20,7 +20,6 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.airbnb.mvrx.viewModel
import im.vector.app.R
import im.vector.app.core.di.ScreenComponent
import im.vector.app.core.extensions.addFragment
@ -29,32 +28,40 @@ import javax.inject.Inject
class CreatePollActivity : SimpleFragmentActivity(), CreatePollViewModel.Factory {
private val viewModel: CreatePollViewModel by viewModel()
@Inject lateinit var viewModelFactory: CreatePollViewModel.Factory
var currentRoomId: String? = null
@Inject lateinit var createPollViewModelFactory: CreatePollViewModel.Factory
override fun injectWith(injector: ScreenComponent) {
super.injectWith(injector)
injector.inject(this)
}
override fun create(initialState: CreatePollViewState) = viewModelFactory.create(initialState)
override fun create(initialState: CreatePollViewState) = createPollViewModelFactory.create(initialState)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
views.toolbar.visibility = View.GONE
val createPollArgs: CreatePollArgs? = intent?.extras?.getParcelable(EXTRA_CREATE_POLL_ARGS)
currentRoomId = createPollArgs?.roomId
if (isFirstCreation()) {
addFragment(
R.id.container,
CreatePollFragment::class.java
CreatePollFragment::class.java,
createPollArgs
)
}
}
companion object {
fun getIntent(context: Context): Intent {
return Intent(context, CreatePollActivity::class.java)
private const val EXTRA_CREATE_POLL_ARGS = "EXTRA_CREATE_POLL_ARGS"
fun getIntent(context: Context, createPollArgs: CreatePollArgs): Intent {
return Intent(context, CreatePollActivity::class.java).apply {
putExtra(EXTRA_CREATE_POLL_ARGS, createPollArgs)
}
}
}
}

View File

@ -17,21 +17,31 @@
package im.vector.app.features.createpoll
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.args
import com.airbnb.mvrx.withState
import im.vector.app.core.extensions.configureWith
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentCreatePollBinding
import kotlinx.parcelize.Parcelize
import javax.inject.Inject
@Parcelize
data class CreatePollArgs(
val roomId: String
) : Parcelable
class CreatePollFragment @Inject constructor(
private val controller: CreatePollController
private val controller: CreatePollController,
val createPollViewModelFactory: CreatePollViewModel.Factory
) : VectorBaseFragment<FragmentCreatePollBinding>(), CreatePollController.Callback {
private val viewModel: CreatePollViewModel by activityViewModel()
private val createPollArgs: CreatePollArgs by args()
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentCreatePollBinding {
return FragmentCreatePollBinding.inflate(inflater, container, false)

View File

@ -24,11 +24,15 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.core.platform.VectorViewModel
import org.matrix.android.sdk.api.session.Session
import timber.log.Timber
class CreatePollViewModel @AssistedInject constructor(@Assisted
initialState: CreatePollViewState) :
VectorViewModel<CreatePollViewState, CreatePollAction, CreatePollViewEvents>(initialState) {
class CreatePollViewModel @AssistedInject constructor(
@Assisted private val initialState: CreatePollViewState,
private val session: Session
) : VectorViewModel<CreatePollViewState, CreatePollAction, CreatePollViewEvents>(initialState) {
private val room = session.getRoom(initialState.roomId)!!
@AssistedFactory
interface Factory {
@ -40,7 +44,7 @@ class CreatePollViewModel @AssistedInject constructor(@Assisted
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: CreatePollViewState): CreatePollViewModel {
val factory = when (viewModelContext) {
is FragmentViewModelContext -> viewModelContext.fragment as? Factory
is FragmentViewModelContext -> (viewModelContext.fragment as CreatePollFragment).createPollViewModelFactory
is ActivityViewModelContext -> viewModelContext.activity as? Factory
}
return factory?.create(state) ?: error("You should let your activity/fragment implements Factory interface")
@ -68,7 +72,7 @@ class CreatePollViewModel @AssistedInject constructor(@Assisted
}
private fun handleOnCreatePoll() = withState { state ->
Timber.d(state.toString())
}
private fun handleOnAddOption() {
@ -82,7 +86,7 @@ class CreatePollViewModel @AssistedInject constructor(@Assisted
private fun handleOnDeleteOption(index: Int) {
setState {
val filteredOptions = options.filterIndexed { ind, _ -> ind != index }
val filteredOptions = options.filterIndexed { ind, _ -> ind != index }
copy(
options = filteredOptions
)
@ -91,7 +95,7 @@ class CreatePollViewModel @AssistedInject constructor(@Assisted
private fun handleOnOptionChanged(index: Int, option: String) {
setState {
val changedOptions = options.mapIndexed { ind, s -> if(ind == index) option else s }
val changedOptions = options.mapIndexed { ind, s -> if (ind == index) option else s }
copy(
options = changedOptions
)

View File

@ -19,6 +19,13 @@ package im.vector.app.features.createpoll
import com.airbnb.mvrx.MavericksState
data class CreatePollViewState(
val roomId: String,
val question: String = "",
val options: List<String> = emptyList()
) : MavericksState
) : MavericksState {
constructor(args: CreatePollArgs) : this(
roomId = args.roomId
)
}

View File

@ -2145,7 +2145,7 @@ class RoomDetailFragment @Inject constructor(
AttachmentTypeSelectorView.Type.AUDIO -> attachmentsHelper.selectAudio(attachmentAudioActivityResultLauncher)
AttachmentTypeSelectorView.Type.CONTACT -> attachmentsHelper.selectContact(attachmentContactActivityResultLauncher)
AttachmentTypeSelectorView.Type.STICKER -> roomDetailViewModel.handle(RoomDetailAction.SelectStickerAttachment)
AttachmentTypeSelectorView.Type.POLL -> navigator.openCreatePoll(requireContext())
AttachmentTypeSelectorView.Type.POLL -> navigator.openCreatePoll(requireContext(), roomDetailArgs.roomId)
}.exhaustive
}

View File

@ -41,6 +41,7 @@ import im.vector.app.features.call.conference.VectorJitsiActivity
import im.vector.app.features.call.transfer.CallTransferActivity
import im.vector.app.features.createdirect.CreateDirectRoomActivity
import im.vector.app.features.createpoll.CreatePollActivity
import im.vector.app.features.createpoll.CreatePollArgs
import im.vector.app.features.crypto.keysbackup.settings.KeysBackupManageActivity
import im.vector.app.features.crypto.keysbackup.setup.KeysBackupSetupActivity
import im.vector.app.features.crypto.recover.BootstrapBottomSheet
@ -499,8 +500,8 @@ class DefaultNavigator @Inject constructor(
context.startActivity(intent)
}
override fun openCreatePoll(context: Context) {
val intent = CreatePollActivity.getIntent(context)
override fun openCreatePoll(context: Context, roomId: String) {
val intent = CreatePollActivity.getIntent(context, CreatePollArgs(roomId = roomId))
context.startActivity(intent)
}

View File

@ -141,5 +141,5 @@ interface Navigator {
fun openCallTransfer(context: Context, callId: String)
fun openCreatePoll(context: Context)
fun openCreatePoll(context: Context, roomId: String)
}