-
Notifications
You must be signed in to change notification settings - Fork 527
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
Fix #5150: Correct home screen topic grid misalignment after returning from lesson #5563
base: develop
Are you sure you want to change the base?
Changes from 5 commits
9a89bed
67d0524
2dddb99
498b7d1
5da06c5
daac2b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,63 +6,56 @@ | |
import androidx.core.view.MarginLayoutParamsCompat; | ||
import androidx.databinding.BindingAdapter; | ||
|
||
/** Holds all custom binding adapters that set margin values. */ | ||
/** | ||
* Holds all custom binding adapters that set margin values. | ||
*/ | ||
public final class MarginBindingAdapters { | ||
|
||
/** Sets the start margin for a view, accounting for RTL scenarios. */ | ||
/** | ||
* Sets the start margin for a view, accounting for RTL scenarios. | ||
*/ | ||
@BindingAdapter("layoutMarginStart") | ||
public static void setLayoutMarginStart(@NonNull View view, float marginStart) { | ||
if (view.getLayoutParams() instanceof MarginLayoutParams) { | ||
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); | ||
MarginLayoutParamsCompat.setMarginStart(params, (int) marginStart); | ||
view.requestLayout(); | ||
view.setLayoutParams(params); | ||
} | ||
} | ||
|
||
/** Sets the end margin for a view, accounting for RTL scenarios. */ | ||
/** | ||
* Sets the end margin for a view, accounting for RTL scenarios. | ||
*/ | ||
@BindingAdapter("layoutMarginEnd") | ||
public static void setLayoutMarginEnd(@NonNull View view, float marginEnd) { | ||
if (view.getLayoutParams() instanceof MarginLayoutParams) { | ||
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); | ||
MarginLayoutParamsCompat.setMarginEnd(params, (int) marginEnd); | ||
view.requestLayout(); | ||
view.setLayoutParams(params); | ||
} | ||
} | ||
|
||
/** Used to set a margin-top for views. */ | ||
/** | ||
* Used to set a margin-top for views. | ||
*/ | ||
@BindingAdapter("layoutMarginTop") | ||
public static void setLayoutMarginTop(@NonNull View view, float marginTop) { | ||
if (view.getLayoutParams() instanceof MarginLayoutParams) { | ||
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); | ||
params.topMargin = (int) marginTop; | ||
view.setLayoutParams(params); | ||
view.requestLayout(); | ||
} | ||
} | ||
|
||
/** Used to set a margin-bottom for views. */ | ||
/** | ||
* Used to set a margin-bottom for views. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the KDocs in this file seem to be able to fit as single-line Kdocs, as they originally were. They did not need to be changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, @adhiamboperes , for the feedback. The changes occurred as a result of pressing No worries, I have now corrected the comments as per your guidance. |
||
@BindingAdapter("layoutMarginBottom") | ||
public static void setLayoutMarginBottom(@NonNull View view, float marginBottom) { | ||
if (view.getLayoutParams() instanceof MarginLayoutParams) { | ||
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); | ||
params.bottomMargin = (int) marginBottom; | ||
view.setLayoutParams(params); | ||
view.requestLayout(); | ||
} | ||
} | ||
|
||
/** Used to set a margin for views. */ | ||
@BindingAdapter("layoutMargin") | ||
public static void setLayoutMargin(@NonNull View view, float margin) { | ||
if (view.getLayoutParams() instanceof MarginLayoutParams) { | ||
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams(); | ||
params.setMargins( | ||
(int) margin, | ||
(int) margin, | ||
(int) margin, | ||
(int) margin | ||
); | ||
view.requestLayout(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ import android.app.Application | |||||
import android.content.Context | ||||||
import android.content.Intent | ||||||
import android.view.View | ||||||
import android.view.ViewGroup | ||||||
import android.widget.TextView | ||||||
import androidx.appcompat.app.AppCompatActivity | ||||||
import androidx.core.view.ViewCompat | ||||||
|
@@ -112,8 +113,11 @@ class MarginBindingAdaptersTest { | |||||
@get:Rule | ||||||
val initializeDefaultLocaleRule = InitializeDefaultLocaleRule() | ||||||
|
||||||
@Inject lateinit var context: Context | ||||||
@Inject lateinit var testCoroutineDispatchers: TestCoroutineDispatchers | ||||||
@Inject | ||||||
lateinit var context: Context | ||||||
|
||||||
@Inject | ||||||
lateinit var testCoroutineDispatchers: TestCoroutineDispatchers | ||||||
|
||||||
@get:Rule | ||||||
val oppiaTestRule = OppiaTestRule() | ||||||
|
@@ -292,6 +296,78 @@ class MarginBindingAdaptersTest { | |||||
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) | ||||||
} | ||||||
|
||||||
@Config(qualifiers = "port") | ||||||
@Test | ||||||
fun testMarginBindableAdapters_layoutParamsSettingPreservesMargins() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
val textView = activityRule.scenario.runWithActivity { | ||||||
val textView: TextView = it.findViewById(R.id.test_margin_text_view) | ||||||
|
||||||
// Set initial margins | ||||||
setLayoutMarginStart(textView, /* marginStart= */ 24f) | ||||||
setLayoutMarginEnd(textView, /* marginEnd= */ 40f) | ||||||
setLayoutMarginTop(textView, /* marginTop= */ 16f) | ||||||
setLayoutMarginBottom(textView, /* marginBottom= */ 32f) | ||||||
|
||||||
return@runWithActivity textView | ||||||
} | ||||||
|
||||||
// Verify that the margins are correctly set after method calls | ||||||
assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f) | ||||||
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) | ||||||
|
||||||
val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams | ||||||
assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f) | ||||||
assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f) | ||||||
} | ||||||
|
||||||
@Config(qualifiers = "land") | ||||||
@Test | ||||||
fun testMarginBindableAdapters_layoutParamsSettingPreservesMargins_landscape() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please apply naming pattern to the the tests below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
val textView = activityRule.scenario.runWithActivity { | ||||||
val textView: TextView = it.findViewById(R.id.test_margin_text_view) | ||||||
|
||||||
// Set initial margins | ||||||
setLayoutMarginStart(textView, /* marginStart= */ 24f) | ||||||
setLayoutMarginEnd(textView, /* marginEnd= */ 40f) | ||||||
setLayoutMarginTop(textView, /* marginTop= */ 16f) | ||||||
setLayoutMarginBottom(textView, /* marginBottom= */ 32f) | ||||||
|
||||||
return@runWithActivity textView | ||||||
} | ||||||
|
||||||
// Verify that the margins are correctly set after method calls | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which method calls? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback, @adhiamboperes. The comment "// Verify that the margins are correctly set after method calls" refers to the following method calls:
If you feel this comment is causing confusion, I’m happy to remove it or revise it for better clarity. |
||||||
assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f) | ||||||
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) | ||||||
|
||||||
val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams | ||||||
assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f) | ||||||
assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f) | ||||||
} | ||||||
|
||||||
@Config(qualifiers = "sw600dp-port") | ||||||
@Test | ||||||
fun testMarginBindableAdapters_layoutParamsSettingPreservesMargins_tablet() { | ||||||
val textView = activityRule.scenario.runWithActivity { | ||||||
val textView: TextView = it.findViewById(R.id.test_margin_text_view) | ||||||
|
||||||
// Set initial margins | ||||||
setLayoutMarginStart(textView, /* marginStart= */ 24f) | ||||||
setLayoutMarginEnd(textView, /* marginEnd= */ 40f) | ||||||
setLayoutMarginTop(textView, /* marginTop= */ 16f) | ||||||
setLayoutMarginBottom(textView, /* marginBottom= */ 32f) | ||||||
|
||||||
return@runWithActivity textView | ||||||
} | ||||||
|
||||||
// Verify that the margins are correctly set after method calls | ||||||
assertThat(textView.marginStart.toFloat()).isWithin(TOLERANCE).of(24f) | ||||||
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f) | ||||||
|
||||||
val layoutParams = textView.layoutParams as ViewGroup.MarginLayoutParams | ||||||
assertThat(layoutParams.topMargin.toFloat()).isWithin(TOLERANCE).of(16f) | ||||||
assertThat(layoutParams.bottomMargin.toFloat()).isWithin(TOLERANCE).of(32f) | ||||||
} | ||||||
|
||||||
private fun testMarginBindableAdapters_topAndBottomIsCorrect() { | ||||||
activityRule.scenario.runWithActivity { | ||||||
val textView: TextView = it.findViewById(R.id.test_margin_text_view) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is worth adding an explanation to the PR descripion describing why and how this change fixes the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.