realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

Display multiple realm results count in single recyclerview adapter #5968

Closed alokomkar closed 6 years ago

alokomkar commented 6 years ago

Hi,

I am currently working on a project requiring me to display counts of multiple realm results based on a selectionType. Due warning : it isn't the most ideal of codes. I am playing around nested recycler view in the parent recycler view.

Here's the code :

class RvCategoryAdapter(var categories: ArrayList<Category>,
                        var permission : Int,
                        private var isAttachmentAvailable : Boolean,
                        val actionClickListener: CategoryItemClickListener) : RecyclerView.Adapter<RvCategoryAdapter.RvViewHolder>() {

    private var context : Context ?= null
    private val adapterMap : HashMap<String, SubCategoryAdapter> = HashMap()

    override fun onBindViewHolder(holder: RvCategoryAdapter.RvViewHolder?, position: Int) {

        val category = categories[position]

        val id = context!!.resources.getIdentifier(category.drawableString, "drawable", context!!.packageName)
        holder!!.tvCategory.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context!!, id), null, null, null)

        holder.tvCategory.compoundDrawablePadding = context!!.resources.getDimensionPixelOffset(R.dimen.default_mini_padding)
        holder.tvCategory.text = category.title
        if (category.subCategories.size == 0) {
            holder.tvCount.show()
            holder.tvCount.text = category.formsCount.toString()
        } else {
            holder.tvCount.hide()
        }

        if( category.title == "Lists" || category.title == "Shared Contacts" ) {
            holder.tvCategory.isEnabled =  !isAttachmentAvailable
            holder.tvCategory.setTextColor(ContextCompat.getColor(holder.tvCategory.context, if( !isAttachmentAvailable ) R.color.colorPrimary else R.color.grey_400 ))
            holder.tvCategory.compoundDrawables[0].alpha = if( !isAttachmentAvailable ) 255 else 100
            holder.tvCount.compoundDrawables[2].alpha = if( !isAttachmentAvailable ) 255 else 100
            holder.tvCount.setTextColor(ContextCompat.getColor(holder.tvCategory.context, if( !isAttachmentAvailable ) R.color.black else R.color.grey_400 ))
        }

        holder.setAdapter( category, category.subCategories, permission, actionClickListener )
        holder.setIsRecyclable(false)

    }

    override fun getItemCount(): Int {
        return categories.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RvCategoryAdapter.RvViewHolder {
        context = parent!!.context
        return RvViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_category_view, parent, false))
    }

    inner class RvViewHolder (categoryView: View?) : RecyclerView.ViewHolder(categoryView), View.OnClickListener {

        override fun onClick(view: View?) {
            val position = adapterPosition
            if( position != RecyclerView.NO_POSITION ) {
                when (view!!.id) {
                    R.id.tvCategory -> {
                        actionClickListener.onItemClick(subCategoryAdapter, categories[position], null, "category")
                    }
                }
            }
        }

        val tvCategory = categoryView!!.findViewById<TextView>(R.id.tvCategory)
        val tvCount = categoryView!!.findViewById<TextView>(R.id.tvCount)
        val rvSubCategory = categoryView!!.findViewById<RecyclerView>(R.id.rvSubCategory)
        var subCategoryAdapter : SubCategoryAdapter ?= null

        init {
            rvSubCategory.layoutManager = LinearLayoutManager( categoryView!!.context )
            tvCategory.setOnClickListener(this)
        }

        fun setAdapter(category: Category, subCategories: ArrayList<SubCategory>, permission: Int, actionClickListener: CategoryItemClickListener) {
            if( subCategoryAdapter == null ) {
                subCategoryAdapter = SubCategoryAdapter( category, subCategories, permission, actionClickListener )
                rvSubCategory.adapter = subCategoryAdapter
                adapterMap.put(category.category_id, subCategoryAdapter!! )
            }
            else {
                subCategoryAdapter!!.notifyDataSetChanged()
            }
        }

    }

    fun notifyAllDataChanged() {
        if( adapterMap.size > 0 ) {
            for( adapter in adapterMap.values ) adapter.notifyDataSetChanged()
        }
        notifyDataSetChanged()
    }

    fun setAttachmentAvailable(attachmentAvailable: Boolean) {
        isAttachmentAvailable = attachmentAvailable
    }
}

Child Recycler view adapter :

