@@ -123,12 +123,18 @@ class ResourcesRepositoryImpl @Inject constructor(
123123 return withRealm { realm ->
124124 val queryObj = realm.where(RealmMyLibrary ::class .java).equalTo(" isPrivate" , false )
125125
126- val data = if (isMyCourseLib) {
127- queryObj.findAll().filter { it.userId?.contains(userId) == true }
128- } else {
129- queryObj.findAll().filter { it.userId?.contains(userId) == false }
126+ if (userId != null ) {
127+ if (isMyCourseLib) {
128+ queryObj.equalTo(" userId" , userId)
129+ } else {
130+ queryObj.not ().equalTo(" userId" , userId)
131+ }
132+ } else if (isMyCourseLib) {
133+ return @withRealm emptyList()
130134 }
131135
136+ val data = queryObj.findAll()
137+
132138 if (query.isEmpty()) {
133139 return @withRealm realm.copyFromRealm(data)
134140 }
@@ -179,9 +185,9 @@ class ResourcesRepositoryImpl @Inject constructor(
179185
180186 val results = queryList(RealmMyLibrary ::class .java) {
181187 equalTo(" isPrivate" , false )
188+ equalTo(" userId" , userId)
182189 }
183190 return filterLibrariesNeedingUpdate(results)
184- .filter { it.userId?.contains(userId) == true }
185191 }
186192
187193 override suspend fun getLibraryForSelectedUser (userId : String ): List <RealmMyLibrary > {
@@ -217,9 +223,9 @@ class ResourcesRepositoryImpl @Inject constructor(
217223
218224 val results = queryList(RealmMyLibrary ::class .java) {
219225 equalTo(" isPrivate" , false )
226+ equalTo(" userId" , userId)
220227 }
221- return filterLibrariesNeedingUpdate(results)
222- .count { it.userId?.contains(userId) == true }
228+ return filterLibrariesNeedingUpdate(results).size
223229 }
224230
225231 override suspend fun resourceTitleExists (title : String ): Boolean {
@@ -408,12 +414,11 @@ class ResourcesRepositoryImpl @Inject constructor(
408414 resourceIds.chunked(chunkSize).forEach { chunk ->
409415 val libraryItems = realm.where(RealmMyLibrary ::class .java)
410416 .`in `(" resourceId" , chunk.toTypedArray())
417+ .not ().equalTo(" userId" , userId)
411418 .findAll()
412419
413420 libraryItems.forEach { libraryItem ->
414- if (libraryItem.userId?.contains(userId) == false ) {
415- libraryItem.setUserId(userId)
416- }
421+ libraryItem.setUserId(userId)
417422 }
418423
419424 val removedLogs = realm.where(org.ole.planet.myplanet.model.RealmRemovedLog ::class .java)
@@ -456,19 +461,22 @@ class ResourcesRepositoryImpl @Inject constructor(
456461
457462 override suspend fun getDownloadSuggestionList (userId : String? ): List <RealmMyLibrary > {
458463 val targetUserId = userId ? : sharedPrefManager.getUserId().ifEmpty { null }
459- val results = queryList(RealmMyLibrary ::class .java) {
460- equalTo(" isPrivate" , false )
461- }
462- val allNeedingUpdate = filterLibrariesNeedingUpdate(results)
463464
464465 if (! targetUserId.isNullOrBlank()) {
465- val userLibraries = allNeedingUpdate.filter { it.userId?.contains(targetUserId) == true }
466- if (userLibraries.isNotEmpty()) {
467- return userLibraries
466+ val userLibraries = queryList(RealmMyLibrary ::class .java) {
467+ equalTo(" isPrivate" , false )
468+ equalTo(" userId" , targetUserId)
469+ }
470+ val userLibrariesNeedingUpdate = filterLibrariesNeedingUpdate(userLibraries)
471+ if (userLibrariesNeedingUpdate.isNotEmpty()) {
472+ return userLibrariesNeedingUpdate
468473 }
469474 }
470475
471- return allNeedingUpdate
476+ val results = queryList(RealmMyLibrary ::class .java) {
477+ equalTo(" isPrivate" , false )
478+ }
479+ return filterLibrariesNeedingUpdate(results)
472480 }
473481
474482 override suspend fun getLibraryByUserId (userId : String ): List <RealmMyLibrary > {
0 commit comments