Skip to content

Commit

Permalink
Fix part of #2116: revert "#4874 Move PromotedStoryListAdapter to Bin…
Browse files Browse the repository at this point in the history
…dableAdapter" (#4951)

This reverts commit `7fd290d68f0440d926f2d443dbd7bfb28ab20547`, fixing
part of #2116.

While testing #2116, we found that the UI for default profile view does
not display correctly, and traced the change to #4874 in [this
discusson](#2116 (comment)).

#4874 introduces a change to `MarginBindingAdapters` which utilizes
`MarginLayoutParamsCompat` to compute the margins of a layout before
drawing it. I suspect that this works for the `Promoted Story` and
`Topic Summary` because we compute the start and end margins of each
item relative to grid columns laid out on the HomeActivity, but no such
implementation exists for the Profile Chooser view. We need to do some
further investigation into this, hence the decision to revert.

We will need to test all the associated screens to make sure they still
look as expected:

![Screenshot 2023-04-19 at 18 01
28](https://user-images.githubusercontent.com/59600948/233117952-8c981fa2-2d0c-45cc-80ff-651862c1fd96.png)


## Essential Checklist
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [x] The PR title and explanation each start with "Fix #bugnum: " (If
this PR fixes part of an issue, prefix the title with "Fix part of
#bugnum: ...".)
- [x] Any changes to
[scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets)
files have their rationale included in the PR explanation.
- [x] The PR follows the [style
guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide).
- [x] The PR does not contain any unnecessary code changes from Android
Studio
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)).
- [x] The PR is made from a branch that's **not** called "develop" and
is up-to-date with "develop".
- [x] The PR is **assigned** to the appropriate reviewers
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)).

## For UI-specific PRs only
This was mainly a re-implimentation per #632, and the UI did not change.
Please refer to #4874 for screenshots.

The Affected screen displays okay with this revert:


![Screenshot_1681916915](https://user-images.githubusercontent.com/59600948/233125096-2242fb7e-eb9f-4b78-8db0-c22abb6b5cc3.png)

Other screens:
|   |   |
|---|---|

|![Screenshot_1681917117](https://user-images.githubusercontent.com/59600948/233126283-45a860c7-26ac-47f6-8b38-e54b1ee7cb1f.png)|![Screenshot_1681917190](https://user-images.githubusercontent.com/59600948/233126350-08033853-4434-462d-9bf3-98040ff2356d.png)|
  • Loading branch information
adhiamboperes authored Apr 21, 2023
1 parent 1ba98dd commit 80d2cd0
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 241 deletions.
1 change: 0 additions & 1 deletion app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ VIEW_MODELS_WITH_RESOURCE_IMPORTS = [
"src/main/java/org/oppia/android/app/home/promotedlist/PromotedStoryListViewModel.kt",
"src/main/java/org/oppia/android/app/home/promotedlist/PromotedStoryViewModel.kt",
"src/main/java/org/oppia/android/app/home/recentlyplayed/PromotedStoryViewModel.kt",
"src/main/java/org/oppia/android/app/home/recentlyplayed/RecentlyPlayedViewModel.kt",
"src/main/java/org/oppia/android/app/home/topiclist/TopicSummaryViewModel.kt",
"src/main/java/org/oppia/android/app/onboarding/OnboadingSlideViewModel.kt",
"src/main/java/org/oppia/android/app/onboarding/OnboardingViewModel.kt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import androidx.annotation.NonNull;
import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat;
import androidx.databinding.BindingAdapter;

/** Holds all custom binding adapters that set margin values. */
Expand All @@ -14,7 +14,12 @@ public final class MarginBindingAdapters {
public static void setLayoutMarginStart(@NonNull View view, float marginStart) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
MarginLayoutParamsCompat.setMarginStart(params, (int) marginStart);
float marginEnd = params.getMarginEnd();
if (isRtlLayout(view)) {
setLayoutDirectionalMargins(view, (int) marginEnd, (int) marginStart);
} else {
setLayoutDirectionalMargins(view, (int) marginStart, (int) marginEnd);
}
view.requestLayout();
}
}
Expand All @@ -24,17 +29,36 @@ public static void setLayoutMarginStart(@NonNull View view, float marginStart) {
public static void setLayoutMarginEnd(@NonNull View view, float marginEnd) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
MarginLayoutParamsCompat.setMarginEnd(params, (int) marginEnd);
float marginStart = params.getMarginStart();
if (isRtlLayout(view)) {
setLayoutDirectionalMargins(view, (int) marginEnd, (int) marginStart);
} else {
setLayoutDirectionalMargins(view, (int) marginStart, (int) marginEnd);
}
view.requestLayout();
}
}

private static void setLayoutDirectionalMargins(
@NonNull View view,
int marginStart,
int marginEnd
) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.setMargins(marginStart, params.topMargin, marginEnd, params.bottomMargin);
}

