All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.sceyt.chatuikit.data.repositories.UsersRepositoryImpl.kt Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.sceyt.chatuikit.data.repositories

import com.sceyt.chat.models.SceytException
import com.sceyt.chat.models.user.BlockUserRequest
import com.sceyt.chat.models.user.UnBlockUserRequest
import com.sceyt.chat.models.user.User
import com.sceyt.chat.models.user.UserListQuery
import com.sceyt.chat.models.user.UserListQueryByIds
import com.sceyt.chat.sceyt_callbacks.UsersCallback
import com.sceyt.chatuikit.SceytChatUIKit
import com.sceyt.chatuikit.data.models.SceytResponse
import com.sceyt.chatuikit.data.models.messages.SceytUser
import com.sceyt.chatuikit.extensions.TAG
import com.sceyt.chatuikit.logger.SceytLog
import com.sceyt.chatuikit.persistence.extensions.safeResume
import com.sceyt.chatuikit.persistence.mappers.toSceytUser
import com.sceyt.chatuikit.persistence.repositories.UsersRepository
import kotlinx.coroutines.suspendCancellableCoroutine

class UsersRepositoryImpl : UsersRepository {
    private lateinit var usersQuery: UserListQuery

    override suspend fun loadUsers(query: String): SceytResponse> {
        return suspendCancellableCoroutine { continuation ->
            val userListQuery = UserListQuery.Builder()
                .order(UserListQuery.UserListQueryOrderKeyType.UserListQueryOrderKeyFirstName)
                .filter(UserListQuery.UserListFilterType.UserListFilterTypeAll)
                .limit(SceytChatUIKit.config.queryLimits.userListQueryLimit)
                .query(query)
                .build().also { usersQuery = it }

            userListQuery.loadNext(object : UsersCallback {
                override fun onResult(users: MutableList?) {
                    if (users.isNullOrEmpty())
                        continuation.safeResume(SceytResponse.Success(arrayListOf()))
                    else {
                        continuation.safeResume(SceytResponse.Success(users.map {
                            it.toSceytUser()
                        }))
                    }
                }

                override fun onError(e: SceytException?) {
                    continuation.safeResume(SceytResponse.Error(e))
                    SceytLog.e(TAG, "loadUsers error: ${e?.message}")
                }
            })
        }
    }


    override suspend fun loadMoreUsers(): SceytResponse> {
        return suspendCancellableCoroutine { continuation ->
            usersQuery.loadNext(object : UsersCallback {
                override fun onResult(users: MutableList?) {
                    if (users.isNullOrEmpty())
                        continuation.safeResume(SceytResponse.Success(arrayListOf()))
                    else {
                        continuation.safeResume(SceytResponse.Success(users.map {
                            it.toSceytUser()
                        }))
                    }
                }

                override fun onError(e: SceytException?) {
                    continuation.safeResume(SceytResponse.Error(e))
                    SceytLog.e(TAG, "loadMoreUsers error: ${e?.message}")
                }
            })
        }
    }

    override suspend fun getSceytUsersByIds(ids: List): SceytResponse> {
        return suspendCancellableCoroutine { continuation ->
            val builder = UserListQueryByIds.Builder()
                .setIds(ids)
                .build()

            builder.load(object : UsersCallback {
                override fun onResult(users: MutableList) {
                    continuation.safeResume(SceytResponse.Success(users.map {
                        it.toSceytUser()
                    }))
                }

                override fun onError(e: SceytException?) {
                    continuation.safeResume(SceytResponse.Error(e))
                    SceytLog.e(TAG, "getSceytUsersByIds error: ${e?.message}")
                }
            })
        }
    }

    override suspend fun getSceytUserById(id: String): SceytResponse {
        return suspendCancellableCoroutine { continuation ->
            val builder = UserListQueryByIds.Builder()
                .setIds(listOf(id))
                .build()

            builder.load(object : UsersCallback {
                override fun onResult(users: MutableList) {
                    if (users.isNotEmpty())
                        continuation.safeResume(SceytResponse.Success(users[0].toSceytUser()))
                    else continuation.safeResume(SceytResponse.Error(SceytException(0, "User not found")))
                }

                override fun onError(e: SceytException?) {
                    continuation.safeResume(SceytResponse.Error(e))
                    SceytLog.e(TAG, "getSceytUserById error: ${e?.message}")
                }
            })
        }
    }

    override suspend fun blockUser(userId: String): SceytResponse> {
        return suspendCancellableCoroutine { continuation ->
            BlockUserRequest(userId).execute(object : UsersCallback {
                override fun onResult(data: MutableList?) {
                    continuation.safeResume(SceytResponse.Success(data?.map {
                        it.toSceytUser()
                    }))
                }

                override fun onError(e: SceytException?) {
                    continuation.safeResume(SceytResponse.Error(e))
                    SceytLog.e(TAG, "blockUser error: ${e?.message}, code: ${e?.code}")
                }
            })
        }
    }

    override suspend fun unblockUser(userId: String): SceytResponse> {
        return suspendCancellableCoroutine { continuation ->
            UnBlockUserRequest(userId).execute(object : UsersCallback {
                override fun onResult(data: MutableList?) {
                    continuation.safeResume(SceytResponse.Success(data?.map {
                        it.toSceytUser()
                    }))
                }

                override fun onError(e: SceytException?) {
                    continuation.safeResume(SceytResponse.Error(e))
                    SceytLog.e(TAG, "unblockUser error: ${e?.message}, code: ${e?.code}")
                }
            })
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy