Skip to content

Commit

Permalink
Android Fix hint margins in Idle state (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderZhu authored Nov 6, 2023
1 parent 3c205a1 commit eb72603
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package org.hyperskill.app.android.step_quiz_hints.delegate

import android.content.Context
import android.text.method.LinkMovementMethod
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.core.view.updateMargins
import coil.ImageLoader
import coil.load
import coil.transform.CircleCropTransformation
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.hyperskill.app.R
import org.hyperskill.app.android.databinding.LayoutStepQuizHintsBinding
import org.hyperskill.app.reactions.domain.model.ReactionType
import org.hyperskill.app.step_quiz_hints.presentation.StepQuizHintsFeature
Expand All @@ -22,7 +26,7 @@ class StepQuizHintsDelegate(
init {
with(binding) {
with(stepQuizSeeHintsButton.root) {
setText(org.hyperskill.app.R.string.step_quiz_hints_show_button_text)
setText(R.string.step_quiz_hints_show_button_text)
setOnClickListener {
onNewMessage(StepQuizHintsFeature.Message.LoadHintButtonClicked)
}
Expand All @@ -32,7 +36,7 @@ class StepQuizHintsDelegate(
stepQuizHintContentTextView.movementMethod = LinkMovementMethod.getInstance()
stepQuizHintContentTextView.setTextIsSelectable(true)

stepQuizSeeNextHintButton.root.setText(org.hyperskill.app.R.string.step_quiz_hints_see_next_hint)
stepQuizSeeNextHintButton.root.setText(R.string.step_quiz_hints_see_next_hint)
stepQuizSeeNextHintButton.root.setOnClickListener {
onNewMessage(StepQuizHintsFeature.Message.LoadHintButtonClicked)
}
Expand Down Expand Up @@ -62,44 +66,64 @@ class StepQuizHintsDelegate(
fun onAction(action: StepQuizHintsFeature.Action.ViewAction) {
when (action) {
StepQuizHintsFeature.Action.ViewAction.ShowNetworkError ->
binding.root.snackbar(messageRes = org.hyperskill.app.R.string.connection_error)
binding.root.snackbar(messageRes = R.string.connection_error)
}
}

fun render(context: Context, state: StepQuizHintsFeature.ViewState) {
viewStateDelegate.switchState(state)
if (state is StepQuizHintsFeature.ViewState.Content.HintCard) {
with(binding.stepQuizHintCard) {
stepQuizHintNameTextView.setTextIfChanged(state.authorName)
stepQuizHintAvatarImageView.load(state.authorAvatar, imageLoader) {
transformations(CircleCropTransformation())
}
if (stepQuizHintContentTextView.originalText != state.hintText) {
stepQuizHintContentTextView.originalText = state.hintText
}
stepQuizHintBeforeRateGroup.isVisible =
state.hintState == StepQuizHintsFeature.ViewState.HintState.REACT_TO_HINT
stepQuizSeeNextHintButton.root.isVisible =
state.hintState == StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT
stepQuizHintDescriptionTextView.isVisible =
state.hintState != StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT
if (state.hintState != StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT) {
@Suppress("KotlinConstantConditions")
stepQuizHintDescriptionTextView.setTextIfChanged(
when (state.hintState) {
StepQuizHintsFeature.ViewState.HintState.REACT_TO_HINT ->
org.hyperskill.app.R.string.step_quiz_hints_helpful_question_text
StepQuizHintsFeature.ViewState.HintState.LAST_HINT ->
org.hyperskill.app.R.string.step_quiz_hints_last_hint_text
StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT ->
error("Can't evaluate text for state = $state")
}.let(context::getString)
binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
updateMargins(
top = if (state is StepQuizHintsFeature.ViewState.Idle) {
0
} else {
context.resources.getDimensionPixelOffset(
org.hyperskill.app.android.R.dimen.step_quiz_hints_top_margin
)
}
if (state.hintState == StepQuizHintsFeature.ViewState.HintState.REACT_TO_HINT) {
stepQuizHintReportTextView.setOnClickListener {
handleHintReportClick(context, onNewMessage)
}
)
}
if (state is StepQuizHintsFeature.ViewState.Content.HintCard) {
renderCardState(state, context)
}
}

private fun renderCardState(
state: StepQuizHintsFeature.ViewState.Content.HintCard,
context: Context
) {
with(binding.stepQuizHintCard) {
stepQuizHintNameTextView.setTextIfChanged(state.authorName)
stepQuizHintAvatarImageView.load(state.authorAvatar, imageLoader) {
transformations(CircleCropTransformation())
}
if (stepQuizHintContentTextView.originalText != state.hintText) {
stepQuizHintContentTextView.originalText = state.hintText
}
stepQuizHintBeforeRateGroup.isVisible =
state.hintState == StepQuizHintsFeature.ViewState.HintState.REACT_TO_HINT
stepQuizSeeNextHintButton.root.isVisible =
state.hintState == StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT
stepQuizHintDescriptionTextView.isVisible =
state.hintState != StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT
if (state.hintState != StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT) {
@Suppress("KotlinConstantConditions")
stepQuizHintDescriptionTextView.setTextIfChanged(
when (state.hintState) {
StepQuizHintsFeature.ViewState.HintState.REACT_TO_HINT ->
R.string.step_quiz_hints_helpful_question_text

StepQuizHintsFeature.ViewState.HintState.LAST_HINT ->
R.string.step_quiz_hints_last_hint_text

StepQuizHintsFeature.ViewState.HintState.SEE_NEXT_HINT ->
error("Can't evaluate text for state = $state")
}.let(context::getString)
)
}
if (state.hintState == StepQuizHintsFeature.ViewState.HintState.REACT_TO_HINT) {
stepQuizHintReportTextView.setOnClickListener {
handleHintReportClick(context, onNewMessage)
}
}
}
Expand All @@ -119,14 +143,14 @@ class StepQuizHintsDelegate(
onNewMessage: (StepQuizHintsFeature.Message) -> Unit
) =
MaterialAlertDialogBuilder(context)
.setTitle(org.hyperskill.app.R.string.step_quiz_hints_report_alert_title)
.setMessage(org.hyperskill.app.R.string.step_quiz_hints_report_alert_text)
.setPositiveButton(org.hyperskill.app.R.string.yes) { dialog, _ ->
.setTitle(R.string.step_quiz_hints_report_alert_title)
.setMessage(R.string.step_quiz_hints_report_alert_text)
.setPositiveButton(R.string.yes) { dialog, _ ->
dialog.dismiss()
onNewMessage(StepQuizHintsFeature.Message.ReportHintNoticeHiddenEventMessage(true))
onNewMessage(StepQuizHintsFeature.Message.ReportHint)
}
.setNegativeButton(org.hyperskill.app.R.string.no) { dialog, _ ->
.setNegativeButton(R.string.no) { dialog, _ ->
dialog.dismiss()
onNewMessage(
StepQuizHintsFeature.Message.ReportHintNoticeHiddenEventMessage(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:layout_marginTop="@dimen/step_quiz_hints_top_margin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down
1 change: 1 addition & 0 deletions androidHyperskillApp/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<dimen name="step_quiz_content_padding_bottom">20dp</dimen>
<dimen name="step_quiz_collapsible_block_header_height">48dp</dimen>
<dimen name="step_quiz_description_padding_top">16dp</dimen>
<dimen name="step_quiz_hints_top_margin">20dp</dimen>

<!-- Sorting quiz -->
<dimen name="step_quiz_sorting_item_elevation">1dp</dimen>
Expand Down

0 comments on commit eb72603

Please sign in to comment.