Skip to content

Commit

Permalink
fix(ui): loại bỏ streak ở màn profile
Browse files Browse the repository at this point in the history
  • Loading branch information
nqmgaming committed Dec 18, 2024
1 parent c6598f2 commit 898dc39
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.pwhs.quickmem.presentation.app.profile

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -20,8 +18,6 @@ import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ButtonDefaults.outlinedButtonColors
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme.colorScheme
Expand All @@ -44,27 +40,21 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.pwhs.quickmem.R
import com.pwhs.quickmem.data.mapper.study_time.toStudyTimeModel
import com.pwhs.quickmem.domain.model.study_time.GetStudyTimeByUserResponseModel
import com.pwhs.quickmem.presentation.app.home.components.StreakCalendar
import com.pwhs.quickmem.presentation.app.paywall.Paywall
import com.pwhs.quickmem.presentation.component.LearningBars
import com.pwhs.quickmem.ui.theme.QuickMemTheme
import com.pwhs.quickmem.ui.theme.firasansExtraboldFont
import com.pwhs.quickmem.ui.theme.premiumColor
import com.pwhs.quickmem.ui.theme.streakTextColor
import com.pwhs.quickmem.ui.theme.streakTitleColor
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
import com.ramcosta.composedestinations.generated.destinations.ChangeAvatarScreenDestination
Expand All @@ -73,7 +63,6 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import com.ramcosta.composedestinations.result.NavResult
import com.ramcosta.composedestinations.result.ResultRecipient
import com.revenuecat.purchases.CustomerInfo
import java.time.LocalDate

@Composable
@Destination<RootGraph>
Expand Down Expand Up @@ -119,8 +108,6 @@ fun ProfileScreen(
viewModel.onEvent(ProfileUiAction.OnChangeCustomerInfo(customerInfo))
},
customerInfo = uiState.customerInfo,
streakCount = uiState.streakCount,
streakDates = uiState.streakDates,
studyTime = uiState.studyTime
)
}
Expand All @@ -138,9 +125,6 @@ fun Profile(
navigateToSettings: () -> Unit = {},
onCustomerInfoChanged: (customerInfo: CustomerInfo) -> Unit = {},
customerInfo: CustomerInfo? = null,
streakCount: Int = 0,
streakDates: List<LocalDate> = emptyList(),
currentDate: LocalDate = LocalDate.now(),
studyTime: GetStudyTimeByUserResponseModel? = null
) {
var isPaywallVisible by remember {
Expand Down Expand Up @@ -289,74 +273,6 @@ fun Profile(
}
}
}

item {
Text(
text = stringResource(R.string.txt_look_at_your_streak),
style = typography.bodyLarge.copy(
fontWeight = FontWeight.ExtraBold,
fontSize = 20.sp
)
)
}

