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
// TODO: how to get room token here?
val roomToken = "???????????????????????????"
// val roomToken = "???????????????????????????"
val roomToken = "i7ht5k9n"
binding.bubble.setOnClickListener {
val pollVoteDialog = PollMainDialogFragment.newInstance(
message.activeUser!!, roomToken, pollId,
roomToken,
pollId,
pollName
)
pollVoteDialog.show(

View file

@ -1,7 +1,5 @@
package com.nextcloud.talk.polls.model
import com.nextcloud.talk.polls.repositories.model.PollDetails
data class Poll(
val id: String,
val question: String?,
@ -15,6 +13,5 @@ data class Poll(
val maxVotes: Int,
val votedSelf: List<Int>?,
val numVoters: Int,
// TODO PollDetails needs own model class
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 {
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.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 io.reactivex.Observable
class PollRepositoryImpl(private val api: NcApi, private val currentUserProvider: CurrentUserProvider) :
class PollRepositoryImpl(private val ncApi: NcApi, private val currentUserProvider: CurrentUserProvider) :
PollRepository {
override fun getPoll(roomToken: String, pollId: String): Observable<Poll> {
// 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()
)
val credentials = ApiUtils.getCredentials(
currentUserProvider.currentUser?.username,
currentUserProvider.currentUser?.token
)
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
@JsonObject
data class PollDetails(
data class PollDetailsResponse(
@JsonField(name = ["actorType"])
var actorType: String? = null,
@JsonField(name = ["actorId"])
var actorId: String? = null,
var actorId: String,
@JsonField(name = ["actorDisplayName"])
var actorDisplayName: String? = null,
var actorDisplayName: String,
@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'
constructor() : this(null, null, null, 0)
constructor() : this(null, "", "", 0)
}

View file

@ -28,7 +28,7 @@ import kotlinx.android.parcel.Parcelize
@JsonObject
data class PollResponse(
@JsonField(name = ["id"])
var id: Int = 0,
var id: String,
@JsonField(name = ["question"])
var question: String? = null,
@ -64,9 +64,9 @@ data class PollResponse(
var numVoters: Int = 0,
@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'
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 com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.DialogPollMainBinding
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.polls.viewmodels.PollViewModel
import javax.inject.Inject
@ -78,7 +77,6 @@ class PollMainDialogFragment(
companion object {
@JvmStatic
fun newInstance(
userEntity: UserEntity,
roomTokenParam: String,
pollId: String,
name: String

View file

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

View file

@ -1,10 +1,12 @@
package com.nextcloud.talk.polls.viewmodels
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.nextcloud.talk.polls.model.Poll
import com.nextcloud.talk.polls.repositories.PollRepository
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
@ -44,17 +46,48 @@ class PollViewModel @Inject constructor(private val repository: PollRepository)
loadPoll()
}
// private fun loadPoll() {
// disposable = repository.getPoll(roomToken, pollId)
// ?.subscribeOn(Schedulers.io())
// ?.observeOn(AndroidSchedulers.mainThread())
// ?.subscribe { poll ->
// _viewState.value = PollOpenState(poll)
// }
// }
private fun loadPoll() {
disposable = repository.getPoll(roomToken, pollId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { poll ->
_viewState.value = PollOpenState(poll)
}
repository.getPoll(roomToken, pollId)
?.doOnSubscribe { disposable = it }
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(PollObserver())
}
override fun onCleared() {
super.onCleared()
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 {
fun media(parameters: Parameters, type: SharedItemType): Observable<SharedMediaItems>?
fun media(
parameters: Parameters,
type: SharedItemType
): Observable<SharedMediaItems>?
fun media(
parameters: Parameters,

View file

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