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#3146 : Create a generic utility for filtering enums #5529

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9b55f71
This commit contains enum filter which is a generic utility for filte…
whyash8 Sep 6, 2024
8a3a709
Merge branch 'develop' into enumfilter
whyash8 Sep 12, 2024
2a58cab
updated repository because of the latest changeS
whyash8 Oct 12, 2024
af2606c
Merge branch 'develop' into enumfilter
whyash8 Oct 13, 2024
d762f8c
Added BUILD.bazel file for enumfilter
whyash8 Oct 18, 2024
41a9011
Merge branch 'oppia:develop' into enumfilter
whyash8 Oct 19, 2024
65f92a0
update regarding bazel
whyash8 Oct 23, 2024
59e35ba
Merge branch 'develop' into enumfilter
whyash8 Oct 23, 2024
828080e
The changes done in the PR which were asked to be done for eg. adding…
whyash8 Oct 24, 2024
222cf89
Merge branch 'develop' into enumfilter
whyash8 Oct 26, 2024
df49e49
changes done as per required and asked
whyash8 Oct 26, 2024
0f95f28
changes in bazel as told
whyash8 Oct 30, 2024
0e67244
Merge branch 'develop' into enumfilter
whyash8 Oct 31, 2024
1062a9e
Merge branch 'develop' into enumfilter
whyash8 Nov 4, 2024
424945d
This commit contains a seperate build.BAZEL for the enumfilterutil i…
whyash8 Nov 5, 2024
8c9b272
changes
whyash8 Nov 5, 2024
b772d24
some changes
whyash8 Nov 5, 2024
d54c6aa
removed some spaces
whyash8 Nov 5, 2024
b531f4a
changes to solve some errors
whyash8 Nov 5, 2024
d10a599
some changes
whyash8 Nov 5, 2024
b4842e1
Merge branch 'develop' into enumfilter
whyash8 Nov 8, 2024
a59f243
Update utility/src/main/java/org/oppia/android/util/enumfilter/BUILD.…
whyash8 Nov 8, 2024
e7a0e78
Update utility/src/main/java/org/oppia/android/util/enumfilter/EnumFi…
whyash8 Nov 8, 2024
68665b6
The changes which are asked to be done for cleaning up the PR and add…
whyash8 Nov 8, 2024
12e17b8
Merge branch 'develop' into enumfilter
whyash8 Nov 12, 2024
ae222c6
Merge branch 'develop' into enumfilter
adhiamboperes Nov 15, 2024
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
# Utilities that are primarily used for frontend/UI purposes.
/utility/src/*/java/org/oppia/android/util/accessibility/ @oppia/android-frontend-reviewers
/utility/src/*/java/org/oppia/android/util/statusbar/ @oppia/android-frontend-reviewers
/utility/src/main/java/org/oppia/android/util/enumfilter/ @oppia/android-frontend-reviewers
/utility/src/*/java/org/oppia/android/util/extensions/ @oppia/android-frontend-reviewers
/utility/src/*/java/org/oppia/android/util/parser/html @oppia/android-frontend-reviewers
/utility/src/*/res/**/*.xml @oppia/android-frontend-reviewers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.oppia.android.app.survey.PreviousAnswerHandler
import org.oppia.android.app.survey.SelectedAnswerAvailabilityReceiver
import org.oppia.android.app.survey.SelectedAnswerHandler
import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.util.enumfilter.filterByEnumCondition
import javax.inject.Inject

