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 2164255
Showing 1 changed file with 4 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.oppia.android.app.home.topiclist

import androidx.appcompat.app.AppCompatActivity
import org.oppia.android.R
import android.util.Log
import org.oppia.android.app.home.HomeItemViewModel
import org.oppia.android.app.model.EphemeralTopicSummary
import org.oppia.android.app.translation.AppLanguageResourceHandler
Expand All @@ -11,7 +12,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 +42,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 2164255

Please sign in to comment.