设备分享业务实现

更新时间:2024-10-22 06:54:39下载pdf

本文介绍如何实现设备分享业务。更多信息,参考 设备分享业务能力

设备分享功能入口

在设备/群组的详情页,新增设备分享入口。

设备分享业务实现

获取分享管理类。

val mShareManager : IDeviceShareManager = DeviceBusinessDataManager.getInstance().getDeviceShareManager()
val manager : ThingDeviceShareManager = ThingDeviceShareManager()

实现分享设备/群组功能

设备分享业务实现 设备分享业务实现

步骤 1:判断设备/群组是否支持分享

manager?.isSupportDeviceShare(resId, resType, object : IThingResultCallback<Boolean> {
            override fun onSuccess(result: Boolean?) {

            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }

        })

步骤 2:获取设备/群组剩余分享次数

resId?.let {
            mShareManager?.querySharedCount(it, resType, object : Business.ResultListener<Long> {
                override fun onFailure(p0: BusinessResponse?, p1: Long?, p2: String?) {

                }

                override fun onSuccess(p0: BusinessResponse?, p1: Long?, p2: String?) {
                    p1?.let {
                        var maxCount = 9999L
                        if (p1 != -1L) {
                            maxCount = p1
                        }
                        if (maxCount >= count) {
                            afterAction.invoke()
                        } else {
                            showToast("No sharing times")
                        }
                    }
                }
            })
        }

若返回 -1,则按照不限次数处理。

步骤 3:分享设备/群组

分享设备有两种方式:

  • 方式 1:分享给指定用户,需要在 UI 界面上获取用户名称。
  • 方式 2:根据分享次数生成短链,其他用户单击短链可以跳转到 App 首页接受分享。

分享给指定用户

manager?.shareToUser(
            resId,
            resType,
            homeId,
            userName,
            object : IThingResultCallback<SharedUserInfoBean> {
                override fun onSuccess(result: SharedUserInfoBean?) {
                    showToast("share success")
                }

                override fun onError(errorCode: String?, errorMessage: String?) {
                    showToast(errorMessage)
                }
            })

生成分享短链

mShareManager?.createShareLink(
            resId.toString(),
            resType,
            homeId,
            5,
            count,
            object : Business.ResultListener<ShareDeviceLinkResultBean> {
                override fun onFailure(
                    p0: BusinessResponse?,
                    p1: ShareDeviceLinkResultBean?,
                    p2: String?
                ) {
                    showToast(p0?.errorMsg)
                }

                override fun onSuccess(
                    p0: BusinessResponse?,
                    p1: ShareDeviceLinkResultBean?,
                    p2: String?
                ) {
                    p1?.let { copyToClipboard(it.code) }
                }
            })

实现接受设备/群组分享功能

设备分享业务实现

步骤 1:检查短链有效性

首先,检测短链是否有效。

manager?.parseShortLinkAvailability(link, object : IThingResultCallback<Boolean> {
            override fun onSuccess(result: Boolean?) {
                if (result == true) {
                    getSharerInfo(link)
                } else {
                    showToast("The link has expired")
                }
            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }
        })

步骤 2:获取短链 Code 内容

接着,获取短链的内容,包括分享的设备、分享人等信息。此时可以弹窗展示这些信息,并且提供接受分享的按钮。

mShareManager?.parseSharedDeviceLinkResult(code,
            object : Business.ResultListener<ShareShortLinkResult> {
                override fun onFailure(
                    p0: BusinessResponse?,
                    p1: ShareShortLinkResult?,
                    p2: String?
                ) {

                }

                override fun onSuccess(
                    p0: BusinessResponse?,
                    p1: ShareShortLinkResult?,
                    p2: String?
                ) {
                    p1?.let {
                        binding.tvUsername.text = p1.resName
                        binding.ivUserIcon.setImageURI(Uri.parse(p1.resIcon))
                        binding.llSharerInfo.visibility = View.VISIBLE
                    }

                }
            })

步骤 3:接受分享

manager?.acceptShare(code, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("accept share success")
            }
        })

实现设备当前分享的用户相关功能

获取设备/群组当前分享的用户

此处指当前设备或群组分享过的用户,可以使用列表进行展示。支持分页获取,但通常数量不会太多,也可以一次性获取所有用户。

 manager?.getReceivers(
            resId,
            resType,
            1,
            100,
            object : IThingResultCallback<List<ShareMember>> {
                override fun onSuccess(result: List<ShareMember>?) {

                }

                override fun onError(errorCode: String?, errorMessage: String?) {

                }
            })

删除设备/群组当前分享的用户

