Skip to content

Commit 0e48c35

Browse files
authored
teams: smoother survey repository querying (fixes #8975) (#8924)
1 parent 47945e9 commit 0e48c35

File tree

6 files changed

+83
-12
lines changed

6 files changed

+83
-12
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "org.ole.planet.myplanet"
1010
minSdk = 26
1111
targetSdk = 36
12-
versionCode = 3697
13-
versionName = "0.36.97"
12+
versionCode = 3698
13+
versionName = "0.36.98"
1414
ndkVersion = '26.3.11579264'
1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
vectorDrawables.useSupportLibrary = true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package org.ole.planet.myplanet.repository
22

33
import org.ole.planet.myplanet.model.RealmStepExam
4+
import org.ole.planet.myplanet.ui.survey.SurveyInfo
45

56
interface SurveyRepository {
67
suspend fun getTeamOwnedSurveys(teamId: String?): List<RealmStepExam>
78
suspend fun getAdoptableTeamSurveys(teamId: String?): List<RealmStepExam>
89
suspend fun getIndividualSurveys(): List<RealmStepExam>
10+
suspend fun getSurveyInfos(
11+
isTeam: Boolean,
12+
teamId: String?,
13+
userId: String?,
14+
surveys: List<RealmStepExam>
15+
): Map<String, SurveyInfo>
916
}

app/src/main/java/org/ole/planet/myplanet/repository/SurveyRepositoryImpl.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package org.ole.planet.myplanet.repository
22

3+
import android.content.Context
4+
import dagger.hilt.android.qualifiers.ApplicationContext
5+
import io.realm.Sort
36
import javax.inject.Inject
47
import org.json.JSONException
58
import org.json.JSONObject
9+
import org.ole.planet.myplanet.R
610
import org.ole.planet.myplanet.datamanager.DatabaseService
711
import org.ole.planet.myplanet.model.RealmStepExam
812
import org.ole.planet.myplanet.model.RealmSubmission
13+
import org.ole.planet.myplanet.ui.survey.SurveyInfo
14+
import org.ole.planet.myplanet.utilities.TimeUtils.formatDate
15+
import org.ole.planet.myplanet.utilities.TimeUtils.getFormattedDateWithTime
916

1017
class SurveyRepositoryImpl @Inject constructor(
18+
@ApplicationContext private val context: Context,
1119
databaseService: DatabaseService
1220
) : RealmRepository(databaseService), SurveyRepository {
1321

@@ -77,4 +85,44 @@ class SurveyRepositoryImpl @Inject constructor(
7785
null
7886
}
7987
}
88+
89+
override suspend fun getSurveyInfos(
90+
isTeam: Boolean,
91+
teamId: String?,
92+
userId: String?,
93+
surveys: List<RealmStepExam>
94+
): Map<String, SurveyInfo> {
95+
val surveyIds = surveys.map { it.id }
96+
val submissions = queryList(RealmSubmission::class.java) {
97+
`in`("parentId", surveyIds.toTypedArray())
98+
}
99+
val surveyInfos = mutableMapOf<String, SurveyInfo>()
100+
for (survey in surveys) {
101+
val surveyId = survey.id ?: continue
102+
val submissionCount = if (isTeam) {
103+
submissions.count { it.parentId == surveyId && it.membershipDoc?.teamId == teamId }.toString()
104+
} else {
105+
submissions.count { it.parentId == surveyId && it.userId == userId }.toString()
106+
}
107+
val lastSubmissionDate = if (isTeam) {
108+
submissions.filter { it.parentId == surveyId && it.membershipDoc?.teamId == teamId }
109+
.maxByOrNull { it.startTime }?.startTime?.let { getFormattedDateWithTime(it) } ?: ""
110+
} else {
111+
submissions.filter { it.parentId == surveyId && it.userId == userId }
112+
.maxByOrNull { it.startTime }?.startTime?.let { getFormattedDateWithTime(it) } ?: ""
113+
}
114+
val creationDate = survey.createdDate.let { formatDate(it, "MMM dd, yyyy") } ?: ""
115+
surveyInfos[surveyId] = SurveyInfo(
116+
surveyId = surveyId,
117+
submissionCount = context.resources.getQuantityString(
118+
R.plurals.survey_taken_count,
119+
submissionCount.toInt(),
120+
submissionCount.toInt()
121+
),
122+
lastSubmissionDate = lastSubmissionDate,
123+
creationDate = creationDate
124+
)
125+
}
126+
return surveyInfos
127+
}
80128
}

