wip: get poll from API

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-08 15:48:05 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 42324419cd
commit c7e9721809
12 changed files with 139 additions and 45 deletions

View File

@ -117,11 +117,13 @@ class IncomingPollMessageViewHolder(incomingView: View, payload: Any) : MessageH
binding.messagePollTitle.text = pollName binding.messagePollTitle.text = pollName
// TODO: how to get room token here? // TODO: how to get room token here?
val roomToken = "???????????????????????????" // val roomToken = "???????????????????????????"
val roomToken = "i7ht5k9n"
binding.bubble.setOnClickListener { binding.bubble.setOnClickListener {
val pollVoteDialog = PollMainDialogFragment.newInstance( val pollVoteDialog = PollMainDialogFragment.newInstance(
message.activeUser!!, roomToken, pollId, roomToken,
pollId,
pollName pollName
) )
pollVoteDialog.show( pollVoteDialog.show(

View File

@ -1,7 +1,5 @@
package com.nextcloud.talk.polls.model package com.nextcloud.talk.polls.model
import com.nextcloud.talk.polls.repositories.model.PollDetails
data class Poll( data class Poll(
val id: String, val id: String,
val question: String?, val question: String?,
@ -15,6 +13,5 @@ data class Poll(
val maxVotes: Int, val maxVotes: Int,
val votedSelf: List<Int>?, val votedSelf: List<Int>?,
val numVoters: Int, val numVoters: Int,
// TODO PollDetails needs own model class
val details: List<PollDetails>? val details: List<PollDetails>?
) )

View File

@ -0,0 +1,8 @@
package com.nextcloud.talk.polls.model
data class PollDetails(
val actorType: String?,
val actorId: String?,
val actorDisplayName: String?,
val optionId: Int
)

View File

@ -5,5 +5,5 @@ import io.reactivex.Observable
interface PollRepository { interface PollRepository {
fun getPoll(roomToken: String, pollId: String): Observable<Poll> fun getPoll(roomToken: String, pollId: String): Observable<Poll>?
} }

View File

@ -23,30 +23,82 @@ package com.nextcloud.talk.polls.repositories
import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.polls.model.Poll import com.nextcloud.talk.polls.model.Poll
import com.nextcloud.talk.polls.model.PollDetails
import com.nextcloud.talk.polls.repositories.model.PollDetailsResponse
import com.nextcloud.talk.polls.repositories.model.PollResponse
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.database.user.CurrentUserProvider import com.nextcloud.talk.utils.database.user.CurrentUserProvider
import io.reactivex.Observable import io.reactivex.Observable
class PollRepositoryImpl(private val api: NcApi, private val currentUserProvider: CurrentUserProvider) : class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvider: CurrentUserProvider) :
PollRepository { PollRepository {
override fun getPoll(roomToken: String, pollId: String): Observable<Poll> { override fun getPoll(roomToken: String, pollId: String): Observable<Poll> {
// TODO actual api call
return Observable.just( val credentials = ApiUtils.getCredentials(
Poll( currentUserProvider.currentUser?.username,
id = "aaa", currentUserProvider.currentUser?.token
question = "what if?",
options = listOf("yes", "no", "maybe", "I don't know"),
votes = listOf(0, 0, 0, 0),
actorType = "",
actorId = "",
actorDisplayName = "",
status = 0,
resultMode = 0,
maxVotes = 1,
votedSelf = listOf(0, 0, 0, 0),
numVoters = 0,
details = emptyList()
) )
return ncApi.getPoll(
credentials,
ApiUtils.getUrlForPoll(
currentUserProvider.currentUser?.baseUrl,
roomToken,
pollId
),
).map { mapToPoll(it.ocs?.data!!) }
// // // TODO actual api call
// return Observable.just(
// Poll(
// id = "aaa",
// question = "what if?",
// options = listOf("yes", "no", "maybe", "I don't know"),
// votes = listOf(0, 0, 0, 0),
// actorType = "",
// actorId = "",
// actorDisplayName = "",
// status = 0,
// resultMode = 0,
// maxVotes = 1,
// votedSelf = listOf(0, 0, 0, 0),
// numVoters = 0,
// details = emptyList()
// )
// )
}
companion object {
private fun mapToPoll(pollResponse: PollResponse): Poll {
val pollDetails = pollResponse.details?.map { it -> mapToPollDetails(it) }
val poll = Poll(
pollResponse.id,
pollResponse.question,
pollResponse.options,
pollResponse.votes,
pollResponse.actorType,
pollResponse.actorId,
pollResponse.actorDisplayName,
pollResponse.status,
pollResponse.resultMode,
pollResponse.maxVotes,
pollResponse.votedSelf,
pollResponse.numVoters,
pollDetails,
)
return poll
}
private fun mapToPollDetails(pollDetailsResponse: PollDetailsResponse): PollDetails {
return PollDetails(
pollDetailsResponse.actorType,
pollDetailsResponse.actorId,
pollDetailsResponse.actorDisplayName,
pollDetailsResponse.optionId,
) )
} }
} }
}

View File

@ -7,20 +7,20 @@ import kotlinx.android.parcel.Parcelize
@Parcelize @Parcelize
@JsonObject @JsonObject
data class PollDetails( data class PollDetailsResponse(
@JsonField(name = ["actorType"]) @JsonField(name = ["actorType"])
var actorType: String? = null, var actorType: String? = null,
@JsonField(name = ["actorId"]) @JsonField(name = ["actorId"])
var actorId: String? = null, var actorId: String,
@JsonField(name = ["actorDisplayName"]) @JsonField(name = ["actorDisplayName"])
var actorDisplayName: String? = null, var actorDisplayName: String,
@JsonField(name = ["optionId"]) @JsonField(name = ["optionId"])
var optionId: Int? = 0, var optionId: Int,
) : Parcelable { ) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null, null, null, 0) constructor() : this(null, "", "", 0)
} }

View File

@ -28,7 +28,7 @@ import kotlinx.android.parcel.Parcelize
@JsonObject @JsonObject
data class PollResponse( data class PollResponse(
@JsonField(name = ["id"]) @JsonField(name = ["id"])
var id: Int = 0, var id: String,
@JsonField(name = ["question"]) @JsonField(name = ["question"])
var question: String? = null, var question: String? = null,
@ -64,9 +64,9 @@ data class PollResponse(
var numVoters: Int = 0, var numVoters: Int = 0,
@JsonField(name = ["details"]) @JsonField(name = ["details"])
var details: ArrayList<PollDetails>? = null, var details: ArrayList<PollDetailsResponse>? = null,
) : Parcelable { ) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(0, null, null, null, null, null, null, 0, 0, 0, null) constructor() : this("id", null, null, null, null, null, null, 0, 0, 0, null, 0, null)
} }

View File

@ -12,7 +12,6 @@ import androidx.lifecycle.ViewModelProvider
import autodagger.AutoInjector import autodagger.AutoInjector
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.DialogPollMainBinding import com.nextcloud.talk.databinding.DialogPollMainBinding
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.polls.viewmodels.PollViewModel import com.nextcloud.talk.polls.viewmodels.PollViewModel
import javax.inject.Inject import javax.inject.Inject
@ -78,7 +77,6 @@ class PollMainDialogFragment(
companion object { companion object {
@JvmStatic @JvmStatic
fun newInstance( fun newInstance(
userEntity: UserEntity,
roomTokenParam: String, roomTokenParam: String,
pollId: String, pollId: String,
name: String name: String

View File

@ -22,6 +22,7 @@
package com.nextcloud.talk.polls.ui package com.nextcloud.talk.polls.ui
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -74,6 +75,7 @@ class PollVoteFragment(private val parentViewModel: PollViewModel) : Fragment()
.also { .also {
it.setOnClickListener { it.setOnClickListener {
// todo // todo
Log.d("bb", "click1")
} }
} }
}?.forEach { }?.forEach {
@ -83,6 +85,7 @@ class PollVoteFragment(private val parentViewModel: PollViewModel) : Fragment()
} }
binding.radioGroup.setOnCheckedChangeListener { group, checkedId -> binding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
// todo set selected in viewmodel // todo set selected in viewmodel
Log.d("bb", "click")
} }
// todo observe viewmodel checked, set view checked with it // todo observe viewmodel checked, set view checked with it
// todo listen to button click, submit // todo listen to button click, submit

View File

@ -1,10 +1,12 @@
package com.nextcloud.talk.polls.viewmodels package com.nextcloud.talk.polls.viewmodels
import android.util.Log
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.nextcloud.talk.polls.model.Poll import com.nextcloud.talk.polls.model.Poll
import com.nextcloud.talk.polls.repositories.PollRepository import com.nextcloud.talk.polls.repositories.PollRepository
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
@ -44,17 +46,48 @@ class PollViewModel @Inject constructor(private val repository: PollRepository)
loadPoll() loadPoll()
} }
// private fun loadPoll() {
// disposable = repository.getPoll(roomToken, pollId)
// ?.subscribeOn(Schedulers.io())
// ?.observeOn(AndroidSchedulers.mainThread())
// ?.subscribe { poll ->
// _viewState.value = PollOpenState(poll)
// }
// }
private fun loadPoll() { private fun loadPoll() {
disposable = repository.getPoll(roomToken, pollId) repository.getPoll(roomToken, pollId)
.subscribeOn(Schedulers.io()) ?.doOnSubscribe { disposable = it }
.observeOn(AndroidSchedulers.mainThread()) ?.subscribeOn(Schedulers.io())
.subscribe { poll -> ?.observeOn(AndroidSchedulers.mainThread())
_viewState.value = PollOpenState(poll) ?.subscribe(PollObserver())
}
} }
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()
disposable?.dispose() disposable?.dispose()
} }
inner class PollObserver : Observer<Poll> {
lateinit var poll: Poll
override fun onSubscribe(d: Disposable) = Unit
override fun onNext(response: Poll) {
poll = response
}
override fun onError(e: Throwable) {
Log.d(TAG, "An error occurred: $e")
}
override fun onComplete() {
_viewState.value = PollOpenState(poll)
}
}
companion object {
private val TAG = PollViewModel::class.java.simpleName
}
} }

View File

@ -28,7 +28,10 @@ import io.reactivex.Observable
interface SharedItemsRepository { interface SharedItemsRepository {
fun media(parameters: Parameters, type: SharedItemType): Observable<SharedMediaItems>? fun media(
parameters: Parameters,
type: SharedItemType
): Observable<SharedMediaItems>?
fun media( fun media(
parameters: Parameters, parameters: Parameters,

View File

@ -46,7 +46,6 @@
app:layout_constraintTop_toTopOf="@+id/message_poll_icon" app:layout_constraintTop_toTopOf="@+id/message_poll_icon"
tools:text="This is the poll title?" /> tools:text="This is the poll title?" />
<FrameLayout <FrameLayout
android:id="@+id/message_poll_content_fragment" android:id="@+id/message_poll_content_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -54,5 +53,4 @@
app:layout_constraintTop_toBottomOf="@id/message_poll_title" app:layout_constraintTop_toBottomOf="@id/message_poll_title"
tools:layout_height="400dp" /> tools:layout_height="400dp" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>