Skip to content

Commit

Permalink
fix oppia#5150, simplify the margins' calculation
Browse files Browse the repository at this point in the history
This is just a proposal, will brush it up if the proposal is approved
  • Loading branch information
nikitaevg committed Sep 22, 2023
1 parent 89e8032 commit 037ee0e
Showing 1 changed file with 3 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.Objects
/** The view model corresponding to individual topic summaries in the topic summary RecyclerView. */
class TopicSummaryViewModel(
private val activity: AppCompatActivity,
ephemeralTopicSummary: EphemeralTopicSummary,
private val ephemeralTopicSummary: EphemeralTopicSummary,
val entityType: String,
private val topicSummaryClickListener: TopicSummaryClickListener,
private val position: Int,
Expand Down Expand Up @@ -41,72 +41,12 @@ class TopicSummaryViewModel(
topicSummaryClickListener.onTopicSummaryClicked(topicSummary)
}

/**
* Determines the start margin for an individual TopicSummary relative to the grid columns laid out on the
* HomeActivity. GridLayout columns are evenly spread out across the entire activity screen but the
* Topic Summaries are positioned towards the center, so start margins are calculated to stagger inside each
* fixed column but centered on the activity's layout, as shown below.
*
* | _____| _____ | _____ |_____ |
* | | | | | | | | | | |
* | | | | | | | | | | |
* | |_____| |_____| | |_____| |_____| |
* | | | | |
* | _____ _____ _____ _____ |
* | | | | | | | | | |
* | | | | | | | | | |
* | |_____| |_____| |_____| |_____| |
* | |
*/
fun computeStartMargin(): Int {
return when (spanCount) {
2 -> when (position % spanCount) {
0 -> outerMargin
else -> innerMargin
}
3 -> when (position % spanCount) {
0 -> outerMargin
1 -> innerMargin
2 -> 0
else -> 0
}
4 -> when (position % spanCount) {
0 -> outerMargin
1 -> innerMargin
2 -> innerMargin / 2
3 -> 0
else -> 0
}
else -> 0
}
return outerMargin
}

/**
* Determines the end margin for an individual TopicSummary relative to the grid columns laid out on the
* HomeActivity. The end margins are calculated to stagger inside each fixed column but centered on the
* activity's layout (see [computeStartMargin]).
*/
fun computeEndMargin(): Int {
return when (spanCount) {
2 -> when (position % spanCount) {
0 -> innerMargin
else -> outerMargin
}
3 -> when (position % spanCount) {
0 -> 0
1 -> innerMargin
2 -> outerMargin
else -> 0
}
4 -> when (position % spanCount) {
0 -> 0
1 -> innerMargin / 2
2 -> innerMargin
3 -> outerMargin
else -> 0
}
else -> 0
}
return innerMargin
}

fun computeLessonCountText(): String {
Expand Down

0 comments on commit 037ee0e

Please sign in to comment.