/** Used to set a margin-top for views. */
@BindingAdapter("app:layoutMarginTop")
public static void setLayoutMarginTop(@NonNull View view, float marginTop) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.topMargin = (int) marginTop;
params.setMargins(
params.getMarginStart(),
(int) marginTop,
params.getMarginEnd(),
params.bottomMargin
);
view.requestLayout();
}
}
Expand All @@ -44,7 +68,12 @@ public static void setLayoutMarginTop(@NonNull View view, float marginTop) {
public static void setLayoutMarginBottom(@NonNull View view, float marginBottom) {
if (view.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.bottomMargin = (int) marginBottom;
params.setMargins(
params.getMarginStart(),
params.topMargin,
params.getMarginEnd(),
(int) marginBottom
);
view.requestLayout();
}
}
Expand All @@ -63,4 +92,8 @@ public static void setLayoutMargin(@NonNull View view, float margin) {
view.requestLayout();
}
}

private static boolean isRtlLayout(View view) {
return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.oppia.android.app.home.recentlyplayed

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.oppia.android.databinding.RecentlyPlayedStoryCardBinding
import org.oppia.android.databinding.SectionTitleBinding

private const val VIEW_TYPE_SECTION_TITLE_TEXT = 1
private const val VIEW_TYPE_SECTION_STORY_ITEM = 2

/**
* Adapter to inflate different items/views inside [RecyclerView] for Ongoing Story List.
*
* @property itemList the items that may be displayed in [RecentlyPlayedFragment]'s recycler view
*/
class PromotedStoryListAdapter(
private val itemList: MutableList<RecentlyPlayedItemViewModel>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var titleIndex: Int = 0
private var storyGridPosition: Int = 0
private var spanCount = 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
// TODO(#632): Generalize this binding to make adding future items easier.
VIEW_TYPE_SECTION_TITLE_TEXT -> {
val inflater = LayoutInflater.from(parent.context)
val binding =
SectionTitleBinding.inflate(
inflater,
parent,
/* attachToParent= */ false
)
SectionTitleViewHolder(binding)
}
VIEW_TYPE_SECTION_STORY_ITEM -> {
val inflater = LayoutInflater.from(parent.context)
val binding =
RecentlyPlayedStoryCardBinding.inflate(
inflater,
parent,
/* attachToParent= */ false
)
PromotedStoryViewHolder(binding)
}
else -> throw IllegalArgumentException("Invalid view type: $viewType")
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) =
when (holder.itemViewType) {
VIEW_TYPE_SECTION_TITLE_TEXT -> {
titleIndex = position
(holder as SectionTitleViewHolder).bind(itemList[position] as SectionTitleViewModel)
}
VIEW_TYPE_SECTION_STORY_ITEM -> {
storyGridPosition = position - titleIndex
(holder as PromotedStoryViewHolder).bind(itemList[position] as PromotedStoryViewModel)
}
else -> throw IllegalArgumentException("Invalid item view type: ${holder.itemViewType}")
}

override fun getItemViewType(position: Int): Int {
return when (itemList[position]) {
is SectionTitleViewModel -> {
VIEW_TYPE_SECTION_TITLE_TEXT
}
is PromotedStoryViewModel -> {
VIEW_TYPE_SECTION_STORY_ITEM
}
else -> throw IllegalArgumentException(
"Invalid type of data $position with item ${itemList[position]}"
)
}
}

override fun getItemCount(): Int {
return itemList.size
}

/**
* Specifies the number of columns of recently played stories shown in the recently played stories
* list.
*
* @param spanCount specifies the number of spaces this item should occupy, based on screen size
*/
fun setSpanCount(spanCount: Int) {
this.spanCount = spanCount
}

private class SectionTitleViewHolder(
val binding: SectionTitleBinding
) : RecyclerView.ViewHolder(binding.root) {
/** Binds the view model that sets section titles. */
fun bind(sectionTitleViewModel: SectionTitleViewModel) {
binding.viewModel = sectionTitleViewModel
}
}

private class PromotedStoryViewHolder(
val binding: RecentlyPlayedStoryCardBinding
) : RecyclerView.ViewHolder(binding.root) {
/** Binds the view model that sets recently played items. */
fun bind(promotedStoryViewModel: PromotedStoryViewModel) {
binding.viewModel = promotedStoryViewModel
}
}
}
Loading

0 comments on commit 80d2cd0

Please sign in to comment.