item {
Card(
modifier = Modifier
.fillMaxSize(),
colors = CardDefaults.cardColors(
containerColor = colorScheme.surface
),
border = BorderStroke(
width = 1.dp,
color = colorScheme.onSurface
)
) {
Column(
modifier = Modifier
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(R.drawable.ic_fire),
modifier = modifier.size(100.dp),
contentDescription = "streak fire",
contentScale = ContentScale.Crop
)
Text(
text = streakCount.toString(),
style = typography.titleLarge.copy(
color = streakTitleColor,
fontWeight = FontWeight.Bold,
fontSize = 52.sp
)
)
Text(
text = when (streakCount) {
1 -> stringResource(R.string.txt_day_streak)
else -> stringResource(R.string.txt_days_streak)
},
style = typography.titleLarge.copy(
color = streakTextColor,
fontWeight = FontWeight.Bold
),
modifier = Modifier.padding(bottom = 16.dp)
)
Text(
text = stringResource(R.string.txt_practice_every_day_so_you_don_t_lose_your_streak),
style = typography.bodyMedium.copy(
fontWeight = FontWeight.Bold
),
textAlign = TextAlign.Center,
modifier = Modifier.padding(bottom = 16.dp)
)
StreakCalendar(
currentDate = currentDate,
streakDates = streakDates
)
}
}
}
item {
if (studyTime?.flip != 0 || studyTime.quiz != 0 || studyTime.total != 0 || studyTime.flip != 0) {
LearningBars(
Expand Down Expand Up @@ -384,7 +300,7 @@ fun Profile(
}
}

@PreviewLightDark
@Preview(showBackground = true)
@Composable
fun ProfilePreview() {
QuickMemTheme {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.pwhs.quickmem.presentation.app.profile

import com.pwhs.quickmem.domain.model.streak.StreakModel
import com.pwhs.quickmem.domain.model.study_time.GetStudyTimeByUserResponseModel
import com.revenuecat.purchases.CustomerInfo
import java.time.LocalDate

data class ProfileUiState(
val userAvatar: String = "",
val username: String = "",
val role : String = "",
val role: String = "",
val customerInfo: CustomerInfo? = null,
val isLoading: Boolean = false,
val streakCount: Int = 0,
val streaks: List<StreakModel> = emptyList(),
val streakDates: List<LocalDate> = emptyList(),
val studyTime: GetStudyTimeByUserResponseModel? = null,

)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.pwhs.quickmem.core.datastore.AppManager
import com.pwhs.quickmem.core.datastore.TokenManager
import com.pwhs.quickmem.core.utils.Resources
import com.pwhs.quickmem.domain.model.streak.StreakModel
import com.pwhs.quickmem.domain.repository.AuthRepository
import com.pwhs.quickmem.domain.repository.StreakRepository
import com.pwhs.quickmem.domain.repository.StudyTimeRepository
import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.Purchases
Expand All @@ -28,8 +26,6 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import timber.log.Timber
import java.time.LocalDate
import java.time.OffsetDateTime
import javax.inject.Inject

@HiltViewModel
Expand All @@ -38,7 +34,6 @@ class ProfileViewModel @Inject constructor(
private val tokenManager: TokenManager,
private val authRepository: AuthRepository,
private val studyTimeRepository: StudyTimeRepository,
private val streakRepository: StreakRepository
) : ViewModel() {
private val _uiState = MutableStateFlow(ProfileUiState())
val uiState: StateFlow<ProfileUiState> = _uiState.asStateFlow()
Expand All @@ -58,11 +53,9 @@ class ProfileViewModel @Inject constructor(
val userId = appManager.userId.firstOrNull() ?: ""
if (token.isNotEmpty() && userId.isNotEmpty()) {
loadProfile()
updateStreak(token = token, userId = userId)
getUserProfile(token = token, userId = userId)
getCustomerInfo()
getStudyTime(token = token, userId = userId)
getStreaksByUserId(token = token, userId = userId)
}
}
}
Expand Down Expand Up @@ -197,69 +190,4 @@ class ProfileViewModel @Inject constructor(
}
}
}

private fun getStreaksByUserId(token: String, userId: String) {
viewModelScope.launch {
streakRepository.getStreaksByUserId(token, userId).collect { resource ->
when (resource) {
is Resources.Loading -> {
_uiState.value = _uiState.value.copy(isLoading = true)
}

is Resources.Success -> {
val streaks = resource.data?.streaks ?: emptyList()
val streakDates = calculateStreakDates(streaks)

_uiState.value = _uiState.value.copy(
isLoading = false,
streaks = streaks,
streakDates = streakDates,
)
}

is Resources.Error -> {
_uiState.value = _uiState.value.copy(isLoading = false)
}
}
}
}
}

private fun updateStreak(token: String, userId: String) {
viewModelScope.launch {
streakRepository.updateStreak(token, userId).collect { resource ->
when (resource) {
is Resources.Loading -> {
_uiState.update {
it.copy(isLoading = true)
}
}

is Resources.Success -> {
_uiState.update {
it.copy(
isLoading = false,
streakCount = resource.data?.streakCount ?: 0
)
}
}

is Resources.Error -> {
_uiState.update {
it.copy(isLoading = false)
}
}
}
}
}
}

private fun calculateStreakDates(streaks: List<StreakModel>): List<LocalDate> {
return streaks.flatMap { streak ->
val firstStreakDate = OffsetDateTime.parse(streak.date).toLocalDate()
(0 until streak.streakCount).map {
firstStreakDate.minusDays(it.toLong())
}
}.distinct()
}
}

0 comments on commit 898dc39

Please sign in to comment.