Skip to content

Commit

Permalink
migrate baseRecyclerParentFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Dec 11, 2024
1 parent b62f96c commit 381b3ab
Showing 1 changed file with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.ole.planet.myplanet.base

import com.google.gson.JsonArray
import io.realm.RealmModel
import io.realm.Sort
import io.realm.kotlin.ext.query
import io.realm.kotlin.query.Sort
import org.ole.planet.myplanet.MainApplication.Companion.realm
import org.ole.planet.myplanet.model.RealmMyCourse
import org.ole.planet.myplanet.model.RealmMyLibrary
import org.ole.planet.myplanet.model.RealmStepExam
Expand All @@ -14,21 +15,21 @@ abstract class BaseRecyclerParentFragment<LI> : BaseResourceFragment() {
fun getList(c: Class<*>): List<LI> {
return when {
c == RealmStepExam::class.java -> {
mRealm.where(c).equalTo("type", "surveys").findAll().toList() as List<LI>
realm.query<RealmStepExam>("type == $0", "surveys").find() as List<LI>
}
isMyCourseLib -> {
getMyLibItems(c as Class<out RealmModel>)
getMyLibItems(c)
}
c == RealmMyLibrary::class.java -> {
RealmMyLibrary.getOurLibrary(model?.id, mRealm.where(c).equalTo("isPrivate", false).findAll().toList()) as List<LI>
val results = realm.query<RealmMyLibrary>("isPrivate == $0", false).find()
RealmMyLibrary.getOurLibrary(model?.id, results) as List<LI>
}
else -> {
val myLibItems = getMyLibItems(c as Class<out RealmModel>)
val results: List<RealmMyCourse> = mRealm.where(RealmMyCourse::class.java)
.isNotEmpty("courseTitle")
.findAll()
.toList()
val ourCourseItems = RealmMyCourse.getOurCourse(model?.id, results)
val myLibItems = getMyLibItems(c)
val results = realm.query<RealmMyCourse>()
.query("courseTitle != $0", "")
.find()
val ourCourseItems = RealmMyCourse.getOurCourse(model?.id ?: "", results)

when (c) {
RealmMyCourse::class.java -> {
Expand Down Expand Up @@ -57,47 +58,54 @@ abstract class BaseRecyclerParentFragment<LI> : BaseResourceFragment() {
fun getList(c: Class<*>, orderBy: String? = null, sort: Sort = Sort.ASCENDING): List<LI> {
return when {
c == RealmStepExam::class.java -> {
mRealm.where(c).equalTo("type", "surveys").sort(orderBy ?: "", sort).findAll().toList() as List<LI>
realm.query<RealmStepExam>("type == $0", "surveys").apply {
orderBy?.let { sort(it, sort) }
}.find() as List<LI>
}
isMyCourseLib -> {
getMyLibItems(c as Class<out RealmModel>, orderBy)
getMyLibItems(c, orderBy)
}
c == RealmMyLibrary::class.java -> {
RealmMyLibrary.getOurLibrary(model?.id, mRealm.where(c).equalTo("isPrivate", false).sort(orderBy ?: "", sort).findAll().toList()) as List<LI>
val results = realm.query<RealmMyLibrary>("isPrivate == $0", false).apply {
orderBy?.let { sort(it, sort) }
}.find()
RealmMyLibrary.getOurLibrary(model?.id, results) as List<LI>
}
else -> {
val results = mRealm.where(RealmMyCourse::class.java).sort(orderBy ?: "", sort).findAll().toList() as List<RealmMyCourse>
RealmMyCourse.getOurCourse(model?.id, results) as List<LI>
val results = realm.query<RealmMyCourse>().apply {
orderBy?.let { sort(it, sort) }
}.find()
RealmMyCourse.getOurCourse(model?.id ?: "", results) as List<LI>
}
}
}

@Suppress("UNCHECKED_CAST")
private fun <T : RealmModel> getMyLibItems(c: Class<T>, orderBy: String? = null): List<LI> {
val query = mRealm.where(c)
val realmResults = if (orderBy != null) {
query.sort(orderBy).findAll()
} else {
query.findAll()
}
val results: List<T> = realmResults.toList()
private fun getMyLibItems(c: Class<*>, orderBy: String? = null): List<LI> {
return when (c) {
RealmMyLibrary::class.java -> {
RealmMyLibrary.getMyLibraryByUserId(model?.id, results as? List<RealmMyLibrary> ?: emptyList()) as List<LI>
val results = realm.query<RealmMyLibrary>().apply {
orderBy?.let { sort(it) }
}.find()
RealmMyLibrary.getMyLibraryByUserId(model?.id, results) as List<LI>
}
RealmMyCourse::class.java -> {
RealmMyCourse.getMyCourseByUserId(model?.id, results as? List<RealmMyCourse> ?: emptyList()) as List<LI>
val results = realm.query<RealmMyCourse>().apply {
orderBy?.let { sort(it) }
}.find()
RealmMyCourse.getMyCourseByUserId(model?.id ?: "", results) as List<LI>
}
else -> throw IllegalArgumentException("Unsupported class: ${c.simpleName}")
}
}

fun getJsonArrayFromList(list: Set<String>): JsonArray {
val array = JsonArray()
list.forEach { array.add(it) }
return array
return JsonArray().apply {
list.forEach { add(it) }
}
}

companion object {
var isSurvey: Boolean = false
}
}
}

0 comments on commit 381b3ab

Please sign in to comment.