Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile/bugfix/general fixes #651

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.bounswe.predictionpolls.data.remote.model.request.CreateDiscretePollR
import com.bounswe.predictionpolls.data.remote.model.request.PollCommentRequest
import com.bounswe.predictionpolls.data.remote.services.PollService
import com.bounswe.predictionpolls.domain.poll.Comment
import com.bounswe.predictionpolls.domain.poll.Poll

class PollRepository(
private val pollService: PollService
Expand Down Expand Up @@ -75,4 +76,16 @@ class PollRepository(
pollService.getPollComments(pollId).map { it.toComment() }
}
}

override suspend fun getOpenedPolls(username: String): List<Poll> {
return execute {
pollService.getOpenedPolls(username).map { it.toPollDomainModel() }
}
}

override suspend fun getOpenedPollsForMe(): List<Poll> {
return execute {
pollService.getOpenedPollsForMe().map { it.toPollDomainModel() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bounswe.predictionpolls.data.remote.repositories

import com.bounswe.predictionpolls.domain.poll.Comment
import com.bounswe.predictionpolls.domain.poll.Poll

interface PollRepositoryInterface {
/**
Expand Down Expand Up @@ -41,4 +42,10 @@ interface PollRepositoryInterface {
suspend fun getComments(
pollId: Int
): List<Comment>

suspend fun getOpenedPolls(
username: String,
): List<Poll>

suspend fun getOpenedPollsForMe(): List<Poll>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

interface PollService {
@POST("/polls/discrete")
Expand Down Expand Up @@ -57,4 +58,12 @@ interface PollService {
suspend fun getPollComments(
@Path("pollId") pollId: Int
): List<GetCommentResponse>

@GET("/polls/opened")
suspend fun getOpenedPolls(
@Query("username") username: String
): List<PollResponse>

@GET("/polls/opened/me")
suspend fun getOpenedPollsForMe(): List<PollResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bounswe.predictionpolls.R
import com.bounswe.predictionpolls.ui.theme.MontserratFontFamily
import com.bounswe.predictionpolls.ui.theme.PredictionPollsTheme

Expand All @@ -44,9 +43,7 @@ fun PollComposable(
optionsContent: @Composable () -> Unit,
dueDate: String,
rejectionText: String,
commentCount: Int,
onProfileCardClicked: () -> Unit,
onShareClicked: () -> Unit,
modifier: Modifier = Modifier
) {
Box(
Expand Down Expand Up @@ -95,40 +92,6 @@ fun PollComposable(
)
}
}
Row(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(vertical = 8.dp),
horizontalArrangement = Arrangement.SpaceAround
) {
Column {
PollIcon(
id = R.drawable.ic_comment,
modifier = Modifier.background(
MaterialTheme.colorScheme.primary,
PollIconShape
)
)
Text(
commentCount.toString(),
modifier = Modifier.align(Alignment.CenterHorizontally),
fontFamily = MontserratFontFamily,
color = MaterialTheme.colorScheme.scrim,
textAlign = TextAlign.Center,
fontSize = 14.sp
)
}
PollIcon(
id = R.drawable.ic_share,
modifier = Modifier.background(MaterialTheme.colorScheme.primary, PollIconShape).clickable(onClick = onShareClicked)
)
PollIcon(
id = R.drawable.ic_warning,
modifier = Modifier.background(MaterialTheme.colorScheme.error, PollIconShape)
)

}
}
}
}
Expand Down Expand Up @@ -256,9 +219,7 @@ private fun PollComposablePreview() {
},
dueDate = "21 Nov 2023",
rejectionText = "Last 5 Days",
commentCount = 265,
onProfileCardClicked = {},
onShareClicked = {},
modifier = Modifier.padding(16.dp)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.bounswe.predictionpolls.R
import com.bounswe.predictionpolls.domain.poll.Poll
import com.bounswe.predictionpolls.domain.poll.PollOption
import com.bounswe.predictionpolls.extensions.fromISO8601
import com.bounswe.predictionpolls.utils.shareLink
import kotlinx.collections.immutable.ImmutableList


Expand Down Expand Up @@ -55,13 +54,9 @@ fun Polls(
},
dueDate = it.dueDate?.fromISO8601() ?: "",
rejectionText = it.rejectionText ?: "",
commentCount = it.commentCount,
onProfileCardClicked = {
onProfileClicked(it.pollCreatorUsername)
},
onShareClicked = {
context.shareLink(frontEndUrl+ "/vote/" + it.polId)
},
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.navigation.compose.composable
import com.bounswe.predictionpolls.domain.annotation.PollAnnotationPages
import com.bounswe.predictionpolls.ui.common.annotation.AnnotationViewModel
import com.bounswe.predictionpolls.ui.editProfile.navigateToEditProfileScreen
import com.bounswe.predictionpolls.ui.vote.navigateToPollVoteScreen

const val MY_PROFILE_SCREEN_ROUTE = "MY_PROFILE_SCREEN_ROUTE"

Expand Down Expand Up @@ -50,6 +51,9 @@ fun NavGraphBuilder.myProfileScreen(navController: NavController) {
},
onFollowClicked = {
},
onPollClicked = {
navController.navigateToPollVoteScreen(it)
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package com.bounswe.predictionpolls.ui.profile

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bounswe.predictionpolls.common.Result
import com.bounswe.predictionpolls.domain.feed.GetFeedUseCase
import com.bounswe.predictionpolls.core.BaseViewModel
import com.bounswe.predictionpolls.data.remote.repositories.PollRepositoryInterface
import com.bounswe.predictionpolls.domain.poll.Poll
import com.bounswe.predictionpolls.domain.profile.FollowUnfollowUseCase
import com.bounswe.predictionpolls.domain.profile.GetCurrentUserProfileUseCase
import com.bounswe.predictionpolls.domain.profile.ProfileInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

private data class MyProfileViewModelState(
val profileInfo: ProfileInfo? = null,
Expand Down Expand Up @@ -63,8 +64,8 @@ private data class MyProfileViewModelState(
class MyProfileViewModel @Inject constructor(
private val getProfileInfoUseCase: GetCurrentUserProfileUseCase,
private val followUnfollowUseCase: FollowUnfollowUseCase,
private val getFeedUseCase: GetFeedUseCase
) : ViewModel() {
private val pollRepository: PollRepositoryInterface,
) : BaseViewModel() {

private val _profileScreenUiState: MutableStateFlow<MyProfileViewModelState> =
MutableStateFlow(MyProfileViewModelState())
Expand Down Expand Up @@ -156,25 +157,27 @@ class MyProfileViewModel @Inject constructor(
fun fetchFeed(page: Int) = viewModelScope.launch {
_profileScreenUiState.update { it.copy(isLoading = true) }

when (val result = getFeedUseCase(page)) {
is Result.Success -> {
launchCatching(
onSuccess = { polls ->
_profileScreenUiState.update {
it.copy(
isLoading = false,
feed = result.data,
feed = polls.toImmutableList(),
error = null
)
}
}

is Result.Error -> {
},
onError = { exception ->
_profileScreenUiState.update {
it.copy(
isLoading = false,
error = result.exception.message,
error = exception?.message,
feed = null
)
}
}
) {
pollRepository.getOpenedPollsForMe()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bounswe.predictionpolls.ui.profile

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
Expand All @@ -23,7 +24,6 @@ import com.bounswe.predictionpolls.ui.common.poll.DiscreteVoteOption
import com.bounswe.predictionpolls.ui.common.poll.PollComposable
import com.bounswe.predictionpolls.ui.common.poll.ReadOnlyDiscretePollOptions
import com.bounswe.predictionpolls.ui.theme.PredictionPollsTheme
import com.bounswe.predictionpolls.utils.shareLink


@Composable
Expand All @@ -32,6 +32,7 @@ fun ProfileScreen(
onProfileClicked: (String) -> Unit,
onEditProfileClicked: (() -> Unit)?,
onFollowClicked: () -> Unit,
onPollClicked: (String) -> Unit,
modifier: Modifier = Modifier
) {
InternalProfileScreen(
Expand Down Expand Up @@ -103,13 +104,12 @@ fun ProfileScreen(
},
dueDate = it.dueDate?.fromISO8601() ?: "",
rejectionText = it.rejectionText ?: "",
commentCount = it.commentCount,
onProfileCardClicked = {
onProfileClicked(it.pollCreatorUsername)
},
onShareClicked = {
context.shareLink(frontEndUrl + "/vote/" + it.polId)
}
modifier = Modifier.clickable {
onPollClicked(it.polId)
},
)
}
}
Expand Down Expand Up @@ -203,9 +203,7 @@ private fun ProfileScreenPreview() {
modifier = Modifier.padding(16.dp),
dueDate = "",
rejectionText = "Last 5 days",
commentCount = 530,
onProfileCardClicked = {},
onShareClicked = {}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.bounswe.predictionpolls.domain.annotation.PollAnnotationPages
import com.bounswe.predictionpolls.ui.common.annotation.AnnotationViewModel
import com.bounswe.predictionpolls.ui.vote.navigateToPollVoteScreen

const val PROFILE_SCREEN_ROUTE = "profile/{username}"

Expand All @@ -33,22 +34,30 @@ fun NavGraphBuilder.profileScreen(navController: NavController) {
PollAnnotationPages.PROFILE(username)
)
profileViewModel.fetchProfileInfo(username)
profileViewModel.fetchFeed(0) // Updated to pass the username
profileViewModel.fetchFeed(username) // Updated to pass the username
}
}

val profileScreenUiState by profileViewModel.profileScreenUiState.collectAsStateWithLifecycle()

ProfileScreen(profileScreenUiState, onProfileClicked = {
navController.navigateToProfileScreen(it)
}, null, onFollowClicked = {
(profileScreenUiState as? ProfileScreenUiState.ProfileAndFeedFetched)?.let { profileScreenUiState ->
if (profileScreenUiState.isFollowedByLoggedUser == true) {
profileViewModel.unfollowUser(profileScreenUiState.profileInfo.userId)
} else
profileViewModel.followUser(profileScreenUiState.profileInfo.userId)
ProfileScreen(
profileScreenUiState,
onProfileClicked = {
navController.navigateToProfileScreen(it)
},
null,
onFollowClicked = {
(profileScreenUiState as? ProfileScreenUiState.ProfileAndFeedFetched)?.let { profileScreenUiState ->
if (profileScreenUiState.isFollowedByLoggedUser == true) {
profileViewModel.unfollowUser(profileScreenUiState.profileInfo.userId)
} else
profileViewModel.followUser(profileScreenUiState.profileInfo.userId)
}
},
onPollClicked = {
navController.navigateToPollVoteScreen(it)
}
})
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.bounswe.predictionpolls.ui.profile

import com.bounswe.predictionpolls.domain.poll.Poll
import com.bounswe.predictionpolls.domain.profile.ProfileInfo
import kotlinx.collections.immutable.ImmutableList

sealed interface ProfileScreenUiState {
data object Loading : ProfileScreenUiState
Expand All @@ -26,7 +25,7 @@ sealed interface ProfileScreenUiState {

data class ProfileAndFeedFetched(
val profileInfo: ProfileInfo,
val feed: ImmutableList<Poll>,
val feed: List<Poll>,
val followerCount: Int,
val followedCount: Int,
val isFollowedByLoggedUser: Boolean?
Expand Down
Loading