class SubCategoryAdapter(var category: Category,
                         var subCategories: ArrayList<SubCategory>,
                         var permission : Int,
                         val actionClickListener: CategoryItemClickListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        val subCategory = getItemAtPosition(position)
        when (getItemViewType(position)) {
            Constants.SUB_CATEGORY_DISPLAY_PERSON -> {
                initDisplayValues(subCategory, holder)
            }
            Constants.SUB_CATEGORY_DISPLAY_PERSON_ADD -> {
                initAddDisplayValues(subCategory, holder)
            }
            Constants.SUB_CATEGORY_ADD_PERSON -> {
                initCategoryValues(subCategory, holder)
            }
            Constants.SUB_CATEGORY_ADD_ITEM -> {
                initItemValues(subCategory, holder)
            }
        }
    }

    private fun initAddDisplayValues(subCategory: SubCategory, holder: RecyclerView.ViewHolder?) {
        val viewHolder: AddDisplayViewHolder = holder as AddDisplayViewHolder
        viewHolder.tvSubTitle.text = subCategory.title
        if( subCategory.personName.isNotEmpty() ) {
            viewHolder.tvSubTitle.text = subCategory.personName
        }

        if( subCategory.formsCount > 0 ) {
            viewHolder.tvCount.text = subCategory.formsCount.toString()
            viewHolder.tvCountOnly.text = subCategory.formsCount.toString()
        }
        else {
            viewHolder.tvCount.text = ""
            viewHolder.tvCountOnly.text = ""
        }

        if( permission > PERMISSION_VIEW_ONLY ) {
            viewHolder.tvCount.show()
            viewHolder.tvCountOnly.hide()
        }
        else {
            viewHolder.tvCount.hide()
            viewHolder.tvCountOnly.show()
        }
    }

    private fun initDisplayValues(subCategory: SubCategory, holder: RecyclerView.ViewHolder?) {
        val viewHolder: DisplayViewHolder = holder as DisplayViewHolder
        viewHolder.tvSubTitle.text = subCategory.title
        if( subCategory.personName.isNotEmpty() ) {
            viewHolder.tvSubTitle.text = subCategory.personName
        }
        if( subCategory.formsCount > 0 )
            viewHolder.tvCount.text = subCategory.formsCount.toString()
        else
            viewHolder.tvCount.text = ""

    }

    private fun initCategoryValues(subCategory: SubCategory, holder: RecyclerView.ViewHolder?) {
        val viewHolder: PersonViewHolder = holder as PersonViewHolder
        viewHolder.tvSubTitle.text = subCategory.title

    }

    private fun initItemValues(subCategory: SubCategory, holder: RecyclerView.ViewHolder?) {
        val viewHolder: ItemViewHolder = holder as ItemViewHolder
        viewHolder.tvSubTitle.text = subCategory.title
        viewHolder.tvCount.text = subCategory.formsCount.toString()
        viewHolder.tvCountOnly.text = subCategory.formsCount.toString()
        if( permission > PERMISSION_VIEW_ONLY ) {
            viewHolder.tvCount.show()
            viewHolder.tvCountOnly.hide()
        }
        else {
            viewHolder.tvCount.hide()
            viewHolder.tvCountOnly.show()
        }
    }

    private fun getItemAtPosition(position: Int): SubCategory {
        return subCategories[position]
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        val layoutInflater = LayoutInflater.from(parent!!.context)
        return when (viewType) {
            Constants.SUB_CATEGORY_ADD_ITEM -> {
                ItemViewHolder(layoutInflater.inflate(R.layout.item_sub_category_add, parent, false))
            }
            Constants.SUB_CATEGORY_ADD_PERSON -> {
                PersonViewHolder(layoutInflater.inflate(R.layout.item_sub_category_add_person, parent, false))
            }
            Constants.SUB_CATEGORY_DISPLAY_PERSON -> {
                DisplayViewHolder(layoutInflater.inflate(R.layout.item_sub_category_display_person, parent, false))
            }
            Constants.SUB_CATEGORY_DISPLAY_PERSON_ADD -> {
                AddDisplayViewHolder(layoutInflater.inflate(R.layout.item_sub_category_display_person_add, parent, false))
            }
            else -> {
                ItemViewHolder(layoutInflater.inflate(R.layout.item_sub_category_add, parent, false))
            }
        }
    }

    override fun getItemCount(): Int {
        return subCategories.size
    }

    override fun getItemViewType(position: Int): Int {
        return subCategories[position].type
    }

    fun updateList(categories: ArrayList<SubCategory>) {
        subCategories = categories
        notifyDataSetChanged()
    }

    inner class ItemViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        val tvSubTitle = itemView!!.findViewById<TextView>(R.id.tvSubTitle)
        val tvCount = itemView!!.findViewById<TextView>(R.id.tvCount)
        val tvCountOnly = itemView!!.findViewById<TextView>(R.id.tvCountOnly)

        override fun onClick(view: View?) {
            val position = adapterPosition
            if (position != RecyclerView.NO_POSITION) {
                when (view!!.id) {
                    R.id.tvCount -> {
                        actionClickListener.onItemClick(this@SubCategoryAdapter, category, getItemAtPosition(position), "add_item")
                    }
                    R.id.tvSubTitle -> {
                        actionClickListener.onItemClick(this@SubCategoryAdapter, category, getItemAtPosition(position), "display")
                    }
                }
            }

        }

        init {
            tvSubTitle.setOnClickListener(this)
            tvCount.setOnClickListener(this)
        }

    }

    inner class PersonViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        val tvSubTitle = itemView!!.findViewById<TextView>(R.id.tvSubTitle)

        override fun onClick(view: View?) {
            val position = adapterPosition
            if (position != RecyclerView.NO_POSITION) {
                when (view!!.id) {
                    R.id.tvSubTitle -> {
                        actionClickListener.onItemClick(this@SubCategoryAdapter, category, getItemAtPosition(position), "add_person")
                    }
                }
            }

        }

        init {
            tvSubTitle.setOnClickListener(this)
        }

    }

    inner class AddDisplayViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        val tvSubTitle = itemView!!.findViewById<TextView>(R.id.tvSubTitle)
        val tvCount = itemView!!.findViewById<TextView>(R.id.tvCount)
        val tvCountOnly = itemView!!.findViewById<TextView>(R.id.tvCountOnly)

        override fun onClick(view: View?) {
            val position = adapterPosition
            if (position != RecyclerView.NO_POSITION) {
                when (view!!.id) {
                    R.id.tvSubTitle -> {
                        actionClickListener.onItemClick(this@SubCategoryAdapter, category, getItemAtPosition(position), "display_person")
                    }
                    R.id.tvCount -> {
                        actionClickListener.onItemClick(this@SubCategoryAdapter, category, getItemAtPosition(position), "add_document")
                    }
                }
            }

        }

        init {
            tvSubTitle.setOnClickListener(this)
            tvCount.setOnClickListener(this)
        }

    }

    inner class DisplayViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        val tvSubTitle = itemView!!.findViewById<TextView>(R.id.tvSubTitle)
        val tvCount = itemView!!.findViewById<TextView>(R.id.tvCount)

        override fun onClick(view: View?) {
            val position = adapterPosition
            if (position != RecyclerView.NO_POSITION) {
                when (view!!.id) {
                    R.id.tvSubTitle -> {
                        actionClickListener.onItemClick(this@SubCategoryAdapter, category, getItemAtPosition(position), "display_person")
                    }
                }
            }

        }

        init {
            tvSubTitle.setOnClickListener(this)
        }

    }

    fun checkForDependentCategory( categoryName: String): Boolean {
        val subCategoryIndex = subCategories.indexOf(SubCategory(categoryName))
        if( subCategoryIndex != -1 ) {
            val subCategory = subCategories[subCategoryIndex]
            return subCategory.formsCount != 0
        }
        else return true

    }
}

