Skip to content

Commit

Permalink
policy format
Browse files Browse the repository at this point in the history
  • Loading branch information
RazvanMocanuTW committed Feb 21, 2024
1 parent 6d0a57f commit 7eb5363
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand Down Expand Up @@ -199,7 +200,8 @@ private fun AppPolicyScreenContent(
item {
MarkdownText(
markdown = state.content,
style = MozillaTypography.Body2
style = MozillaTypography.Body2,
linkColor = Color.Blue
)
Spacer(modifier = Modifier.height(MozillaDimension.L))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import javax.inject.Inject

@HiltViewModel
class AppPolicyScreenViewModel @Inject constructor(
private val tikTokReporterRepository: TikTokReporterRepository,
savedStateHandle: SavedStateHandle
private val tikTokReporterRepository: TikTokReporterRepository, savedStateHandle: SavedStateHandle
) : ViewModel() {

private val _state = MutableStateFlow(State())
Expand All @@ -43,60 +42,58 @@ class AppPolicyScreenViewModel @Inject constructor(
policyType = NestedDestination.AppPolicy.Type.valueOf(type)

viewModelScope.launch(Dispatchers.Unconfined) {
_refreshAction
.collect {
_isLoading.update { true }

val policies =
if (policyType == NestedDestination.AppPolicy.Type.Study || !isForOnboarding) {
val selectedStudy = tikTokReporterRepository.getSelectedStudy()

if (selectedStudy.isFailure) {
val error = selectedStudy.exceptionOrNull()!!.toTikTokReporterError()
_isLoading.update { false }
_uiAction.send(UiAction.ShowError(error))
return@collect
}

selectedStudy.getOrNull()!!.policies
} else {

val policyResult = tikTokReporterRepository.getAppPolicies()
if (policyResult.isFailure) {
val error = policyResult.exceptionOrNull()!!.toTikTokReporterError()
_isLoading.update { false }
_uiAction.send(UiAction.ShowError(error))
return@collect
}

policyResult.getOrNull().orEmpty()
}

val policy = policies.firstOrNull { policy ->
when (policyType) {
NestedDestination.AppPolicy.Type.TermsAndConditions -> policy.type == Policy.Type.TermsAndConditions
NestedDestination.AppPolicy.Type.PrivacyPolicy -> policy.type == Policy.Type.Privacy
NestedDestination.AppPolicy.Type.Study -> policy.type == Policy.Type.TermsAndConditions
}
} ?: kotlin.run {
_refreshAction.collect {
_isLoading.update { true }

val policies = if (policyType == NestedDestination.AppPolicy.Type.Study || !isForOnboarding) {
val selectedStudy = tikTokReporterRepository.getSelectedStudy()

if (selectedStudy.isFailure) {
val error = selectedStudy.exceptionOrNull()!!.toTikTokReporterError()
_isLoading.update { false }
_uiAction.send(UiAction.ShowError(error))
return@collect
}

val uiAction = if (policyType == NestedDestination.AppPolicy.Type.Study) UiAction.OnGoToStudyOnboarding
else UiAction.OnGoToStudies
_uiAction.send(uiAction)
selectedStudy.getOrNull()!!.policies
} else {

val policyResult = tikTokReporterRepository.getAppPolicies()
if (policyResult.isFailure) {
val error = policyResult.exceptionOrNull()!!.toTikTokReporterError()
_isLoading.update { false }
_uiAction.send(UiAction.ShowError(error))
return@collect
}

_isLoading.update { false }
_state.update {
it.copy(
title = policy.title,
subtitle = policy.subtitle,
content = policy.text,
)
policyResult.getOrNull().orEmpty()
}

val policy = policies.firstOrNull { policy ->
when (policyType) {
NestedDestination.AppPolicy.Type.TermsAndConditions -> policy.type == Policy.Type.TermsAndConditions
NestedDestination.AppPolicy.Type.PrivacyPolicy -> policy.type == Policy.Type.Privacy
NestedDestination.AppPolicy.Type.Study -> policy.type == Policy.Type.TermsAndConditions
}
} ?: kotlin.run {
_isLoading.update { false }

val uiAction = if (policyType == NestedDestination.AppPolicy.Type.Study) UiAction.OnGoToStudyOnboarding
else UiAction.OnGoToStudies
_uiAction.send(uiAction)

return@collect
}

_isLoading.update { false }
_state.update {
it.copy(
title = policy.title,
subtitle = policy.subtitle,
content = policy.text.replace("\\n", "\n").replace("#", "").replace("\n*", "\n")
)
}
}
}
}

Expand All @@ -118,10 +115,7 @@ class AppPolicyScreenViewModel @Inject constructor(
}

data class State(
val title: String = "",
val subtitle: String = "",
val content: String = "",
val showError: Boolean = false
val title: String = "", val subtitle: String = "", val content: String = "", val showError: Boolean = false
)

sealed class UiAction {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ coil-compose = "2.5.0"
accompanist = "0.33.2-alpha"
media3 = "1.2.0"
glean = "55.0.0"
compose-markdown = "0.4.0"
compose-markdown = "0.4.1"


[libraries]
Expand Down

0 comments on commit 7eb5363

Please sign in to comment.