diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessagePollContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessagePollContent.kt new file mode 100644 index 0000000000..91a2ebfd10 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessagePollContent.kt @@ -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 +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollAnswer.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollAnswer.kt new file mode 100644 index 0000000000..4322a15a76 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollAnswer.kt @@ -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 +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollCreationInfo.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollCreationInfo.kt new file mode 100644 index 0000000000..66e59025b7 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollCreationInfo.kt @@ -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? = null +) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollQuestion.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollQuestion.kt new file mode 100644 index 0000000000..488ac3d87c --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/PollQuestion.kt @@ -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 +) diff --git a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollActivity.kt b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollActivity.kt index c325cd0609..d58c666a58 100644 --- a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollActivity.kt +++ b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollActivity.kt @@ -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) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollFragment.kt b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollFragment.kt index ad366da62e..8e065de5e6 100644 --- a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollFragment.kt +++ b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollFragment.kt @@ -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(), 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) diff --git a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewModel.kt b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewModel.kt index 71eae3969f..3ccb1b17c3 100644 --- a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewModel.kt @@ -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(initialState) { +class CreatePollViewModel @AssistedInject constructor( + @Assisted private val initialState: CreatePollViewState, + private val session: Session +) : VectorViewModel(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 ) diff --git a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewState.kt b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewState.kt index f9d46d5605..41629a390c 100644 --- a/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewState.kt +++ b/vector/src/main/java/im/vector/app/features/createpoll/CreatePollViewState.kt @@ -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 = emptyList() -) : MavericksState +) : MavericksState { + + constructor(args: CreatePollArgs) : this( + roomId = args.roomId + ) +} + diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt index d275dccf39..70e97ad0b2 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt @@ -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 } diff --git a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt index d727f24ade..b1bbf645d6 100644 --- a/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt +++ b/vector/src/main/java/im/vector/app/features/navigation/DefaultNavigator.kt @@ -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) } diff --git a/vector/src/main/java/im/vector/app/features/navigation/Navigator.kt b/vector/src/main/java/im/vector/app/features/navigation/Navigator.kt index 0ff17887f9..264593fe18 100644 --- a/vector/src/main/java/im/vector/app/features/navigation/Navigator.kt +++ b/vector/src/main/java/im/vector/app/features/navigation/Navigator.kt @@ -141,5 +141,5 @@ interface Navigator { fun openCallTransfer(context: Context, callId: String) - fun openCreatePoll(context: Context) + fun openCreatePoll(context: Context, roomId: String) }