manager?.removeReceiver(memberId, resId, resType, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("delete success")

            }
        })

修改用户的分享有效期

分享时间有两种模式:

  • 永久有效,结束时间只需传入 0
  • 一段时间有效,需要选择结束的日期并传入。
private fun showChangeTimeDialog(memberId: Long) {
        ChangeTimeDialog.Builder(this)
            .setButtonListener(object : ChangeTimeDialog.OnClickListener {
                override fun onForeverButtonClick() {
                    changeTime(memberId,0, 0)
                }

                override fun onSometimeButtonClick(endTime: Long) {
                    changeTime(memberId,1, endTime)
                }
            })
            .build()
            .show()
    }

    private fun changeTime(memberId: Long,mode: Int, endTime: Long) {
        mShareManager!!.updateSharedDeadline(memberId,
            resId!!, resType, mode, endTime, object : Business.ResultListener<Boolean> {
                override fun onFailure(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                    showToast(p0?.errorMsg)
                }

                override fun onSuccess(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                    showToast("change time success")
                }

            })
    }

实现最近分享过的用户相关功能

获取最近分享过的用户

这指的是整合账户维度的,所有设备或群组分享过的用户,同样可以使用列表进行展示。

manager?.getRelationMembers(object : IThingResultCallback<List<ShareMember>> {
            override fun onSuccess(result: List<ShareMember>?) {
                var receivedUserList2: MutableList<ReceiverItemBean> = ArrayList()
                result?.indices?.forEach { i ->
                    val bean = ReceiverItemBean(
                        result[i].iconUrl,
                        result[i].userName,
                        result[i].memberId,
                        result[i].uid
                    )
                    receivedUserList2.add(i, bean)
                }
                allUserAdapter.setData(receivedUserList2, null)
            }

            override fun onError(errorCode: String?, errorMessage: String?) {
                L.i(TAG, errorMessage)
            }
        })

移除最近分享过的用户

mShareManager?.deleteSharedContact(userId, object : Business.ResultListener<Boolean> {
            override fun onFailure(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                showToast(p0?.errorMsg)
            }

            override fun onSuccess(p0: BusinessResponse?, p1: Boolean?, p2: String?) {
                showToast("delete success")
            }
        })

实现与分享者有关功能

分享者是指将设备或群组分享给“我”的用户。

查看分享者列表

manager?.getShareReceivedUserList(object : IThingResultCallback<List<SharedUserInfoBean>> {
            override fun onSuccess(result: List<SharedUserInfoBean>?) {
                var receivedUserList3: MutableList<ReceiverItemBean> = ArrayList()
                result?.indices?.forEach { i ->
                    val bean = ReceiverItemBean(
                        result[i].iconUrl,
                        result[i].userName,
                        result[i].memeberId,
                        null
                    )
                    receivedUserList3.add(i, bean)
                }
                sharerListAdapter.setData(receivedUserList3, null)
            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }
        })

删除分享者及其分享的所有设备

manager?.removeReceivedUserShare(memberId, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("remove success")
            }
        })

获取分享者信息及其分享的设备

可以创建一个分享者详情页,进入该页面时获取分享者的信息。

设备分享业务实现
manager?.getSharerInfoDetail(memberId, object : IThingResultCallback<ShareReceivedUserDetailBean> {
            override fun onSuccess(result: ShareReceivedUserDetailBean?) {
                result?.let {
                    binding.etNickname.setText(result.remarkName)
                    val beans: MutableList<ItemBean> = ArrayList(8)
                    for (deviceBean in result.devices) {
                        beans.add(getItemBeanFromDevs(deviceBean))
                    }
                    adapter.setData(beans)
                }
            }

            override fun onError(errorCode: String?, errorMessage: String?) {
                showToast(errorMessage)
            }

        })

查看分享者名称

manager?.getSharerName(resId,resType,object :IThingResultCallback<String>{
            override fun onSuccess(result: String?) {

            }

            override fun onError(errorCode: String?, errorMessage: String?) {

            }
        })

修改分享者昵称

private fun changeNickname(){
        val newName = binding.etNickname.text.toString()
        manager?.renameReceivedShareNickname(memberId,newName,object :IResultCallback{
            override fun onError(code: String?, error: String?) {
                showToast(error)
            }

            override fun onSuccess() {
                showToast("change success")
            }
        })
    }

删除分享

删除已接受分享的设备/群组。

manager?.removeReceivedShare(resId,resType,object :IResultCallback{
            override fun onError(code: String?, error: String?) {

            }

            override fun onSuccess() {

            }
        })