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

Merged
merged 26 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.oppia.android.app.survey.SelectedAnswerHandler
import org.oppia.android.app.translation.AppLanguageResourceHandler
import org.oppia.android.app.viewmodel.ObservableArrayList
import javax.inject.Inject
import org.oppia.android.util.enumfilter.filterByEnumCondition

/** [SurveyAnswerItemViewModel] for providing the type of user question options. */
class UserTypeItemsViewModel @Inject constructor(
Expand Down Expand Up @@ -96,9 +97,14 @@ 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
val observableList=ObservableArrayList<MultipleChoiceOptionContentViewModel>()
val filteredUserTypes=filterByEnumCondition(
UserTypeAnswer.values().toList(),
{it},
{it.isValid()}
)
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
observableList+=filteredUserTypes
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved

.mapIndexed { index, userTypeOption ->
when (userTypeOption) {
UserTypeAnswer.LEARNER ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.oppia.android.util.accessibility.AccessibilityService
import org.oppia.android.util.data.AsyncResult
import org.oppia.android.util.data.DataProviders.Companion.toLiveData
import javax.inject.Inject
import org.oppia.android.util.enumfilter.filterByEnumCondition

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

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

val inProgressChapterCount=
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 @@ -16,6 +16,7 @@ import org.oppia.android.util.networking.NetworkConnectionUtil
import java.lang.IllegalStateException
import javax.inject.Inject
import javax.inject.Singleton
import org.oppia.android.util.enumfilter.filterByEnumCondition

/**
* Controller for handling performance metrics event logging.
Expand Down Expand Up @@ -128,10 +129,12 @@ 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
?: getLeastRecentMediumPriorityEventIndex(oppiaMetricLogs)
filterByEnumCondition(
oppiaMetricLogs.oppiaMetricLogList.withIndex().toList(),
{it.value.priority},
{it==Priority.LOW_PRIORITY}
).minByOrNull{it.value.timestampMillis}?.index
?:getLeastRecentMediumPriorityEventIndex(oppiaMetricLogs)

/**
* Returns the index of the least recent event from the existing store on the basis of recency and
Expand All @@ -142,10 +145,13 @@ 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
?: getLeastRecentGeneralEventIndex(oppiaMetricLogs)
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. */
private fun getLeastRecentGeneralEventIndex(oppiaMetricLogs: OppiaMetricLogs): Int? =
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,10 @@
package org.oppia.android.util.enumfilter
// This function is used to filter the list of items based on the enum condition.
// This function takes the collection of items, enumExtractor function which extracts the enum from the item and condition function which checks the condition on the enum.
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
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