/** [SurveyAnswerItemViewModel] for the market fit question options. */
Expand Down Expand Up @@ -98,8 +99,12 @@ class MarketFitItemsViewModel @Inject constructor(
private fun getMarketFitOptions(): ObservableList<MultipleChoiceOptionContentViewModel> {
val appName = resourceHandler.getStringInLocale(R.string.app_name)
val observableList = ObservableArrayList<MultipleChoiceOptionContentViewModel>()
observableList += MarketFitAnswer.values()
.filter { it.isValid() }
val filteredmarketFitAnswer = filterByEnumCondition(
MarketFitAnswer.values().toList(),
{ marketFitAnswer -> marketFitAnswer },
{ marketFitAnswer -> marketFitAnswer.isValid() }
)
observableList += filteredmarketFitAnswer
.mapIndexed { index, marketFitAnswer ->
when (marketFitAnswer) {
MarketFitAnswer.VERY_DISAPPOINTED -> MultipleChoiceOptionContentViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.oppia.android.app.survey.SelectedAnswerAvailabilityReceiver
import org.oppia.android.app.survey.SelectedAnswerHandler
import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.app.viewmodel.ObservableArrayList
import org.oppia.android.util.enumfilter.filterByEnumCondition
import javax.inject.Inject

/** [SurveyAnswerItemViewModel] for providing the type of user question options. */
Expand Down Expand Up @@ -97,46 +98,36 @@ class UserTypeItemsViewModel @Inject constructor(

private fun getUserTypeOptions(): ObservableArrayList<MultipleChoiceOptionContentViewModel> {
val observableList = ObservableArrayList<MultipleChoiceOptionContentViewModel>()
observableList += UserTypeAnswer.values()
.filter { it.isValid() }
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
.mapIndexed { index, userTypeOption ->
when (userTypeOption) {
UserTypeAnswer.LEARNER ->
MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(
R.string.user_type_answer_learner
),
index,
this
)
UserTypeAnswer.TEACHER -> MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(
R.string.user_type_answer_teacher
),
index,
this
)

UserTypeAnswer.PARENT ->
MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(
R.string.user_type_answer_parent
),
index,
this
)

UserTypeAnswer.OTHER ->
MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(
R.string.user_type_answer_other
),
index,
this
)
else -> throw IllegalStateException("Invalid UserTypeAnswer")
}
val filteredUserTypes = filterByEnumCondition(
UserTypeAnswer.values().toList(),
{ userTypeAnswer -> userTypeAnswer },
{ userTypeAnswer -> userTypeAnswer.isValid() }
)
observableList += filteredUserTypes.mapIndexed { index, userTypeOption ->
when (userTypeOption) {
UserTypeAnswer.LEARNER -> MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(R.string.user_type_answer_learner),
index,
this
)
UserTypeAnswer.TEACHER -> MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(R.string.user_type_answer_teacher),
index,
this
)
UserTypeAnswer.PARENT -> MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(R.string.user_type_answer_parent),
index,
this
)
UserTypeAnswer.OTHER -> MultipleChoiceOptionContentViewModel(
resourceHandler.getStringInLocale(R.string.user_type_answer_other),
index,
this
)
else -> throw IllegalStateException("Invalid UserTypeAnswer")
}
}
return observableList
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.oppia.android.domain.oppialogger.OppiaLogger
import org.oppia.android.util.accessibility.AccessibilityService
import org.oppia.android.util.data.AsyncResult
import org.oppia.android.util.data.DataProviders.Companion.toLiveData
import org.oppia.android.util.enumfilter.filterByEnumCondition
import javax.inject.Inject

