From 381b3ab094cb7f7cf518791e50ec1e2eab5bc904 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 11 Dec 2024 17:11:37 +0300 Subject: [PATCH] migrate baseRecyclerParentFragment --- .../base/BaseRecyclerParentFragment.kt | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseRecyclerParentFragment.kt b/app/src/main/java/org/ole/planet/myplanet/base/BaseRecyclerParentFragment.kt index dacdd24bc1..f7fd5a3993 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseRecyclerParentFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseRecyclerParentFragment.kt @@ -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 @@ -14,21 +15,21 @@ abstract class BaseRecyclerParentFragment
  • : BaseResourceFragment() { fun getList(c: Class<*>): List
  • { return when { c == RealmStepExam::class.java -> { - mRealm.where(c).equalTo("type", "surveys").findAll().toList() as List
  • + realm.query("type == $0", "surveys").find() as List
  • } isMyCourseLib -> { - getMyLibItems(c as Class) + getMyLibItems(c) } c == RealmMyLibrary::class.java -> { - RealmMyLibrary.getOurLibrary(model?.id, mRealm.where(c).equalTo("isPrivate", false).findAll().toList()) as List
  • + val results = realm.query("isPrivate == $0", false).find() + RealmMyLibrary.getOurLibrary(model?.id, results) as List
  • } else -> { - val myLibItems = getMyLibItems(c as Class) - val results: List = mRealm.where(RealmMyCourse::class.java) - .isNotEmpty("courseTitle") - .findAll() - .toList() - val ourCourseItems = RealmMyCourse.getOurCourse(model?.id, results) + val myLibItems = getMyLibItems(c) + val results = realm.query() + .query("courseTitle != $0", "") + .find() + val ourCourseItems = RealmMyCourse.getOurCourse(model?.id ?: "", results) when (c) { RealmMyCourse::class.java -> { @@ -57,47 +58,54 @@ abstract class BaseRecyclerParentFragment
  • : BaseResourceFragment() { fun getList(c: Class<*>, orderBy: String? = null, sort: Sort = Sort.ASCENDING): List
  • { return when { c == RealmStepExam::class.java -> { - mRealm.where(c).equalTo("type", "surveys").sort(orderBy ?: "", sort).findAll().toList() as List
  • + realm.query("type == $0", "surveys").apply { + orderBy?.let { sort(it, sort) } + }.find() as List
  • } isMyCourseLib -> { - getMyLibItems(c as Class, 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
  • + val results = realm.query("isPrivate == $0", false).apply { + orderBy?.let { sort(it, sort) } + }.find() + RealmMyLibrary.getOurLibrary(model?.id, results) as List
  • } else -> { - val results = mRealm.where(RealmMyCourse::class.java).sort(orderBy ?: "", sort).findAll().toList() as List - RealmMyCourse.getOurCourse(model?.id, results) as List
  • + val results = realm.query().apply { + orderBy?.let { sort(it, sort) } + }.find() + RealmMyCourse.getOurCourse(model?.id ?: "", results) as List
  • } } } + @Suppress("UNCHECKED_CAST") - private fun getMyLibItems(c: Class, orderBy: String? = null): List
  • { - val query = mRealm.where(c) - val realmResults = if (orderBy != null) { - query.sort(orderBy).findAll() - } else { - query.findAll() - } - val results: List = realmResults.toList() + private fun getMyLibItems(c: Class<*>, orderBy: String? = null): List
  • { return when (c) { RealmMyLibrary::class.java -> { - RealmMyLibrary.getMyLibraryByUserId(model?.id, results as? List ?: emptyList()) as List
  • + val results = realm.query().apply { + orderBy?.let { sort(it) } + }.find() + RealmMyLibrary.getMyLibraryByUserId(model?.id, results) as List
  • } RealmMyCourse::class.java -> { - RealmMyCourse.getMyCourseByUserId(model?.id, results as? List ?: emptyList()) as List
  • + val results = realm.query().apply { + orderBy?.let { sort(it) } + }.find() + RealmMyCourse.getMyCourseByUserId(model?.id ?: "", results) as List
  • } else -> throw IllegalArgumentException("Unsupported class: ${c.simpleName}") } } fun getJsonArrayFromList(list: Set): JsonArray { - val array = JsonArray() - list.forEach { array.add(it) } - return array + return JsonArray().apply { + list.forEach { add(it) } + } } companion object { var isSurvey: Boolean = false } -} +} \ No newline at end of file