app/src/main/java/org/ole/planet/myplanet/ui/survey/AdapterSurvey.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class AdapterSurvey(
3434
val teamId: String?,
3535
private val surveyAdoptListener: SurveyAdoptListener,
3636
private val settings: SharedPreferences,
37-
private val userProfileDbHandler: UserProfileDbHandler
37+
private val userProfileDbHandler: UserProfileDbHandler,
38+
private val surveyInfoMap: Map<String, SurveyInfo>
3839
) : RecyclerView.Adapter<AdapterSurvey.ViewHolderSurvey>() {
3940
private var examList: List<RealmStepExam> = emptyList()
4041
private var listener: OnHomeItemClickListener? = null
@@ -187,13 +188,10 @@ class AdapterSurvey(
187188
startSurvey.visibility = View.GONE
188189
}
189190

190-
tvNoSubmissions.text = when {
191-
isTeam -> getNoOfSubmissionByTeam(teamId, exam.id, mRealm)
192-
else -> getNoOfSubmissionByUser(exam.id, exam.courseId, userId, mRealm)
193-
}
194-
tvDateCompleted.text = getRecentSubmissionDate(exam.id, exam.courseId, userId, mRealm)
195-
val creationTime = exam.id?.let { RealmStepExam.getSurveyCreationTime(it, mRealm) }
196-
tvDate.text = creationTime?.let { formatDate(it, "MMM dd, yyyy") } ?: ""
191+
val surveyInfo = surveyInfoMap[exam.id]
192+
tvNoSubmissions.text = surveyInfo?.submissionCount ?: ""
193+
tvDateCompleted.text = surveyInfo?.lastSubmissionDate ?: ""
194+
tvDate.text = surveyInfo?.creationDate ?: ""
197195
}
198196
}
199197

app/src/main/java/org/ole/planet/myplanet/ui/survey/SurveyFragment.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SurveyFragment : BaseRecyclerFragment<RealmStepExam?>(), SurveyAdoptListen
4646
private val serverUrlMapper = ServerUrlMapper()
4747
private var loadSurveysJob: Job? = null
4848
private var currentSurveys: List<RealmStepExam> = emptyList()
49+
private val surveyInfoMap = mutableMapOf<String, SurveyInfo>()
4950

5051
@Inject
5152
lateinit var syncManager: SyncManager
@@ -81,7 +82,8 @@ class SurveyFragment : BaseRecyclerFragment<RealmStepExam?>(), SurveyAdoptListen
8182
teamId,
8283
this,
8384
settings,
84-
profileDbHandler
85+
profileDbHandler,
86+
surveyInfoMap
8587
)
8688
prefManager = SharedPrefManager(requireContext())
8789

@@ -267,14 +269,22 @@ class SurveyFragment : BaseRecyclerFragment<RealmStepExam?>(), SurveyAdoptListen
267269
fun updateAdapterData(isTeamShareAllowed: Boolean? = null) {
268270
val useTeamShareAllowed = isTeamShareAllowed ?: currentIsTeamShareAllowed
269271
currentIsTeamShareAllowed = useTeamShareAllowed
270-
272+
val userProfileModel = profileDbHandler.userModel
271273
loadSurveysJob?.cancel()
272274
loadSurveysJob = launchWhenViewIsReady {
273275
currentSurveys = when {
274276
isTeam && useTeamShareAllowed -> surveyRepository.getAdoptableTeamSurveys(teamId)
275277
isTeam -> surveyRepository.getTeamOwnedSurveys(teamId)
276278
else -> surveyRepository.getIndividualSurveys()
277279
}
280+
val surveyInfos = surveyRepository.getSurveyInfos(
281+
isTeam,
282+
teamId,
283+
userProfileModel?.id,
284+
currentSurveys
285+
)
286+
surveyInfoMap.clear()
287+
surveyInfoMap.putAll(surveyInfos)
278288
applySearchFilter()
279289
}
280290
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.ole.planet.myplanet.ui.survey
2+
3+
data class SurveyInfo(
4+
val surveyId: String,
5+
val submissionCount: String,
6+
val lastSubmissionDate: String,
7+
val creationDate: String
8+
)

0 commit comments

Comments
 (0)