Проблема с реализацией закрепа в RecyclerView

нужно что бы при нажатии на кнопку обьект поднимался вверх и был первым из закрепленных, а закрепленные отображались первыми в свою очередь из всех обьектов, проблема в том что при нажатии на кнопку, иногда обьект не делает то что должен но в бд изменется значение, также я то реализовал спощбю значения в бд, при закреплении значение обьекта в бд присваивается 1, а остальным закрепленным увеличивается на один, и при этом не может быть возможности быть одинаковым значениям у элементов, но вот такжке иногда элемннту которому должен присвоится 1, прсваивается другое число

код функции закрепления:

noteadapter.zakrepfun = {notex ->
        binding.deletenotesbutton0.isVisible = false
        binding.archivenotesbutton0.isVisible = false
        binding.newnotebutton.setImageDrawable(
            getDrawable(
                requireActivity(),
                android.R.drawable.ic_input_add
            )
        )
        arrnotes.clear()
        val y = mutableListOf<note>()
        var bool = true
        notesdata.observe(viewLifecycleOwner){
            it.forEach { i ->
                  if (i.zakrep >= 1)
                      y.add(i)
            }
        }

        y.find {zakrn ->
            zakrn == notex
        }.let {
            if (it != null) {
                notex.zakrep = 0
                bool = false
            }
        }
        if (bool){
            y.forEach {
                it.zakrep += 1
                viewLifecycleOwner.lifecycleScope.launch {
                    noterep.update(it)
                }
            }
            notex.zakrep += 1
        }
        viewLifecycleOwner.lifecycleScope.launch {
            noterep.update(notex)
        }

        if (bund != null) {
            updaterec(noteadapter,bund)
        }
    }
fun updaterec(noteadapter : recyclerviewclass, bund: String) {
    var notes = mutableListOf<note>()
    notesdata.observe(viewLifecycleOwner) {
        if (bund == "note"){
            it.forEach { i ->
                if (i.deleted == false && i.archived == false) {
                    notes.add(i)
                }
            }
        }
        if (bund == "deleted"){
            it.forEach { i ->
                if (i.deleted == true && i.archived == false) {
                    notes.add(i)
                }
            }
        }
        if (bund == "archived"){
            it.forEach { i ->
                if (i.deleted == false && i.archived == true) {
                    notes.add(i)
                }
            }
        }
        var arrzakrep0 = mutableListOf<note>()
        var arrzakrep1 = mutableListOf<note>()
        notes.forEach { i ->
            if (i.zakrep >= 1)
                arrzakrep0.add(i)

        }
        var x = 0
        var c = true
        arrzakrep0.forEach {
            var y = mutableListOf<note>()
            arrzakrep0.forEach { i ->
                c = true
                arrzakrep1.forEach { ii ->
                    if (i == ii)
                        c = false
                }
                if (c){
                    if (x == 0){
                        x = i.zakrep
                        y.add(i)
                    }
                    else{
                        if (x >= i.zakrep){
                            x = i.zakrep
                            y.clear()
                            y.add(i)
                        }
                    }
                }
            }
            if (y.size == 1){
                arrzakrep1.add(y[0])
                y.clear()
        }
            x = 0
        }
        arrzakrep1.forEach {
            notes.remove(it)
        }
        notes = arrzakrep1.plus(notes) as MutableList<note>
        noteadapter.update(notes,buttonc,textc)
        notes.clear()
        c = true
    }
}

адаптер:

