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

Fix #5150: Correct home screen topic grid misalignment after returning from lesson #5563

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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,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);
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}

/** 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.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 Ctrl + Alt + L, which automatically formatted the code.

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
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -292,6 +296,78 @@ class MarginBindingAdaptersTest {
assertThat(textView.marginEnd.toFloat()).isWithin(TOLERANCE).of(40f)
}

@Config(qualifiers = "port")
@Test
fun testMarginBindableAdapters_layoutParamsSettingPreservesMargins() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun testMarginBindableAdapters_layoutParamsSettingPreservesMargins() {
fun testMarginBindableAdapters_setLayoutParams_preservesMargins() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun testMarginBindableAdapters_layoutParamsSettingPreservesMargins_landscape() {
fun testMarginBindableAdapters_landscapeMode_setLayoutParams_preservesMargins() {

Please apply naming pattern to the the tests below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which method calls?

Copy link
Contributor Author

@TanishMoral11 TanishMoral11 Dec 18, 2024

Choose a reason for hiding this comment

The 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:

  • setLayoutMarginStart()
  • setLayoutMarginEnd()
  • setLayoutMarginTop()
  • setLayoutMarginBottom()

If you feel this comment is causing confusion, I’m happy to remove it or revise it for better clarity.
Please let me know your preference.

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)
Expand Down
Loading