Child Category :

class SubCategory(
        var title: String = "",
        var drawableString: String = "",
        var formsCount: Int = 0,
        var type: Int = 0,
        var subCategoryId: String = "",
        var personName: String = ""
) : Parcelable {
    constructor(source: Parcel) : this(
            source.readString(),
            source.readString(),
            source.readInt(),
            source.readInt(),
            source.readString(),
            source.readString()
    )

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeString(title)
        writeString(drawableString)
        writeInt(formsCount)
        writeInt(type)
        writeString(subCategoryId)
        writeString(personName)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as SubCategory

        if (subCategoryId != other.subCategoryId) return false
        if (personName != other.personName) return false

        return true
    }

    override fun hashCode(): Int {
        var result = subCategoryId.hashCode()
        result = 31 * result + personName.hashCode()
        return result
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<SubCategory> = object : Parcelable.Creator<SubCategory> {
            override fun createFromParcel(source: Parcel): SubCategory = SubCategory(source)
            override fun newArray(size: Int): Array<SubCategory?> = arrayOfNulls(size)
        }
    }

    private var financialItems : RealmResults<Financial>?= null
    private var paymentItems : RealmResults<Payment>?= null
    private var propertyItems : RealmResults<Property>?= null
    private var vehicleItems : RealmResults<Vehicle>?= null
    private var assetItems : RealmResults<Asset>?= null
    private var insuranceItems : RealmResults<Insurance>?= null
    private var taxesItems : RealmResults<Taxes>?= null
    private var homeListItems : RealmResults<HomeList>?= null

    private var documentsItems : RealmResults<Documents>?= null
    private var loyaltyItems : RealmResults<Loyalty>?= null
    private var travelItems : RealmResults<Travel>?= null
    private var vacationsItems : RealmResults<Vacations>?= null
    private var travelList : RealmResults<TravelList>?= null

    private var mainMemoriesItems : RealmResults<MainMemories>?= null
    private var memoryTimelineItems : RealmResults<MemoryTimeline>?= null
    private var memoriesList : RealmResults<MemoriesList>?= null

    private var educationItems : RealmResults<Education>?= null
    private var mainEducationItems : RealmResults<MainEducation>?= null
    private var workItems : RealmResults<Work>?= null
    private var educationList : RealmResults<EducationList>?= null

    private var interestItems : RealmResults<Interests>?= null
    private var interestsList : RealmResults<InterestsList>?= null

    private var checkupsItems : RealmResults<Checkups>?= null
    private var emergencyContactsItems : RealmResults<EmergencyContacts>?= null
    private var eyeglassPrescriptionsItems : RealmResults<EyeglassPrescriptions>?= null
    private var healthcareProvidersItems : RealmResults<HealthcareProviders>?= null
    private var identificationItems : RealmResults<Identification>?= null
    private var medicalConditionsItems : RealmResults<MedicalConditions>?= null
    private var medicalHistoryItems : RealmResults<MedicalHistory>?= null
    private var medicationsItems : RealmResults<Medications>?= null
    private var vitalNumbersItems : RealmResults<VitalNumbers>?= null
    private var wellnessItems : RealmResults<Wellness>?= null
    private var wellnessList : RealmResults<WellnessList>?= null

    private var loyaltyProgramsItems : RealmResults<LoyaltyPrograms>?= null
    private var recentPurchaseItems : RealmResults<RecentPurchase>?= null
    private var shoppingItems : RealmResults<Shopping>?= null
    private var clothingSizesItems : RealmResults<ClothingSizes>?= null
    private var shoppingList : RealmResults<ShoppingList>?= null

    private var certificateItems : RealmResults<Certificate>?= null
    private var governmentItems : RealmResults<Government>?= null
    private var licenseItems : RealmResults<License>?= null
    private var personalItems : RealmResults<Personal>?= null
    private var socialItems : RealmResults<Social>?= null
    private var taxIDItems : RealmResults<TaxID>?= null
    private var personalList : RealmResults<PersonalList>?= null

    private var contactsItems : RealmResults<Contacts>?= null
    private var mainContactsItems : RealmResults<MainContacts>?= null
    private var contactsList : RealmResults<ContactsList>?= null

    fun onResultsLoyaltyProgramsItems(t: RealmResults<LoyaltyPrograms>?) {
        loyaltyProgramsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsRecentPurchaseItems(t: RealmResults<RecentPurchase>?) {
        recentPurchaseItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsShoppingItems(t: RealmResults<Shopping>?) {
        shoppingItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsClothingSizesItems(t: RealmResults<ClothingSizes>?) {
        clothingSizesItems = t
        formsCount = t!!.count { it.selectionType == personName.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == personName.encryptString() }}
    }

    fun onResultsShoppingList(t: RealmResults<ShoppingList>?) {
        shoppingList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsCertificateItems(t: RealmResults<Certificate>?) {
        certificateItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsGovernmentItems(t: RealmResults<Government>?) {
        governmentItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsLicenseItems(t: RealmResults<License>?) {
        licenseItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsPersonalItems(t: RealmResults<Personal>?) {
        personalItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsSocialItems(t: RealmResults<Social>?) {
        socialItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsTaxIDItems(t: RealmResults<TaxID>?) {
        taxIDItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsPersonalList(t: RealmResults<PersonalList>?) {
        personalList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsContactsItems(t: RealmResults<Contacts>?) {
        contactsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMainContactsItems(t: RealmResults<MainContacts>?) {
        mainContactsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsContactsList(t: RealmResults<ContactsList>?) {
        contactsList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsDocumentsFetched(t: RealmResults<Documents>?) {
        documentsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsLoyaltyFetched(t: RealmResults<Loyalty>?) {
        loyaltyItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsTravelFetched(t: RealmResults<Travel>?) {
        travelItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsVacationsFetched(t: RealmResults<Vacations>?) {
        vacationsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsTravelListFetched(t: RealmResults<TravelList>?) {
        travelList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMainMemoriesFetched(t: RealmResults<MainMemories>?) {
        mainMemoriesItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMemoryTimelineFetched(t: RealmResults<MemoryTimeline>?) {
        memoryTimelineItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMemoriesListFetched(t: RealmResults<MemoriesList>?) {
        memoriesList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsEducationListFetched(t: RealmResults<EducationList>?) {
        educationList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsEducationFetched(t: RealmResults<Education>?) {
        educationItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMainEducationFetched(t: RealmResults<MainEducation>?) {
        mainEducationItems = t
        formsCount = t!!.count { it.selectionType == personName.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == personName.encryptString() }}
    }

    fun onResultsWorkFetched(t: RealmResults<Work>?) {
        workItems = t
        formsCount = t!!.count { it.selectionType == personName.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == personName.encryptString() }}
    }

    fun onResultsInterestsFetched(t: RealmResults<Interests>?) {
        interestItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsInterestsListFetched(t: RealmResults<InterestsList>?) {
        interestsList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsCheckupsFetched(t: RealmResults<Checkups>?) {
        checkupsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsEmergencyContactsFetched(t: RealmResults<EmergencyContacts>?) {
        emergencyContactsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsEyeglassPrescriptionsFetched(t: RealmResults<EyeglassPrescriptions>?) {
        eyeglassPrescriptionsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsHealthcareProvidersFetched(t: RealmResults<HealthcareProviders>?) {
        healthcareProvidersItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsIdentificationFetched(t: RealmResults<Identification>?) {
        identificationItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMedicalConditionsFetched(t: RealmResults<MedicalConditions>?) {
        medicalConditionsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMedicalHistoryFetched(t: RealmResults<MedicalHistory>?) {
        medicalHistoryItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsMedicationsFetched(t: RealmResults<Medications>?) {
        medicationsItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsVitalNumbersFetched(t: RealmResults<VitalNumbers>?) {
        vitalNumbersItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsWellnessFetched(t: RealmResults<Wellness>?) {
        wellnessItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsWellnessListFetched(t: RealmResults<WellnessList>?) {
        wellnessList = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsPropertyFetched(t: RealmResults<Property>? ) {
        propertyItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsVehicleFetched(t: RealmResults<Vehicle>?) {
        vehicleItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsTaxesFetched(t: RealmResults<Taxes>?) {
        taxesItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsInsuranceFetched(t: RealmResults<Insurance>?) {
        insuranceItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsAssetFetched(t: RealmResults<Asset>?) {
        assetItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsHomeListFetched(t: RealmResults<HomeList>?) {
        homeListItems = t
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsPaymentFetched(t: RealmResults<Payment>?) {
        paymentItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    fun onResultsFinancialFetched(t: RealmResults<Financial>?) {
        financialItems = t
        formsCount = t!!.count { it.selectionType == subCategoryId.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == subCategoryId.encryptString() }}
    }

    private var checkupCount: Int = 0
    private var emergencyContactsCount: Int = 0
    private var eyeglassPrescriptionsCount: Int = 0
    private var healthcareProvidersCount: Int = 0
    private var identificationCount: Int = 0
    private var medicalConditionsCount: Int = 0
    private var medicalHistoryCount: Int = 0
    private var medicationsCount: Int = 0
    private var vitalNumbersCount: Int = 0

    fun onAllWellnessDocuments(checkupsFetched: RealmResults<Checkups>?,
                               emergencyContactsFetched: RealmResults<EmergencyContacts>?,
                               eyeglassPrescriptionsFetched: RealmResults<EyeglassPrescriptions>?,
                               healthcareProvidersFetched: RealmResults<HealthcareProviders>?,
                               identificationFetched: RealmResults<Identification>?,
                               medicalConditionsFetched: RealmResults<MedicalConditions>?,
                               medicalHistoryFetched: RealmResults<MedicalHistory>?,
                               medicationsFetched: RealmResults<Medications>?,
                               vitalNumbersFetched: RealmResults<VitalNumbers>?) {
        checkupsFetched!!.addChangeListener { results -> checkupCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount() }
        emergencyContactsFetched!!.addChangeListener { results -> emergencyContactsCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        eyeglassPrescriptionsFetched!!.addChangeListener { results -> eyeglassPrescriptionsCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        healthcareProvidersFetched!!.addChangeListener { results -> healthcareProvidersCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        identificationFetched!!.addChangeListener { results -> identificationCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        medicalConditionsFetched!!.addChangeListener { results -> medicalConditionsCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        medicalHistoryFetched!!.addChangeListener { results -> medicalHistoryCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        medicationsFetched!!.addChangeListener { results -> medicationsCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
        vitalNumbersFetched!!.addChangeListener { results -> vitalNumbersCount = results.count { it.selectionType == personName.encryptString() }
            calculateCount()}
    }

    private fun calculateCount() {
        formsCount =  checkupCount+
                emergencyContactsCount+
                eyeglassPrescriptionsCount+
                healthcareProvidersCount+
                identificationCount+
                medicalConditionsCount+
                medicalHistoryCount+
                medicationsCount+
                vitalNumbersCount
    }

    override fun toString(): String {
        return "SubCategory(title='$title', drawableString='$drawableString', formsCount=$formsCount, type=$type, subCategoryId='$subCategoryId', personName='$personName')"
    }

}

Parent Category :

class Category(
        var category_id: String = "",
        var title: String = "",
        var drawableString: String = "",
        var formsCount: Int = 0,
        var subCategories: ArrayList<SubCategory> = ArrayList()
) : Parcelable {

    constructor(source: Parcel) : this(
            source.readString(),
            source.readString(),
            source.readString(),
            source.readInt(),
            source.createTypedArrayList(SubCategory.CREATOR)
    )

    override fun describeContents() = 0

    override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
        writeString(category_id)
        writeString(title)
        writeString(drawableString)
        writeInt(formsCount)
        writeTypedList(subCategories)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Category

        if (category_id != other.category_id) return false

        return true
    }

    override fun hashCode(): Int {
        return category_id.hashCode()
    }

    companion object {
        @JvmField
        val CREATOR: Parcelable.Creator<Category> = object : Parcelable.Creator<Category> {
            override fun createFromParcel(source: Parcel): Category = Category(source)
            override fun newArray(size: Int): Array<Category?> = arrayOfNulls(size)
        }
    }

    private var financialItems : RealmResults<Financial>?= null
    private var paymentItems : RealmResults<Payment>?= null
    private var propertyItems : RealmResults<Property>?= null
    private var vehicleItems : RealmResults<Vehicle>?= null
    private var assetItems : RealmResults<Asset>?= null
    private var insuranceItems : RealmResults<Insurance>?= null
    private var taxesItems : RealmResults<Taxes>?= null
    private var homeListItems : RealmResults<HomeList>?= null

    private var documentsItems : RealmResults<Documents>?= null
    private var loyaltyItems : RealmResults<Loyalty>?= null
    private var travelItems : RealmResults<Travel>?= null
    private var vacationsItems : RealmResults<Vacations>?= null
    private var travelList : RealmResults<TravelList>?= null

    private var mainMemoriesItems : RealmResults<MainMemories>?= null
    private var memoryTimelineItems : RealmResults<MemoryTimeline>?= null
    private var memoriesList : RealmResults<MemoriesList>?= null

    private var educationItems : RealmResults<Education>?= null
    private var mainEducationItems : RealmResults<MainEducation>?= null
    private var workItems : RealmResults<Work>?= null
    private var educationList : RealmResults<EducationList>?= null

    private var interestItems : RealmResults<Interests>?= null
    private var interestsList : RealmResults<InterestsList>?= null

    private var checkupsItems : RealmResults<Checkups>?= null
    private var emergencyContactsItems : RealmResults<EmergencyContacts>?= null
    private var eyeglassPrescriptionsItems : RealmResults<EyeglassPrescriptions>?= null
    private var healthcareProvidersItems : RealmResults<HealthcareProviders>?= null
    private var identificationItems : RealmResults<Identification>?= null
    private var medicalConditionsItems : RealmResults<MedicalConditions>?= null
    private var medicalHistoryItems : RealmResults<MedicalHistory>?= null
    private var medicationsItems : RealmResults<Medications>?= null
    private var vitalNumbersItems : RealmResults<VitalNumbers>?= null
    private var wellnessItems : RealmResults<Wellness>?= null
    private var wellnessList : RealmResults<WellnessList>?= null

    private var loyaltyProgramsItems : RealmResults<LoyaltyPrograms>?= null
    private var recentPurchaseItems : RealmResults<RecentPurchase>?= null
    private var shoppingItems : RealmResults<Shopping>?= null
    private var clothingSizesItems : RealmResults<ClothingSizes>?= null
    private var shoppingList : RealmResults<ShoppingList>?= null

    private var certificateItems : RealmResults<Certificate>?= null
    private var governmentItems : RealmResults<Government>?= null
    private var licenseItems : RealmResults<License>?= null
    private var personalItems : RealmResults<Personal>?= null
    private var socialItems : RealmResults<Social>?= null
    private var taxIDItems : RealmResults<TaxID>?= null
    private var personalList : RealmResults<PersonalList>?= null

    private var contactsItems : RealmResults<Contacts>?= null
    private var mainContactsItems : RealmResults<MainContacts>?= null
    private var contactsList : RealmResults<ContactsList>?= null

    fun onResultsLoyaltyProgramsItems(t: RealmResults<LoyaltyPrograms>?) {
        loyaltyProgramsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsRecentPurchaseItems(t: RealmResults<RecentPurchase>?) {
        recentPurchaseItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsShoppingItems(t: RealmResults<Shopping>?) {
        shoppingItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsClothingSizesItems(t: RealmResults<ClothingSizes>?) {
        clothingSizesItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsShoppingList(t: RealmResults<ShoppingList>?) {
        shoppingList = t
        formsCount = t!!.count { it.selectionType == "Shopping".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "Shopping".encryptString() }}
    }

    fun onResultsCertificateItems(t: RealmResults<Certificate>?) {
        certificateItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsGovernmentItems(t: RealmResults<Government>?) {
        governmentItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsLicenseItems(t: RealmResults<License>?) {
        licenseItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsPersonalItems(t: RealmResults<Personal>?) {
        personalItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsSocialItems(t: RealmResults<Social>?) {
        socialItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsTaxIDItems(t: RealmResults<TaxID>?) {
        taxIDItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsPersonalList(t: RealmResults<PersonalList>?) {
        personalList = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsPersonalList(t: RealmResults<PersonalList>?, type : String ) {
        personalList = t
        formsCount = t!!.count { it.selectionType == type.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == type.encryptString() }}
    }

    fun onResultsContactsItems(t: RealmResults<Contacts>?) {
        contactsItems = t
        formsCount = t!!.count { it.selectionType == "Contacts".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "Contacts".encryptString() }}
    }

    fun onResultsMainContactsItems(t: RealmResults<MainContacts>?) {
        mainContactsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsContactsList(t: RealmResults<ContactsList>?) {
        contactsList = t
        formsCount = t!!.count { it.selectionType == "Contacts".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "Contacts".encryptString() }}
    }

    fun onResultsDocumentsFetched(t: RealmResults<Documents>?) {
        documentsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsLoyaltyFetched(t: RealmResults<Loyalty>?) {
        loyaltyItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsTravelFetched(t: RealmResults<Travel>?) {
        travelItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsVacationsFetched(t: RealmResults<Vacations>?) {
        vacationsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsTravelListFetched(t: RealmResults<TravelList>?) {
        travelList = t
        formsCount = t!!.count { it.selectionType == "Travel".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "Travel".encryptString() }}
    }

    fun onResultsMainMemoriesFetched(t: RealmResults<MainMemories>?) {
        mainMemoriesItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMemoryTimelineFetched(t: RealmResults<MemoryTimeline>?) {
        memoryTimelineItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMemoryTimelineFetched(t: RealmResults<MemoryTimeline>?, type : String) {
        memoryTimelineItems = t
        formsCount = t!!.count { it.selectionType == type.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == type.encryptString() }}
    }

    fun onResultsMemoriesListFetched(t: RealmResults<MemoriesList>?) {
        memoriesList = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMemoriesListFetched(t: RealmResults<MemoriesList>?, type : String) {
        memoriesList = t
        formsCount = t!!.count { it.selectionType == type.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == type.encryptString() }}
    }

    fun onResultsEducationListFetched(t: RealmResults<EducationList>?) {
        educationList = t
        formsCount = t!!.count { it.selectionType == "Education".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "Education".encryptString() }}
    }

    fun onResultsEducationFetched(t: RealmResults<Education>?) {
        educationItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMainEducationFetched(t: RealmResults<MainEducation>?) {
        mainEducationItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsWorkFetched(t: RealmResults<Work>?) {
        workItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsInterestsFetched(t: RealmResults<Interests>?) {
        interestItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsInterestsListFetched(t: RealmResults<InterestsList>?) {
        interestsList = t
        formsCount = t!!.count { it.selectionType == "Interests".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "Interests".encryptString() }}
    }

    fun onResultsCheckupsFetched(t: RealmResults<Checkups>?) {
        checkupsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsEmergencyContactsFetched(t: RealmResults<EmergencyContacts>?) {
        emergencyContactsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsEyeglassPrescriptionsFetched(t: RealmResults<EyeglassPrescriptions>?) {
        eyeglassPrescriptionsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsHealthcareProvidersFetched(t: RealmResults<HealthcareProviders>?) {
        healthcareProvidersItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsIdentificationFetched(t: RealmResults<Identification>?) {
        identificationItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMedicalConditionsFetched(t: RealmResults<MedicalConditions>?) {
        medicalConditionsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMedicalHistoryFetched(t: RealmResults<MedicalHistory>?) {
        medicalHistoryItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsMedicationsFetched(t: RealmResults<Medications>?) {
        medicationsItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsVitalNumbersFetched(t: RealmResults<VitalNumbers>?) {
        vitalNumbersItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsWellnessFetched(t: RealmResults<Wellness>?) {
        wellnessItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsWellnessFetched(t: RealmResults<Wellness>?, type : String ) {
        wellnessItems = t
        formsCount = t!!.count { it.selectionType == type.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == type.encryptString() }}
    }

    fun onResultsWellnessListFetched(t: RealmResults<WellnessList>?) {
        wellnessList = t
        formsCount = t!!.count { it.selectionType == "WellNess".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "WellNess".encryptString() }}
    }

    fun onResultsWellnessListFetched(t: RealmResults<WellnessList>?, detailsId : Long) {
        wellnessList = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.detailsId == detailsId }}
    }

    fun onResultsPropertyFetched(t: RealmResults<Property>? ) {
        propertyItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsVehicleFetched(t: RealmResults<Vehicle>?) {
        vehicleItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsTaxesFetched(t: RealmResults<Taxes>?) {
        taxesItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsInsuranceFetched(t: RealmResults<Insurance>?) {
        insuranceItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsAssetFetched(t: RealmResults<Asset>?) {
        assetItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsHomeListFetched(t: RealmResults<HomeList>?) {
        homeListItems = t
        formsCount = t!!.count { it.selectionType == "HomeBanking".encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == "HomeBanking".encryptString() }}
    }

    fun onResultsPaymentFetched(t: RealmResults<Payment>?) {
        paymentItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

    fun onResultsFinancialFetched(t: RealmResults<Financial>?) {
        financialItems = t
        formsCount = t!!.count { it.selectionType == category_id.encryptString() }
        t!!.addChangeListener { results -> formsCount = t.count { it.selectionType == category_id.encryptString() }}
    }

}

So the issue is above code works fine - I am able to get refresh working on all of the items - but performance is a nightmare. I am looking for help in terms of optimising the code.

cmelchior commented 6 years ago

Hi @alokomkar Questions about the usage of Realm are better suited for Stack Overflow. This repo is for tracking feature requests and bugs.

Zhuinden commented 6 years ago

(truth be told, this isn't really suitable for stack overflow either.)