/** The presenter for [TopicLessonsFragment]. */
Expand Down Expand Up @@ -161,18 +162,18 @@ class TopicLessonsFragmentPresenter @Inject constructor(

val chapterSummaries = storySummaryViewModel
.storySummary.chapterList
val completedChapterCount =
chapterSummaries.map(ChapterSummary::getChapterPlayState)
.filter {
it == ChapterPlayState.COMPLETED
}
.size
val completedChapterCount = filterByEnumCondition(
chapterSummaries.map(ChapterSummary::getChapterPlayState),
{ it },
{ it == ChapterPlayState.COMPLETED }
).size

val inProgressChapterCount =
chapterSummaries.map(ChapterSummary::getChapterPlayState)
.filter {
it == ChapterPlayState.IN_PROGRESS_SAVED
}
.size
filterByEnumCondition(
chapterSummaries.map(ChapterSummary::getChapterPlayState),
{ it },
{ it == ChapterPlayState.IN_PROGRESS_SAVED }
).size

val storyPercentage: Int =
(completedChapterCount * 100) / storySummaryViewModel.storySummary.chapterCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ kt_android_library(
"//domain/src/main/java/org/oppia/android/domain/oppialogger:prod_module",
"//model/src/main/proto:performance_metrics_event_logger_java_proto_lite",
"//utility/src/main/java/org/oppia/android/util/data:data_provider",
"//utility/src/main/java/org/oppia/android/util/enumfilter:enum_filter_util",
"//utility/src/main/java/org/oppia/android/util/logging:console_logger",
"//utility/src/main/java/org/oppia/android/util/logging:exception_logger",
"//utility/src/main/java/org/oppia/android/util/logging/performancemetrics:performance_metrics_assessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.oppia.android.app.model.ScreenName
import org.oppia.android.data.persistence.PersistentCacheStore
import org.oppia.android.domain.oppialogger.PerformanceMetricsLogStorageCacheSize
import org.oppia.android.util.data.DataProvider
import org.oppia.android.util.enumfilter.filterByEnumCondition
import org.oppia.android.util.logging.ConsoleLogger
import org.oppia.android.util.logging.ExceptionLogger
import org.oppia.android.util.logging.performancemetrics.PerformanceMetricsAssessor
Expand Down Expand Up @@ -128,9 +129,11 @@ class PerformanceMetricsController @Inject constructor(
* priority is returned.
*/
private fun getLeastRecentMetricLogIndex(oppiaMetricLogs: OppiaMetricLogs): Int? =
oppiaMetricLogs.oppiaMetricLogList.withIndex()
.filter { it.value.priority == Priority.LOW_PRIORITY }
.minByOrNull { it.value.timestampMillis }?.index
filterByEnumCondition(
oppiaMetricLogs.oppiaMetricLogList.withIndex().toList(),
{ it.value.priority },
{ it == Priority.LOW_PRIORITY }
).minByOrNull { it.value.timestampMillis }?.index
?: getLeastRecentMediumPriorityEventIndex(oppiaMetricLogs)

/**
Expand All @@ -142,9 +145,11 @@ class PerformanceMetricsController @Inject constructor(
* priority is returned.
*/
private fun getLeastRecentMediumPriorityEventIndex(oppiaMetricLogs: OppiaMetricLogs): Int? =
oppiaMetricLogs.oppiaMetricLogList.withIndex()
.filter { it.value.priority == Priority.MEDIUM_PRIORITY }
.minByOrNull { it.value.timestampMillis }?.index
filterByEnumCondition(
oppiaMetricLogs.oppiaMetricLogList.withIndex().toList(),
{ it.value.priority },
{ it == Priority.MEDIUM_PRIORITY }
).minByOrNull { it.value.timestampMillis }?.index
?: getLeastRecentGeneralEventIndex(oppiaMetricLogs)

/** Returns the index of the least recent event regardless of their priority. */
Expand Down
4 changes: 4 additions & 0 deletions scripts/assets/test_file_exemptions.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -4310,6 +4310,10 @@ test_file_exemption {
exempted_file_path: "utility/src/main/java/org/oppia/android/util/extensions/ContextExtensions.kt"
test_file_not_required: true
}
test_file_exemption {
exempted_file_path: "utility/src/main/java/org/oppia/android/util/enumfilter/EnumFilterUtil.kt"
test_file_not_required: true
}
test_file_exemption {
exempted_file_path: "utility/src/main/java/org/oppia/android/util/extensions/StringExtensions.kt"
source_file_is_incompatible_with_code_coverage: true
Expand Down
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
General purpose utility for filtering enums.
"""

load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_library")

kt_android_library(
name = "enum_filter_util",
srcs = [
"EnumFilterUtil.kt",
],
visibility = ["//:oppia_api_visibility"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.oppia.android.util.enumfilter

/**
* Filters a collection based on a condition applied to an enum property of each element.
*
* @param E the type of enum values.
* @param T the type of elements in the collection.
* @param collection the collection of elements to filter.
* @param enumExtractor a function that extracts the enum value from each element.
* @param condition a predicate function that determines if an enum value should be included in the result.
* @return a list of elements from the collection that satisfy the condition when their enum property is evaluated.
*/

inline fun <E : Enum<E>, T> filterByEnumCondition(
collection: Collection<T>,
enumExtractor: (T) -> E,
condition: (E) -> Boolean
): List<T> {
return collection.filter { condition(enumExtractor(it)) }
}
Loading