class recyclerviewclass() :
    RecyclerView.Adapter<recyclerviewclass.MyViewHolder>() {
    private val array = mutableListOf<note>()
    private lateinit var layout : LinearLayout
    private var textc = 0
    private var buttonsc = 0
    @SuppressLint("NotifyDataSetChanged")
    fun update(newlist : List<note>,x : Int,y : Int){
        array.clear()
        array.addAll(newlist)
        textc = y
        buttonsc = x
        notifyDataSetChanged()
    }

    var layouts = mutableListOf<FrameLayout>()
    var onbuttonclickfordelete: ((note) -> Unit)? = null
    var onbuttonlongclick : ((note) -> Unit)? = null
    var onbuttonclick : ((note) -> Unit)? = null
    var zakrepfun : ((note) -> Unit)? = null
    var arrnotes = mutableListOf<note>()
    var layouts2 = mutableListOf<LinearLayout>()
    var bool = true
    fun invisiblecheckboxs(){
        layouts.forEach {
            it.checkBox.isChecked = false
            it.checkBox.visibility = GONE
        }
        arrnotes.clear()
    }
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val layout = itemView.findViewById<FrameLayout>(R.id.layoutrecyclernote)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.recyclerviewitem, parent, false)
        return MyViewHolder(itemView)
    }

    @SuppressLint("ResourceAsColor", "SuspiciousIndentation")
    override fun onBindViewHolder(holder: MyViewHolder, i: Int) {
            layouts.add(holder.layout)
            holder.layout.setBackgroundColor(buttonsc)
            holder.layout.note.setTextColor(textc)
            holder.layout.namenote.setTextColor(textc)
            holder.layout.button5.setOnClickListener {
                zakrepfun?.invoke(array[i])
                holder.layout.button5.visibility = GONE
                layouts2.clear()
                invisiblecheckboxs()
            }
            holder.layout.button5.visibility = GONE
            holder.layout.imageView2 .visibility = GONE
            if (array[i].zakrep >= 1){
                holder.layout.button5.setImageResource(R.drawable.objects_pin_b_s)
                holder.layout.imageView2.visibility = VISIBLE
            }
            holder.layout.namenote.setText(array[i].namenote)
            holder.layout.note.setText(array[i].note)
            holder.layout.setOnClickListener {
                if (arrnotes.size >= 1) {
                    bool = true
                    arrnotes.find {
                        array[i] == it
                    }?.let {
                        bool = false
                        arrnotes.remove(it)
                        layouts2.remove(holder.layout.linearn)
                        when(arrnotes.size) {
                            0->{layouts.forEach { i ->
                                i.checkBox.isVisible = false
                                }
                                if (array[i].zakrep>0)
                                    holder.layout.imageView2.visibility = VISIBLE
                                holder.layout.button5.visibility = GONE
                            }
                        1->{
                            layouts2[0].button5.visibility = VISIBLE
                            layouts2[0].imageView2.visibility = GONE
                        }
                        }
                        if (array[i].zakrep>0)
                            holder.layout.imageView2.visibility = VISIBLE
                        holder.layout.button5.visibility = GONE
                        holder.layout.checkBox.isChecked = false
                        onbuttonclickfordelete?.invoke(it)
                    }
                    if (bool) {
                        arrnotes.add(array[i])
                        layouts2.add(holder.layout.linearn)
                        holder.layout.checkBox.isChecked = true
                        onbuttonlongclick?.invoke(array[i])
                    }

                } else {
                    onbuttonclick?.invoke(array[i])
                }
            }
            holder.layout.setOnLongClickListener {
                bool = true
                arrnotes.find {
                    array[i] == it
                }?.let {
                    bool = false
                    arrnotes.remove(it)
                    layouts2.remove(holder.layout.linearn)
                    when(arrnotes.size) {
                        0->{layouts.forEach { i ->
                            i.checkBox.isVisible = false
                        }
                            if (array[i].zakrep>0)
                                holder.layout.imageView2.visibility = VISIBLE
                            holder.layout.button5.visibility = GONE
                        }
                        1->{
                            layouts2[0].button5.visibility = VISIBLE
                            layouts2[0].imageView2.visibility = GONE
                        }
                    }
                    if (array[i].zakrep>0)
                        holder.layout.imageView2.visibility = VISIBLE
                    holder.layout.button5.visibility = GONE
                    holder.layout.checkBox.isChecked = false
                    onbuttonclickfordelete?.invoke(it)
                }
                if (bool) {
                    arrnotes.add(array[i])
                    layouts2.add(holder.layout.linearn)
                    if (arrnotes.size == 1) {
                        layout = holder.layout.linearn
                        holder.layout.imageView2.visibility = GONE
                        holder.layout.button5.visibility = VISIBLE
                        layouts.forEach { i ->
                            i.checkBox.isVisible = true
                        }
                    }
                    else{
                        layout.imageView2.visibility = GONE
                        layout.button5.visibility = GONE
                    }
                    holder.layout.checkBox.isChecked = true
                    onbuttonlongclick?.invoke(array[i])
                }
                return@setOnLongClickListener true
            }
    }

    override fun getItemCount(): Int {
       return array.size

    }
}

Ответы (0 шт):