diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/AdapterTeamResource.kt b/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/AdapterTeamResource.kt index 3a04588936..b663aee066 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/AdapterTeamResource.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/AdapterTeamResource.kt @@ -10,11 +10,17 @@ import io.realm.Realm import org.ole.planet.myplanet.callback.OnHomeItemClickListener import org.ole.planet.myplanet.databinding.RowTeamResourceBinding import org.ole.planet.myplanet.model.RealmMyLibrary +import org.ole.planet.myplanet.model.RealmMyTeam import org.ole.planet.myplanet.model.RealmMyTeam.Companion.getTeamCreator -import org.ole.planet.myplanet.ui.team.teamResource.AdapterTeamResource.ViewHolderTeamResource -class AdapterTeamResource(private val context: Context, private val list: List, mRealm: Realm, teamId: String?, private val settings: SharedPreferences) : RecyclerView.Adapter() { - private lateinit var rowTeamResourceBinding: RowTeamResourceBinding +class AdapterTeamResource( + private val context: Context, + private val list: MutableList, + private val mRealm: Realm, + teamId: String?, + private val settings: SharedPreferences +) : RecyclerView.Adapter() { + private var listener: OnHomeItemClickListener? = null private val teamCreator: String = getTeamCreator(teamId, mRealm) @@ -25,19 +31,26 @@ class AdapterTeamResource(private val context: Context, private val list: List= list.size) return + + mRealm.executeTransaction { realm -> + val itemToDelete = realm.where(RealmMyTeam::class.java) + .equalTo("resourceId", resource.id) + .findFirst() + + if (itemToDelete != null) { + itemToDelete.resourceId = "" + itemToDelete.updated = true + } + } + + list.removeAt(position) + notifyItemRemoved(position) + } + + class ViewHolderTeamResource(val rowTeamResourceBinding: RowTeamResourceBinding) : RecyclerView.ViewHolder(rowTeamResourceBinding.root) } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/TeamResourceFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/TeamResourceFragment.kt index 1526878509..d27618e33a 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/TeamResourceFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/team/teamResource/TeamResourceFragment.kt @@ -47,8 +47,14 @@ class TeamResourceFragment : BaseTeamFragment(), TeamPageListener { } private fun showLibraryList() { - val libraries: List = mRealm.where(RealmMyLibrary::class.java).`in`("id", getResourceIds(teamId, mRealm).toTypedArray()).findAll() - adapterLibrary = settings?.let { AdapterTeamResource(requireActivity(), libraries, mRealm, teamId, it) }!! + val libraries: MutableList = mRealm.where(RealmMyLibrary::class.java) + .`in`("id", getResourceIds(teamId, mRealm).toTypedArray()) + .findAll() + .toMutableList() + + adapterLibrary = settings?.let { + AdapterTeamResource(requireActivity(), libraries, mRealm, teamId, it) + }!! fragmentTeamResourceBinding.rvResource.layoutManager = GridLayoutManager(activity, 3) fragmentTeamResourceBinding.rvResource.adapter = adapterLibrary showNoData(fragmentTeamResourceBinding.tvNodata, adapterLibrary.itemCount, "teamResources")